Transform.kt 7.46 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
Jonas Waeber's avatar
Jonas Waeber committed
38
39
import org.memobase.rdf.WD
import rdf.MB
Jonas Waeber's avatar
Jonas Waeber committed
40

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

Jonas Waeber's avatar
Jonas Waeber committed
46
47
48
49
50

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

        val resource = model.createResource(NS.mbcb + input.field_memobase_id)
51
52
        resource.addProperty(RDF.type, RICO.CorporateBody)
        resource.addProperty(RICO.type, "memobaseInstitution")
Jonas Waeber's avatar
Jonas Waeber committed
53
        resource.addLiteral(MB.isPublished, input.status)
Jonas Waeber's avatar
Jonas Waeber committed
54
55
56
57
58
59
60

        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
61
62
63
64
        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))
65
        resource.addProperty(RICO.identifiedBy, oldIdentifier)
Jonas Waeber's avatar
Jonas Waeber committed
66

Jonas Waeber's avatar
Jonas Waeber committed
67
68
69
70
        input.recordset_ids.forEach {
            resource.addProperty(RICO.isHolderOf, NS.mbrs + it)
        }

Jonas Waeber's avatar
Jonas Waeber committed
71
72
73
74
75
76
77
        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"))

Jonas Waeber's avatar
Jonas Waeber committed
78
        input.field_address.forEach { address ->
79
            val location = generateLocationResource(model, address)
Jonas Waeber's avatar
Jonas Waeber committed
80
            resource.addProperty(RICO.hasLocation, location)
Jonas Waeber's avatar
Jonas Waeber committed
81
        }
Jonas Waeber's avatar
Jonas Waeber committed
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
        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
99
100
101
        input.field_institution_types.forEach {
            resource.addProperty(WD.typeOfInstitution, model.createResource(NS.wd + it.substringAfterLast("/")))
        }
102
103
        if (input.computed_teaser_image_url != null)
            resource.addLiteral(WD.image, input.computed_teaser_image_url)
Jonas Waeber's avatar
Jonas Waeber committed
104
        return Pair(resource.uri, model)
Jonas Waeber's avatar
Jonas Waeber committed
105
106
    }

Jonas Waeber's avatar
Jonas Waeber committed
107
108
    fun createRecordSet(input: RecordSet): Pair<String, Model> {
        return Pair("", ModelFactory.createDefaultModel())
Jonas Waeber's avatar
Jonas Waeber committed
109
110
    }

111
112
    private fun generateLocationResource(model: Model, address: Address): Resource {
        val location = model.createResource()
113

Jonas Waeber's avatar
Jonas Waeber committed
114
115
116
117
118
119
        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
120
        }
Jonas Waeber's avatar
Jonas Waeber committed
121
122
123
124
125
126
        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
127

Jonas Waeber's avatar
Jonas Waeber committed
128
        val postalCode = address.postal_code.trim()
Jonas Waeber's avatar
Jonas Waeber committed
129

130
        location.addProperty(RDF.type, RICO.Place)
Jonas Waeber's avatar
Jonas Waeber committed
131
        location.addProperty(WD.postalCode, literal(postalCode))
Jonas Waeber's avatar
Jonas Waeber committed
132
133
134

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

135
        val canton = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
136
137
138
139
140
141
142
143
144
145
146
147
148
        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"))
            }
149
            canton.addProperty(RICO.type, literal("canton"))
Jonas Waeber's avatar
Jonas Waeber committed
150
151
        }

152
        val municipality = model.createResource()
Jonas Waeber's avatar
Jonas Waeber committed
153
154
155
156
157
158
159
160
161
162
        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
163
            }
164
            municipality.addProperty(RICO.type, "municipality")
Jonas Waeber's avatar
Jonas Waeber committed
165
166
167
        }
        // country is currently hard coded to switzerland!
        location.addProperty(WD.country, WD.switzerland)
Jonas Waeber's avatar
Jonas Waeber committed
168
169
170
171
172
173
174
175
        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
176
    }
Jonas Waeber's avatar
Jonas Waeber committed
177

178

Jonas Waeber's avatar
Jonas Waeber committed
179
180
    private fun langLiteral(text: String, language: String): Literal =
        ResourceFactory.createLangLiteral(text.trim(), language)
181

Jonas Waeber's avatar
Jonas Waeber committed
182
    private fun literal(text: String): Literal = ResourceFactory.createPlainLiteral(text.trim())
Jonas Waeber's avatar
Jonas Waeber committed
183
}