InstitutionSearchDocBuilder.kt 5.23 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
27
28
29
import org.memobase.helpers.Extract
import org.memobase.helpers.InstitutionTypeMapper
import org.memobase.helpers.KEYS
import org.memobase.model.InstitutionSearchDoc
import org.memobase.model.LanguageContainer
import org.memobase.model.Schema
30
import org.memobase.rdf.NS
31

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

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

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

63
        val type = institution[KEYS.wikidataInstance].let {
64
            when (it) {
65
66
67
68
69
70
71
72
73
                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()
                }
            }
        }

74
75
        val name = extractLanguageContainer(institution[KEYS.name], "NoNameFound")
        val description = extractLanguageContainer(institution[KEYS.descriptiveNote], "NoDescriptionFound")
76
77
78
79
80
81
82
83
84
85
86
87
88
89

        return InstitutionSearchDoc(
            institutionId = Extract.extractIdValue(identifiers, KEYS.IdentifierType.main) ?: "NoIdentifierFound",
            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,
90
            description = description,
91
            documentType = listOf(LanguageContainer.placeholder("PLACEHOLDER")),
92
93
94
95
96
97
            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(),
98
            numberOfDocuments = 0
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

    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) }
                }
            }
        }
    }
128
}