PersonContainerBuilder.kt 3.67 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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
 * 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.JsonArray
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.rdf.NS

class PersonContainerBuilder(
    private val sourceProperty: String,
    private val sourceResource: JsonObject,
    private val creationRelationType: String?,
    private val inputMap: Map<String, JsonObject>
) :
    IFieldBuilder {

    private val containers = mutableListOf<PersonContainer>()

    override fun filter(jsonObject: JsonObject): Boolean {
Jonas Waeber's avatar
Jonas Waeber committed
40
        if (jsonObject[KEYS.atType] != NS.rico + KEYS.Person) return false
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
        return if (creationRelationType != null) {
            jsonObject[KEYS.agentIsTargetOfCreationRelation].let {
                if (it != null)
                    inputMap[it].let { creationRelation ->
                        creationRelation?.get(KEYS.ricoType) == creationRelationType
                    }
                else
                    false
            }
        } else {
            sourceResource[sourceProperty].let {
                when (it) {
                    is String -> it == jsonObject[KEYS.entityId] as String
                    is JsonObject -> it[KEYS.entityId] as String == jsonObject[KEYS.entityId] as String
                    is JsonArray<*> -> {
                        it.any { child ->
                            when (child) {
                                is String -> child == jsonObject[KEYS.entityId] as String
                                is JsonObject -> child[KEYS.entityId] as String == jsonObject[KEYS.entityId] as String
                                else -> false
                            }
                        }
                    }
                    else -> false
                }
            }
        }
    }

    override fun append(jsonObject: JsonObject): String {
        val name = Extract.languageContainer("person-name", jsonObject[KEYS.name])
            .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
        }
        val facet = FacetBuildHelpers.person(jsonObject)
        containers.add(
            PersonContainer(
                name,
                relation,
                facet
            )
        )
        return "Created person container for person ${jsonObject[KEYS.entityId]}"
    }

    override fun build(): List<PersonContainer> {
        return containers
    }
}