RecordResource.kt 6.54 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
19
20
21
22
23
/*
 * mapper-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/>.
 */

package org.memobase.builder

import org.apache.jena.rdf.model.Literal
import org.apache.jena.rdf.model.Model
import org.apache.jena.rdf.model.ModelFactory
24
import org.apache.jena.rdf.model.Property
Jonas Waeber's avatar
Jonas Waeber committed
25
import org.apache.jena.rdf.model.Resource
26
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
30
31
32
33
import org.memobase.mapping.KEYS
import org.memobase.rdf.DC
import org.memobase.rdf.EBUCORE
import org.memobase.rdf.NS
import org.memobase.rdf.RDA
import org.memobase.rdf.RDF
import org.memobase.rdf.RICO
Jonas Waeber's avatar
Jonas Waeber committed
34
import org.memobase.rdf.SKOS
Jonas Waeber's avatar
Jonas Waeber committed
35
36

abstract class RecordResource(institutionId: String) : IResource {
37
    private val log = LogManager.getLogger("ResourceBuilder")
Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

    val model: Model = ModelFactory.createDefaultModel()

    abstract val resource: Resource
    protected val institutionUri: Resource = institutionUri(institutionId)

    init {
        model.setNsPrefix("rico", NS.rico)
        model.setNsPrefix("rdf", NS.rdf)
        model.setNsPrefix("dct", NS.dct)
        model.setNsPrefix("ebucore", NS.ebucore)
        model.setNsPrefix("skos", NS.skos)
        model.setNsPrefix("rdau", NS.rdau)
    }

    protected fun addRdfType(type: Resource) {
        resource.addProperty(RDF.type, type)
    }
Jonas Waeber's avatar
Jonas Waeber committed
56
    override fun addLiteral(property: String, value: Literal) {
Jonas Waeber's avatar
Jonas Waeber committed
57
58
59
        resource.addLiteral(KEYS.keysToPropertyMap[property], value)
    }

60
61
62
63
64
65
66
67
68
69
    override fun addRicoConcept(rdfType: String, ricoType: String, value: List<Literal>) {
        when (rdfType) {
            KEYS.titles ->
                addRicoConcept(RICO.Title, ricoType, RICO.title, value, RICO.hasTitle)
            KEYS.identifiers ->
                addRicoConcept(RICO.Identifier, ricoType, RICO.identifier, value, RICO.identifiedBy)
            KEYS.languages ->
                addRicoConcept(RICO.Language, ricoType, RICO.name, value, RICO.hasLanguage)
            else -> throw Exception("Unknown rdf:type alias for rico:Concept $rdfType.")
        }
Jonas Waeber's avatar
Jonas Waeber committed
70
71
    }

72
    private fun addRicoConcept(rdfType: Resource, ricoType: String, valueProperty: Property, value: List<Literal>, resourceProperty: Property) {
Jonas Waeber's avatar
Jonas Waeber committed
73
        val blank = model.createResource()
74
75
76
77
78
79
        blank.addProperty(RDF.type, rdfType)
        blank.addProperty(RICO.type, ricoType)
        value.forEach {
            blank.addProperty(valueProperty, it)
        }
        resource.addProperty(resourceProperty, blank)
Jonas Waeber's avatar
Jonas Waeber committed
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
    }

    override fun addSkosConcept(type: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, SKOS.Concept)
        for (property in properties) {
            blank.addProperty(SKOS.get(property.first), property.second)
        }
        when (type) {
            KEYS.genre -> resource.addProperty(EBUCORE.hasGenre, blank)
            KEYS.subject -> resource.addProperty(RICO.hasSubject, blank)
        }
    }

    override fun addPlace(type: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.Place)
        for (property in properties) {
            blank.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        when (type) {
            KEYS.placeOfCapture -> resource.addProperty(RDA.hasPlaceOfCapture, blank)
            KEYS.relatedPlaces -> resource.addProperty(DC.spatial, blank)
        }
    }

    override fun addDate(property: String, value: String) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.DateSet)
        blank.addProperty(RICO.expressedDate, literal(value))
        resource.addProperty(KEYS.keysToPropertyMap[property], blank)
    }

    override fun addCreationRelation(type: String, agentType: String, properties: List<Pair<String, Literal>>) {
        val relation = model.createResource()
        relation.addProperty(RICO.type, type)
        relation.addProperty(RICO.creationRelationHasSource, resource)
        relation.addProperty(RDF.type, RICO.CreationRelation)
        val agent = model.createResource()
        agent.addProperty(RDF.type, KEYS.agentTypeMap[agentType])
120
        agent.addProperty(RICO.agentIsTargetOfCreationRelation, relation)
Jonas Waeber's avatar
Jonas Waeber committed
121
122
123
124
125
126
127
        for (property in properties) {
            agent.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        relation.addProperty(RICO.creationRelationHasTarget, agent)
        resource.addProperty(RICO.recordResourceOrInstantiationIsSourceOfCreationRelation, relation)
    }

128
129
130
131
132
133
134
135
136
    override fun addAgent(relation: String, agentType: String, properties: List<Pair<String, Literal>>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, KEYS.agentTypeMap[agentType])
        for (property in properties) {
            blank.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        resource.addProperty(KEYS.agentPropertiesMap[relation], blank)
    }

Jonas Waeber's avatar
Jonas Waeber committed
137
    override fun addRule(type: String, value: List<Pair<String, List<Literal>>>) {
138
139
140
141
142
        if (value.isNotEmpty()) {
            val blank = model.createResource()
            blank.addProperty(RDF.type, RICO.Rule)
            blank.addProperty(RICO.type, literal(type))
            value.forEach {
Jonas Waeber's avatar
Jonas Waeber committed
143
144
145
                it.second.forEach { literal ->
                    blank.addProperty(KEYS.keysToPropertyMap[it.first], literal)
                }
146
147
148
149
150
            }
            blank.addProperty(RICO.regulates, resource)
            resource.addProperty(RICO.regulatedBy, blank)
        } else {
            log.error("Add no rule for type $type, because there were no content properties mapped.")
151
152
153
154
155
        }
    }

    override fun langLiteral(text: String, language: String): Literal = model.createLiteral(text.trim(), language)
    override fun literal(text: String): Literal = model.createLiteral(text.trim())
Jonas Waeber's avatar
Jonas Waeber committed
156
157
158
159
160


    protected fun uri(ns: String, name: String): String = ns + name
    private fun institutionUri(id: String): Resource = model.createResource(uri(NS.memint, id))
}