RicoResource.kt 5.16 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*
   Copyright 2020 Jonas Waeber

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
Jonas Waeber's avatar
Jonas Waeber committed
16
17
package ch.memobase.rdf

Jonas Waeber's avatar
Jonas Waeber committed
18
19
import ch.memobase.exceptions.InvalidInputException
import org.apache.jena.rdf.model.*
Jonas Waeber's avatar
Jonas Waeber committed
20
21
import org.apache.jena.riot.RDFDataMgr
import org.apache.jena.riot.RDFFormat
22
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
23
import java.io.StringWriter
Jonas Waeber's avatar
Jonas Waeber committed
24
25
26
27

class RicoResource(val resource: Resource) {
    constructor(resource: Resource, rdfType: Resource) : this(resource.addProperty(RDF.type, rdfType))
    constructor(resource: Resource, rdfType: Resource, ricoType: String) : this(
Jonas Waeber's avatar
Jonas Waeber committed
28
        resource.addProperty(RDF.type, rdfType).addProperty(RICO.type, ricoType)
Jonas Waeber's avatar
Jonas Waeber committed
29
30
    )

31
32
33
    private val log = LogManager.getLogger("RicoResource")

    private var rdfType = retrieveRdfType(resource)
Jonas Waeber's avatar
Jonas Waeber committed
34
35
    private val ricoType: String? = resource.getProperty(RICO.type)?.string

36
37
38
39
    private fun retrieveRdfType(resource: Resource): Resource {
        return try {
            resource.getProperty(RDF.type).resource
        } catch (ex: IllegalStateException) {
Jonas Waeber's avatar
Jonas Waeber committed
40
41
42
43
44
45
            val id = if (resource.isAnon) {
                resource.id.labelString
            } else {
                resource.uri
            }
            val message = "Resource without a rdf:type: $id" + ex.localizedMessage
46
47
48
49
50
            log.error(message)
            throw InvalidInputException(message)
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
    private fun getLabel(): String {
        return when (rdfType) {
            RICO.Identifier -> getStringLiteral(RICO.identifier).orEmpty()
            RICO.Record, RICO.Title -> getStringLiteral(RICO.title).orEmpty()
            RICO.DateRange, RICO.DateSet, RICO.SingleDate ->
                getStringLiteral(RICO.normalizedDateValue) ?: getStringLiteral(RICO.expressedDate).orEmpty()
            else -> getStringLiteral(RICO.name) ?: ""
        }
    }

    fun hasType(type: Resource): Boolean {
        return rdfType == type
    }

    fun hasProperty(property: Property): Boolean {
        return resource.hasProperty(property)
    }

    fun hasProperty(property: Property, value: String): Boolean {
        return resource.hasProperty(property, value)
    }

    fun hasProperty(property: Property, value: Resource): Boolean {
        return resource.hasProperty(property, value)
    }

    fun hasProperty(property: Property, value: RicoResource): Boolean {
        return resource.hasProperty(property, value.resource)
    }

    fun hasLiteral(property: Property, value: String): Boolean {
        return resource.hasLiteral(property, value)
    }

    fun listProperties(property: Property): Iterable<Statement> {
        return resource.listProperties(property).toList()
    }

    fun listProperties(): Iterable<Statement> {
        return resource.listProperties().toList()
    }

    fun getStringLiteral(property: Property): String? {
        return resource.getProperty(property)?.string
    }

    fun getLiteralLanguage(property: Property): String {
        return resource.getProperty(property).language
    }

    fun getResource(property: Property): RicoResource {
        return RicoResource(resource.getPropertyResourceValue(property))
    }

    fun addProperty(property: Property, objectResource: Resource): RicoResource {
        resource.addProperty(property, objectResource)
        return this
    }

    fun addProperty(property: Property, objectResource: RicoResource): RicoResource {
        resource.addProperty(property, objectResource.resource)
        return this
    }


    fun addProperty(predicate: Property, rdfNode: RDFNode): RicoResource {
        resource.addProperty(predicate, rdfNode)
        return this
    }

    fun addLiteral(property: Property, value: String): RicoResource {
        resource.addProperty(property, value)
        return this
    }

    fun addLiteral(property: Property, value: String, lang: String): RicoResource {
        resource.addProperty(property, value, lang)
        return this
    }

    fun removeAllProperties(property: Property): RicoResource {
        resource.removeAll(property)
        return this
    }

    fun removeAllProperties(property: Property, value: String): RicoResource {
        resource.model.remove(resource.listProperties(property).filterKeep { it.`object`.isLiteral }
Jonas Waeber's avatar
Jonas Waeber committed
138
            .filterKeep { it.string == value }.toList())
Jonas Waeber's avatar
Jonas Waeber committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
        return this
    }

    fun replaceRdfType(new: Resource): RicoResource {
        resource.removeAll(RDF.type)
        resource.addProperty(RDF.type, new)
        rdfType = new
        return this
    }

    override fun toString(): String {
        val out = StringWriter()
        val model = ModelFactory.createDefaultModel()
        model.add(resource.listProperties())
        RDFDataMgr.write(out, model, RDFFormat.TURTLE_PRETTY)
        return out.toString()
    }
}