SearchDocTransform.kt 11.1 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
23
24
25
26
import org.memobase.builders.IFieldBuilder
import org.memobase.builders.PersonContainerBuilder
import org.memobase.builders.PersonFacetBuilder
import org.memobase.builders.PlaceFacetBuilder
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.memobase.helpers.CarrierType
Jonas Waeber's avatar
Jonas Waeber committed
28
import org.memobase.helpers.Extract
Jonas Waeber's avatar
Jonas Waeber committed
29
import org.memobase.helpers.Filter
30
import org.memobase.helpers.ReuseStatementMap
Jonas Waeber's avatar
Jonas Waeber committed
31
32
33
import org.memobase.model.LanguageContainer
import org.memobase.model.NameContainer
import org.memobase.model.SearchDoc
34
import org.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
35

Jonas Waeber's avatar
Jonas Waeber committed
36
class SearchDocTransform {
Jonas Waeber's avatar
Jonas Waeber committed
37
    private val log = LogManager.getLogger("SearchDocTransform")
Jonas Waeber's avatar
Jonas Waeber committed
38
    fun transform(input: Map<String, JsonObject>): SearchDoc {
39

Jonas Waeber's avatar
Jonas Waeber committed
40
        val record = input["record"] ?: error("No record defined in this message.")
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
        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" }

        val personFacetBuilder = PersonFacetBuilder()
        val subjectPersonBuilder = PersonContainerBuilder("hasSubject", record, null, input)
        val publisherPersonBuilder = PersonContainerBuilder("publishedBy", record, null, input)
        val producersPersonBuilder = PersonContainerBuilder("P60441", record, null, input)
        val contributorPersonBuilder = PersonContainerBuilder("", record, KEYS.contributor, input)
        val creatorPersonBuilder = PersonContainerBuilder("", record, KEYS.creator, input)

        val placeFacetBuilder = PlaceFacetBuilder()

        for (item in input.values) {
            for (builder: IFieldBuilder in listOf(
                personFacetBuilder,
                subjectPersonBuilder,
                publisherPersonBuilder,
                producersPersonBuilder,
                contributorPersonBuilder,
                creatorPersonBuilder,
                placeFacetBuilder
            )) {
                if (builder.filter(item)) {
                    builder.append(item)
                }
            }
        }
70

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

Jonas Waeber's avatar
Jonas Waeber committed
73
74
75
76
        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
77

Jonas Waeber's avatar
Jonas Waeber committed
78
79
80
81
82
        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
83

Jonas Waeber's avatar
Jonas Waeber committed
84
85
        val genre = Filter.entitiesByProperty("hasGenre", record, input)
        val subjects = Filter.entitiesByProperty("hasSubject", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
86

Jonas Waeber's avatar
Jonas Waeber committed
87
88
        val publishers = Filter.entitiesByProperty("publishedBy", record, input)
        val producers = Filter.entitiesByProperty("P60441", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
89
        val creationRelationAgents =
Jonas Waeber's avatar
Jonas Waeber committed
90
            Filter.entitiesByProperty("recordResourceOrInstantiationIsSourceOfCreationRelation", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
91

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

Jonas Waeber's avatar
Jonas Waeber committed
94
95
        val physicalRules = Filter.entitiesByProperty("regulatedBy", physicalObject, input)
        val physicalIdentifiers = Filter.entitiesByProperty("identifiedBy", physicalObject, input)
96

97
98
        val accessPhysical = Extract.typedEntityByType(physicalRules, "type", "access", "name").flatMap { it.toList() }
        val accessDigital = Extract.typedEntityByType(digitalRules, "type", "access", "name").flatMap { it.toList() }
99

100
        val usageDigital = Extract.typedEntityByType(digitalRules, "type", "usage", "sameAs").flatMap { it.toList() }
Jonas Waeber's avatar
Jonas Waeber committed
101
        val format = CarrierType.extract(Filter.entitiesByProperty("hasCarrierType", physicalObject, input))
102
103
104
        if (format.isEmpty()) {
            log.error("Found no carrier types for record $id.")
        }
105

Jonas Waeber's avatar
Jonas Waeber committed
106
        return SearchDoc(
107
108
109
110
            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
111
            sourceID = try {
112
                Extract.extractSourceId(recordIdentifiers)
Jonas Waeber's avatar
Jonas Waeber committed
113
114
115
116
            } catch (ex: NoSuchElementException) {
                log.error("No source id found for record $id")
                "NoSourceIdFound"
            },
117
            sameAs = Extract.listOfStrings(record["sameAs"]),
Jonas Waeber's avatar
Jonas Waeber committed
118
            abstract = Extract.languageContainer("abstract", record["abstract"]),
Jonas Waeber's avatar
Jonas Waeber committed
119
120
121
            id = id,
            institution = listOf(
                NameContainer(
122
123
                    LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                    listOf(record["heldBy"] as String)
Jonas Waeber's avatar
Jonas Waeber committed
124
125
126
                )
            ),
            recordSet = NameContainer(
127
128
                LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                listOf(record["isPartOf"] as String)
Jonas Waeber's avatar
Jonas Waeber committed
129
            ),
Jonas Waeber's avatar
Jonas Waeber committed
130
131
132
133
            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
134
135
136
137
138
            temporal = Extract.extractDate(temporal),
            dateCreated = Extract.extractDate(datesCreated),
            dateIssued = Extract.extractDate(datesIssued),
            placeCapture = Extract.extractPlaces(placeCaptured),
            placeRelated = Extract.extractPlaces(placesRelated),
139
            placeFacet = placeFacetBuilder.build(),
140
141
142
143
144
145
146
            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"),
147
148
149
150
151
152
153
154

            personSubject = subjectPersonBuilder.build(),
            personProducer = producersPersonBuilder.build(),
            personPublisher = publisherPersonBuilder.build(),
            personContributor = contributorPersonBuilder.build(),
            personCreator = creatorPersonBuilder.build(),
            personsFacet = personFacetBuilder.build(),

155
156
157
            corporateBodySubject = Extract.typedEntityByType(subjects, "@type", "CorporateBody", "name"),
            corporateBodyProducer = Extract.typedEntityByType(producers, "@type", "CorporateBody", "name"),
            corporateBodyPublisher = Extract.typedEntityByType(publishers, "@type", "CorporateBody", "name"),
Jonas Waeber's avatar
Jonas Waeber committed
158
159
160
161
            corporateBodyContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
162
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
163
            ),
164
165

            corporateBodyCreator = Extract.extractTypedCreationRelationAgent(
Jonas Waeber's avatar
Jonas Waeber committed
166
167
168
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
169
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
170
            ),
171
172
173
174
            agentSubject = Extract.typedEntityByType(subjects, "@type", "Agent", "name"),
            agentProducer = Extract.typedEntityByType(producers, "@type", "Agent", "name"),
            agentPublisher = Extract.typedEntityByType(publishers, "@type", "Agent", "name"),
            agentContributor = Extract.extractTypedCreationRelationAgent(
Jonas Waeber's avatar
Jonas Waeber committed
175
176
                creationRelationAgents,
                input,
177
178
                creationRelationTypeParam = "contributor",
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
179
            ),
180
            agentCreator = Extract.extractTypedCreationRelationAgent(
Jonas Waeber's avatar
Jonas Waeber committed
181
182
183
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
184
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
185
            ),
186
187
188
189
190
191

            // DIGITAL & PHYSICAL
            access = accessPhysical + accessDigital,

            // DIGITAL
            accessDigital = accessDigital,
192
193
            durationDigital = Extract.listOfStrings(digitalObject?.get("duration")),
            colourDigital = Extract.listOfStrings(digitalObject?.get("P60558")),
Jonas Waeber's avatar
Jonas Waeber committed
194
            digitalObjectNote = Extract.languageContainer("descriptiveNote", digitalObject?.get("descriptiveNote")),
195
            locator = Extract.listOfStrings(digitalObject?.get("locator")),
196
197
198
199
            usageConditionsDigital = Extract.languageContainer(
                "conditionsOfUse",
                digitalObject?.get("conditionsOfUse")
            ),
200
201
202
203
204
            usageDigital = usageDigital,
            usageDigitalGroup = usageDigital.map { ReuseStatementMap.getValue(it) },

            // PHYSICAL
            accessPhysical = accessPhysical,
205
            durationPhysical = Extract.listOfStrings(physicalObject?.get("duration")),
Jonas Waeber's avatar
Jonas Waeber committed
206
            colourPhysical = Extract.languageContainer("P60558", physicalObject?.get("P60558")),
207
208
209
210
            physicalCharacteristics = Extract.languageContainer(
                "physicalCharacteristics",
                physicalObject?.get("physicalCharacteristics")
            ),
Jonas Waeber's avatar
Jonas Waeber committed
211
            physicalObjectNote = Extract.languageContainer("descriptiveNote", physicalObject?.get("descriptiveNote")),
212
213
214
215
            usageConditionsPhysical = Extract.languageContainer(
                "conditionsOfUse",
                physicalObject?.get("conditionsOfUse")
            ),
216
            usagePhysical = Extract.typedEntityByType(physicalRules, "type", "usage", "sameAs").flatMap { it.toList() },
217
218
            callNumber = Extract.typedEntityByType(physicalIdentifiers, "type", "callNumber", "identifier")
                .flatMap { it.toList() },
219
            format = format
Jonas Waeber's avatar
Jonas Waeber committed
220
221
222
        )
    }
}