FacetContainer.kt 2.56 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.model.LanguageContainer.Companion
Jonas Waeber's avatar
Jonas Waeber committed
23

Jonas Waeber's avatar
Jonas Waeber committed
24
@JsonInclude(JsonInclude.Include.NON_EMPTY)
Jonas Waeber's avatar
Jonas Waeber committed
25
data class FacetContainer(
Jonas Waeber's avatar
Jonas Waeber committed
26
    val name: LanguageContainer,
27
    val filter: String?,
28
    val facet: List<String>?
Jonas Waeber's avatar
Jonas Waeber committed
29 30
) {
    companion object {
31 32 33 34 35
        val EMPTY = FacetContainer(
            LanguageContainer.EMPTY,
            null,
            emptyList()
        )
Jonas Waeber's avatar
Jonas Waeber committed
36 37 38 39 40 41 42 43 44 45 46 47 48
        val DEFAULT = FacetContainer(
            LanguageContainer.DEFAULT,
            null,
            emptyList()
        )

        fun placeholder(placeholder: String): FacetContainer {
            return FacetContainer(
                LanguageContainer.placeholder(placeholder),
                placeholder,
                listOf(placeholder)
            )
        }
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75

        fun fromMap(map: Any?): FacetContainer {
            return when (map) {
                is Map<*, *> -> {
                    val languageContainer = LanguageContainer(
                        extractMapItem(map as Map<String, Any?>, "de"),
                        extractMapItem(map, "fr"),
                        extractMapItem(map, "it"),
                        extractMapItem(map, "unt")
                    )
                    FacetContainer(languageContainer, null, extractMapItem(map, "facet"))
                }
                else -> EMPTY
            }
        }
        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
76 77
    }
}