SearchDocTransform.kt 10.3 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
/*
 * search-doc-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

import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
23
import org.memobase.helpers.CarrierType
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.memobase.helpers.Extract
Jonas Waeber's avatar
Jonas Waeber committed
25
import org.memobase.helpers.Filter
26
import org.memobase.helpers.ReuseStatementMap
Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
import org.memobase.model.LanguageContainer
import org.memobase.model.NameContainer
import org.memobase.model.SearchDoc
30
import org.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
31

Jonas Waeber's avatar
Jonas Waeber committed
32
class SearchDocTransform {
Jonas Waeber's avatar
Jonas Waeber committed
33
    private val log = LogManager.getLogger("SearchDocTransform")
Jonas Waeber's avatar
Jonas Waeber committed
34
35
    fun transform(input: Map<String, JsonObject>): SearchDoc {
        val record = input["record"] ?: error("No record defined in this message.")
36
37
        val digitalObject = input.values.firstOrNull { it["@type"] == NS.rico + "Instantiation" && it["type"] == "digitalObject" }
        val physicalObject = input.values.firstOrNull { it["@type"] == NS.rico + "Instantiation" && it["type"] == "physicalObject" }
38

Jonas Waeber's avatar
Jonas Waeber committed
39
        val id = record["@id"] as String
Jonas Waeber's avatar
Jonas Waeber committed
40

Jonas Waeber's avatar
Jonas Waeber committed
41
42
43
44
        val recordIdentifiers = Filter.entitiesByProperty("identifiedBy", record, input)
        val recordTitles = Filter.entitiesByProperty("hasTitle", record, input)
        val recordLanguages = Filter.entitiesByProperty("hasLanguage", record, input)
        val recordRules = Filter.entitiesByProperty("regulatedBy", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
45

Jonas Waeber's avatar
Jonas Waeber committed
46
47
48
49
50
        val datesCreated = Filter.entitiesByProperty("created", record, input)
        val datesIssued = Filter.entitiesByProperty("issued", record, input)
        val temporal = Filter.entitiesByProperty("temporal", record, input)
        val placesRelated = Filter.entitiesByProperty("spatial", record, input)
        val placeCaptured = Filter.entitiesByProperty("P60556", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
51

Jonas Waeber's avatar
Jonas Waeber committed
52
53
        val genre = Filter.entitiesByProperty("hasGenre", record, input)
        val subjects = Filter.entitiesByProperty("hasSubject", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
54

Jonas Waeber's avatar
Jonas Waeber committed
55
56
        val publishers = Filter.entitiesByProperty("publishedBy", record, input)
        val producers = Filter.entitiesByProperty("P60441", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
57
        val creationRelationAgents =
Jonas Waeber's avatar
Jonas Waeber committed
58
            Filter.entitiesByProperty("recordResourceOrInstantiationIsSourceOfCreationRelation", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
59

Jonas Waeber's avatar
Jonas Waeber committed
60
        val digitalRules = Filter.entitiesByProperty("regulatedBy", digitalObject, input)
61

Jonas Waeber's avatar
Jonas Waeber committed
62
63
        val physicalRules = Filter.entitiesByProperty("regulatedBy", physicalObject, input)
        val physicalIdentifiers = Filter.entitiesByProperty("identifiedBy", physicalObject, input)
64

65
66
        val accessPhysical = Extract.typedEntityByType(physicalRules, "type", "access", "name").flatMap { it.toList() }
        val accessDigital = Extract.typedEntityByType(digitalRules, "type", "access", "name").flatMap { it.toList() }
67

68
        val usageDigital = Extract.typedEntityByType(digitalRules, "type", "usage", "sameAs").flatMap { it.toList() }
Jonas Waeber's avatar
Jonas Waeber committed
69
        val format = CarrierType.extract(Filter.entitiesByProperty("hasCarrierType", physicalObject, input))
70
71
72
        if (format.isEmpty()) {
            log.error("Found no carrier types for record $id.")
        }
73

Jonas Waeber's avatar
Jonas Waeber committed
74
        return SearchDoc(
75
76
77
78
            title = Extract.typedEntityByType(recordTitles, "type", "main", "title"),
            seriesTitle = Extract.typedEntityByType(recordTitles, "type", "series", "title"),
            broadcastTitle = Extract.typedEntityByType(recordTitles, "type", "broadcast", "title"),
            type = record["type"] as String,
Jonas Waeber's avatar
Jonas Waeber committed
79
            sourceID = try {
80
                Extract.extractSourceId(recordIdentifiers)
Jonas Waeber's avatar
Jonas Waeber committed
81
82
83
84
            } catch (ex: NoSuchElementException) {
                log.error("No source id found for record $id")
                "NoSourceIdFound"
            },
85
            sameAs = Extract.listOfStrings(record["sameAs"]),
Jonas Waeber's avatar
Jonas Waeber committed
86
            abstract = Extract.languageContainer("abstract", record["abstract"]),
Jonas Waeber's avatar
Jonas Waeber committed
87
88
89
            id = id,
            institution = listOf(
                NameContainer(
90
91
                    LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                    listOf(record["heldBy"] as String)
Jonas Waeber's avatar
Jonas Waeber committed
92
93
94
                )
            ),
            recordSet = NameContainer(
95
96
                LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                listOf(record["isPartOf"] as String)
Jonas Waeber's avatar
Jonas Waeber committed
97
            ),
Jonas Waeber's avatar
Jonas Waeber committed
98
99
100
101
            descriptiveNote = Extract.languageContainer("descriptiveNote", record["descriptiveNote"]),
            scopeAndContent = Extract.languageContainer("scopeAndContent", record["scopeAndContent"]),
            relatedMaterial = Extract.languageContainer("relation", record["relation"]),
            source = Extract.languageContainer("source", record["source"]),
Jonas Waeber's avatar
Jonas Waeber committed
102
103
104
105
106
            temporal = Extract.extractDate(temporal),
            dateCreated = Extract.extractDate(datesCreated),
            dateIssued = Extract.extractDate(datesIssued),
            placeCapture = Extract.extractPlaces(placeCaptured),
            placeRelated = Extract.extractPlaces(placesRelated),
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
            place = Extract.facetEntity(placeCaptured + placesRelated, "name"),
            rightsHolder = Extract.typedEntityByType(recordRules, "type", "holder", "name"),
            memoriavClaim = record["P60451"] != null,
            languageCaption = Extract.typedEntityByType(recordLanguages, "type", "caption", "name"),
            languageContent = Extract.typedEntityByType(recordLanguages, "type", "content", "name"),
            language = Extract.facetEntity(recordLanguages, "name"),
            genre = Extract.facetEntity(genre, "prefLabel"),
            keywords = Extract.facetEntity(subjects, "prefLabel"),
            agentSubject = Extract.typedEntityByType(subjects, "@type", "Agent", "name"),
            personSubject = Extract.typedEntityByType(subjects, "@type", "Person", "name"),
            corporateBodySubject = Extract.typedEntityByType(subjects, "@type", "CorporateBody", "name"),
            agentProducer = Extract.typedEntityByType(producers, "@type", "Agent", "name"),
            personProducer = Extract.typedEntityByType(producers, "@type", "Person", "name"),
            corporateBodyProducer = Extract.typedEntityByType(producers, "@type", "CorporateBody", "name"),
            agentPublisher = Extract.typedEntityByType(publishers, "@type", "Agent", "name"),
            personPublisher = Extract.typedEntityByType(publishers, "@type", "Person", "name"),
            corporateBodyPublisher = Extract.typedEntityByType(publishers, "@type", "CorporateBody", "name"),
Jonas Waeber's avatar
Jonas Waeber committed
124
125
126
127
            agentContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
128
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
129
130
131
132
133
            ),
            personContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
134
                agentTypeParam = NS.rico + "Person"
Jonas Waeber's avatar
Jonas Waeber committed
135
136
137
138
139
            ),
            corporateBodyContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
140
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
141
142
143
144
145
            ),
            agentCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
146
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
147
148
149
150
151
            ),
            personCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
152
                agentTypeParam = NS.rico + "Person"
Jonas Waeber's avatar
Jonas Waeber committed
153
154
155
156
157
            ),
            corporateBodyCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
158
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
159
            ),
160
            // TODO: Implement collected agents!
Jonas Waeber's avatar
Jonas Waeber committed
161
162
            persons = emptyList(),
            corporateBodies = emptyList(),
163
            agents = emptyList(),
164
165
166
167
168
169

            // DIGITAL & PHYSICAL
            access = accessPhysical + accessDigital,

            // DIGITAL
            accessDigital = accessDigital,
170
171
            durationDigital = Extract.listOfStrings(digitalObject?.get("duration")),
            colourDigital = Extract.listOfStrings(digitalObject?.get("P60558")),
Jonas Waeber's avatar
Jonas Waeber committed
172
            digitalObjectNote = Extract.languageContainer("descriptiveNote", digitalObject?.get("descriptiveNote")),
173
            locator = Extract.listOfStrings(digitalObject?.get("locator")),
Jonas Waeber's avatar
Jonas Waeber committed
174
            usageConditionsDigital = Extract.languageContainer("conditionsOfUse", digitalObject?.get("conditionsOfUse")),
175
176
177
178
179
            usageDigital = usageDigital,
            usageDigitalGroup = usageDigital.map { ReuseStatementMap.getValue(it) },

            // PHYSICAL
            accessPhysical = accessPhysical,
180
            durationPhysical = Extract.listOfStrings(physicalObject?.get("duration")),
Jonas Waeber's avatar
Jonas Waeber committed
181
182
183
184
            colourPhysical = Extract.languageContainer("P60558", physicalObject?.get("P60558")),
            physicalCharacteristics = Extract.languageContainer("physicalCharacteristics", physicalObject?.get("physicalCharacteristics")),
            physicalObjectNote = Extract.languageContainer("descriptiveNote", physicalObject?.get("descriptiveNote")),
            usageConditionsPhysical = Extract.languageContainer("conditionsOfUse", physicalObject?.get("conditionsOfUse")),
185
186
            usagePhysical = Extract.typedEntityByType(physicalRules, "type", "usage", "sameAs").flatMap { it.toList() },
            callNumber = Extract.typedEntityByType(physicalIdentifiers, "type", "callNumber", "identifier").flatMap { it.toList() },
187
            format = format
Jonas Waeber's avatar
Jonas Waeber committed
188
189
190
        )
    }
}