Commit 0270b0fb authored by Jonas Waeber's avatar Jonas Waeber
Browse files

Refactor & doc date facet builder.

parent a5458009
......@@ -18,7 +18,10 @@
package org.memobase.helpers
import org.apache.logging.log4j.LogManager
object DateFacetBuildHelpers {
private val log = LogManager.getLogger(DateFacetBuildHelpers.javaClass.canonicalName)
private const val separator = "~"
......@@ -27,26 +30,46 @@ object DateFacetBuildHelpers {
private const val centuryName = "Jahrhundert"
/**
* Builds the hierarchical facet for a normalized SingleDate date.
*
* @param date Normalized date value with structure YYYY-MM-DD.
*
* @return The facet values to contruct the hierarchy with century and decade.
*/
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",
"$level_2$separator$century$separator$decade$separator"
)
}
private val yearRegex = Regex("(\\d{4}).*")
/**
* Builds the hierarchical facet values for normalized date ranges.
*
* @param date Normalized date range (ISO-8601)
*
* @return The facet values used by outermedia.
*/
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 {
""
val matchResult = yearRegex.matchEntire(secondPart)
matchResult.let { m ->
if (m != null) {
m.groups[1]?.value ?: ""
} else {
log.warn("Could not match year in normalized date range until value: $date.")
""
}
}
} else {
log.debug("Normalized date range does not have an until value: $date.")
""
}
......@@ -64,68 +87,62 @@ object DateFacetBuildHelpers {
val untilCentury = getCentury(until)
val untilDecade = getDecade(until)
if (fromCentury == untilCentury && fromDecade == untilDecade) {
listOf(
"$level_1$separator$fromCentury$separator",
"$level_2$separator$fromCentury$separator$fromDecade$separator"
)
} else if (fromCentury == untilCentury && fromDecade != untilDecade) {
val results = mutableListOf(
"$level_1$separator$fromCentury$separator",
"$level_2$separator$fromCentury$separator$fromDecade$separator"
)
var fromDecadeAsInt = fromDecade.substring(0, 4).toInt()
val untilDecadeAsInt = untilDecade.substring(0, 4).toInt()
while (fromDecadeAsInt < untilDecadeAsInt) {
fromDecadeAsInt += 10
results.add(
"$level_2$separator$fromCentury$separator${
getDecade(
fromDecadeAsInt
)
}$separator"
if (fromCentury == untilCentury) {
// from & until are in the same century.
if (fromDecade == untilDecade) {
// from & until are in the same decade.
listOf(
"$level_1$separator$fromCentury$separator",
"$level_2$separator$fromCentury$separator$fromDecade$separator"
)
} else {
// from & until are not in the same decade.
// as these are normalized values it should never happen that the until is before from.
val results = mutableListOf(
"$level_1$separator$fromCentury$separator",
"$level_2$separator$fromCentury$separator$fromDecade$separator"
)
var fromDecadeAsInt = fromDecade.substring(0, 4).toInt()
val untilDecadeAsInt = untilDecade.substring(0, 4).toInt()
while (fromDecadeAsInt < untilDecadeAsInt) {
fromDecadeAsInt += 10
results.add(
"$level_2$separator$fromCentury$separator${
getDecade(
fromDecadeAsInt
)
}$separator"
)
}
results
}
results
} else {
// from & until are not in the same century.
val results = mutableListOf(
"$level_1$separator$fromCentury$separator"
"$level_1$separator$fromCentury$separator"
)
// this number is one higher than it should be
// this number is one higher than it should be and is corrected in getCentury.
var fromCenturyAsInt = fromCentury.substring(0, 2).toInt()
val untilCenturyAsInt = untilCentury.substring(0, 2).toInt()
// first adds all the centuries required
while (fromCenturyAsInt < untilCenturyAsInt) {
results.add(
"$level_1$separator${
getCentury(
fromCenturyAsInt
)
}$separator"
)
results.add("$level_1$separator${getCentury(fromCenturyAsInt)}$separator")
fromCenturyAsInt += 1
}
results.add(
"$level_2$separator$fromCentury$separator$fromDecade$separator"
)
// then add the original decade.
results.add("$level_2$separator$fromCentury$separator$fromDecade$separator")
var fromDecadeAsInt = fromDecade.substring(0, 4).toInt()
val untilDecadeAsInt = untilDecade.substring(0, 4).toInt()
// then add all the decades until the last decade is reached.
while (fromDecadeAsInt < untilDecadeAsInt) {
fromDecadeAsInt += 10
results.add(
"$level_2$separator${
getCentury(
fromDecadeAsInt / 100
)
}$separator${
getDecade(
fromDecadeAsInt
)
}$separator"
)
results.add("$level_2$separator${getCentury(fromDecadeAsInt / 100)}" +
"$separator${getDecade(fromDecadeAsInt)}$separator")
}
// unsorted output.
results
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment