Transform.kt 4.48 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
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
Jonas Waeber's avatar
Jonas Waeber committed
9
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
10
11
12
import org.memobase.rdf.*
import java.io.StringWriter

Jonas Waeber's avatar
Jonas Waeber committed
13
14
class Transform(private val municipalities: Map<String, Municipality>) {
    private val log = LogManager.getLogger("Transform")
Jonas Waeber's avatar
Jonas Waeber committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32

    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"))

Jonas Waeber's avatar
Jonas Waeber committed
33
34
35
36
        (source["field_addresses"] as List<JsonObject>).forEach { fieldAddress ->
            val location = model.createResource()
            val streetAddress = fieldAddress["address_line1"] as String
            val secondAddressLine = fieldAddress["address_line2"] as String?
Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
41
42
43
            val combinedStreetAddress = if (secondAddressLine != null) {
                streetAddress + "\n" + secondAddressLine
            } else {
                streetAddress
            }
            val streetNumber = streetAddress.substringAfterLast(" ")
            val street = streetAddress.replace(streetNumber, "").trim()
Jonas Waeber's avatar
Jonas Waeber committed
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

            val postalCode = (fieldAddress["postal_code"] as String).trim()
            val municipality = if (municipalities.containsKey(postalCode)) {
                municipalities[postalCode]
            } else {
                // TODO: This information needs to reach the user!
                log.error("Invalid postal code: $postalCode")
                null
            }

            location.addProperty(RDF.type, WD.location)
            location.addProperty(WD.street, literal(street))
            location.addProperty(WD.streetNumber, literal(streetNumber))
            location.addProperty(WD.streetAddress, literal(combinedStreetAddress))
            location.addProperty(WD.postalCode, literal(postalCode))
            // does not enrich city, canton or cantons, if the postal code is not in the list.
            if (municipality != null) {
                // canton
                location.addProperty(WD.adminUnit, model.createResource(municipality.canton))
                // city
                location.addProperty(WD.adminUnit, model.createResource(municipality.id))
                // coordinates
                municipality.coordinates.forEach { coordinate ->
                    location.addProperty(WD.coordinates, literal(coordinate))
                }
            }
            //val country = it["country_code"] as String
            // country is currently hard coded to switzerland!
            location.addProperty(WD.country, WD.switzerland)
            resource.addProperty(WD.streetAddress, location)
Jonas Waeber's avatar
Jonas Waeber committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        }

        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())


}