RecordSetSearchDocBuilder.kt 4.07 KB
Newer Older
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/*
 * 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

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<String, JsonObject>): Schema {
        val recordSet =
                input["recordSet"] ?: throw InvalidInputException("No recordSet entity found in message $key.")
        val identifiers = mutableListOf<JsonObject>()
        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) {
Jonas Waeber's avatar
Jonas Waeber committed
61
                        is Boolean -> it
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
98
                        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) }
                }
            }
        }
    }
}