Transform.kt 3.41 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package org.memobase

import com.beust.klaxon.JsonObject
import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
import org.apache.kafka.streams.KeyValue
import org.memobase.rdf.*
import java.io.StringWriter

class Transform {

    private val model = ModelFactory.createDefaultModel()
    private var uri: String = ""

    fun createInstitution(source: JsonObject): Transform {
        val resource = model.createResource(NS.memint + source["field_memobase_id"])
        uri = resource.uri

        val identifier = model.createResource()
        identifier.addProperty(RDF.type, RICO.Identifier)
        identifier.addProperty(RICO.type, literal("main"))
        identifier.addProperty(RICO.identifier, literal(source["field_memobase_id"] as String))
        resource.addProperty(RICO.identifiedBy, identifier)

        resource.addProperty(WD.isil, literal(source["field_isil"] as String))
        resource.addProperty(RICO.name, langLiteral(source["field_name"] as String, "de"))
        resource.addProperty(RICO.descriptiveNote, langLiteral(source["field_text"] as String, "de"))

        (source["field_addresses"] as List<JsonObject>).forEach {
            val postalAddress = model.createResource()
            val streetAddress = it["address_line1"] as String
            val secondAddressLine = it["address_line2"] as String?
            val combinedStreetAddress = if (secondAddressLine != null) {
                streetAddress + "\n" + secondAddressLine
            } else {
                streetAddress
            }
            val streetNumber = streetAddress.substringAfterLast(" ")
            val street = streetAddress.replace(streetNumber, "").trim()
            postalAddress.addProperty(RDF.type, WD.postalAddress)
            postalAddress.addProperty(WD.street, literal(street))
            postalAddress.addProperty(WD.streetNumber, literal(streetNumber))
            postalAddress.addProperty(WD.streetAddress, literal(combinedStreetAddress))
            postalAddress.addProperty(WD.postalCode, literal(it["postal_code"] as String))
            postalAddress.addProperty(WD.adminUnit, literal(it["locality"] as String))
            postalAddress.addProperty(WD.adminUnit, literal(it["country"] as String))
            resource.addProperty(WD.streetAddress, postalAddress)
        }

        resource.addProperty(WD.website, literal(source["field_website"] as String))
        resource.addProperty(WD.emailAddress, literal(source["field_email"] as String))
        resource.addProperty(WD.onlineArchive, literal(source["field_online_archive"] as String))
        resource.addProperty(SCHEMA.sameAs, literal(source["wikidata_id"] as String))
        resource.addProperty(WD.image, literal(source["image"] as String))
        resource.addProperty(WD.logo, literal(source["logo"] as String))
        resource.addProperty(WD.typeOfInstitution, literal(source["instance_of"] as String))
        return this
    }


    fun write(): KeyValue<String, String> {
        return StringWriter().use { writer ->
            RDFDataMgr.write(writer, model, RDFFormat.NTRIPLES_UTF8)
            return@use KeyValue(uri, writer.toString().trim())
        }


    }


    private fun langLiteral(text: String, language: String): Literal = model.createLiteral(text.trim(), language)
    private fun literal(text: String): Literal = model.createLiteral(text.trim())


}