PersonContainerBuilder.kt 2.89 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
/*
 * 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
import org.memobase.model.PersonContainer
import org.memobase.rdf.NS

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

    private val containers = mutableListOf<PersonContainer>()

    override fun filter(jsonObject: JsonObject): Boolean {
Jonas Waeber's avatar
Jonas Waeber committed
37
        if (jsonObject[KEYS.atType] != NS.rico + KEYS.Person) return false
38
39
40
41
42
43
44
45
46
47
        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
48
            identifiers.contains(jsonObject[KEYS.entityId])
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
        }
    }

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