LanguageContainer.kt 5.09 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
/*
 * 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/>.
 */

Jonas Waeber's avatar
Jonas Waeber committed
19
package org.memobase.model
Jonas Waeber's avatar
Jonas Waeber committed
20

Jonas Waeber's avatar
Jonas Waeber committed
21
import com.fasterxml.jackson.annotation.JsonInclude
22
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
23

Jonas Waeber's avatar
Jonas Waeber committed
24
@JsonInclude(JsonInclude.Include.NON_NULL)
25
data class LanguageContainer(
Jonas Waeber's avatar
Jonas Waeber committed
26 27 28 29
    val de: List<String> = emptyList(),
    val fr: List<String> = emptyList(),
    val it: List<String> = emptyList(),
    val un: List<String> = emptyList() // if the language is not known
Jonas Waeber's avatar
Jonas Waeber committed
30
) {
31
    companion object {
Jonas Waeber's avatar
Jonas Waeber committed
32
        val EMPTY = LanguageContainer()
33 34 35 36 37 38 39 40 41 42
        val DEFAULT = LanguageContainer(
            listOf(KEYS.missingLabelDe),
            listOf(KEYS.missingLabelFr),
            listOf(KEYS.missingLabelIt),
            listOf(KEYS.missingLabelEn)
        )

        fun placeholder(placeholder: String): LanguageContainer {
            return LanguageContainer(listOf(placeholder), listOf(placeholder), listOf(placeholder), listOf(placeholder))
        }
Jonas Waeber's avatar
Jonas Waeber committed
43

44
        fun fromMap(map: Any?): LanguageContainer {
Jonas Waeber's avatar
Jonas Waeber committed
45 46
            return when (map) {
                is Map<*, *> -> {
47 48 49 50 51 52 53
                    val languageContainer = LanguageContainer(
                        extractMapItem(map as Map<String, Any?>, "de"),
                        extractMapItem(map, "fr"),
                        extractMapItem(map, "it"),
                        extractMapItem(map, "unt")
                    )
                    languageContainer
Jonas Waeber's avatar
Jonas Waeber committed
54 55 56 57
                }
                else -> EMPTY
            }
        }
58 59 60 61 62 63 64 65 66 67 68 69 70
        private fun extractMapItem(map: Map<String, Any?>, language: String): List<String> {
            return when (val item = map[language]) {
                is String -> listOf(item)
                is List<*> ->
                    try {
                        item as List<String>
                    } catch (ex: ClassCastException) {
                        emptyList<String>()
                    }
                else -> emptyList()
            }
        }

Jonas Waeber's avatar
Jonas Waeber committed
71 72
        const val valueJsonLDFieldName = "@value"
        const val languageJsonLDFieldName = "@language"
73

Jonas Waeber's avatar
Jonas Waeber committed
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
    }

    fun add(value: Any?): LanguageContainer {
        return when (value) {
            null -> this
            is String ->
                LanguageContainer(
                    de, fr, it, un + value
                )
            is Map<*, *> -> {
                value as Map<String, Any?>
                mapRdfLanguageTags(value)
            }
            is List<*> -> {
                value.map { item ->
                    when (item) {
                        is String -> LanguageContainer(un = listOf(item))
                        is Map<*, *> -> {
                            item as Map<String, Any?>
                            mapRdfLanguageTags(item)
                        }
                        else -> EMPTY
                    }

                }.reduce { acc, languageContainer -> acc.merge(languageContainer) }
            }
            else -> this
        }
    }

    private fun mapRdfLanguageTags(value: Map<String, Any?>): LanguageContainer {
        val item = value[valueJsonLDFieldName].let {
            when (it) {
                is String -> it
                else -> null
            }
        }
        val language = value[languageJsonLDFieldName].let {
            when (it) {
                is String -> it
                else -> null
            }
        }
        return LanguageContainer(
            if (language == "de" && item != null) de + item else de,
            if (language == "fr" && item != null) fr + item else fr,
            if (language == "it" && item != null) it + item else it,
            if (language == null && item != null) un + item else un
        )
123 124
    }

Jonas Waeber's avatar
Jonas Waeber committed
125 126
    fun toList(): List<String> {
        return de + fr + it + un
Jonas Waeber's avatar
Jonas Waeber committed
127 128
    }

Jonas Waeber's avatar
Jonas Waeber committed
129 130 131 132 133 134 135
    fun merge(container: LanguageContainer): LanguageContainer {
        return LanguageContainer(
            de + container.de,
            fr + container.fr,
            it + container.it,
            un + container.un
        )
Jonas Waeber's avatar
Jonas Waeber committed
136
    }
137 138 139 140 141 142 143 144 145 146

    fun any(): String {
        return when {
            un.isNotEmpty() -> un[0]
            de.isNotEmpty() -> de[0]
            fr.isNotEmpty() -> fr[0]
            it.isNotEmpty() -> it[0]
            else -> ""
        }
    }
147 148 149 150 151 152 153 154 155

    fun fillInEmpty(): LanguageContainer {
        return LanguageContainer(
            if (de.isEmpty()) listOf(KEYS.missingLabelDe) else de,
            if (fr.isEmpty()) listOf(KEYS.missingLabelFr) else fr,
            if (it.isEmpty()) listOf(KEYS.missingLabelIt) else it,
            un
        )
    }
156
}