Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
make lastModificationDate mandatory and add UpgradePlugin
- Loading branch information
Showing
6 changed files
with
108 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
...c/main/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2288.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import com.typesafe.scalalogging.Logger | ||
import org.knora.webapi.messages.util.rdf.{RdfFeatureFactory, RdfModel, Statement} | ||
import org.knora.webapi.store.triplestore.upgrade.UpgradePlugin | ||
|
||
import scala.concurrent.duration.DurationLong | ||
|
||
class UpgradePluginPR2288(log: Logger) extends UpgradePlugin { | ||
private val nodeFactory = RdfFeatureFactory.getRdfNodeFactory() | ||
|
||
private val creationDateIri = nodeFactory.makeIriNode("http://www.knora.org/ontology/knora-base#creationDate") | ||
private val lastModDateIri = nodeFactory.makeIriNode("http://www.knora.org/ontology/knora-base#lastModificationDate") | ||
|
||
override def transform(model: RdfModel): Unit = { | ||
val t0 = System.nanoTime() | ||
log.info(s"Starting ${this.getClass.getSimpleName}") | ||
|
||
val statementsWithCreationDateIri = | ||
LazyList.from(model.find(subj = None, pred = Some(creationDateIri), obj = None)) | ||
|
||
val subjectHasNoLastModificationDate: Statement => Boolean = statement => | ||
model.find(subj = Some(statement.subj), pred = Some(lastModDateIri), obj = None).isEmpty | ||
|
||
val newStatements = statementsWithCreationDateIri | ||
.filter(subjectHasNoLastModificationDate) | ||
.map(s => nodeFactory.makeStatement(s.subj, lastModDateIri, s.obj, s.context)) | ||
|
||
model.addStatements(newStatements.toSet) | ||
|
||
log.info(s"Created ${newStatements.size} new statements:\n${newStatements.mkString("++ ", "\n", "")}") | ||
log.info(s"Finished ${this.getClass.getSimpleName} in " + (System.nanoTime() - t0).nanos.toCoarsest.toString()) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...st/scala/org/knora/webapi/store/triplestore/upgrade/plugins/UpgradePluginPR2288Spec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package org.knora.webapi.store.triplestore.upgrade.plugins | ||
|
||
import com.typesafe.scalalogging.LazyLogging | ||
import org.knora.webapi.messages.util.rdf._ | ||
|
||
class UpgradePluginPR2288Spec extends UpgradePluginSpec with LazyLogging { | ||
|
||
val plugin = new UpgradePluginPR2288(log) | ||
|
||
val nf = RdfFeatureFactory.getRdfNodeFactory() | ||
val lastModDateIri = nf.makeIriNode("http://www.knora.org/ontology/knora-base#lastModificationDate") | ||
val thingWithoutIri = nf.makeIriNode("http://rdfh.ch/0001/thing-without-mod-date") | ||
val thingWithoutValue = | ||
nf.makeDatatypeLiteral("2020-01-01T10:00:00.673298Z", "http://www.w3.org/2001/XMLSchema#dateTime") | ||
val thingWithIri = nf.makeIriNode("http://rdfh.ch/0001/thing-with-mod-date") | ||
val thingWithValue = | ||
nf.makeDatatypeLiteral("2020-03-01T10:00:00.673298Z", "http://www.w3.org/2001/XMLSchema#dateTime") | ||
|
||
val modelStr = | ||
""" | ||
|@prefix xsd: <http://www.w3.org/2001/XMLSchema#> . | ||
|@prefix knora-base: <http://www.knora.org/ontology/knora-base#> . | ||
|@prefix anything: <http://www.knora.org/ontology/0001/anything#> . | ||
| | ||
|<http://rdfh.ch/0001/thing-without-mod-date> | ||
| a anything:Thing ; | ||
| knora-base:creationDate "2020-01-01T10:00:00.673298Z"^^xsd:dateTime . | ||
| | ||
|<http://rdfh.ch/0001/thing-with-mod-date> | ||
| a anything:Thing ; | ||
| knora-base:creationDate "2020-02-01T10:00:00.673298Z"^^xsd:dateTime ; | ||
| knora-base:lastModificationDate "2020-03-01T10:00:00.673298Z"^^xsd:dateTime . | ||
| | ||
|""".stripMargin | ||
|
||
"Upgrade plugin PR2288" should { | ||
"add a statement if creationDate is given but no lastModificationDate" in { | ||
val model: RdfModel = stringToModel(modelStr) | ||
val sizeBefore = model.size | ||
|
||
plugin.transform(model) | ||
|
||
val expected = nf.makeStatement(thingWithoutIri, lastModDateIri, thingWithoutValue) | ||
assert(model.contains(expected), "Statement is present") | ||
assert(model.size - sizeBefore == 1, "One statement was added ") | ||
} | ||
"not change existing statements if creationDate and lastModificationDate are present" in { | ||
val model: RdfModel = stringToModel(modelStr) | ||
|
||
plugin.transform(model) | ||
|
||
val expected = nf.makeStatement(thingWithIri, lastModDateIri, thingWithValue) | ||
assert(model.contains(expected)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters