RecordSetSearchDocBuilder.kt 8.04 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 22 23 24
import ch.memobase.rdf.DC
import ch.memobase.rdf.RDA
import ch.memobase.rdf.RDF
import ch.memobase.rdf.RICO
25 26 27 28 29
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
30
import org.memobase.helpers.JSON
31 32
import org.memobase.helpers.KEYS
import org.memobase.model.FacetContainer
Jonas Waeber's avatar
Jonas Waeber committed
33
import org.memobase.model.IntegerRange
34 35 36 37 38 39
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
40
    private val log = LogManager.getLogger(this::class.java)
41 42 43

    fun transform(key: String, input: Map<String, JsonObject>): Schema {
        val recordSet =
Jonas Waeber's avatar
Jonas Waeber committed
44 45 46
            input[JSON.recordSetTag] ?: throw InvalidInputException("No record set entity found in message $key.")
        val publicationIds = Extract.identifiers(recordSet[RICO.isSubjectOf.localName])
        val relatedRecordSetIds = Extract.identifiers(recordSet[RICO.isRecordResourceAssociatedWithRecordResource.localName])
Jonas Waeber's avatar
Jonas Waeber committed
47
        val metadataLanguages = mutableListOf<JsonObject>()
Jonas Waeber's avatar
Jonas Waeber committed
48 49 50 51 52
        var originalTitles = LanguageContainer.EMPTY
        var projectTitles = LanguageContainer.EMPTY
        var relatedRecordSets = LanguageContainer.EMPTY
        var publicationTitles = LanguageContainer.EMPTY
        var relatedDocumentTitles = LanguageContainer.EMPTY
53 54
        input.values.forEach {
            when {
Jonas Waeber's avatar
Jonas Waeber committed
55
                it[RICO.type.localName] == KEYS.LanguageType.metadata -> {
Jonas Waeber's avatar
Jonas Waeber committed
56
                    metadataLanguages.add(it)
57
                }
Jonas Waeber's avatar
Jonas Waeber committed
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
                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])
                }
                it[KEYS.atType] == RICO.RecordSet.uri &&
                    relatedRecordSetIds.contains(it[KEYS.entityId]) -> {
                    relatedRecordSets = relatedRecordSets.add(it[RICO.title.localName])
                }
                it[KEYS.atType] == RICO.Record.uri -> {
                    if (publicationIds.contains(it[KEYS.entityId])) {
                        publicationTitles = publicationTitles.add(it[RICO.title.localName])
                    } else {
                        relatedDocumentTitles = relatedDocumentTitles.add(it[RICO.title.localName])
                    }
                }
77 78 79
            }
        }

Jonas Waeber's avatar
Jonas Waeber committed
80 81
        val name = extractLanguageContainer(recordSet[RICO.title.localName], "")
        val dates = Extract.identifiers(recordSet[RICO.isAssociatedWithDate.localName]).mapNotNull {
Jonas Waeber's avatar
Jonas Waeber committed
82 83
            input[it]
        }.map {
Jonas Waeber's avatar
Jonas Waeber committed
84
            it[RICO.normalizedDateValue.localName] as String
Jonas Waeber's avatar
Jonas Waeber committed
85 86 87 88 89 90
        }
        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
91
                } else
Jonas Waeber's avatar
Jonas Waeber committed
92 93
                    IntegerRange(splitDate[0].toInt(), splitDate[0].toInt())
            } catch (ex: NumberFormatException) {
Jonas Waeber's avatar
Jonas Waeber committed
94
                null
Jonas Waeber's avatar
Jonas Waeber committed
95 96
            }
        } else {
Jonas Waeber's avatar
Jonas Waeber committed
97
            null
98
        }
Jonas Waeber's avatar
Jonas Waeber committed
99 100
        val uri = recordSet[KEYS.entityId] as String
        val id = uri.substringAfterLast("/")
Jonas Waeber's avatar
Jonas Waeber committed
101
        val institution = recordSet[RICO.heldBy.localName] as String
Jonas Waeber's avatar
Jonas Waeber committed
102 103
        val institutionId = institution.substringAfterLast("/")

Jonas Waeber's avatar
Jonas Waeber committed
104 105 106 107 108 109 110 111 112 113
        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], "")
114 115

        return RecordSetSearchDoc(
Jonas Waeber's avatar
Jonas Waeber committed
116 117 118 119 120 121 122 123
            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
124
                    }
Jonas Waeber's avatar
Jonas Waeber committed
125 126 127 128 129
                }
            },
            periodOfTimeAsYear = date,
            institution = elasticSearchWrapper.getInstitutionName(institutionId),
            supportedByMemoriav = recordSet[KEYS.sponsoredBy] != null,
130

Jonas Waeber's avatar
Jonas Waeber committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
            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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            },
            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
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
        )
    }

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