DateContainerBuilder.kt 1.97 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
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
package org.memobase.builders

import com.beust.klaxon.JsonObject
import org.memobase.KEYS
import org.memobase.helpers.DateFacetBuildHelpers
import org.memobase.model.DateContainer
import org.memobase.rdf.NS

class DateContainerBuilder(private val containedIds: List<String>) : IFieldBuilder {

    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
    }

    override fun append(jsonObject: JsonObject): String {
        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
        }
        val qualifier = jsonObject["dateQualifier"] as String?
        val certainty = jsonObject["certainty"] as String?
        val facetList = when (jsonObject["@type"] as String) {
            NS.rico + "SingleDate" ->
                if (isNormalized)
                    DateFacetBuildHelpers.buildFromNormalizedSingleDate(date)
                else emptyList()
            NS.rico + "DateRange" ->
                if (isNormalized)
                    DateFacetBuildHelpers.buildFromNormalizedDateRange(date)
                else emptyList()
            else -> emptyList()
        }
        dateContainers.add(
            DateContainer(
                date = date,
                sort = sort,
                qualifier = qualifier,
                certainty = certainty,
                facet = facetList
            )
        )
        return "Transformed date to date container."
    }

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