Commit 4443288e authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Implement locator replacement in rdf handler

parent ab768e50
......@@ -64,7 +64,8 @@ class Ingester(private val producer: Producer, private val sftpClient: SftpClien
val instantiationOutput = StringWriter()
instantiationPair.second.write(instantiationOutput)
try {
fedoraClient.createOrUpdateRdfResource(URI(instantiationPair.first), instantiationOutput.toString(), RdfContentTypes.NTRIPLES)
val data = instantiationOutput.toString()
fedoraClient.createOrUpdateRdfResource(URI(instantiationPair.first), data, RdfContentTypes.NTRIPLES)
log.info("Ingested instantiation ${instantiationPair.first}.")
producer.sendReport(
Report(instantiationPair.first, ReportStatus.success, "Ingested an instantiation in fedora."))
......
......@@ -2,13 +2,17 @@ package org.memobase
import org.apache.jena.rdf.model.Model
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.rdf.model.Statement
import org.apache.jena.rdf.model.impl.SelectorImpl
import org.apache.jena.rdf.model.impl.StatementImpl
import org.apache.log4j.LogManager
import org.memobase.rdf.EBUCORE
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
import java.io.ByteArrayInputStream
class RdfHandler(data: String) {
private val log = LogManager.getLogger("RdfHandler")
private val model = ModelFactory.createDefaultModel()
init {
......@@ -50,10 +54,32 @@ class RdfHandler(data: String) {
}
model.add(statement)
}
if (resource.hasProperty(EBUCORE.locator)) {
replaceSftpLocator(resource.uri, model)
}
Pair(resource.uri, model)
}.toList()
}
private fun replaceSftpLocator(uri: String, model: Model) {
var newStatement: Statement? = null
val string: String? = null
val removedStatement = model.listStatements(SelectorImpl(null, EBUCORE.locator, string)).mapWith {
if (it.`object`.asLiteral().string.startsWith("sftp:")) {
newStatement = StatementImpl(it.subject, it.predicate, model.createLiteral("$uri/binary"))
}
it
}.nextOptional()
removedStatement.let {
if (it.isPresent && newStatement != null) {
model.remove(it.get())
model.add(newStatement)
}
}
log.error("Removed statement $removedStatement.")
log.error("Added statement $newStatement.")
}
fun getSftpLocators(): List<Pair<String, String?>> {
return model.listSubjectsWithProperty(RICO.type, "digitalObject").filterKeep {
it.hasProperty(EBUCORE.locator)
......
......@@ -95,4 +95,28 @@ class TestRdfHandler {
Pair("https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2", "/path/to/file/filename.jpg")
)
@Test
fun `test replace sftp locators`() {
val rdfHandler = RdfHandler(readFile("inputSftp.nt"))
val list = rdfHandler.getInstantiations()
list.forEachIndexed { index, pair ->
val out = StringWriter()
RDFDataMgr.write(out, pair.second, Lang.NTRIPLES)
val sortedOut = sort(out.toString().split("\n")).trim()
assertThat(sortedOut)
.isEqualTo(sort(readFile("replaceSftpLocatorOutput$index.nt").split("\n")).trim())
assertThat(pair.first)
.isEqualTo(uris2[index])
}
}
private val uris2 = listOf(
"https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1",
"https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2"
)
}
\ No newline at end of file
......@@ -24,7 +24,7 @@
</Console>
</Appenders>
<Loggers>
<Root level="info">
<Root level="error">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
......
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Instantiation> .
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1> <https://www.ica.org/standards/RiC/ontology#type> "digitalObject" .
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-1> <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#locator> "http://digitalisate.afz.ethz.ch/?&guid=0a9aef73bc2845eabc3c20db7c141bea" .
\ No newline at end of file
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <https://www.ica.org/standards/RiC/ontology#Instantiation> .
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2> <https://www.ica.org/standards/RiC/ontology#type> "digitalObject" .
<https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2> <http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#locator> "https://memobase.ch/instantiation/digital/AFZ-IB_Becker_Audiovisuals_63-2/binary" .
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment