Commit 888f104a authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Change record type mapping

Can now use any field type
parent 09ecfb38
......@@ -21,6 +21,7 @@ package org.memobase.builder
import org.apache.jena.rdf.model.Resource
import org.memobase.helpers.StringHelpers
import org.memobase.mapping.KEYS
import org.memobase.mapping.fields.SimpleAnnotationField
import org.memobase.rdf.NS
import org.memobase.rdf.RICO
......
......@@ -21,12 +21,15 @@ package org.memobase.builder
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
import org.memobase.mapping.exceptions.InvalidMappingException
import org.memobase.mapping.fields.ConstantField
import org.memobase.mapping.fields.MappedAnnotationField
import org.memobase.mapping.fields.SimpleAnnotationField
import org.memobase.mapping.mappers.IFieldMapper
import java.io.StringWriter
class ResourceBuilder(
recordIdField: String,
recordType: String,
recordType: SimpleAnnotationField,
recordFieldMappers: List<IFieldMapper>,
physicalObjectFieldMappers: List<IFieldMapper>,
digitalObjectFieldMappers: List<IFieldMapper>,
......@@ -41,8 +44,17 @@ class ResourceBuilder(
} else {
throw InvalidMappingException("No id for record found in field '$recordIdField' for source $source.")
}
val recordTypeValue = when (recordType) {
is MappedAnnotationField -> source[recordType.field].let {
it ?: throw InvalidMappingException("No type for record in field '${recordType.field} for source $source.")
}
is ConstantField -> recordType.constant
}
val record =
Record(recordId, recordType, recordSetId, institutionId)
Record(recordId, recordTypeValue, recordSetId, institutionId)
for (recordFieldMapper in recordFieldMappers) {
recordFieldMapper.apply(source, record)
}
......
......@@ -49,13 +49,12 @@ import kotlin.system.exitProcess
@Suppress("UNCHECKED_CAST")
class MappingConfig(directory: String) {
var uriField: String = ""
var recordType = ""
private var localRecordType: SimpleAnnotationField? = null
val recordType: SimpleAnnotationField
val recordFieldMappers = mutableListOf<IFieldMapper>()
val physicalObjectFieldMappers = mutableListOf<IFieldMapper>()
val digitalObjectFieldMappers = mutableListOf<IFieldMapper>()
private val ruleTypes = listOf(KEYS.holder, KEYS.access, KEYS.usage)
private val recordTypes = listOf("Film", "Foto", "Radio", "Ton", "Tonbildschau", "TV", "Video")
private val log = LogManager.getLogger("MappingConfigParser")
......@@ -88,8 +87,10 @@ class MappingConfig(directory: String) {
log.error("Mapping is missing uri field in record, which is required.")
exitProcess(1)
}
if (recordType == "") {
log.error("Mapping is missing record.type field which is required. Assign one value out of $recordTypes.")
if (localRecordType != null) {
recordType = localRecordType as SimpleAnnotationField
} else {
log.error("Mapping is missing record.type field which is required. Assign one value out of $recordTypes or a field.")
exitProcess(1)
}
}
......@@ -100,12 +101,7 @@ class MappingConfig(directory: String) {
when (val key = entry.key) {
KEYS.uri -> uriField = entry.value as String
KEYS.type -> {
val value = entry.value as String
if (recordTypes.contains(value)) {
recordType = value
} else {
throw InvalidMappingException("Record Type mapping is not a valid value. Choose one of $recordTypes.")
}
localRecordType = parseSimpleAnnotationField(key, entry.value)
}
// literal properties
KEYS.name, KEYS.title, KEYS.descriptiveNote, KEYS.scopeAndContent, KEYS.sameAs,
......
......@@ -29,6 +29,7 @@ import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.MethodSource
import org.memobase.helpers.StringHelpers
import org.memobase.mapping.MappingConfig
import org.memobase.mapping.fields.ConstantField
import org.memobase.settings.SettingsLoader
import java.io.File
import java.nio.charset.Charset
......@@ -48,7 +49,7 @@ class Tests {
val config = MappingConfig(configTestBasePath + "minimalValid")
assertThat(config).isNotNull
.hasFieldOrPropertyWithValue("uriField", "TestField")
.hasFieldOrPropertyWithValue("recordType", "Foto")
.hasFieldOrPropertyWithValue("recordType", ConstantField("type", "Foto"))
}
......
record:
uri: TestField
type: Foto
\ No newline at end of file
type:
const: Foto
\ No newline at end of file
record:
uri: signatur
type: Video # is always a constant!
type:
const: Video
digital:
locator: # ebucore:locator
......
record:
uri: signatur
type: Tonbildschau # is always a constant!
type:
const: Tonbildschau
title:
de: titel
descriptiveNote: # rico:descriptiveNote
......
record:
uri: signatur
type: Foto
type:
const: Foto
physical:
physicalCharacteristics: # rico:physicalCharacteristics
......
record:
uri: original_id
type: Foto # is always a constant!
type:
const: Foto
title:
de: titel
descriptiveNote: # rico:descriptiveNote
......
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