SearchDocTransform.kt 10.4 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.Extract
24
import org.memobase.helpers.ReuseStatementMap
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
import org.memobase.model.LanguageContainer
import org.memobase.model.NameContainer
import org.memobase.model.SearchDoc
28
import org.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
29

Jonas Waeber's avatar
Jonas Waeber committed
30
class SearchDocTransform {
Jonas Waeber's avatar
Jonas Waeber committed
31
    private val log = LogManager.getLogger("SearchDocTransform")
Jonas Waeber's avatar
Jonas Waeber committed
32
33
    fun transform(input: Map<String, JsonObject>): SearchDoc {
        val record = input["record"] ?: error("No record defined in this message.")
34
35
        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" }
36

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

39
40
41
42
        val recordIdentifiers = Extract.getEntitiesFromIds("identifiedBy", record, input)
        val recordTitles = Extract.getEntitiesFromIds("hasTitle", record, input)
        val recordLanguages = Extract.getEntitiesFromIds("hasLanguage", record, input)
        val recordRules = Extract.getEntitiesFromIds("regulatedBy", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
43

44
45
46
47
48
        val datesCreated = Extract.getEntitiesFromIds("created", record, input)
        val datesIssued = Extract.getEntitiesFromIds("issued", record, input)
        val temporal = Extract.getEntitiesFromIds("temporal", record, input)
        val placesRelated = Extract.getEntitiesFromIds("spatial", record, input)
        val placeCaptured = Extract.getEntitiesFromIds("P60556", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
49

50
51
        val genre = Extract.getEntitiesFromIds("hasGenre", record, input)
        val subjects = Extract.getEntitiesFromIds("hasSubject", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
52

53
54
        val publishers = Extract.getEntitiesFromIds("publishedBy", record, input)
        val producers = Extract.getEntitiesFromIds("P60441", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
55
        val creationRelationAgents =
56
            Extract.getEntitiesFromIds("recordResourceOrInstantiationIsSourceOfCreationRelation", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
57

58
        val digitalRules = Extract.getEntitiesFromIds("regulatedBy", digitalObject, input)
59

60
61
        val physicalRules = Extract.getEntitiesFromIds("regulatedBy", physicalObject, input)
        val physicalIdentifiers = Extract.getEntitiesFromIds("identifiedBy", physicalObject, input)
62

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

66
67
        val usageDigital = Extract.typedEntityByType(digitalRules, "type", "usage", "sameAs").flatMap { it.toList() }
        val format = Extract.carrierType(Extract.getEntitiesFromIds("hasCarrierType", physicalObject, input))
68
69
70
        if (format.isEmpty()) {
            log.error("Found no carrier types for record $id.")
        }
71

Jonas Waeber's avatar
Jonas Waeber committed
72
        return SearchDoc(
73
74
75
76
            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
77
            sourceID = try {
78
                Extract.extractSourceId(recordIdentifiers)
Jonas Waeber's avatar
Jonas Waeber committed
79
80
81
82
            } catch (ex: NoSuchElementException) {
                log.error("No source id found for record $id")
                "NoSourceIdFound"
            },
83
84
            sameAs = Extract.listOfStrings(record["sameAs"]),
            abstract = Extract.extractLanguageContainer("abstract", record["abstract"]),
Jonas Waeber's avatar
Jonas Waeber committed
85
86
87
88
89
90
91
92
93
94
95
            id = id,
            institution = listOf(
                NameContainer(
                    LanguageContainer(listOf("Institution"), listOf("institution"), listOf("istituzione"), emptyList()),
                    listOf("https://memobase.ch/institution/MEMORIAV")
                )
            ),
            recordSet = NameContainer(
                LanguageContainer(listOf("Bestand"), listOf("collection"), listOf("fondo"), emptyList()),
                listOf("https://memobase.ch/recordSet/EXAMPLE")
            ),
96
97
98
99
            descriptiveNote = Extract.extractLanguageContainer("descriptiveNote", record["descriptiveNote"]),
            scopeAndContent = Extract.extractLanguageContainer("scopeAndContent", record["scopeAndContent"]),
            relatedMaterial = Extract.extractLanguageContainer("relation", record["relation"]),
            source = Extract.extractLanguageContainer("source", record["source"]),
Jonas Waeber's avatar
Jonas Waeber committed
100
101
102
103
104
            temporal = Extract.extractDate(temporal),
            dateCreated = Extract.extractDate(datesCreated),
            dateIssued = Extract.extractDate(datesIssued),
            placeCapture = Extract.extractPlaces(placeCaptured),
            placeRelated = Extract.extractPlaces(placesRelated),
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
            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
122
123
124
125
            agentContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
126
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
127
128
129
130
131
            ),
            personContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
132
                agentTypeParam = NS.rico + "Person"
Jonas Waeber's avatar
Jonas Waeber committed
133
134
135
136
137
            ),
            corporateBodyContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
138
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
139
140
141
142
143
            ),
            agentCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
144
                agentTypeParam = NS.rico + "Agent"
Jonas Waeber's avatar
Jonas Waeber committed
145
146
147
148
149
            ),
            personCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
150
                agentTypeParam = NS.rico + "Person"
Jonas Waeber's avatar
Jonas Waeber committed
151
152
153
154
155
            ),
            corporateBodyCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
156
                agentTypeParam = NS.rico + "CorporateBody"
Jonas Waeber's avatar
Jonas Waeber committed
157
            ),
158
            // TODO: Implement collected agents!
Jonas Waeber's avatar
Jonas Waeber committed
159
160
            persons = emptyList(),
            corporateBodies = emptyList(),
161
            agents = emptyList(),
162
163
164
165
166
167

            // DIGITAL & PHYSICAL
            access = accessPhysical + accessDigital,

            // DIGITAL
            accessDigital = accessDigital,
168
169
170
171
172
            durationDigital = Extract.listOfStrings(digitalObject?.get("duration")),
            colourDigital = Extract.listOfStrings(digitalObject?.get("P60558")),
            digitalObjectNote = Extract.extractLanguageContainer("descriptiveNote", digitalObject?.get("descriptiveNote")),
            locator = Extract.listOfStrings(digitalObject?.get("locator")),
            usageConditionsDigital = Extract.extractLanguageContainer("conditionsOfUse", digitalObject?.get("conditionsOfUse")),
173
174
175
176
177
            usageDigital = usageDigital,
            usageDigitalGroup = usageDigital.map { ReuseStatementMap.getValue(it) },

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