AgentContainerBuilder.kt 3.91 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.builders

21
import ch.memobase.rdf.NS
22
import com.beust.klaxon.JsonObject
23
import org.apache.logging.log4j.LogManager
24
25
import org.memobase.helpers.Extract
import org.memobase.helpers.FacetBuildHelpers
26
import org.memobase.helpers.KEYS
27
import org.memobase.model.AgentWithRelationContainer
28
import org.memobase.model.LanguageContainer
29

30
class AgentContainerBuilder(
31
32
33
34
        private val identifiers: List<String>,
        private val agentType: String,
        private val creationRelationType: String?,
        private val inputMap: Map<String, JsonObject>
Jonas Waeber's avatar
Jonas Waeber committed
35
) : IFieldBuilder {
36
    private val log = LogManager.getLogger("AgentContainerBuilder")
37
    private val containers = mutableListOf<AgentWithRelationContainer>()
38
39

    override fun filter(jsonObject: JsonObject): Boolean {
40
        if (jsonObject[KEYS.atType] != NS.rico + agentType) return false
41
42
43
44
45
46
47
48
49
50
        return if (creationRelationType != null) {
            jsonObject[KEYS.agentIsTargetOfCreationRelation].let {
                if (it != null)
                    inputMap[it].let { creationRelation ->
                        creationRelation?.get(KEYS.ricoType) == creationRelationType
                    }
                else
                    false
            }
        } else {
Jonas Waeber's avatar
Jonas Waeber committed
51
            identifiers.contains(jsonObject[KEYS.entityId])
52
53
54
        }
    }

55
    override fun append(key: String, jsonObject: JsonObject): String {
56
57
        val names = Extract.languageContainer("agent-name", jsonObject[KEYS.name])
        val name = if (names.isNotEmpty())
58
            names.reduce { acc, languageContainer -> acc.merge(languageContainer) }
59
60
61
        else
            LanguageContainer(emptyList(), emptyList(), emptyList(), listOf("NoNameFound"))
        log.warn("Creation relation without names in agent ${jsonObject["@id"]}")
62
63
64
        val relation = if (jsonObject.containsKey(KEYS.agentIsTargetOfCreationRelation)) {
            val creationRelation = inputMap[jsonObject[KEYS.agentIsTargetOfCreationRelation]]
            creationRelation.let {
65
66
67
68
69
70
71
72
73
                if (it != null) {
                    val languages = Extract.languageContainer("creation-relation-name", it["name"])
                    if (languages.isNotEmpty())
                        languages.reduce { acc, languageContainer -> acc.merge(languageContainer) }
                    else {
                        log.warn("Creation relation without names in agent ${jsonObject["@id"]}")
                        LanguageContainer(emptyList(), emptyList(), emptyList(), emptyList())
                    }
                } else
74
75
76
77
78
                    null
            }
        } else {
            null
        }
79
        val filterAndFacet = if (agentType == KEYS.Person) {
80
81
            FacetBuildHelpers.person(jsonObject)
        } else {
82
            Pair(null, null)
83
        }
84
        containers.add(
85
86
87
88
89
90
                AgentWithRelationContainer(
                        name,
                        relation,
                        filter = filterAndFacet.first,
                        facet = filterAndFacet.second
                )
91
92
93
94
        )
        return "Created person container for person ${jsonObject[KEYS.entityId]}"
    }

95
    override fun build(): List<AgentWithRelationContainer> {
96
97
98
        return containers
    }
}