|
| 1 | +/* |
| 2 | + * Copyright © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.knora.webapi.store.triplestore.upgrade.plugins |
| 7 | + |
| 8 | +import com.typesafe.scalalogging.Logger |
| 9 | +import org.knora.webapi.feature.FeatureFactoryConfig |
| 10 | +import org.knora.webapi.messages.OntologyConstants |
| 11 | +import org.knora.webapi.messages.util.rdf._ |
| 12 | +import org.knora.webapi.store.triplestore.upgrade.UpgradePlugin |
| 13 | + |
| 14 | +/** |
| 15 | + * Transforms a repository for Knora PR 2079. |
| 16 | + * Adds missing datatype ^^<http://www.w3.org/2001/XMLSchema#anyURI> and/or value to valueHasUri |
| 17 | + */ |
| 18 | +class UpgradePluginPR2079(featureFactoryConfig: FeatureFactoryConfig, log: Logger) extends UpgradePlugin { |
| 19 | + private val nodeFactory: RdfNodeFactory = RdfFeatureFactory.getRdfNodeFactory(featureFactoryConfig) |
| 20 | + |
| 21 | + override def transform(model: RdfModel): Unit = { |
| 22 | + val statementsToRemove: collection.mutable.Set[Statement] = collection.mutable.Set.empty |
| 23 | + val statementsToAdd: collection.mutable.Set[Statement] = collection.mutable.Set.empty |
| 24 | + val newObjectValue: String => DatatypeLiteral = (value: String) => |
| 25 | + nodeFactory.makeDatatypeLiteral(value, OntologyConstants.Xsd.Uri) |
| 26 | + |
| 27 | + for (statement: Statement <- model) { |
| 28 | + if (statement.pred.iri == OntologyConstants.KnoraBase.ValueHasUri) { |
| 29 | + statement.obj match { |
| 30 | + case literal: DatatypeLiteral => |
| 31 | + if (literal.datatype != OntologyConstants.Xsd.Uri) { |
| 32 | + statementsToRemove += statement |
| 33 | + |
| 34 | + statementsToAdd += nodeFactory.makeStatement( |
| 35 | + subj = statement.subj, |
| 36 | + pred = statement.pred, |
| 37 | + obj = newObjectValue(literal.value), |
| 38 | + context = statement.context |
| 39 | + ) |
| 40 | + |
| 41 | + log.info( |
| 42 | + s"Transformed valueHasIri: $literal to ${newObjectValue(literal.value)}." |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + case _ => () |
| 47 | + } |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + model.removeStatements(statementsToRemove.toSet) |
| 52 | + model.addStatements(statementsToAdd.toSet) |
| 53 | + } |
| 54 | +} |
0 commit comments