FacettedContainerBuilder.kt 2.11 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
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.memobase.helpers.Constants
Jonas Waeber's avatar
Jonas Waeber committed
25
import org.memobase.model.FacetContainer
Jonas Waeber's avatar
Jonas Waeber committed
26

Jonas Waeber's avatar
Jonas Waeber committed
27
28
29
/**
 * @param identifiers: A list of identifiers to filter on.
 */
Jonas Waeber's avatar
Jonas Waeber committed
30
class FacettedContainerBuilder(
31
32
33
34
        private val identifiers: List<String>,
        private val targetType: String,
        private val nameProperty: String,
        private val facetFunction: (jsonObject: JsonObject) -> Pair<String?, List<String>>
Jonas Waeber's avatar
Jonas Waeber committed
35
36
) : IFieldBuilder {

Jonas Waeber's avatar
Jonas Waeber committed
37
    private val containers = mutableListOf<FacetContainer>()
Jonas Waeber's avatar
Jonas Waeber committed
38
39

    override fun filter(jsonObject: JsonObject): Boolean {
Jonas Waeber's avatar
Jonas Waeber committed
40
41
        if (jsonObject[Constants.atType] != NS.rico + targetType) return false
        return identifiers.contains(jsonObject[Constants.entityId])
Jonas Waeber's avatar
Jonas Waeber committed
42
43
    }

44
    override fun append(key: String, jsonObject: JsonObject): String {
45
        val filterAndFacet = facetFunction(jsonObject)
Jonas Waeber's avatar
Jonas Waeber committed
46
        containers.add(
47
48
49
50
51
52
                FacetContainer(
                        Extract.languageContainer("", jsonObject[nameProperty])
                                .reduce { acc, languageContainer -> acc.merge(languageContainer) },
                        filter = filterAndFacet.first,
                        facet = filterAndFacet.second
                )
Jonas Waeber's avatar
Jonas Waeber committed
53
54
55
56
        )
        return ""
    }

Jonas Waeber's avatar
Jonas Waeber committed
57
    override fun build(): List<FacetContainer> {
Jonas Waeber's avatar
Jonas Waeber committed
58
59
60
        return containers
    }
}