Extract.kt 4.78 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 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.helpers

21
import com.beust.klaxon.JsonArray
Jonas Waeber's avatar
Jonas Waeber committed
22 23 24 25 26
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
import org.memobase.model.LanguageContainer

object Extract {
27
    private val log = LogManager.getLogger("ExtractHelper")
Jonas Waeber's avatar
Jonas Waeber committed
28

Jonas Waeber's avatar
Jonas Waeber committed
29
    private fun languageTag(entity: JsonObject, language: String): List<String> {
Jonas Waeber's avatar
Jonas Waeber committed
30 31 32 33 34 35 36 37
        return entity["@value"].let {
            if (entity["@language"] == language && it != null)
                listOf(it as String)
            else
                emptyList()
        }
    }

Jonas Waeber's avatar
Jonas Waeber committed
38
    fun languageContainer(parent: String, entity: Any?): List<LanguageContainer> {
Jonas Waeber's avatar
Jonas Waeber committed
39 40 41 42 43
        return when (entity) {
            is String -> listOf(LanguageContainer(emptyList(), emptyList(), emptyList(), listOf(entity)))
            is JsonObject ->
                listOf(
                    LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
44 45 46
                        de = languageTag(entity, "de"),
                        fr = languageTag(entity, "fr"),
                        it = languageTag(entity, "it"),
Jonas Waeber's avatar
Jonas Waeber committed
47 48 49 50 51 52 53 54 55 56
                        un = emptyList()
                    )
                )
            is List<*> ->
                listOf(entity.mapNotNull { subEntity ->
                    when (subEntity) {
                        is String ->
                            LanguageContainer(emptyList(), emptyList(), emptyList(), listOf(subEntity))
                        is JsonObject ->
                            LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
57 58 59
                                de = languageTag(subEntity, "de"),
                                fr = languageTag(subEntity, "fr"),
                                it = languageTag(subEntity, "it"),
Jonas Waeber's avatar
Jonas Waeber committed
60 61 62
                                un = emptyList()
                            )
                        else -> {
63
                            log.error("Invalid structure in $subEntity for $parent.")
Jonas Waeber's avatar
Jonas Waeber committed
64 65 66 67 68 69
                            null
                        }
                    }
                }.reduce { acc, languageContainer ->
                    acc.merge(languageContainer)
                })
Jonas Waeber's avatar
Jonas Waeber committed
70
            null -> {
71
                log.info("No values for key $parent.")
Jonas Waeber's avatar
Jonas Waeber committed
72 73
                emptyList()
            }
Jonas Waeber's avatar
Jonas Waeber committed
74
            else -> {
75
                log.error("No values for key $parent.")
Jonas Waeber's avatar
Jonas Waeber committed
76 77 78 79 80 81 82 83 84 85 86 87
                emptyList()
            }
        }
    }

    fun typedEntityByType(
        entities: List<JsonObject>,
        field: String,
        type: String,
        annotationField: String
    ): List<LanguageContainer> {
        return entities.filter { it[field] == type }.flatMap {
Jonas Waeber's avatar
Jonas Waeber committed
88
            languageContainer(it[KEYS.atType] as String, it[annotationField])
Jonas Waeber's avatar
Jonas Waeber committed
89 90 91
        }
    }

92
    fun extractIdValue(entities: List<JsonObject>, type: String): String? {
Jonas Waeber's avatar
Jonas Waeber committed
93
        return entities.first {
Jonas Waeber's avatar
Jonas Waeber committed
94
            it[KEYS.ricoType].let { identifier ->
Jonas Waeber's avatar
Jonas Waeber committed
95 96 97 98 99
                if (identifier == null)
                    false
                else
                    identifier == type
            }
100
        }.getOrDefault("identifier", null) as String?
Jonas Waeber's avatar
Jonas Waeber committed
101 102 103 104 105 106 107 108
    }

    fun listOfStrings(value: Any?): List<String> {
        return when (value) {
            is String -> listOf(value)
            is List<*> ->
                if (value[0] is String)
                    value as List<String>
109 110
                else {
                    log.error("Could not extract strings from $value.")
Jonas Waeber's avatar
Jonas Waeber committed
111
                    emptyList()
112 113
                }

Jonas Waeber's avatar
Jonas Waeber committed
114 115 116 117
            else ->
                emptyList()
        }
    }
118

Jonas Waeber's avatar
Jonas Waeber committed
119
    fun identifiers(value: Any?): List<String> {
120 121
        return when (value) {
            is String -> listOf(value)
Jonas Waeber's avatar
Jonas Waeber committed
122
            is JsonObject -> value[KEYS.atType].let { if (it is String) listOf(it) else emptyList() }
123 124 125 126
            is JsonArray<*> ->
                value.mapNotNull { item ->
                    when (item) {
                        is String -> item
Jonas Waeber's avatar
Jonas Waeber committed
127
                        is JsonObject -> value[KEYS.atType].let { id: Any? ->
128 129 130 131 132 133 134 135 136 137
                            if (id is String)
                                id
                            else null
                        }
                        else -> null
                    }
                }
            else -> emptyList()
        }
    }
Jonas Waeber's avatar
Jonas Waeber committed
138
}