Transform.kt 7.02 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

21
22
23
24
25
26
import java.io.StringWriter
import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.Model
import org.apache.jena.rdf.model.ModelFactory
import org.apache.jena.rdf.model.Resource
import org.apache.jena.rdf.model.ResourceFactory
Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
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
30
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
31
32
33
import org.memobase.model.Address
import org.memobase.model.Institution
import org.memobase.model.RecordSet
34
35
36
import org.memobase.rdf.NS
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
Jonas Waeber's avatar
Jonas Waeber committed
37
import org.memobase.rdf.SCHEMA
38
import rdf.WD
Jonas Waeber's avatar
Jonas Waeber committed
39

Jonas Waeber's avatar
Jonas Waeber committed
40
41
42
43
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
44

Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
48
49
50
51
52
53
54
55
56

    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
57
58
59
60
        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))
61
        resource.addProperty(RICO.identifiedBy, oldIdentifier)
Jonas Waeber's avatar
Jonas Waeber committed
62

Jonas Waeber's avatar
Jonas Waeber committed
63
64
65
66
67
68
69
70
        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 ->
71
            val location = generateLocationResource(model, address)
Jonas Waeber's avatar
Jonas Waeber committed
72
            resource.addProperty(RICO.hasLocation, location)
Jonas Waeber's avatar
Jonas Waeber committed
73
        }
Jonas Waeber's avatar
Jonas Waeber committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
        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))
            }
        }
        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
91
92
93
        input.field_institution_types.forEach {
            resource.addProperty(WD.typeOfInstitution, model.createResource(NS.wd + it.substringAfterLast("/")))
        }
Jonas Waeber's avatar
Jonas Waeber committed
94
        return Pair(resource.uri, model)
Jonas Waeber's avatar
Jonas Waeber committed
95
96
    }

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

101
102
    private fun generateLocationResource(model: Model, address: Address): Resource {
        val location = model.createResource()
103

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

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

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

        location.addProperty(WD.coordinates, literal(address.coordinates))

125
        val canton = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
126
127
128
129
130
131
132
133
134
135
136
137
138
        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"))
            }
139
            canton.addProperty(RICO.type, literal("canton"))
Jonas Waeber's avatar
Jonas Waeber committed
140
141
        }

142
        val municipality = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
143
144
145
146
147
148
149
150
151
152
        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
153
            }
154
            municipality.addProperty(RICO.type, "municipality")
Jonas Waeber's avatar
Jonas Waeber committed
155
156
157
        }
        // country is currently hard coded to switzerland!
        location.addProperty(WD.country, WD.switzerland)
Jonas Waeber's avatar
Jonas Waeber committed
158
159
160
161
162
163
164
165
        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
166
    }
Jonas Waeber's avatar
Jonas Waeber committed
167

168
169


Jonas Waeber's avatar
Jonas Waeber committed
170
171
    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
172
}