PersonFacetBuilder.kt 3.09 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
40
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
/*
 * 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.AsciiFolder
import org.memobase.helpers.Extract
import org.memobase.rdf.NS

class PersonFacetBuilder(private val ricoType: String) : IFieldBuilder {

    private val separator = "~"
    private val terminator = "#"

    private val level1 = "0"
    private val level2 = "1"
    private val isAlphaChar = Regex("[A-Za-z]")

    private val personFacetValues = mutableSetOf<String>()

    override fun filter(jsonObject: JsonObject, map: Map<String, JsonObject>): Boolean {
        return if (jsonObject[KEYS.atType].let {
                when (it) {
                    is String -> it == NS.rico + KEYS.Person
                    else -> false
                }
            }) {
            val ids = Extract.identifier(jsonObject[KEYS.agentIsTargetOfCreationRelation])
            ids
                .map { map[it] }
                .map { it?.get(KEYS.ricoType) }
                .any { it != null && it is String && it == ricoType }
        } else {
            false
        }
    }

    override fun append(jsonObject: JsonObject): String {
        val name = when {
            jsonObject.containsKey("lastName") -> {
                jsonObject["lastName"] as String
            }
            jsonObject.containsKey("name") -> {
                jsonObject["name"] as String
            }
            else -> {
                return "Failed to process person ${jsonObject["@id"]} for person facet, because the person does not have a name."
            }
        }
        val displayName = jsonObject["lastName"].let { lastName ->
            when (lastName) {
                is String -> lastName + jsonObject["firstName"].let { if (it is String) ", $it" else "" }
                else -> jsonObject["name"]
            }
        }
        val foldedName = AsciiFolder.foldToASCII(name)
        val firstChar = foldedName.first { isAlphaChar.matches(it.toString()) }
        val capitalLetter = firstChar.toUpperCase()
        personFacetValues.add("$level1$separator$capitalLetter$separator$terminator")
        personFacetValues.add("$level2$separator$capitalLetter$separator$displayName$separator$terminator")
        return "Successfully added person to facet list."
    }

    override fun build(): List<String> {
        return personFacetValues.toList().sortedBy { v -> v.substring(2) }
    }
}