Transform.kt 6.85 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.Municipality
27
28
29
import org.memobase.rdf.NS
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
Jonas Waeber's avatar
Jonas Waeber committed
30
import rdf.WD
Jonas Waeber's avatar
Jonas Waeber committed
31
import java.io.StringWriter
Jonas Waeber's avatar
Jonas Waeber committed
32
33
34
35
import org.memobase.model.Address
import org.memobase.model.Institution
import org.memobase.model.RecordSet
import org.memobase.rdf.SCHEMA
Jonas Waeber's avatar
Jonas Waeber committed
36

Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
class Transform {
    private val log = LogManager.getLogger("InstitutionTransform")
    private val cantons = Helpers.getCantons()
    private val municipalities = Helpers.getMunicipalities()
Jonas Waeber's avatar
Jonas Waeber committed
41

Jonas Waeber's avatar
Jonas Waeber committed
42
43
44
45
46
47
48
49
50
51
52
53

    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)

Jonas Waeber's avatar
Jonas Waeber committed
54
55
56
57
58
59
        val oldIdentifier = model.createResource()
        oldIdentifier.addProperty(RDF.type, RICO.Identifier)
        oldIdentifier.addProperty(RICO.type, literal("oldMemobase"))
        oldIdentifier.addProperty(RICO.identifier, literal(input.field_old_memobase_id))
        resource.addProperty(RICO.identifiedBy, identifier)

Jonas Waeber's avatar
Jonas Waeber committed
60
61
62
63
64
65
66
67
68
69
        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
70
71
        }

Jonas Waeber's avatar
Jonas Waeber committed
72
73
74
75
76
77
78
79
80
        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))
            }
        }
81

Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
85
86
87
88
89
        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))
        }
Jonas Waeber's avatar
Jonas Waeber committed
90
91
92
        input.field_institution_types.forEach {
            resource.addProperty(WD.typeOfInstitution, model.createResource(NS.wd + it.substringAfterLast("/")))
        }
Jonas Waeber's avatar
Jonas Waeber committed
93
        return Pair(resource.uri, model)
Jonas Waeber's avatar
Jonas Waeber committed
94
95
    }

Jonas Waeber's avatar
Jonas Waeber committed
96
97
    fun createRecordSet(input: RecordSet): Pair<String, Model> {
        return Pair("", ModelFactory.createDefaultModel())
Jonas Waeber's avatar
Jonas Waeber committed
98
99
    }

Jonas Waeber's avatar
Jonas Waeber committed
100
101
    private fun generateLocationResource(address: Address): Resource {
        val location = ResourceFactory.createResource()
102

Jonas Waeber's avatar
Jonas Waeber committed
103
104
105
106
107
108
        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
109
        }
Jonas Waeber's avatar
Jonas Waeber committed
110
111
112
113
114
115
        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
116

Jonas Waeber's avatar
Jonas Waeber committed
117
        val postalCode = address.postal_code.trim()
Jonas Waeber's avatar
Jonas Waeber committed
118

119
        location.addProperty(RDF.type, RICO.Place)
Jonas Waeber's avatar
Jonas Waeber committed
120
        location.addProperty(WD.postalCode, literal(postalCode))
Jonas Waeber's avatar
Jonas Waeber committed
121
122

        location.addProperty(WD.coordinates, literal(address.coordinates))
Jonas Waeber's avatar
Jonas Waeber committed
123
        // does not enrich city, canton or cantons, if the postal code is not in the list.
Jonas Waeber's avatar
Jonas Waeber committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151

        val canton = ResourceFactory.createResource()
        canton.addProperty(RDF.type, RICO.Place)
        location.addProperty(WD.adminUnit, canton)
        cantons[address.administrative_area].let {
            // this should always be the case!
            if (it != null) {
                canton.addProperty(RICO.name, langLiteral(it.de, "de"))
                canton.addProperty(RICO.name, langLiteral(it.fr, "fr"))
                canton.addProperty(RICO.name, langLiteral(it.it, "it"))
                canton.addProperty(SCHEMA.sameAs, it.id)
            } else {
                // this shouldn't happen!
                canton.addProperty(RICO.name, literal("Unknown"))
            }
        }

        val municipality = ResourceFactory.createResource()
        municipality.addProperty(RDF.type, RICO.Place)
        location.addProperty(WD.adminUnit, municipality)
        municipalities[postalCode].let {
            if (it != null) {
                municipality.addProperty(RICO.name, langLiteral(it.de, "de"))
                municipality.addProperty(RICO.name, langLiteral(it.fr, "fr"))
                municipality.addProperty(RICO.name, langLiteral(it.it, "it"))
                municipality.addProperty(SCHEMA.sameAs, it.id)
            } else {
                municipality.addProperty(RICO.name, literal(address.locality))
Jonas Waeber's avatar
Jonas Waeber committed
152
153
154
155
            }
        }
        // country is currently hard coded to switzerland!
        location.addProperty(WD.country, WD.switzerland)
Jonas Waeber's avatar
Jonas Waeber committed
156
157
158
159
160
161
162
163
        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
164
    }
Jonas Waeber's avatar
Jonas Waeber committed
165

Jonas Waeber's avatar
Jonas Waeber committed
166
167
    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
168
}