Meta.kt 2.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
/*
 * 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/>.
 */
18
19
package org.memobase.helpers

20
import ch.memobase.rdf.NS
21
22
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
23
import org.memobase.model.FacetContainer
24
25
import org.memobase.model.LanguageContainer

Jonas Waeber's avatar
Jonas Waeber committed
26
/**
Jonas Waeber's avatar
Jonas Waeber committed
27
Extraction helpers for institutions and record sets.
Jonas Waeber's avatar
Jonas Waeber committed
28
 */
29
object Meta {
30
    private val log = LogManager.getLogger("InstitutionAndRecordSetExtractionHelper")
31

Jonas Waeber's avatar
Jonas Waeber committed
32
    fun extractInstitution(record: JsonObject): List<FacetContainer> {
33
34
35
        return extract(KEYS.heldBy, record)
    }

Jonas Waeber's avatar
Jonas Waeber committed
36
    fun extractRecordSet(record: JsonObject): FacetContainer {
Jonas Waeber's avatar
Jonas Waeber committed
37
38
39
40
        val containers = extract(KEYS.isPartOf, record)
        return if (containers.isNotEmpty()) {
            containers[0]
        } else {
41
            FacetContainer.placeholder("NoRecordSetInRecord")
Jonas Waeber's avatar
Jonas Waeber committed
42
        }
43
44
    }

Jonas Waeber's avatar
Jonas Waeber committed
45
    private fun extract(key: String, record: JsonObject): List<FacetContainer> {
46
47
48
        return record[key].let { items ->
            when (items) {
                is String -> listOf(
49
50
51
52
53
                        FacetContainer(
                                LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                                null,
                                listOf(removeNamespace(key, items))
                        )
54
55
                )
                is List<*> -> items.map { item ->
Jonas Waeber's avatar
Jonas Waeber committed
56
                    FacetContainer(
57
58
59
                            LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                            null,
                            listOf(removeNamespace(key, item as String))
60
61
62
                    )
                }
                else -> {
63
                    log.error("No property $key in record ${record[KEYS.entityId]}.")
64
65
66
67
68
69
70
71
72
73
74
75
76
77
                    emptyList()
                }
            }
        }
    }

    private fun removeNamespace(key: String, value: String): String {
        return when (key) {
            KEYS.isPartOf -> value.removePrefix(NS.mbrs)
            KEYS.heldBy -> value.removePrefix(NS.mbcb)
            else -> value
        }
    }
}