RecordSetSearchDocBuilder.kt 8.4 KB
Newer Older
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

Jonas Waeber's avatar
Jonas Waeber committed
21
import ch.memobase.rdf.DC
22
import ch.memobase.rdf.NS
Jonas Waeber's avatar
Jonas Waeber committed
23 24
import ch.memobase.rdf.RDA
import ch.memobase.rdf.RICO
25
import ch.memobase.rdf.RICO.Types.RecordSet
26 27 28 29 30
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
Jonas Waeber's avatar
Jonas Waeber committed
31
import org.memobase.helpers.JSON
32 33
import org.memobase.helpers.KEYS
import org.memobase.model.FacetContainer
Jonas Waeber's avatar
Jonas Waeber committed
34
import org.memobase.model.IntegerRange
35 36 37 38 39 40
import org.memobase.model.LanguageContainer
import org.memobase.model.RecordSetSearchDoc
import org.memobase.model.Schema


class RecordSetSearchDocBuilder(private val elasticSearchWrapper: ElasticSearchWrapper) {
Jonas Waeber's avatar
Jonas Waeber committed
41
    private val log = LogManager.getLogger(this::class.java)
42 43 44

    fun transform(key: String, input: Map<String, JsonObject>): Schema {
        val recordSet =
Jonas Waeber's avatar
Jonas Waeber committed
45
            input[JSON.recordSetTag] ?: throw InvalidInputException("No record set entity found in message $key.")
46 47
        val relatedRecordSetIds =
            Extract.identifiers(recordSet[RICO.isRecordResourceAssociatedWithRecordResource.localName])
Jonas Waeber's avatar
Jonas Waeber committed
48
        val metadataLanguages = mutableListOf<JsonObject>()
Jonas Waeber's avatar
Jonas Waeber committed
49 50 51 52 53
        var originalTitles = LanguageContainer.EMPTY
        var projectTitles = LanguageContainer.EMPTY
        var relatedRecordSets = LanguageContainer.EMPTY
        var publicationTitles = LanguageContainer.EMPTY
        var relatedDocumentTitles = LanguageContainer.EMPTY
54 55
        input.values.forEach {
            when {
Jonas Waeber's avatar
Jonas Waeber committed
56
                it[RICO.type.localName] == KEYS.LanguageType.metadata -> {
Jonas Waeber's avatar
Jonas Waeber committed
57
                    metadataLanguages.add(it)
58
                }
Jonas Waeber's avatar
Jonas Waeber committed
59 60 61 62 63 64 65 66
                it[KEYS.atType] == RICO.Title.uri &&
                        it[RICO.type.localName] == KEYS.TitleTypes.original -> {
                    originalTitles = originalTitles.add(it[RICO.title.localName])
                }
                it[KEYS.atType] == RICO.CorporateBody.uri &&
                        it[RICO.type.localName] == KEYS.CorporateBodyType.memoriavProject -> {
                    projectTitles = projectTitles.add(it[RICO.title.localName])
                }
67
                it[KEYS.atType] == RICO.RecordSet.uri && it[RICO.type.localName] == RecordSet.related -> {
Jonas Waeber's avatar
Jonas Waeber committed
68 69 70
                    relatedRecordSets = relatedRecordSets.add(it[RICO.title.localName])
                }
                it[KEYS.atType] == RICO.Record.uri -> {
71
                    if (it[RICO.type.localName] == RICO.Types.Record.publication) {
Jonas Waeber's avatar
Jonas Waeber committed
72
                        publicationTitles = publicationTitles.add(it[RICO.title.localName])
73
                    } else if (it[RICO.type.localName] == RICO.Types.Record.related) {
Jonas Waeber's avatar
Jonas Waeber committed
74 75 76
                        relatedDocumentTitles = relatedDocumentTitles.add(it[RICO.title.localName])
                    }
                }
77 78
            }
        }
79 80 81 82 83 84 85
        // related record sets which are present in memobase.
        relatedRecordSetIds.forEach { id ->
            if (id.startsWith(NS.mbrs)) {
                val languageContainer = elasticSearchWrapper.getRecordSetName(id.substringAfterLast("/"))
                relatedRecordSets = relatedRecordSets.merge(languageContainer)
            }
        }
86

Jonas Waeber's avatar
Jonas Waeber committed
87 88
        val name = extractLanguageContainer(recordSet[RICO.title.localName], "")
        val dates = Extract.identifiers(recordSet[RICO.isAssociatedWithDate.localName]).mapNotNull {
Jonas Waeber's avatar
Jonas Waeber committed
89 90
            input[it]
        }.map {
Jonas Waeber's avatar
Jonas Waeber committed
91
            it[RICO.normalizedDateValue.localName] as String
Jonas Waeber's avatar
Jonas Waeber committed
92 93 94 95 96 97
        }
        val date = if (dates.isNotEmpty()) {
            try {
                val splitDate = dates[0].split("/")
                if (splitDate.size == 2) {
                    IntegerRange(splitDate[0].toInt(), splitDate[1].toInt())
Jonas Waeber's avatar
Jonas Waeber committed
98
                } else
Jonas Waeber's avatar
Jonas Waeber committed
99 100
                    IntegerRange(splitDate[0].toInt(), splitDate[0].toInt())
            } catch (ex: NumberFormatException) {
Jonas Waeber's avatar
Jonas Waeber committed
101
                null
Jonas Waeber's avatar
Jonas Waeber committed
102 103
            }
        } else {
Jonas Waeber's avatar
Jonas Waeber committed
104
            null
105
        }
Jonas Waeber's avatar
Jonas Waeber committed
106 107
        val uri = recordSet[KEYS.entityId] as String
        val id = uri.substringAfterLast("/")
Jonas Waeber's avatar
Jonas Waeber committed
108
        val institution = recordSet[RICO.heldBy.localName] as String
Jonas Waeber's avatar
Jonas Waeber committed
109 110
        val institutionId = institution.substringAfterLast("/")

Jonas Waeber's avatar
Jonas Waeber committed
111 112 113 114 115 116 117 118 119 120
        val description = extractLanguageContainer(recordSet[RICO.descriptiveNote.localName], "")
        val rights = extractLanguageContainer(recordSet[RICO.conditionsOfUse.localName], "")
        val access = extractLanguageContainer(recordSet[RICO.conditionsOfAccess.localName], "")
        val accessMemobase = extractLanguageContainer(recordSet[RDA.hasRestrictionOnAccess.localName], "")
        val history = extractLanguageContainer(recordSet[RICO.history.localName], "")
        val integrity = extractLanguageContainer(recordSet[RICO.integrity.localName], "")
        val extent = extractLanguageContainer(recordSet[RICO.recordResourceExtent.localName], "")
        val scopeAndContent = extractLanguageContainer(recordSet[RICO.scopeAndContent.localName], "")
        val conformsTo = extractLanguageContainer(recordSet[DC.conformsTo.localName], "")
        val dataImport = extractLanguageContainer(recordSet[RICO.descriptiveNote.localName], "")
121 122

        return RecordSetSearchDoc(
Jonas Waeber's avatar
Jonas Waeber committed
123 124 125 126 127 128 129 130
            recordSetId = id,
            isPublished = recordSet[KEYS.isPublished].let {
                when (it) {
                    is Boolean -> it
                    is String -> it.toBoolean()
                    else -> {
                        log.error("Found no isPublished property on record set $key. Set to false.")
                        false
131
                    }
Jonas Waeber's avatar
Jonas Waeber committed
132 133 134 135 136
                }
            },
            periodOfTimeAsYear = date,
            institution = elasticSearchWrapper.getInstitutionName(institutionId),
            supportedByMemoriav = recordSet[KEYS.sponsoredBy] != null,
137

Jonas Waeber's avatar
Jonas Waeber committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
            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 = metadataLanguages.map {
                FacetContainer(
                    extractLanguageContainer(it[KEYS.name], "NoMetadataLanguageSet"),
                    it[KEYS.sameAs].let { wikidataUri ->
                        when (wikidataUri) {
                            is String -> wikidataUri.substringAfterLast("/")
                            else -> null
                        }
                    },
                    emptyList()
                )
Jonas Waeber's avatar
Jonas Waeber committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
            },
            scopeAndContent = scopeAndContent,
            accessMemobase = accessMemobase,
            context = history,
            originalTitle = originalTitles,
            extent = extent,
            selection = integrity,
            indexing = conformsTo,
            rights = rights,
            description = description,
            access = access,
            project = projectTitles,
            relatedRecordSets = relatedRecordSets,
            relatedPublications = publicationTitles,
            relatedDocuments = relatedDocumentTitles,
            dataImport = dataImport
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
        )
    }

    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) }
                }
            }
        }
    }
}