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
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.jena.riot.Lang
import org.apache.jena.riot.RDFDataMgr
import org.apache.log4j.LogManager
import org.memobase.rdf.EBUCORE
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
import java.io.StringReader
class RdfHandler(data: String, externalBaseUrl: String) {
private val log = LogManager.getLogger("RdfHandler")
class RdfHandler(data: String, private val externalBaseUrl: String) {
private val log = LogManager.getLogger("IngestRdfHandler")
private val model = ModelFactory.createDefaultModel()
private val externalBaseUrl = externalBaseUrl
init {
model.read(ByteArrayInputStream(data.toByteArray()), "", "NTRIPLES")
RDFDataMgr.read(model, ByteArrayInputStream(data.toByteArray()) , Lang.NTRIPLES)
}
fun getRecord(): Pair<String, Model> {
......@@ -45,7 +47,7 @@ class RdfHandler(data: String, externalBaseUrl: String) {
}
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> {
......@@ -79,9 +81,10 @@ class RdfHandler(data: String, externalBaseUrl: String) {
private fun replaceSftpLocator(uri: String, model: Model) {
var newStatement: Statement? = null
val string: String? = null
val replacementLocator = "$uri/${Service.BINARY_FILE_URI_PATH}"
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"))
if (it.`object`.asLiteral().string.startsWith(Service.SFTP_PREFIX)) {
newStatement = StatementImpl(it.subject, it.predicate, model.createLiteral(replacementLocator))
}
it
}.nextOptional()
......@@ -91,17 +94,16 @@ class RdfHandler(data: String, externalBaseUrl: String) {
model.add(newStatement)
}
}
log.error("Removed statement $removedStatement.")
log.error("Added statement $newStatement.")
log.info("Replaced sftp locator with fedora url: $replacementLocator.")
}
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)
}.mapWith {
val url = it.getProperty(EBUCORE.locator).`object`.asLiteral().string
if (url.startsWith("sftp:")) {
Pair(it.uri, url.replace("sftp:", ""))
if (url.startsWith(Service.SFTP_PREFIX)) {
Pair(it.uri, url.replace(Service.SFTP_PREFIX, ""))
} else {
Pair(it.uri, null)
}
......@@ -109,7 +111,7 @@ class RdfHandler(data: String, externalBaseUrl: 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)
}.mapWith {
Pair(it.uri, it.getProperty(EBUCORE.hasMimeType).`object`.asLiteral().string)
......
......@@ -31,10 +31,14 @@ import org.memobase.settings.SettingsLoader
class Service(fileName: String = "app.yml") : Closeable {
companion object {
const val DIGITAL_OBJECT_TYPE = "digitalObject"
const val FEDORA_PROPERTIES_PREFIX = "fedora"
const val CONSUMER_MAX_POLL_RECORDS = "10"
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 {
return FedoraClientImpl.builder()
.properties(appSettings, FEDORA_PROPERTIES_PREFIX)
......@@ -42,7 +46,7 @@ class Service(fileName: String = "app.yml") : Closeable {
}
}
val settings = SettingsLoader(
private val settings = SettingsLoader(
listOf(
"$FEDORA_PROPERTIES_PREFIX.internalBaseUrl",
"$FEDORA_PROPERTIES_PREFIX.externalBaseUrl",
......@@ -82,14 +86,6 @@ class Service(fileName: String = "app.yml") : Closeable {
}
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(
settings.sftpSettings,
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