DateContainerBuilder.kt 3.48 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*
 * 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/>.
 */
Jonas Waeber's avatar
Jonas Waeber committed
18
19
package org.memobase.builders

20
import ch.memobase.rdf.NS
21
import com.beust.klaxon.JsonArray
Jonas Waeber's avatar
Jonas Waeber committed
22
import com.beust.klaxon.JsonObject
23
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
24
import org.memobase.helpers.DateFacetBuildHelpers
25
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
26
27
28
import org.memobase.model.DateContainer

class DateContainerBuilder(private val containedIds: List<String>) : IFieldBuilder {
29
    private val log = LogManager.getLogger("DateContainerBuilder")
Jonas Waeber's avatar
Jonas Waeber committed
30
31
32
33
34
35
36
37
38
39

    private val dateContainers = mutableListOf<DateContainer>()

    override fun filter(jsonObject: JsonObject): Boolean {
        return if (containedIds.contains(jsonObject[KEYS.entityId]))
            listOf(NS.rico + KEYS.DateRange, NS.rico + KEYS.SingleDate).contains(jsonObject[KEYS.atType])
        else
            false
    }

40
    override fun append(key: String, jsonObject: JsonObject): String {
Jonas Waeber's avatar
Jonas Waeber committed
41
42
43
44
45
46
47
48
49
50
51
        val isNormalized = jsonObject.containsKey("normalizedDateValue")
        val date = if (isNormalized) {
            jsonObject["normalizedDateValue"] as String
        } else {
            jsonObject["expressedDate"] as String
        }
        val sort = if (isNormalized) {
            date
        } else {
            null
        }
52
53
        val qualifier = when (val value = jsonObject["dateQualifier"]) {
            is String -> listOf(value)
Jonas Waeber's avatar
Jonas Waeber committed
54
            is JsonArray<*> -> value.mapNotNull { it as String? }
55
56
57
58
            else -> emptyList()
        }
        val certainty = when (val value = jsonObject["certainty"]) {
            is String -> listOf(value)
Jonas Waeber's avatar
Jonas Waeber committed
59
            is JsonArray<*> -> value.mapNotNull { it as String? }
60
61
            else -> emptyList()
        }
Jonas Waeber's avatar
Jonas Waeber committed
62
63
64
65
66
67
        val facetList = when (jsonObject["@type"] as String) {
            NS.rico + "SingleDate" ->
                if (isNormalized)
                    DateFacetBuildHelpers.buildFromNormalizedSingleDate(date)
                else emptyList()
            NS.rico + "DateRange" ->
68
69
70
71
72
73
74
75
76
77
                if (isNormalized) {
                    try {
                        DateFacetBuildHelpers.buildFromNormalizedDateRange(date)
                    } catch (ex: NumberFormatException) {
                        log.error("Could not parse normalized date $date in resource $key.")
                        emptyList<String>()
                    }
                } else {
                    emptyList()
                }
Jonas Waeber's avatar
Jonas Waeber committed
78
79
80
            else -> emptyList()
        }
        dateContainers.add(
81
82
83
84
85
86
87
                DateContainer(
                        date = date,
                        sort = sort,
                        qualifier = qualifier,
                        certainty = certainty,
                        facet = facetList
                )
Jonas Waeber's avatar
Jonas Waeber committed
88
89
90
91
92
93
94
95
        )
        return "Transformed date to date container."
    }

    override fun build(): List<DateContainer> {
        return dateContainers
    }
}