Commit 3cad3d5b authored by Jonas Waeber's avatar Jonas Waeber

Make record id parsing type save and add some meaningful warning messages.

parent 97b6a20d
Pipeline #21597 passed with stage
in 2 minutes and 53 seconds
......@@ -22,6 +22,7 @@ import ch.memobase.mapping.KEYS
import ch.memobase.mapping.MapperConfiguration
import ch.memobase.mapping.fields.ConstantField
import ch.memobase.mapping.fields.MappedAnnotationField
import com.beust.klaxon.JsonArray
import java.io.StringWriter
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
......@@ -42,9 +43,15 @@ class ResourceBuilder(
fun extractRecordId(): ResourceBuilder {
if (source.containsKey(config.uri)) {
recordId = source[config.uri] as String
when (val id = source[config.uri]) {
is String -> recordId = id
is Int -> recordId = id.toString()
is JsonArray<*> -> "Found multiple values in the field '${config.uri}' for identifiers: ${id.joinToString(", ")}."
null -> errorMessage = "The value for id is 'null' in field '${config.uri}'."
else -> errorMessage = "Invalid value '$id' for id in field '${config.uri}'."
}
} else {
errorMessage = "No id for record found in field '${config.uri}' for source $source."
errorMessage = "No id for record found in field '${config.uri}'."
}
return this
}
......
......@@ -33,64 +33,19 @@ import org.junit.jupiter.api.TestInstance
class LocalTestRun {
private val headerMetadata = HeaderMetadata(
"snp-001",
"bab-001",
"1",
"snp",
"bab",
false,
"record",
"identifierMain",
0, 0, 0, 0
)
@Test
@Disabled
fun `test a single file`() {
val folder = "/home/jonas/memobase/data/snp-001"
val inputPath = "/home/jonas/memobase/data/test/step-2/Fonoteca-18BD1047_A29"
val outputPath = "/home/jonas/memobase/data/test/step-3/snp-001-18BD1047_A29"
val mappingFile = "/config/mapping.yml"
val klaxon = Klaxon()
val configurationParser = MappingConfigurationParser(File(folder + mappingFile).readBytes())
val configuration = configurationParser.get()
val item = klaxon.parse<Map<String, Any>>(File(inputPath)).orEmpty()
val builder = ResourceBuilder(
item,
configuration,
headerMetadata.institutionId,
headerMetadata.recordSetId,
headerMetadata.isPublished
)
val result = builder.extractRecordId()
.extractRecordTypeValue()
.generateRecord()
.generatePhysicalObject()
.generateDigitalObject()
.addDerivedConnection()
val writtenResult = result
.writeRecord(RDFFormat.NTRIPLES_UTF8)
val writtenResultTurtle = result
.writeRecord(RDFFormat.TURTLE_PRETTY)
FileOutputStream(File(outputPath)).use {
it.bufferedWriter().use { writer ->
writer.write(writtenResult.second)
}
}
FileOutputStream(File("output.ttl")).use {
it.bufferedWriter().use { writer ->
writer.write(writtenResultTurtle.second)
}
}
}
@Test
@Disabled
fun `test local folder`() {
val folder = "/home/jonas/memobase/data/snp-001"
val folder = "/home/jonas/memobase/data/bab-001"
val inputFolder = "/home/jonas/memobase/data/test/step-2"
val outputFolder = "/home/jonas/memobase/data/test/step-3"
val mappingFile = "/config/mapping.yml"
......
Markdown is supported
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