SearchDocTransform.kt 7.71 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
32
    fun transform(input: Map<String, JsonObject>): SearchDoc {
        val record = input["record"] ?: error("No record defined in this message.")
        val id = record["@id"] as String
Jonas Waeber's avatar
Jonas Waeber committed
33

Jonas Waeber's avatar
Jonas Waeber committed
34
35
36
37
        val identifiers = input.values.filter { item -> item["@type"] == "rico:Identifier" }
        val titles = input.values.filter { item -> item["@type"] == "rico:Title" }
        val languages = input.values.filter { item -> item["@type"] == "rico:Language" }
        val rules = input.values.filter { item -> item["@type"] == "rico:Rule" }
Jonas Waeber's avatar
Jonas Waeber committed
38

Jonas Waeber's avatar
Jonas Waeber committed
39
40
41
42
43
        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
44

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

Jonas Waeber's avatar
Jonas Waeber committed
48
49
50
51
        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
52

Jonas Waeber's avatar
Jonas Waeber committed
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
138
139
140
141
        return SearchDoc(
            title = Extract.typedEntityByType(titles, "rico:type", "main", "rico:title"),
            seriesTitle = Extract.typedEntityByType(titles, "rico:type", "series", "rico:title"),
            broadcastTitle = Extract.typedEntityByType(titles, "rico:type", "broadcast", "rico:title"),
            type = record["rico:type"] as String,
            sourceID = try {
                Extract.extractSourceId(identifiers)
            } 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"),
            rightsHolder = Extract.typedEntityByType(rules, "rico:type", "holder", "rico:name"),
            memoriavClaim = record["rdau:P60451"] != null,
            languageCaption = Extract.typedEntityByType(languages, "rico:type", "caption", "rico:name"),
            languageContent = Extract.typedEntityByType(languages, "rico:type", "content", "rico:name"),
            language = Extract.facetEntity(languages, "rico:name"),
            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(),
            agents = emptyList()
Jonas Waeber's avatar
Jonas Waeber committed
142
143
144
        )
    }
}