Transform.kt 5.58 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
/*
 * Drupal Sync 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/>.
 */

Jonas Waeber's avatar
Jonas Waeber committed
19
20
package org.memobase

Jonas Waeber's avatar
Jonas Waeber committed
21
import org.apache.jena.rdf.model.*
Jonas Waeber's avatar
Jonas Waeber committed
22
23
24
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
25
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
26
import org.memobase.model.*
27
28
29
30
import org.memobase.rdf.NS
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
import org.memobase.rdf.WD
Jonas Waeber's avatar
Jonas Waeber committed
31
32
import java.io.StringWriter

Jonas Waeber's avatar
Jonas Waeber committed
33
34
class Transform(private val municipalities: Map<String, Municipality>) {
    private val log = LogManager.getLogger("Transform")
Jonas Waeber's avatar
Jonas Waeber committed
35

Jonas Waeber's avatar
Jonas Waeber committed
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

    fun createInstitution(input: Institution): Pair<String, Model> {
        val model = ModelFactory.createDefaultModel()

        val resource = model.createResource(NS.mbcb + input.field_memobase_id)

        val identifier = model.createResource()
        identifier.addProperty(RDF.type, RICO.Identifier)
        identifier.addProperty(RICO.type, literal("main"))
        identifier.addProperty(RICO.identifier, literal(input.field_memobase_id))
        resource.addProperty(RICO.identifiedBy, identifier)

        // TODO: proper multi language integration!
        resource.addProperty(RICO.name, langLiteral(input.title, "de"))
        resource.addProperty(RICO.name, langLiteral(input.title_fr, "fr"))
        resource.addProperty(RICO.name, langLiteral(input.title_it, "it"))
        resource.addProperty(RICO.descriptiveNote, langLiteral(input.field_text.value, "de"))
        resource.addProperty(RICO.descriptiveNote, langLiteral(input.field_text_fr.value, "fr"))
        resource.addProperty(RICO.descriptiveNote, langLiteral(input.field_text_it.value, "it"))

        input.field_address.forEach {address ->
            val location = generateLocationResource(address)
            resource.addProperty(RICO.hasLocation, location)
Jonas Waeber's avatar
Jonas Waeber committed
59
60
        }

Jonas Waeber's avatar
Jonas Waeber committed
61
62
63
64
65
66
67
68
69
        input.field_isil.let {
            if (it != null)
                resource.addProperty(WD.isil, literal(it))
        }
        input.field_email.let {
            if (it != null) {
                resource.addProperty(WD.emailAddress, literal(it))
            }
        }
70

Jonas Waeber's avatar
Jonas Waeber committed
71
72
73
74
75
76
77
78
79
        input.field_website.let {
            if (it != null)
                resource.addProperty(WD.website, literal(it.uri))
        }
        input.field_link_archive_catalog.let {
            if (it != null)
                resource.addProperty(WD.onlineArchive, literal(it.uri))
        }
        return Pair(resource.uri, model)
Jonas Waeber's avatar
Jonas Waeber committed
80
81
    }

Jonas Waeber's avatar
Jonas Waeber committed
82
83
    fun createRecordSet(input: RecordSet): Pair<String, Model> {
        return Pair("", ModelFactory.createDefaultModel())
Jonas Waeber's avatar
Jonas Waeber committed
84
85
    }

Jonas Waeber's avatar
Jonas Waeber committed
86
87
    private fun generateLocationResource(address: Address): Resource {
        val location = ResourceFactory.createResource()
88

Jonas Waeber's avatar
Jonas Waeber committed
89
90
91
92
93
94
        val streetAddress = address.address_line1
        val secondAddressLine = address.address_line2
        val combinedStreetAddress = if (secondAddressLine.isNullOrEmpty()) {
            streetAddress
        } else {
            streetAddress + "\n" + secondAddressLine
Jonas Waeber's avatar
Jonas Waeber committed
95
        }
Jonas Waeber's avatar
Jonas Waeber committed
96
97
98
99
100
101
        val streetNumber = streetAddress.substringAfterLast(" ")
        val street = streetAddress.replace(streetNumber, "").trim()
        location.addProperty(WD.street, literal(street))
        location.addProperty(WD.streetNumber, literal(streetNumber))
        location.addProperty(WD.streetAddress, literal(combinedStreetAddress))

Jonas Waeber's avatar
Jonas Waeber committed
102

Jonas Waeber's avatar
Jonas Waeber committed
103
        val postalCode = address.postal_code.trim()
Jonas Waeber's avatar
Jonas Waeber committed
104
105
106
107
108
109
110
        val municipality = if (municipalities.containsKey(postalCode)) {
            municipalities[postalCode]
        } else {
            // the input validation in drupal should ensure that this never happens.
            log.error("Invalid postal code: $postalCode")
            null
        }
111
        location.addProperty(RDF.type, RICO.Place)
Jonas Waeber's avatar
Jonas Waeber committed
112
113
114
115
        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
Jonas Waeber's avatar
Jonas Waeber committed
116
            location.addProperty(WD.adminUnit, ResourceFactory.createResource(municipality.canton))
Jonas Waeber's avatar
Jonas Waeber committed
117
            // city
Jonas Waeber's avatar
Jonas Waeber committed
118
119
            location.addProperty(WD.adminUnit, ResourceFactory.createResource(municipality.id))
            // TODO: Replace this with the coordinates from source!
Jonas Waeber's avatar
Jonas Waeber committed
120
121
122
123
124
125
            municipality.coordinates.forEach { coordinate ->
                location.addProperty(WD.coordinates, literal(coordinate))
            }
        }
        // country is currently hard coded to switzerland!
        location.addProperty(WD.country, WD.switzerland)
Jonas Waeber's avatar
Jonas Waeber committed
126
127
128
129
130
131
132
133
        return location
    }

    fun write(uri: String, model: Model): KeyValue<String, String> {
        return StringWriter().use { writer ->
            RDFDataMgr.write(writer, model, RDFFormat.NTRIPLES_UTF8)
            return@use KeyValue(uri, writer.toString().trim())
        }
Jonas Waeber's avatar
Jonas Waeber committed
134
    }
Jonas Waeber's avatar
Jonas Waeber committed
135

Jonas Waeber's avatar
Jonas Waeber committed
136
137
    private fun langLiteral(text: String, language: String): Literal = ResourceFactory.createLangLiteral(text.trim(), language)
    private fun literal(text: String): Literal = ResourceFactory.createPlainLiteral(text.trim())
Jonas Waeber's avatar
Jonas Waeber committed
138
}