DateContainerBuilder.kt 2.77 KB
Newer Older
Jonas Waeber's avatar
Jonas Waeber committed
1
2
package org.memobase.builders

3
import ch.memobase.rdf.NS
4
import com.beust.klaxon.JsonArray
Jonas Waeber's avatar
Jonas Waeber committed
5
import com.beust.klaxon.JsonObject
6
import org.apache.logging.log4j.LogManager
Jonas Waeber's avatar
Jonas Waeber committed
7
import org.memobase.helpers.DateFacetBuildHelpers
8
import org.memobase.helpers.KEYS
Jonas Waeber's avatar
Jonas Waeber committed
9
10
11
import org.memobase.model.DateContainer

class DateContainerBuilder(private val containedIds: List<String>) : IFieldBuilder {
12
    private val log = LogManager.getLogger("DateContainerBuilder")
Jonas Waeber's avatar
Jonas Waeber committed
13
14
15
16
17
18
19
20
21
22

    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
    }

23
    override fun append(key: String, jsonObject: JsonObject): String {
Jonas Waeber's avatar
Jonas Waeber committed
24
25
26
27
28
29
30
31
32
33
34
        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
        }
35
36
        val qualifier = when (val value = jsonObject["dateQualifier"]) {
            is String -> listOf(value)
Jonas Waeber's avatar
Jonas Waeber committed
37
            is JsonArray<*> -> value.mapNotNull { it as String? }
38
39
40
41
            else -> emptyList()
        }
        val certainty = when (val value = jsonObject["certainty"]) {
            is String -> listOf(value)
Jonas Waeber's avatar
Jonas Waeber committed
42
            is JsonArray<*> -> value.mapNotNull { it as String? }
43
44
            else -> emptyList()
        }
Jonas Waeber's avatar
Jonas Waeber committed
45
46
47
48
49
50
        val facetList = when (jsonObject["@type"] as String) {
            NS.rico + "SingleDate" ->
                if (isNormalized)
                    DateFacetBuildHelpers.buildFromNormalizedSingleDate(date)
                else emptyList()
            NS.rico + "DateRange" ->
51
52
53
54
55
56
57
58
59
60
                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
61
62
63
            else -> emptyList()
        }
        dateContainers.add(
64
65
66
67
68
69
70
                DateContainer(
                        date = date,
                        sort = sort,
                        qualifier = qualifier,
                        certainty = certainty,
                        facet = facetList
                )
Jonas Waeber's avatar
Jonas Waeber committed
71
72
73
74
75
76
77
78
        )
        return "Transformed date to date container."
    }

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