InstitutionSearchDocBuilder.kt 3.76 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
24
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
import org.memobase.helpers.*
25
import org.memobase.model.*
26

27
class InstitutionSearchDocBuilder(path: String) {
28
    private val log = LogManager.getLogger("InstitutionSearchDocBuilder")
29
    private val institutionTypeMapper = InstitutionTypeMapper(path)
30

31
32
33
34
35
    fun transform(key: String, input: Map<String, JsonObject>): Schema {
        val institution = input["institution"] ?: throw InvalidInputException("No institution entity found in message $key.")
        val identifiers = Filter.entitiesByProperty(KEYS.identifiedBy, institution, input)
        val locations = Filter.entitiesByProperty(KEYS.hasLocation, institution, input)
        val cantons = mutableListOf<JsonObject>()
36

37
38
39
40
41
        input.values.forEach {
            if (it[KEYS.ricoType] == KEYS.LocationType.canton) {
                cantons.add(it)
            }
        }
42

43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
        val type = institution[KEYS.wikidataInstance].let {
            when(it) {
                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()
                }
            }
        }

        val name = Extract.languageContainer("institution", institution[KEYS.name]).let { names ->
            when {
                names.isEmpty() -> {
                    LanguageContainer.placeholder("NoNameFound")
                }
                names.size == 1 -> {
                    names[0]
                }
                else -> {
                    names.reduce { acc, languageContainer -> acc.merge(languageContainer) }
                }
            }
        }
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
        val canton = cantons.map {
            it[KEYS.name].let { name ->
                Extract.languageContainer("canton", name).reduce { acc, languageContainer -> acc.merge(languageContainer) }
            }
        }.let { c ->
            when {
                c.isEmpty() -> {
                    listOf(LanguageContainer.placeholder("NoCantonNameFound"))
                }
                else -> c
            }
        }

        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,
            documentType = listOf(LanguageContainer.placeholder("PLACEHOLDER")),
            keyVisualLink = "placeholderlink",
            canton = canton,
            numberOfRecordSets = 0,
            numberOfDocuments = 0
99
100
101
        )
    }
}