DateFacetBuildHelpers.kt 5.15 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
/*
 * 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
19
package org.memobase.helpers
Jonas Waeber's avatar
Jonas Waeber committed
20

Jonas Waeber's avatar
Jonas Waeber committed
21
object DateFacetBuildHelpers {
Jonas Waeber's avatar
Jonas Waeber committed
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

    private const val separator = "~"
    private const val terminator = "#"

    private const val level_1 = "0"
    private const val level_2 = "1"

    private const val centuryName = "Jahrhundert"

    fun buildFromNormalizedSingleDate(date: String): List<String> {
        val century = getCentury(date.substring(0, 4))
        val decade = getDecade(date.substring(0, 4))

        return listOf(
            "$level_1$separator$century$separator",
Jonas Waeber's avatar
Jonas Waeber committed
37
            "$level_2$separator$century$separator$decade$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
        )
    }

    fun buildFromNormalizedDateRange(date: String): List<String> {
        val from = date.substring(0, 4)
        val until = if (date.contains("/")) {
            val secondPart = date.split("/")[1]
            if (Regex("\\d{4}.*").matches(secondPart)) {
                secondPart.substring(0, 4)
            } else {
                ""
            }
        } else {
            ""
        }

        return if (until.isEmpty()) {
            val century = getCentury(from)
            val decade = getDecade(from)
            listOf(
                "$level_1$separator$century$separator",
Jonas Waeber's avatar
Jonas Waeber committed
59
                "$level_2$separator$century$separator$decade$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
60
61
62
63
64
65
66
67
68
69
70
            )
        } else {
            val fromCentury = getCentury(from)
            val fromDecade = getDecade(from)

            val untilCentury = getCentury(until)
            val untilDecade = getDecade(until)

            if (fromCentury == untilCentury && fromDecade == untilDecade) {
                listOf(
                    "$level_1$separator$fromCentury$separator",
Jonas Waeber's avatar
Jonas Waeber committed
71
                    "$level_2$separator$fromCentury$separator$fromDecade$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
72
73
74
75
                )
            } else if (fromCentury == untilCentury && fromDecade != untilDecade) {
                val results = mutableListOf(
                    "$level_1$separator$fromCentury$separator",
Jonas Waeber's avatar
Jonas Waeber committed
76
                    "$level_2$separator$fromCentury$separator$fromDecade$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
77
78
79
80
81
82
83
                )
                var fromDecadeAsInt = fromDecade.substring(0, 4).toInt()
                val untilDecadeAsInt = untilDecade.substring(0, 4).toInt()

                while (fromDecadeAsInt != untilDecadeAsInt) {
                    fromDecadeAsInt += 10
                    results.add(
Jonas Waeber's avatar
Jonas Waeber committed
84
85
                        "$level_2$separator$fromCentury$separator${getDecade(
                            fromDecadeAsInt
Jonas Waeber's avatar
Jonas Waeber committed
86
                        )}$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
87
88
89
90
91
92
93
94
95
96
97
98
99
                    )
                }
                results
            } else {
                val results = mutableListOf(
                    "$level_1$separator$fromCentury$separator"
                )
                // this number is one higher than it should be
                var fromCenturyAsInt = fromCentury.substring(0, 2).toInt()
                val untilCenturyAsInt = untilCentury.substring(0, 2).toInt()

                while (fromCenturyAsInt != untilCenturyAsInt) {
                    results.add(
Jonas Waeber's avatar
Jonas Waeber committed
100
101
102
                        "$level_1$separator${getCentury(
                            fromCenturyAsInt
                        )}$separator")
Jonas Waeber's avatar
Jonas Waeber committed
103
104
105
                    fromCenturyAsInt += 1
                }
                results.add(
Jonas Waeber's avatar
Jonas Waeber committed
106
                    "$level_2$separator$fromCentury$separator$fromDecade$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
107
108
109
110
111
112
113
                )
                var fromDecadeAsInt = fromDecade.substring(0, 4).toInt()
                val untilDecadeAsInt = untilDecade.substring(0, 4).toInt()

                while (fromDecadeAsInt != untilDecadeAsInt) {
                    fromDecadeAsInt += 10
                    results.add(
Jonas Waeber's avatar
Jonas Waeber committed
114
115
116
117
                        "$level_2$separator${getCentury(
                            fromDecadeAsInt / 100
                        )}$separator${getDecade(
                            fromDecadeAsInt
Jonas Waeber's avatar
Jonas Waeber committed
118
                        )}$separator$terminator"
Jonas Waeber's avatar
Jonas Waeber committed
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
                    )
                }
                results
            }
        }
    }

    private fun getCentury(year: String): String {
        return getCentury(year.substring(0, 2).toInt())
    }

    private fun getCentury(century: Int): String {
        return "${century + 1}.$centuryName"
    }

    private fun getDecade(year: String): String {
        return getDecade(year.toInt())
    }

    private fun getDecade(year: Int): String {
        val reminder = year % 10
        val startYear = year - reminder
        return "${startYear + 1}-${startYear + 10}"
    }
}