RecordResource.kt 5.95 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
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * 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
import org.apache.jena.rdf.model.Resource
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
import org.memobase.skos.SKOS

abstract class RecordResource(institutionId: String) : IResource {

    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)
    }
    override fun addStringLiteral(property: String, value: String) {
        resource.addLiteral(KEYS.keysToPropertyMap[property], value)
    }

    override fun addLangLiteral(property: String, value: String, tag: String) {
        resource.addLiteral(KEYS.keysToPropertyMap[property], model.createLiteral(value, tag))
    }

    override fun addLanguage(type: String, text: String) {
        val blank = model.createResource()
        blank.addProperty(RICO.name, literal(text))
        blank.addProperty(RICO.type, type)
        blank.addProperty(RDF.type, RICO.Language)
        resource.addProperty(RICO.hasLanguage, blank)
    }

    override fun addTitle(type: String, text: String, language: String) {
        val blank = model.createResource()
        blank.addProperty(RICO.title, langLiteral(text, language))
        blank.addProperty(RICO.type, type)
        blank.addProperty(RDF.type, RICO.Title)
        blank.addProperty(RICO.isTitleOf, resource)
        resource.addProperty(RICO.hasTitle, blank)
    }

    protected fun addIdentifier(type: String, identifier: String, institution: Resource) {
        val blank = model.createResource()
80
        blank.addProperty(RICO.identifier, literal(identifier))
Jonas Waeber's avatar
Jonas Waeber committed
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
        blank.addProperty(RICO.type, type)
        blank.addProperty(RDF.type, RICO.Identifier)
        resource.addProperty(RICO.identifiedBy, blank)
    }

    override fun addIdentifier(type: String, identifier: String) {
        addIdentifier(type, identifier, institutionUri)
    }

    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])
        for (property in properties) {
            agent.addProperty(KEYS.keysToPropertyMap[property.first], property.second)
        }
        relation.addProperty(RICO.creationRelationHasTarget, agent)
        resource.addProperty(RICO.recordResourceOrInstantiationIsSourceOfCreationRelation, relation)
    }

135
136
137
138
139
140
141
    override fun addRule(type: String, value: List<Literal>) {
        val blank = model.createResource()
        blank.addProperty(RDF.type, RICO.Rule)
        blank.addProperty(RICO.type, literal(type))
        value.forEach {
            blank.addProperty(RICO.title, it)
        }
142
143
        blank.addProperty(RICO.regulates, resource)
        resource.addProperty(RICO.regulatedBy, blank)
144
145
146
147
    }

    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
148
149
150
151
152


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