/*
* 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
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
import org.memobase.helpers.Date
import org.memobase.helpers.ElasticSearchWrapper
import org.memobase.helpers.Extract
import org.memobase.helpers.KEYS
import org.memobase.model.FacetContainer
import org.memobase.model.LanguageContainer
import org.memobase.model.RecordSetSearchDoc
import org.memobase.model.Schema
import org.memobase.model.IntegerRange
class RecordSetSearchDocBuilder(private val elasticSearchWrapper: ElasticSearchWrapper) {
private val log = LogManager.getLogger("RecordSetSearchDocBuilder")
fun transform(key: String, input: Map): Schema {
val recordSet =
input["recordSet"] ?: throw InvalidInputException("No recordSet entity found in message $key.")
val identifiers = mutableListOf()
input.values.forEach {
when {
it[KEYS.ricoType] == KEYS.IdentifierType.main -> {
identifiers.add(it)
}
}
}
val name = extractLanguageContainer(recordSet[KEYS.title], "NoNameFound")
val description = extractLanguageContainer(recordSet[KEYS.descriptiveNote], "NoDescriptionFound")
val id = Extract.extractIdValue(identifiers, KEYS.IdentifierType.main) ?: "NoIdentifierFound"
val institution = recordSet[KEYS.heldBy] as String?
if (institution != null) {
// TODO:
}
return RecordSetSearchDoc(
recordSetId = id,
isPublished = recordSet[KEYS.isPublished].let {
when (it) {
is String -> it.toBoolean()
else -> {
log.error("Found no isPublished property on record set $key. Set to false.")
false
}
}
},
scopeAndContent = description,
periodOfTimeAsYear = IntegerRange(1920, 2020),
institution = FacetContainer(LanguageContainer.placeholder("NoNameInstitution"), filter = institution, facet = emptyList()),
supportedByMemoriav = recordSet[KEYS.sponsoredBy] != null,
name = name,
documentType = elasticSearchWrapper.getDocumentTypesFromRecords(id, KEYS.QueryFields.recordSetFacet),
keyVisualLink = recordSet[KEYS.wikidataImage].let { if (it != null) it as String else "NoKeyVisualLinkDefined" },
numberOfDocuments = elasticSearchWrapper.countNumberOfDocuments(id),
lastUpdatedDate = Date.now,
languageOfMetadata = FacetContainer(LanguageContainer.placeholder("Deutsch"), filter = null, facet = emptyList())
)
}
private fun extractLanguageContainer(value: Any?, placeholder: String): LanguageContainer {
return Extract.languageContainer("record set", value).let { items ->
when {
items.isEmpty() -> {
LanguageContainer.placeholder(placeholder)
}
items.size == 1 -> {
items[0]
}
else -> {
items.reduce { acc, languageContainer -> acc.merge(languageContainer) }
}
}
}
}
}