InstitutionSearchDocBuilder.kt 5.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*
 * 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

21
import com.beust.klaxon.JsonArray
22
23
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
24
25
26
import org.memobase.helpers.Extract
import org.memobase.helpers.InstitutionTypeMapper
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
27
import org.memobase.model.FacetContainer
28
29
30
import org.memobase.model.InstitutionSearchDoc
import org.memobase.model.LanguageContainer
import org.memobase.model.Schema
31
import org.memobase.rdf.NS
32

33
class InstitutionSearchDocBuilder(path: String) {
34
    private val log = LogManager.getLogger("InstitutionSearchDocBuilder")
35
    private val institutionTypeMapper = InstitutionTypeMapper(path)
36

37
    fun transform(key: String, input: Map<String, JsonObject>): Schema {
38
39
40
        val institution =
            input["institution"] ?: throw InvalidInputException("No institution entity found in message $key.")
        val identifiers = mutableListOf<JsonObject>()
Jonas Waeber's avatar
Jonas Waeber committed
41
        val cantons = mutableListOf<FacetContainer>()
42
43
44
        val municipalities = mutableListOf<LanguageContainer>()
        val addresses = mutableListOf<String>()
        val postalCodes = mutableListOf<String>()
45

46
        input.values.forEach {
47
48
            when {
                it[KEYS.ricoType] == KEYS.LocationType.canton -> {
Jonas Waeber's avatar
Jonas Waeber committed
49
                    cantons.add(FacetContainer(extractAdminUnit(it, "canton", "NoCantonFound"), null, emptyList()))
50
51
52
53
54
55
56
                }
                it[KEYS.ricoType] == KEYS.LocationType.municipality -> {
                    municipalities.add(extractAdminUnit(it, "municipality", "NoCityFound"))
                }
                it[KEYS.ricoType] == KEYS.IdentifierType.main -> {
                    identifiers.add(it)
                }
57
                it[KEYS.atType] == NS.rico + KEYS.Place -> {
58
59
60
                    addresses.addAll(Extract.listOfStrings(it[KEYS.wikidataAddresses]))
                    postalCodes.addAll(Extract.listOfStrings(it[KEYS.wikidataPostalCodes]))
                }
61
62
            }
        }
63

64
        val type = institution[KEYS.wikidataInstance].let {
65
            when (it) {
66
67
68
69
70
71
72
73
74
                is String -> listOf(institutionTypeMapper.getValue(it))
                is JsonArray<*> -> it.map { any -> institutionTypeMapper.getValue(any as String) }
                else -> {
                    log.error("Found no institution types on institution $key")
                    emptyList()
                }
            }
        }

75
76
        val name = extractLanguageContainer(institution[KEYS.name], "NoNameFound")
        val description = extractLanguageContainer(institution[KEYS.descriptiveNote], "NoDescriptionFound")
Jonas Waeber's avatar
Jonas Waeber committed
77
        val id = Extract.extractIdValue(identifiers, KEYS.IdentifierType.main) ?: "NoIdentifierFound"
78
        return InstitutionSearchDoc(
Jonas Waeber's avatar
Jonas Waeber committed
79
            institutionId = id,
80
81
82
83
84
85
86
87
88
89
90
            published = institution[KEYS.isPublished].let {
                when (it) {
                    is String -> it.toBoolean()
                    else -> {
                        log.error("Found no isPublished property on institution $key. Set to false.")
                        false
                    }
                }
            },
            type = type,
            name = name,
91
            description = description,
Jonas Waeber's avatar
Jonas Waeber committed
92
            documentType = listOf(FacetContainer.placeholder("PLACEHOLDER")),
93
94
95
96
97
98
            keyVisualLink = institution[KEYS.wikidataImage].let { if (it != null) it as String else "NoKeyVisualLinkDefined" },
            canton = cantons,
            city = municipalities,
            address = addresses,
            postalCodes = postalCodes,
            numberOfRecordSets = Extract.listOfStrings(institution[KEYS.isHolderOf]).count(),
Jonas Waeber's avatar
Jonas Waeber committed
99
100
101
102
            numberOfDocuments = 0,
            teaserColor = institution[KEYS.teaserColor].let {
                if (it == null) {
                    log.error("No teaser colour found for institution $id.")
103
                    ""
Jonas Waeber's avatar
Jonas Waeber committed
104
105
106
107
108
109
110
                }
                else
                    it as String
            },
            teaserColorComputed = institution[KEYS.teaserColorComputed].let {
                if (it == null) {
                    log.error("No computed teaser colour found for institution $id.")
111
                    ""
Jonas Waeber's avatar
Jonas Waeber committed
112
113
114
115
                }
                else
                    it as String
            }
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
144

    private fun extractLanguageContainer(value: Any?, placeholder: String): LanguageContainer {
        return Extract.languageContainer("institution", value).let { items ->
            when {
                items.isEmpty() -> {
                    LanguageContainer.placeholder(placeholder)
                }
                items.size == 1 -> {
                    items[0]
                }
                else -> {
                    items.reduce { acc, languageContainer -> acc.merge(languageContainer) }
                }
            }
        }
    }

    private fun extractAdminUnit(item: JsonObject, parent: String, placeholder: String): LanguageContainer {
        return item[KEYS.name].let { name ->
            Extract.languageContainer(parent, name).let {
                when {
                    it.isEmpty() -> LanguageContainer.placeholder(placeholder)
                    else -> it.reduce { acc, languageContainer -> acc.merge(languageContainer) }
                }
            }
        }
    }
145
}