PersonFacetBuilder.kt 1.87 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
24
import org.apache.logging.log4j.LogManager
import org.memobase.helpers.FacetBuildHelpers
25
import org.memobase.helpers.KEYS
26

27
28
class PersonFacetBuilder : IFieldBuilder {
    private val log = LogManager.getLogger("PersonFacetBuilder")
29
30
31

    private val personFacetValues = mutableSetOf<String>()

32
33
34
35
36
37
    override fun filter(jsonObject: JsonObject): Boolean {
        return jsonObject[KEYS.atType].let {
            when (it) {
                is String -> it == NS.rico + KEYS.Person
                else -> false
            }
38
39
40
        }
    }

41
    override fun append(key: String, jsonObject: JsonObject): String {
42
        val result = FacetBuildHelpers.person(jsonObject)
43
44
        return if (result.second.isNotEmpty()) {
            personFacetValues.addAll(result.second)
45
46
47
48
49
            ""
        } else {
            val message = "Failed to create facet values for persons: ${jsonObject[KEYS.entityId]}."
            log.warn(message)
            message
50
51
52
53
54
55
56
        }
    }

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