Commit 45c44e78 authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Implement prefix field for datatype properties

parent b2837554
......@@ -24,6 +24,7 @@ import org.memobase.mapping.fields.ConfigField
import org.memobase.mapping.fields.ConstField
import org.memobase.mapping.fields.LanguageField
import org.memobase.mapping.fields.ListField
import org.memobase.mapping.fields.PrefixField
import org.memobase.mapping.fields.SimpleField
import org.memobase.mapping.mappers.AgentFieldMapper
import org.memobase.mapping.mappers.ConstantFieldMapper
......@@ -34,6 +35,7 @@ import org.memobase.mapping.mappers.LanguageMapper
import org.memobase.mapping.mappers.LanguageFieldMapper
import org.memobase.mapping.mappers.ListFieldMapper
import org.memobase.mapping.mappers.PlaceFieldMapper
import org.memobase.mapping.mappers.PrefixFieldMapper
import org.memobase.mapping.mappers.RuleFieldMapper
import org.memobase.mapping.mappers.SkosConceptFieldMapper
import org.memobase.mapping.mappers.SimpleFieldMapper
......@@ -43,6 +45,7 @@ import org.memobase.rdf.SKOS
import kotlin.reflect.full.createInstance
import kotlin.system.exitProcess
@Suppress("UNCHECKED_CAST")
class MappingConfig(directory: String) {
var uriField: String = ""
var recordType = ""
......@@ -218,10 +221,12 @@ class MappingConfig(directory: String) {
is ConstField -> ConstantFieldMapper(field)
is ListField -> ListFieldMapper(field)
is LanguageField -> LanguageFieldMapper(field)
is PrefixField -> PrefixFieldMapper(field)
else -> throw InvalidMappingException("This can't happen since there is no other class of this type...")
}
}
@Suppress("UNCHECKED_CAST")
private fun buildIdentifierMapper(value: Any?): List<IFieldMapper> {
return when (value) {
is Map<*, *> -> {
......@@ -236,6 +241,7 @@ class MappingConfig(directory: String) {
}
}
@Suppress("UNCHECKED_CAST")
private fun buildAgentMapper(key: String, value: Any?): List<IFieldMapper> {
when (value) {
is Map<*, *> -> {
......@@ -375,6 +381,18 @@ class MappingConfig(directory: String) {
is Map<*, *> -> {
return if (value.containsKey("const")) {
ConstField(entry.key, value["const"] as String)
} else if (value.containsKey("prefix")) {
value["prefix"].let {
if (it is Map<*, *>) {
if (it.containsKey("field") && it.containsKey("prefix")) {
PrefixField(entry.key, it["prefix"] as String, it["field"] as String)
} else {
throw InvalidMappingException("The prefix field under ${entry.key} expected a map with the keys 'prefix' and 'field'.")
}
} else {
throw InvalidMappingException("The prefix field expects a mapping inside. Found $it instead.")
}
}
} else {
val mutableList = mutableListOf<Pair<String, List<String>>>()
for (k in value.keys) {
......
/*
* mapper-service
* Copyright (C) 2020 Memoriav
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.memobase.mapping.fields
data class PrefixField(val key: String, val prefix: String, val field: String) : ConfigField()
\ No newline at end of file
/*
* mapper-service
* Copyright (C) 2020 Memoriav
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package org.memobase.mapping.mappers
import org.memobase.builder.IResource
import org.memobase.mapping.fields.ConstField
import org.memobase.mapping.fields.PrefixField
class PrefixFieldMapper(private val prefixField: PrefixField) : IFieldMapper {
override fun apply(source: Map<String, String>, subject: IResource) {
source[prefixField.field].let {
if (it != null) {
subject.addStringLiteral(prefixField.key, prefixField.prefix + it.trim())
}
}
}
}
\ 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