AgentContainerBuilder.kt 3.05 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
/*
 * 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

import com.beust.klaxon.JsonObject
import org.memobase.KEYS
import org.memobase.helpers.Extract
import org.memobase.helpers.FacetBuildHelpers
25
import org.memobase.model.AgentWithRelationContainer
26
27
import org.memobase.rdf.NS

28
class AgentContainerBuilder(
Jonas Waeber's avatar
Jonas Waeber committed
29
    private val identifiers: List<String>,
30
    private val agentType: String,
31
32
    private val creationRelationType: String?,
    private val inputMap: Map<String, JsonObject>
Jonas Waeber's avatar
Jonas Waeber committed
33
) : IFieldBuilder {
34

35
    private val containers = mutableListOf<AgentWithRelationContainer>()
36
37

    override fun filter(jsonObject: JsonObject): Boolean {
38
        if (jsonObject[KEYS.atType] != NS.rico + agentType) return false
39
40
41
42
43
44
45
46
47
48
        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
49
            identifiers.contains(jsonObject[KEYS.entityId])
50
51
52
53
        }
    }

    override fun append(jsonObject: JsonObject): String {
54
        val name = Extract.languageContainer("agent-name", jsonObject[KEYS.name])
55
56
57
58
59
60
61
62
63
64
65
66
67
            .reduce { acc, languageContainer -> acc.merge(languageContainer) }
        val relation = if (jsonObject.containsKey(KEYS.agentIsTargetOfCreationRelation)) {
            val creationRelation = inputMap[jsonObject[KEYS.agentIsTargetOfCreationRelation]]
            creationRelation.let {
                if (it != null)
                    Extract.languageContainer("creation-relation-name", it["name"])
                        .reduce { acc, languageContainer -> acc.merge(languageContainer) }
                else
                    null
            }
        } else {
            null
        }
68
69
70
71
72
        val facet = if (agentType == KEYS.Person) {
            FacetBuildHelpers.person(jsonObject)
        } else {
            null
        }
73
        containers.add(
74
            AgentWithRelationContainer(
75
76
77
78
79
80
81
82
                name,
                relation,
                facet
            )
        )
        return "Created person container for person ${jsonObject[KEYS.entityId]}"
    }

83
    override fun build(): List<AgentWithRelationContainer> {
84
85
86
        return containers
    }
}