Skip to content

Commit

Permalink
Fix date parsing for CV pdf generation
Browse files Browse the repository at this point in the history
  • Loading branch information
dragutin-nav committed Dec 27, 2023
1 parent a1880ea commit 7f48407
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions src/main/java/no/nav/veilarbvedtaksstotte/service/JsonViewer.kt
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package no.nav.veilarbvedtaksstotte.service

import com.google.gson.Gson
import com.google.gson.JsonArray
import com.google.gson.JsonObject
import com.google.gson.*
import lombok.extern.slf4j.Slf4j
import no.nav.veilarbvedtaksstotte.utils.SecureLog
import org.apache.commons.lang3.StringUtils
import org.json.JSONException
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.YearMonth
import java.time.format.DateTimeFormatter
import java.util.*


@Slf4j
class JsonViewer() {
Expand All @@ -36,17 +36,17 @@ class JsonViewer() {
val keySet = jsonObject.keySet()
for (key in keySet) {
try {
val value: Any = jsonObject.get(key)
if (value is String || value is Number || value is Boolean) {
if (value is String && isLink(key)) {
output += getLink(key, value)
val value: JsonElement = jsonObject.get(key)
if (value.isJsonPrimitive) {
if (isLink(key)) {
output += getLink(key, value.asString)
}
output += "<div class='json-key-wrapper'>";
output += "<span class='json-key'>";
output += prettifyKey(key) + ": ";
output += "</span>";
output += "<span>";
output += scalarToString(value);
output += scalarToString(value.asJsonPrimitive);
output += "</span>";
output += "</div>"
} else if (value is JsonArray) {
Expand All @@ -72,8 +72,8 @@ class JsonViewer() {
output += "</div>";
output += "</div>";
}
} catch (e: JSONException) {
SecureLog.secureLog.warn("Cant parse input json string " + jsonObject, e)
} catch (e: Exception) {
SecureLog.secureLog.warn("Can't parse input json string " + jsonObject, e)
}
}

Expand All @@ -85,12 +85,11 @@ class JsonViewer() {
return key.contains("lenke", true);
}

private fun scalarToString(value: Any): String {
if (value is Boolean) {
val boolValue: Boolean = value;
if (boolValue == true) return "Ja" else return "Nei";
private fun scalarToString(value: JsonPrimitive): String {
if (value.isBoolean) {
if (value.asBoolean) return "Ja" else return "Nei";
}
return fixDateFormat(value.toString());
return fixDateFormat(value.asString);
}

private fun getLink(key: String, value: String): String {
Expand Down Expand Up @@ -137,13 +136,17 @@ class JsonViewer() {

private fun fixDateFormat(value: String): String {
try {
val locale = Locale.forLanguageTag("nb");
if (value.matches(Regex("^\\d{4}-\\d{2}-\\d{2}\$"))) {
return LocalDate.parse(value).format(DateTimeFormatter.ofPattern("DD. MMMM YYYY"))
return LocalDate.parse(value)
.format(DateTimeFormatter.ofPattern("dd. MMM YYYY").withLocale(locale))
} else if (value.matches(Regex("^\\d{4}-\\d{2}\$"))) {
return LocalDate.parse(value).format(DateTimeFormatter.ofPattern("MMMM YYYY"))
} else if (value.matches(Regex("^\\d{4}-\\d{2}-\\d{2}T"))) {
val yearMonth = YearMonth.parse(value);
return LocalDate.of(yearMonth.year, yearMonth.month, 1)
.format(DateTimeFormatter.ofPattern("MMM YYYY").withLocale(locale))
} else if (value.matches(Regex("^\\d{4}-\\d{2}-\\d{2}[T]\\d{2}(.)*\$"))) {
return LocalDateTime.parse(value, DateTimeFormatter.ISO_DATE_TIME)
.format(DateTimeFormatter.ofPattern("DD. MMM YYYY kl. HH:mm"))
.format(DateTimeFormatter.ofPattern("dd. MMM YYYY 'kl.' HH:mm").withLocale(locale))
}
return value;
} catch (e: Exception) {
Expand Down

0 comments on commit 7f48407

Please sign in to comment.