Meta.kt 2 KB
Newer Older
1
2
package org.memobase.helpers

3
import ch.memobase.rdf.NS
4
5
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
6
import org.memobase.model.FacetContainer
7
8
import org.memobase.model.LanguageContainer

Jonas Waeber's avatar
Jonas Waeber committed
9
/**
Jonas Waeber's avatar
Jonas Waeber committed
10
Extraction helpers for institutions and record sets.
Jonas Waeber's avatar
Jonas Waeber committed
11
 */
12
object Meta {
13
    private val log = LogManager.getLogger("InstitutionAndRecordSetExtractionHelper")
14

Jonas Waeber's avatar
Jonas Waeber committed
15
    fun extractInstitution(record: JsonObject): List<FacetContainer> {
16
17
18
        return extract(KEYS.heldBy, record)
    }

Jonas Waeber's avatar
Jonas Waeber committed
19
    fun extractRecordSet(record: JsonObject): FacetContainer {
Jonas Waeber's avatar
Jonas Waeber committed
20
21
22
23
        val containers = extract(KEYS.isPartOf, record)
        return if (containers.isNotEmpty()) {
            containers[0]
        } else {
24
            FacetContainer.placeholder("NoRecordSetInRecord")
Jonas Waeber's avatar
Jonas Waeber committed
25
        }
26
27
    }

Jonas Waeber's avatar
Jonas Waeber committed
28
    private fun extract(key: String, record: JsonObject): List<FacetContainer> {
29
30
31
        return record[key].let { items ->
            when (items) {
                is String -> listOf(
32
33
34
35
36
                        FacetContainer(
                                LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                                null,
                                listOf(removeNamespace(key, items))
                        )
37
38
                )
                is List<*> -> items.map { item ->
Jonas Waeber's avatar
Jonas Waeber committed
39
                    FacetContainer(
40
41
42
                            LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList()),
                            null,
                            listOf(removeNamespace(key, item as String))
43
44
45
                    )
                }
                else -> {
46
                    log.error("No property $key in record ${record[KEYS.entityId]}.")
47
48
49
50
51
52
53
54
55
56
57
58
59
60
                    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
        }
    }
}