PlaceFacetBuilder.kt 2.19 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * 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/>.
 */

19
package ch.memobase.builders
20

21
import ch.memobase.rdf.NS
22
23
import com.beust.klaxon.JsonObject
import org.apache.logging.log4j.LogManager
24
25
26
import ch.memobase.helpers.FacetBuildHelpers
import ch.memobase.helpers.Constants
import ch.memobase.model.SimpleFacetContainer
27
28
29
30
31

class PlaceFacetBuilder : IFieldBuilder {
    private val log = LogManager.getLogger("PersonFacetBuilder")

    private val placeFacetValues = mutableSetOf<String>()
32
    private val placeNames = mutableSetOf<String>()
33
34

    override fun filter(jsonObject: JsonObject): Boolean {
Jonas Waeber's avatar
Jonas Waeber committed
35
        return jsonObject[Constants.atType].let {
36
            when (it) {
Jonas Waeber's avatar
Jonas Waeber committed
37
                is String -> it == NS.rico + Constants.Place
38
39
40
41
42
                else -> false
            }
        }
    }

43
    override fun append(key: String, jsonObject: JsonObject): String {
44
        val result = FacetBuildHelpers.place(jsonObject)
45
        return if (result.second.isNotEmpty()) {
46
47
48
49
            result.first.let {
                if (it != null)
                    placeNames.add(it)
            }
50
            placeFacetValues.addAll(result.second)
51
52
            ""
        } else {
Jonas Waeber's avatar
Jonas Waeber committed
53
            val message = "Failed to create facet values for place: ${jsonObject[Constants.entityId]}."
54
55
56
57
58
            log.warn(message)
            message
        }
    }

59
60
61
62
63
    override fun build(): SimpleFacetContainer {
        return SimpleFacetContainer(
                placeFacetValues.toList().sortedBy { v -> v.substring(2) },
                placeNames.toList().sorted()
        )
64
65
    }
}