In order to mitigate against the brute force attacks against Gitlab accounts, we are moving to all edu-ID Logins. We would like to remind you to link your account with your edu-id. Login will be possible only by edu-ID after November 30, 2021. Here you can find the instructions for linking your account.

If you don't have a SWITCH edu-ID, you can create one with this guide here

kind regards

This Server has been upgraded to GitLab release 14.2.6

Commit 1a5a77de authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor agent & corporateBody transformation

parent 288d6581
......@@ -45,6 +45,8 @@ object KEYS {
const val creator = "creator"
const val Person = "Person"
const val CorporateBody = "CorporateBody"
const val Agent = "Agent"
const val Place = "Place"
const val Title = "Title"
......
......@@ -22,8 +22,6 @@ import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
import org.memobase.builders.*
import org.memobase.helpers.*
import org.memobase.model.FacettedContainer
import org.memobase.model.LanguageContainer
import org.memobase.model.SearchDoc
import org.memobase.rdf.NS
......@@ -47,11 +45,23 @@ class SearchDocTransform {
val temporalIds = Extract.identifiers(record[KEYS.temporal])
val personFacetBuilder = PersonFacetBuilder()
val subjectPersonBuilder = PersonContainerBuilder(hasSubjectIds, null, input)
val publisherPersonBuilder = PersonContainerBuilder(publishedByIds, null, input)
val producersPersonBuilder = PersonContainerBuilder(producerIds, null, input)
val contributorPersonBuilder = PersonContainerBuilder(emptyList(), KEYS.contributor, input)
val creatorPersonBuilder = PersonContainerBuilder(emptyList(), KEYS.creator, input)
val subjectPersonBuilder = AgentContainerBuilder(hasSubjectIds, KEYS.Person, null, input)
val publisherPersonBuilder = AgentContainerBuilder(publishedByIds, KEYS.Person, null, input)
val producersPersonBuilder = AgentContainerBuilder(producerIds, KEYS.Person, null, input)
val contributorPersonBuilder = AgentContainerBuilder(emptyList(), KEYS.Person, KEYS.contributor, input)
val creatorPersonBuilder = AgentContainerBuilder(emptyList(), KEYS.Person, KEYS.creator, input)
val subjectCorporateBodyBuilder = AgentContainerBuilder(hasSubjectIds, KEYS.CorporateBody, null, input)
val publisherCorporateBodyBuilder = AgentContainerBuilder(publishedByIds, KEYS.CorporateBody, null, input)
val producersCorporateBodyBuilder = AgentContainerBuilder(producerIds, KEYS.CorporateBody, null, input)
val contributorCorporateBodyBuilder = AgentContainerBuilder(emptyList(), KEYS.CorporateBody, KEYS.contributor, input)
val creatorCorporateBodyBuilder = AgentContainerBuilder(emptyList(), KEYS.CorporateBody, KEYS.creator, input)
val subjectAgentBuilder = AgentContainerBuilder(hasSubjectIds, KEYS.Agent, null, input)
val publisherAgentBuilder = AgentContainerBuilder(publishedByIds, KEYS.Agent, null, input)
val producersAgentBuilder = AgentContainerBuilder(producerIds, KEYS.Agent, null, input)
val contributorAgentBuilder = AgentContainerBuilder(emptyList(), KEYS.Agent, KEYS.contributor, input)
val creatorAgentBuilder = AgentContainerBuilder(emptyList(), KEYS.Agent, KEYS.creator, input)
val placesRelatedBuilder = FacettedContainerBuilder(spatialIds, KEYS.Place, KEYS.name, FacetBuildHelpers::place)
val placeCapturedBuilder =
......@@ -73,6 +83,17 @@ class SearchDocTransform {
producersPersonBuilder,
contributorPersonBuilder,
creatorPersonBuilder,
subjectCorporateBodyBuilder,
publisherCorporateBodyBuilder,
producersCorporateBodyBuilder,
contributorCorporateBodyBuilder,
creatorCorporateBodyBuilder,
subjectAgentBuilder,
publisherAgentBuilder,
producersAgentBuilder,
contributorAgentBuilder,
creatorAgentBuilder,
placeFacetBuilder,
placeCapturedBuilder,
placesRelatedBuilder,
......@@ -97,11 +118,6 @@ class SearchDocTransform {
val genre = Filter.entitiesByProperty("hasGenre", record, input)
val subjects = Filter.entitiesByProperty("hasSubject", record, input)
val publishers = Filter.entitiesByProperty("publishedBy", record, input)
val producers = Filter.entitiesByProperty("P60441", record, input)
val creationRelationAgents =
Filter.entitiesByProperty("recordResourceOrInstantiationIsSourceOfCreationRelation", record, input)
val digitalRules = Filter.entitiesByProperty("regulatedBy", digitalObject, input)
val physicalRules = Filter.entitiesByProperty("regulatedBy", physicalObject, input)
......@@ -157,37 +173,17 @@ class SearchDocTransform {
personCreator = creatorPersonBuilder.build(),
personsFacet = personFacetBuilder.build(),
corporateBodySubject = Extract.typedEntityByType(subjects, "@type", "CorporateBody", "name"),
corporateBodyProducer = Extract.typedEntityByType(producers, "@type", "CorporateBody", "name"),
corporateBodyPublisher = Extract.typedEntityByType(publishers, "@type", "CorporateBody", "name"),
corporateBodyContributor = Extract.extractTypedCreationRelationAgent(
creationRelationAgents,
input,
creationRelationTypeParam = "contributor",
agentTypeParam = NS.rico + "CorporateBody"
),
corporateBodyCreator = Extract.extractTypedCreationRelationAgent(
creationRelationAgents,
input,
creationRelationTypeParam = "creator",
agentTypeParam = NS.rico + "CorporateBody"
),
agentSubject = Extract.typedEntityByType(subjects, "@type", "Agent", "name"),
agentProducer = Extract.typedEntityByType(producers, "@type", "Agent", "name"),
agentPublisher = Extract.typedEntityByType(publishers, "@type", "Agent", "name"),
agentContributor = Extract.extractTypedCreationRelationAgent(
creationRelationAgents,
input,
creationRelationTypeParam = "contributor",
agentTypeParam = NS.rico + "Agent"
),
agentCreator = Extract.extractTypedCreationRelationAgent(
creationRelationAgents,
input,
creationRelationTypeParam = "creator",
agentTypeParam = NS.rico + "Agent"
),
corporateBodySubject = subjectCorporateBodyBuilder.build(),
corporateBodyProducer = producersCorporateBodyBuilder.build(),
corporateBodyPublisher = publisherCorporateBodyBuilder.build(),
corporateBodyContributor = contributorCorporateBodyBuilder.build(),
corporateBodyCreator = creatorCorporateBodyBuilder.build(),
agentSubject = subjectAgentBuilder.build(),
agentProducer = producersAgentBuilder.build(),
agentPublisher = publisherAgentBuilder.build(),
agentContributor = contributorAgentBuilder.build(),
agentCreator = creatorAgentBuilder.build(),
// DIGITAL & PHYSICAL
access = accessPhysical + accessDigital,
......
......@@ -22,19 +22,20 @@ import com.beust.klaxon.JsonObject
import org.memobase.KEYS
import org.memobase.helpers.Extract
import org.memobase.helpers.FacetBuildHelpers
import org.memobase.model.PersonContainer
import org.memobase.model.AgentWithRelationContainer
import org.memobase.rdf.NS
class PersonContainerBuilder(
class AgentContainerBuilder(
private val identifiers: List<String>,
private val agentType: String,
private val creationRelationType: String?,
private val inputMap: Map<String, JsonObject>
) : IFieldBuilder {
private val containers = mutableListOf<PersonContainer>()
private val containers = mutableListOf<AgentWithRelationContainer>()
override fun filter(jsonObject: JsonObject): Boolean {
if (jsonObject[KEYS.atType] != NS.rico + KEYS.Person) return false
if (jsonObject[KEYS.atType] != NS.rico + agentType) return false
return if (creationRelationType != null) {
jsonObject[KEYS.agentIsTargetOfCreationRelation].let {
if (it != null)
......@@ -50,7 +51,7 @@ class PersonContainerBuilder(
}
override fun append(jsonObject: JsonObject): String {
val name = Extract.languageContainer("person-name", jsonObject[KEYS.name])
val name = Extract.languageContainer("agent-name", jsonObject[KEYS.name])
.reduce { acc, languageContainer -> acc.merge(languageContainer) }
val relation = if (jsonObject.containsKey(KEYS.agentIsTargetOfCreationRelation)) {
val creationRelation = inputMap[jsonObject[KEYS.agentIsTargetOfCreationRelation]]
......@@ -64,9 +65,13 @@ class PersonContainerBuilder(
} else {
null
}
val facet = FacetBuildHelpers.person(jsonObject)
val facet = if (agentType == KEYS.Person) {
FacetBuildHelpers.person(jsonObject)
} else {
null
}
containers.add(
PersonContainer(
AgentWithRelationContainer(
name,
relation,
facet
......@@ -75,7 +80,7 @@ class PersonContainerBuilder(
return "Created person container for person ${jsonObject[KEYS.entityId]}"
}
override fun build(): List<PersonContainer> {
override fun build(): List<AgentWithRelationContainer> {
return containers
}
}
......@@ -21,11 +21,8 @@ package org.memobase.helpers
import com.beust.klaxon.JsonArray
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
import org.memobase.model.DateContainer
import org.memobase.model.FacettedContainer
import org.memobase.model.LanguageContainer
import org.memobase.model.RelationNameContainer
import org.memobase.rdf.NS
object Extract {
private val log = LogManager.getLogger("ExtractSearchDoc")
......@@ -110,35 +107,6 @@ object Extract {
}["identifier"] as String? ?: error("Identifier contains no property rico:identifier: $entities.")
}
fun extractTypedCreationRelationAgent(
creationRelations: List<JsonObject>,
input: Map<String, JsonObject>,
creationRelationTypeParam: String,
agentTypeParam: String
): List<RelationNameContainer> {
return creationRelations.mapNotNull { creationRelation ->
val creationRelationType = creationRelation["type"] as String
if (creationRelationTypeParam != creationRelationType) return@mapNotNull null
val name = languageContainer("creation-relation-name", creationRelation["name"])
if (name.isEmpty()) {
log.warn("No name for creation relation found: $creationRelation.")
}
val agent = Filter.entitiesByProperty("creationRelationHasTarget", creationRelation, input).first()
val agentType = agent["@type"] as String
if (agentType != agentTypeParam) return@mapNotNull null
val agentName = languageContainer(agent["@id"] as String, agent["name"])
if (agentName.isEmpty()) {
log.error("Agent/Person/CorporateBody without rico:name found: ${agent["@id"]}.")
null
} else {
return@mapNotNull RelationNameContainer(
agentName.reduce { acc, languageContainer -> acc.merge(languageContainer) },
if (name.isNotEmpty()) name.reduce { acc, languageContainer -> acc.merge(languageContainer) }
else LanguageContainer(emptyList(), emptyList(), emptyList(), listOf("Unknown")))
}
}
}
fun listOfStrings(value: Any?): List<String> {
return when (value) {
is String -> listOf(value)
......
......@@ -18,8 +18,8 @@
package org.memobase.model
data class PersonContainer(
data class AgentWithRelationContainer(
val name: LanguageContainer,
val relation: LanguageContainer?,
val facet: List<String>
val facet: List<String>?
)
/*
* 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.model
import com.fasterxml.jackson.annotation.JsonInclude
@JsonInclude(JsonInclude.Include.NON_NULL)
data class RelationNameContainer(
val name: LanguageContainer,
val relation: LanguageContainer
)
......@@ -47,24 +47,24 @@ data class SearchDoc(
val language: List<FacettedContainer>,
// Agents
val personSubject: List<PersonContainer>,
val personCreator: List<PersonContainer>,
val personContributor: List<PersonContainer>,
val personPublisher: List<PersonContainer>,
val personProducer: List<PersonContainer>,
val personSubject: List<AgentWithRelationContainer>,
val personCreator: List<AgentWithRelationContainer>,
val personContributor: List<AgentWithRelationContainer>,
val personPublisher: List<AgentWithRelationContainer>,
val personProducer: List<AgentWithRelationContainer>,
val personsFacet: List<String>,
val corporateBodySubject: List<LanguageContainer>,
val corporateBodyCreator: List<RelationNameContainer>,
val corporateBodyContributor: List<RelationNameContainer>,
val corporateBodyPublisher: List<LanguageContainer>,
val corporateBodyProducer: List<LanguageContainer>,
val agentSubject: List<LanguageContainer>,
val agentCreator: List<RelationNameContainer>,
val agentContributor: List<RelationNameContainer>,
val agentPublisher: List<LanguageContainer>,
val agentProducer: List<LanguageContainer>,
val corporateBodySubject: List<AgentWithRelationContainer>,
val corporateBodyCreator: List<AgentWithRelationContainer>,
val corporateBodyContributor: List<AgentWithRelationContainer>,
val corporateBodyPublisher: List<AgentWithRelationContainer>,
val corporateBodyProducer: List<AgentWithRelationContainer>,
val agentSubject: List<AgentWithRelationContainer>,
val agentCreator: List<AgentWithRelationContainer>,
val agentContributor: List<AgentWithRelationContainer>,
val agentPublisher: List<AgentWithRelationContainer>,
val agentProducer: List<AgentWithRelationContainer>,
// Places
val placeRelated: List<FacettedContainer>,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment