Unverified Commit d4e87eb1 authored by Sebastian Schüpbach's avatar Sebastian Schüpbach
Browse files

catch date parsing errors

parent 4f1c831e
Pipeline #20897 passed with stages
in 5 minutes and 51 seconds
......@@ -20,14 +20,23 @@ package ch.memobase
import de.digitalcollections.iiif.model.enums.ViewingHint
import de.digitalcollections.iiif.model.jackson.IiifObjectMapper
import de.digitalcollections.iiif.model.sharedcanvas.{Canvas => IIIFCanvas, Manifest => IIIFManifest, Sequence => IIIFSequence}
import de.digitalcollections.iiif.model.{MetadataEntry, OtherContent, PropertyValue, ImageContent => IIIFImageContent}
import de.digitalcollections.iiif.model.sharedcanvas.{
Canvas => IIIFCanvas,
Manifest => IIIFManifest,
Sequence => IIIFSequence
}
import de.digitalcollections.iiif.model.{
MetadataEntry,
OtherContent,
PropertyValue,
ImageContent => IIIFImageContent
}
import ujson.{Arr => JArr, Obj => JObj}
import java.time.format.DateTimeFormatter
import java.time.{LocalDate, LocalTime, OffsetDateTime, ZoneOffset}
import scala.collection.mutable
import scala.util.Try
import scala.util.{Failure, Success, Try}
class Manifest {
......@@ -210,47 +219,64 @@ class Manifest {
private lazy val untilYear = "^(\\?/\\d{4})$".r
private lazy val fromYear = "^(\\d{4}/\\?)$".r
private def parseTimestamp(date: => LocalDate): DateExtractionResult = {
Try(date) match {
case Success(v) => DateExtractionResult(Some(v))
case Failure(ex) =>
DateExtractionResult(None, warnings = Some(ex.getMessage))
}
}
//noinspection ScalaStyle
private def createTimestamp(value: String): DateExtractionResult =
value match {
case date(value) =>
DateExtractionResult(Some(LocalDate.parse(value, dateTimeFormatter)))
parseTimestamp(LocalDate.parse(value, dateTimeFormatter))
case onlyYear(value) =>
DateExtractionResult(
Some(LocalDate.parse(s"$value-01-01", dateTimeFormatter))
)
parseTimestamp(LocalDate.parse(s"$value-01-01", dateTimeFormatter))
case yearRange(value) =>
val dateRangeStart = value.split("/")(0)
val dateRangeEnd = value.split("/")(0)
DateExtractionResult(
Some(
LocalDate.parse(s"${value.split("/")(0)}-01-01", dateTimeFormatter)
),
List(
("Date range start", dateRangeStart),
("Date range end", dateRangeEnd)
)
)
parseTimestamp(
LocalDate.parse(s"${value.split("/")(0)}-01-01", dateTimeFormatter)
) match {
case der @ DateExtractionResult(Some(_), _, None) =>
der.copy(metadata =
List(
("Date range start", dateRangeStart),
("Date range end", dateRangeEnd)
)
)
case der @ _ => der
}
case fromYear(value) =>
val dateRangeStart = value.split("/")(0)
DateExtractionResult(
Some(
LocalDate.parse(s"$dateRangeStart-01-01", dateTimeFormatter)
),
List(
("Date range end", dateRangeStart),
("Date range end", "(unknown)")
)
)
parseTimestamp(
LocalDate.parse(s"$dateRangeStart-01-01", dateTimeFormatter)
) match {
case der @ DateExtractionResult(Some(_), _, None) =>
der.copy(metadata =
List(
("Date range end", dateRangeStart),
("Date range end", "(unknown)")
)
)
case der @ _ => der
}
case untilYear(value) =>
DateExtractionResult(
Some(
LocalDate.parse(s"${value.split("/")(1)}-01-01", dateTimeFormatter)
),
List(
("Date range end", "(unknown)"),
("Date range start", "(unknown)")
)
)
parseTimestamp(
LocalDate.parse(s"${value.split("/")(1)}-01-01", dateTimeFormatter)
) match {
case der @ DateExtractionResult(Some(_), _, None) =>
der.copy(metadata =
List(
("Date range end", "(unknown)"),
("Date range end", "(unknown)")
)
)
case der @ _ => der
}
case value =>
DateExtractionResult(
None,
......
Supports Markdown
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