SuggestContainerBuilder.kt 3.61 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.builders

21
import ch.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
22
23
import com.beust.klaxon.JsonObject
import org.memobase.helpers.Extract
24
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import org.memobase.model.SuggestContainer

class SuggestContainerBuilder(private val hasSubjectIds: List<String>) : IFieldBuilder {

    private val titles = mutableListOf<String>()
    private val seriesTitles = mutableListOf<String>()
    private val broadcastTitles = mutableListOf<String>()
    private val keywords = mutableListOf<String>()

    override fun filter(jsonObject: JsonObject): Boolean {
        return when {
            jsonObject[KEYS.atType] == NS.skos + KEYS.Concept -> {
                hasSubjectIds.contains(jsonObject[KEYS.entityId])
            }
            jsonObject[KEYS.atType] == NS.rico + KEYS.Title -> true
            else -> false
        }
    }

44
    override fun append(key: String, jsonObject: JsonObject): String {
Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
48
49
50
51
52
53
54
55
        return when (jsonObject[KEYS.atType]) {
            NS.skos + KEYS.Concept -> parseKeywords(jsonObject)
            NS.rico + KEYS.Title -> parseTitles(jsonObject)
            else -> ""
        }
    }

    private fun parseTitles(jsonObject: JsonObject): String {
        return when (jsonObject[KEYS.ricoType]) {
            KEYS.TitleTypes.main -> {
                titles.addAll(
56
57
                        Extract.languageContainer("main-title", jsonObject[KEYS.title])
                                .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
61
62
                )
                ""
            }
            KEYS.TitleTypes.series -> {
                seriesTitles.addAll(
63
64
                        Extract.languageContainer("series-title", jsonObject[KEYS.title])
                                .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
Jonas Waeber's avatar
Jonas Waeber committed
65
66
67
68
69
                )
                ""
            }
            KEYS.TitleTypes.broadcast -> {
                broadcastTitles.addAll(
70
71
                        Extract.languageContainer("broadcast-title", jsonObject[KEYS.title])
                                .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
Jonas Waeber's avatar
Jonas Waeber committed
72
73
74
75
76
77
78
79
80
                )
                ""
            }
            else -> "Unknown title type ${jsonObject[KEYS.ricoType]}."
        }
    }

    private fun parseKeywords(jsonObject: JsonObject): String {
        keywords.addAll(
81
82
                Extract.languageContainer("keywords-concept", jsonObject[KEYS.prefLabel])
                        .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
Jonas Waeber's avatar
Jonas Waeber committed
83
84
85
86
87
88
        )
        return ""
    }

    override fun build(): List<SuggestContainer> {
        return listOf(
89
90
91
92
93
94
                SuggestContainer(
                        title = titles,
                        seriesTitle = seriesTitles,
                        broadcastTitle = broadcastTitles,
                        keywords = keywords
                )
Jonas Waeber's avatar
Jonas Waeber committed
95
96
97
        )
    }
}