Commit b5ca4b43 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Make variables for constants in code

parent cdd62f33
...@@ -6,18 +6,20 @@ import org.apache.jena.rdf.model.ModelFactory ...@@ -6,18 +6,20 @@ import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.rdf.model.Statement import org.apache.jena.rdf.model.Statement
import org.apache.jena.rdf.model.impl.SelectorImpl import org.apache.jena.rdf.model.impl.SelectorImpl
import org.apache.jena.rdf.model.impl.StatementImpl import org.apache.jena.rdf.model.impl.StatementImpl
import org.apache.jena.riot.Lang
import org.apache.jena.riot.RDFDataMgr
import org.apache.log4j.LogManager import org.apache.log4j.LogManager
import org.memobase.rdf.EBUCORE import org.memobase.rdf.EBUCORE
import org.memobase.rdf.RDF import org.memobase.rdf.RDF
import org.memobase.rdf.RICO import org.memobase.rdf.RICO
import java.io.StringReader
class RdfHandler(data: String, externalBaseUrl: String) { class RdfHandler(data: String, private val externalBaseUrl: String) {
private val log = LogManager.getLogger("RdfHandler") private val log = LogManager.getLogger("IngestRdfHandler")
private val model = ModelFactory.createDefaultModel() private val model = ModelFactory.createDefaultModel()
private val externalBaseUrl = externalBaseUrl
init { init {
model.read(ByteArrayInputStream(data.toByteArray()), "", "NTRIPLES") RDFDataMgr.read(model, ByteArrayInputStream(data.toByteArray()) , Lang.NTRIPLES)
} }
fun getRecord(): Pair<String, Model> { fun getRecord(): Pair<String, Model> {
...@@ -45,7 +47,7 @@ class RdfHandler(data: String, externalBaseUrl: String) { ...@@ -45,7 +47,7 @@ class RdfHandler(data: String, externalBaseUrl: String) {
} }
private fun isInternalNonBinaryResource(uri: String): Boolean { private fun isInternalNonBinaryResource(uri: String): Boolean {
return uri.startsWith(externalBaseUrl) && !uri.endsWith("/binary") return uri.startsWith(externalBaseUrl) && !uri.endsWith("/${Service.BINARY_FILE_URI_PATH}")
} }
fun getReferencedNonBinaryResources(): List<String> { fun getReferencedNonBinaryResources(): List<String> {
...@@ -79,9 +81,10 @@ class RdfHandler(data: String, externalBaseUrl: String) { ...@@ -79,9 +81,10 @@ class RdfHandler(data: String, externalBaseUrl: String) {
private fun replaceSftpLocator(uri: String, model: Model) { private fun replaceSftpLocator(uri: String, model: Model) {
var newStatement: Statement? = null var newStatement: Statement? = null
val string: String? = null val string: String? = null
val replacementLocator = "$uri/${Service.BINARY_FILE_URI_PATH}"
val removedStatement = model.listStatements(SelectorImpl(null, EBUCORE.locator, string)).mapWith { val removedStatement = model.listStatements(SelectorImpl(null, EBUCORE.locator, string)).mapWith {
if (it.`object`.asLiteral().string.startsWith("sftp:")) { if (it.`object`.asLiteral().string.startsWith(Service.SFTP_PREFIX)) {
newStatement = StatementImpl(it.subject, it.predicate, model.createLiteral("$uri/binary")) newStatement = StatementImpl(it.subject, it.predicate, model.createLiteral(replacementLocator))
} }
it it
}.nextOptional() }.nextOptional()
...@@ -91,17 +94,16 @@ class RdfHandler(data: String, externalBaseUrl: String) { ...@@ -91,17 +94,16 @@ class RdfHandler(data: String, externalBaseUrl: String) {
model.add(newStatement) model.add(newStatement)
} }
} }
log.error("Removed statement $removedStatement.") log.info("Replaced sftp locator with fedora url: $replacementLocator.")
log.error("Added statement $newStatement.")
} }
fun getSftpLocators(): List<Pair<String, String?>> { fun getSftpLocators(): List<Pair<String, String?>> {
return model.listSubjectsWithProperty(RICO.type, "digitalObject").filterKeep { return model.listSubjectsWithProperty(RICO.type, Service.DIGITAL_OBJECT_TYPE).filterKeep {
it.hasProperty(EBUCORE.locator) it.hasProperty(EBUCORE.locator)
}.mapWith { }.mapWith {
val url = it.getProperty(EBUCORE.locator).`object`.asLiteral().string val url = it.getProperty(EBUCORE.locator).`object`.asLiteral().string
if (url.startsWith("sftp:")) { if (url.startsWith(Service.SFTP_PREFIX)) {
Pair(it.uri, url.replace("sftp:", "")) Pair(it.uri, url.replace(Service.SFTP_PREFIX, ""))
} else { } else {
Pair(it.uri, null) Pair(it.uri, null)
} }
...@@ -109,7 +111,7 @@ class RdfHandler(data: String, externalBaseUrl: String) { ...@@ -109,7 +111,7 @@ class RdfHandler(data: String, externalBaseUrl: String) {
} }
fun getMimeType(): List<Pair<String, String?>> { fun getMimeType(): List<Pair<String, String?>> {
return model.listSubjectsWithProperty(RICO.type, "digitalObject").filterKeep { return model.listSubjectsWithProperty(RICO.type, Service.DIGITAL_OBJECT_TYPE).filterKeep {
it.hasProperty(EBUCORE.hasMimeType) it.hasProperty(EBUCORE.hasMimeType)
}.mapWith { }.mapWith {
Pair(it.uri, it.getProperty(EBUCORE.hasMimeType).`object`.asLiteral().string) Pair(it.uri, it.getProperty(EBUCORE.hasMimeType).`object`.asLiteral().string)
......
...@@ -31,10 +31,14 @@ import org.memobase.settings.SettingsLoader ...@@ -31,10 +31,14 @@ import org.memobase.settings.SettingsLoader
class Service(fileName: String = "app.yml") : Closeable { class Service(fileName: String = "app.yml") : Closeable {
companion object { companion object {
const val DIGITAL_OBJECT_TYPE = "digitalObject"
const val FEDORA_PROPERTIES_PREFIX = "fedora" const val FEDORA_PROPERTIES_PREFIX = "fedora"
const val CONSUMER_MAX_POLL_RECORDS = "10" const val CONSUMER_MAX_POLL_RECORDS = "10"
const val CONSUMER_MAX_INTERVAL_MS = "3600000" // 3600000ms = 1h const val CONSUMER_MAX_INTERVAL_MS = "3600000" // 3600000ms = 1h
const val BINARY_FILE_URI_PATH = "binary"
const val SFTP_PREFIX = "sftp:"
fun createFedoraClient(appSettings: Properties): FedoraClient { fun createFedoraClient(appSettings: Properties): FedoraClient {
return FedoraClientImpl.builder() return FedoraClientImpl.builder()
.properties(appSettings, FEDORA_PROPERTIES_PREFIX) .properties(appSettings, FEDORA_PROPERTIES_PREFIX)
...@@ -42,7 +46,7 @@ class Service(fileName: String = "app.yml") : Closeable { ...@@ -42,7 +46,7 @@ class Service(fileName: String = "app.yml") : Closeable {
} }
} }
val settings = SettingsLoader( private val settings = SettingsLoader(
listOf( listOf(
"$FEDORA_PROPERTIES_PREFIX.internalBaseUrl", "$FEDORA_PROPERTIES_PREFIX.internalBaseUrl",
"$FEDORA_PROPERTIES_PREFIX.externalBaseUrl", "$FEDORA_PROPERTIES_PREFIX.externalBaseUrl",
...@@ -82,14 +86,6 @@ class Service(fileName: String = "app.yml") : Closeable { ...@@ -82,14 +86,6 @@ class Service(fileName: String = "app.yml") : Closeable {
} }
private fun processRecord(record: ConsumerRecord<String, String>): Report { private fun processRecord(record: ConsumerRecord<String, String>): Report {
if (record.value().contains("ERROR")) {
return Report(
id = record.key(),
status = ReportStatus.failure,
message = ReportMessages.ingestFailed(record.key())
)
}
val ingester = Ingester( val ingester = Ingester(
settings.sftpSettings, settings.sftpSettings,
createFedoraClient(settings.appSettings), createFedoraClient(settings.appSettings),
......
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