Skip to content

Commit

Permalink
fix: Ensure all project's restricted view settings are correctly pers…
Browse files Browse the repository at this point in the history
…isted (#3125)
  • Loading branch information
seakayone authored Mar 18, 2024
1 parent d753fcc commit 51cde02
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 4 deletions.
2 changes: 1 addition & 1 deletion webapi/src/main/resources/knora-ontologies/knora-base.ttl
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
rdf:type owl:Ontology ;
rdfs:label "The Knora base ontology"@en ;
:attachedToProject knora-admin:SystemProject ;
:ontologyVersion "knora-base v30" .
:ontologyVersion "knora-base v31" .


#################################################################
Expand Down
2 changes: 1 addition & 1 deletion webapi/src/main/scala/org/knora/webapi/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ package object webapi {
* The version of `knora-base` and of the other built-in ontologies that this version of Knora requires.
* Must be the same as the object of `knora-base:ontologyVersion` in the `knora-base` ontology being used.
*/
val KnoraBaseVersion: String = "knora-base v30"
val KnoraBaseVersion: String = "knora-base v31"

/**
* `IRI` is a synonym for `String`, used to improve code readability.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import com.typesafe.scalalogging.Logger

import org.knora.webapi.messages.store.triplestoremessages.RdfDataObject
import org.knora.webapi.store.triplestore.upgrade.plugins.*
import org.knora.webapi.store.triplestore.upgrade.plugins.UpgradePluginPR3110

/**
* The plan for updating a repository to work with the current version of Knora.
Expand Down Expand Up @@ -47,7 +46,7 @@ object RepositoryUpdatePlan {
PluginForKnoraBaseVersion(versionNumber = 28, plugin = new MigrateOnlyBuiltInGraphs), // PR 3038
PluginForKnoraBaseVersion(versionNumber = 29, plugin = new UpgradePluginPR3110()),
PluginForKnoraBaseVersion(versionNumber = 30, plugin = new UpgradePluginPR3111()),
// KEEP IT ON THE BOTTOM
PluginForKnoraBaseVersion(versionNumber = 31, plugin = new UpgradePluginPR3112()),
)

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.knora.webapi.store.triplestore.upgrade.plugins

import org.eclipse.rdf4j.sparqlbuilder.core.SparqlBuilder.`var` as variable
import org.eclipse.rdf4j.sparqlbuilder.core.query.*
import org.eclipse.rdf4j.sparqlbuilder.core.query.ModifyQuery
import org.eclipse.rdf4j.sparqlbuilder.rdf.Rdf

import org.knora.webapi.slice.admin.AdminConstants
import org.knora.webapi.slice.admin.domain.model.RestrictedView.Size
import org.knora.webapi.slice.admin.repo.rdf.Vocabulary
import org.knora.webapi.store.triplestore.upgrade.GraphsForMigration
import org.knora.webapi.store.triplestore.upgrade.MigrateSpecificGraphs

/**
* Certain restricted views have a watermark that is not a boolean. This plugin removes the invalid watermark triples.
*/
class UpgradePluginPR3112 extends AbstractSparqlUpdatePlugin {

override def graphsForMigration: GraphsForMigration =
MigrateSpecificGraphs.from(AdminConstants.adminDataNamedGraph)

private val removeWatermarkIfBothSet: ModifyQuery = {
val (project, prevWatermark, prevSize) = (variable("project"), variable("prevWatermark"), variable("prevSize"))
Queries
.MODIFY()
.prefix(Vocabulary.KnoraAdmin.NS)
.delete(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, prevWatermark))
.from(Vocabulary.NamedGraphs.knoraAdminIri)
.where(
project
.isA(Vocabulary.KnoraAdmin.KnoraProject)
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, prevWatermark)
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewSize, prevSize)
.from(Vocabulary.NamedGraphs.knoraAdminIri),
)
}

private val addDefaultRestrictedViewSizeToProjectsWithout = {
val project = variable("project")
Queries
.MODIFY()
.prefix(Vocabulary.KnoraAdmin.NS)
.`with`(Vocabulary.NamedGraphs.knoraAdminIri)
.insert(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, Rdf.literalOf(Size.default.value)))
.where(
project
.isA(Vocabulary.KnoraAdmin.KnoraProject)
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, variable("size")))
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, variable("watermark")))
.from(Vocabulary.NamedGraphs.knoraAdminIri),
)
}

private val replaceWatermarkFalseWithDefaultRestrictedViewSize = {
val project = variable("project")
Queries
.MODIFY()
.prefix(Vocabulary.KnoraAdmin.NS)
.`with`(Vocabulary.NamedGraphs.knoraAdminIri)
.insert(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, Rdf.literalOf(Size.default.value)))
.delete(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, Rdf.literalOf(false)))
.from(Vocabulary.NamedGraphs.knoraAdminIri)
.where(
project
.isA(Vocabulary.KnoraAdmin.KnoraProject)
.andHas(Vocabulary.KnoraAdmin.projectRestrictedViewWatermark, Rdf.literalOf(false))
.filterNotExists(project.has(Vocabulary.KnoraAdmin.projectRestrictedViewSize, variable("size")))
.from(Vocabulary.NamedGraphs.knoraAdminIri),
)
}

override def getQueries: List[ModifyQuery] = List(
removeWatermarkIfBothSet,
addDefaultRestrictedViewSizeToProjectsWithout,
replaceWatermarkFalseWithDefaultRestrictedViewSize,
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright © 2021 - 2024 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/

package org.knora.webapi.store.triplestore.upgrade.plugins

import zio.test.Spec
import zio.test.ZIOSpecDefault
import zio.test.assertTrue

object UpgradePluginPR3112Spec extends ZIOSpecDefault with UpgradePluginTestOps {

val spec: Spec[Any, Nothing] = suite("UpgradePluginPR3111")(
test(
"given project with invalid RestrictedView settings, " +
"when the plugin is run, " +
"then the size is retained or a default size is set.",
) {
// given
val plugin = new UpgradePluginPR3112()
val triG =
s"""
|@prefix knora-admin: <http://www.knora.org/ontology/knora-admin#> .
|@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
|
|GRAPH <http://www.knora.org/data/admin> {
| <http://rdfh.ch/projects/0001> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewWatermark true ;
| knora-admin:projectRestrictedViewSize "!512,512"^^xsd:string .
|
| <http://rdfh.ch/projects/0002> a knora-admin:knoraProject .
|
| <http://rdfh.ch/projects/0003> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewWatermark false.
|
| <http://rdfh.ch/projects/0004> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewWatermark true.
|
| <http://rdfh.ch/projects/0005> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewSize "!555,555"^^xsd:string .
|}
|""".stripMargin
val model = createJenaModelFromTriG(triG)

// when
plugin.transform(model)

// then
val project0001HasOnlyViewSize =
"""
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|ASK {
| GRAPH <http://www.knora.org/data/admin> {
| <http://rdfh.ch/projects/0001> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewSize "!512,512"^^xsd:string .
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0001> knora-admin:projectRestrictedViewWatermark ?any . }
| }
|}
|""".stripMargin

val project0002HasDefaultRestrictedViewSize =
"""
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|ASK {
| GRAPH <http://www.knora.org/data/admin> {
| <http://rdfh.ch/projects/0002> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewSize "!128,128"^^xsd:string .
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0002> knora-admin:projectRestrictedViewWatermark ?any . }
| }
|}
|""".stripMargin

val watermarkFalseIsReplacedByDefaultRestrictedViewSize =
"""
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|ASK {
| GRAPH <http://www.knora.org/data/admin> {
| <http://rdfh.ch/projects/0003> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewSize "!128,128"^^xsd:string .
| FILTER NOT EXISTS { <http://rdfh.ch/projects/0003> knora-admin:projectRestrictedViewWatermark ?any . }
| }
|}
|""".stripMargin

val validProjectsRemainUntouched =
"""
|PREFIX knora-admin: <http://www.knora.org/ontology/knora-admin#>
|PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
|
|ASK {
| GRAPH <http://www.knora.org/data/admin> {
|
| <http://rdfh.ch/projects/0004> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewWatermark true.
|
| <http://rdfh.ch/projects/0005> a knora-admin:knoraProject ;
| knora-admin:projectRestrictedViewSize "!555,555"^^xsd:string .
| }
|}
|""".stripMargin

assertTrue(
queryAsk(project0001HasOnlyViewSize, model),
queryAsk(project0002HasDefaultRestrictedViewSize, model),
queryAsk(watermarkFalseIsReplacedByDefaultRestrictedViewSize, model),
queryAsk(validProjectsRemainUntouched, model),
)
},
)
}

0 comments on commit 51cde02

Please sign in to comment.