/*
* 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 .
*/
package org.memobase.builders
import ch.memobase.rdf.NS
import com.beust.klaxon.JsonObject
import org.memobase.helpers.Extract
import org.memobase.helpers.Constants
import org.memobase.model.FacetContainer
/**
* @param identifiers: A list of identifiers to filter on.
*/
class FacettedContainerBuilder(
private val identifiers: List,
private val targetType: String,
private val nameProperty: String,
private val facetFunction: (jsonObject: JsonObject) -> Pair>
) : IFieldBuilder {
private val containers = mutableListOf()
override fun filter(jsonObject: JsonObject): Boolean {
if (jsonObject[Constants.atType] != NS.rico + targetType) return false
return identifiers.contains(jsonObject[Constants.entityId])
}
override fun append(key: String, jsonObject: JsonObject): String {
val filterAndFacet = facetFunction(jsonObject)
containers.add(
FacetContainer(
Extract.languageContainer("", jsonObject[nameProperty])
.reduce { acc, languageContainer -> acc.merge(languageContainer) },
filter = filterAndFacet.first,
facet = filterAndFacet.second
)
)
return ""
}
override fun build(): List {
return containers
}
}