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

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

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

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

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

Jonas Waeber's avatar
Jonas Waeber committed
48
49
        val genre = Extract.getEntitiesFromIds("ebucore:hasGenre", record, input)
        val subjects = Extract.getEntitiesFromIds("rico:hasSubject", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
50

Jonas Waeber's avatar
Jonas Waeber committed
51
52
53
54
        val publishers = Extract.getEntitiesFromIds("rico:publishedBy", record, input)
        val producers = Extract.getEntitiesFromIds("rdau:P60441", record, input)
        val creationRelationAgents =
            Extract.getEntitiesFromIds("rico:recordResourceOrInstantiationIsSourceOfCreationRelation", record, input)
Jonas Waeber's avatar
Jonas Waeber committed
55

Jonas Waeber's avatar
Jonas Waeber committed
56
        return SearchDoc(
57
58
59
            title = Extract.typedEntityByType(recordTitles, "rico:type", "main", "rico:title"),
            seriesTitle = Extract.typedEntityByType(recordTitles, "rico:type", "series", "rico:title"),
            broadcastTitle = Extract.typedEntityByType(recordTitles, "rico:type", "broadcast", "rico:title"),
Jonas Waeber's avatar
Jonas Waeber committed
60
61
            type = record["rico:type"] as String,
            sourceID = try {
62
                Extract.extractSourceId(recordIdentifiers)
Jonas Waeber's avatar
Jonas Waeber committed
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
            } catch (ex: NoSuchElementException) {
                log.error("No source id found for record $id")
                "NoSourceIdFound"
            },
            sameAs = Extract.listOfStrings(record["schema:sameAs"]),
            abstract = Extract.extractLanguageContainer("dct:abstract", record["dct:abstract"]),
            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")
            ),
            descriptiveNote = Extract.extractLanguageContainer("rico:descriptiveNote", record["rico:descriptiveNote"]),
            scopeAndContent = Extract.extractLanguageContainer("rico:scopeAndContent", record["rico:scopeAndContent"]),
            relatedMaterial = Extract.extractLanguageContainer("dct:relation", record["dct:relation"]),
            source = Extract.extractLanguageContainer("rico:source", record["rico:source"]),
            temporal = Extract.extractDate(temporal),
            dateCreated = Extract.extractDate(datesCreated),
            dateIssued = Extract.extractDate(datesIssued),
            placeCapture = Extract.extractPlaces(placeCaptured),
            placeRelated = Extract.extractPlaces(placesRelated),
            place = Extract.facetEntity(placeCaptured + placesRelated, "rico:name"),
90
            rightsHolder = Extract.typedEntityByType(recordRules, "rico:type", "holder", "rico:name"),
Jonas Waeber's avatar
Jonas Waeber committed
91
            memoriavClaim = record["rdau:P60451"] != null,
92
93
94
            languageCaption = Extract.typedEntityByType(recordLanguages, "rico:type", "caption", "rico:name"),
            languageContent = Extract.typedEntityByType(recordLanguages, "rico:type", "content", "rico:name"),
            language = Extract.facetEntity(recordLanguages, "rico:name"),
Jonas Waeber's avatar
Jonas Waeber committed
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
138
139
140
141
142
143
            genre = Extract.facetEntity(genre, "skos:prefLabel"),
            keywords = Extract.facetEntity(subjects, "skos:prefLabel"),
            agentSubject = Extract.typedEntityByType(subjects, "@type", "rico:Agent", "rico:name"),
            personSubject = Extract.typedEntityByType(subjects, "@type", "rico:Person", "rico:name"),
            corporateBodySubject = Extract.typedEntityByType(subjects, "@type", "rico:CorporateBody", "rico:name"),
            agentProducer = Extract.typedEntityByType(producers, "@type", "rico:Agent", "rico:name"),
            personProducer = Extract.typedEntityByType(producers, "@type", "rico:Person", "rico:name"),
            corporateBodyProducer = Extract.typedEntityByType(producers, "@type", "rico:CorporateBody", "rico:name"),
            agentPublisher = Extract.typedEntityByType(publishers, "@type", "rico:Agent", "rico:name"),
            personPublisher = Extract.typedEntityByType(publishers, "@type", "rico:Person", "rico:name"),
            corporateBodyPublisher = Extract.typedEntityByType(publishers, "@type", "rico:CorporateBody", "rico:name"),
            agentContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
                agentTypeParam = "rico:Agent"
            ),
            personContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
                agentTypeParam = "rico:Person"
            ),
            corporateBodyContributor = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "contributor",
                agentTypeParam = "rico:CorporateBody"
            ),
            agentCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
                agentTypeParam = "rico:Agent"
            ),
            personCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
                agentTypeParam = "rico:Person"
            ),
            corporateBodyCreator = Extract.extractTypedCreationRelationAgent(
                creationRelationAgents,
                input,
                creationRelationTypeParam = "creator",
                agentTypeParam = "rico:CorporateBody"
            ),
            persons = emptyList(),
            corporateBodies = emptyList(),
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
            agents = emptyList(),
            accessDigital = emptyList(),
            callNumber = emptyList(),
            accessPhysical = emptyList(),
            access = emptyList(),
            colourDigital = emptyList(),
            colourPhysical = emptyList(),
            digitalObjectNote = emptyList(),
            durationDigital = emptyList(),
            durationPhysical = emptyList(),
            locator = emptyList(),
            physicalCharacteristics = emptyList(),
            physicalObjectNote = emptyList(),
            usageConditionsDigital = emptyList(),
            usageConditionsPhysical = emptyList(),
            usageDigital = emptyList(),
            usageDigitalGroup = emptyList(),
            usagePhysical = emptyList()
Jonas Waeber's avatar
Jonas Waeber committed
162
163
164
        )
    }
}