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

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

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

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

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

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

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

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

Jonas Waeber's avatar
Jonas Waeber committed
120
        val postalCode = address.postal_code.trim()
Jonas Waeber's avatar
Jonas Waeber committed
121

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

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

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

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

170 171


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