SuggestContainerBuilder.kt 3.51 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
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*
 * 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

import com.beust.klaxon.JsonObject
import org.memobase.KEYS
import org.memobase.helpers.Extract
import org.memobase.model.SuggestContainer
import org.memobase.rdf.NS

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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
        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(
                    Extract.languageContainer("main-title", jsonObject[KEYS.title])
                        .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
                )
                ""
            }
            KEYS.TitleTypes.series -> {
                seriesTitles.addAll(
                    Extract.languageContainer("series-title", jsonObject[KEYS.title])
                        .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
                )
                ""
            }
            KEYS.TitleTypes.broadcast -> {
                broadcastTitles.addAll(
                    Extract.languageContainer("broadcast-title", jsonObject[KEYS.title])
                        .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
                )
                ""
            }
            else -> "Unknown title type ${jsonObject[KEYS.ricoType]}."
        }
    }

    private fun parseKeywords(jsonObject: JsonObject): String {
        keywords.addAll(
            Extract.languageContainer("keywords-concept", jsonObject[KEYS.prefLabel])
                .reduce { acc, languageContainer -> acc.merge(languageContainer) }.toList()
        )
        return ""
    }

    override fun build(): List<SuggestContainer> {
        return listOf(
            SuggestContainer(
                title = titles,
                seriesTitle = seriesTitles,
                broadcastTitle = broadcastTitles,
                keywords = keywords
            )
        )
    }
}