From defac6afcc0b0727ad254d004c13e6d12917c348 Mon Sep 17 00:00:00 2001 From: George Kalampokis Date: Tue, 28 Sep 2021 14:17:03 +0300 Subject: [PATCH] Fix issue with date formats on RDA import --- .../eu/eudat/logic/managers/RDAManager.java | 5 +-- .../utilities/json/MultiDateDeserializer.java | 41 +++++++++++++++++++ .../main/java/eu/eudat/models/rda/Dmp.java | 4 ++ .../models/rda/mapper/LanguageRDAMapper.java | 5 +-- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/MultiDateDeserializer.java diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/RDAManager.java b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/RDAManager.java index 242a365c9..f277d98ac 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/managers/RDAManager.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/managers/RDAManager.java @@ -1,6 +1,5 @@ package eu.eudat.logic.managers; -import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.data.entities.DMP; @@ -12,7 +11,6 @@ import org.springframework.stereotype.Component; import javax.transaction.Transactional; import java.io.IOException; -import java.io.Serializable; import java.text.SimpleDateFormat; @Component @@ -43,9 +41,8 @@ public class RDAManager { public DMP convertToEntity(String json, String[] profiles) throws IOException { ObjectMapper mapper = new ObjectMapper(); - mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'")); - Dmp rda = mapper.readValue(json, RDAModel.class).getDmp(); + Dmp rda = mapper.readValue(json, RDAModel.class).getDmp(); return dmpRDAMapper.toEntity(rda, profiles); } } diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/MultiDateDeserializer.java b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/MultiDateDeserializer.java new file mode 100644 index 000000000..636b8faa2 --- /dev/null +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/utilities/json/MultiDateDeserializer.java @@ -0,0 +1,41 @@ +package eu.eudat.logic.utilities.json; + +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; + +import java.io.IOException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +public class MultiDateDeserializer extends StdDeserializer { + + private static final List DATE_FORMATS = Arrays.asList("yyyy-MM-dd'T'HH:mm:ss'Z'", "yyyy-MM-dd'T'HH:mm:ss.S"); + + + public MultiDateDeserializer() { + super(Date.class); + } + + protected MultiDateDeserializer(Class vc) { + super(vc); + } + + @Override + public Date deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { + String text = p.getText(); + + for (String dateFormat: DATE_FORMATS) { + try { + return new SimpleDateFormat(dateFormat).parse(text); + } catch (ParseException ignored) { + } + } + throw new JsonParseException(p, "No supported Date format"); + } +} diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/Dmp.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/Dmp.java index 773ac5f72..a6dfb29d0 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/Dmp.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/Dmp.java @@ -9,6 +9,8 @@ import java.util.List; import java.util.Map; import com.fasterxml.jackson.annotation.*; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import eu.eudat.logic.utilities.json.MultiDateDeserializer; /** @@ -71,6 +73,7 @@ public class Dmp implements Serializable * */ @JsonProperty("created") + @JsonDeserialize(using = MultiDateDeserializer.class) @JsonPropertyDescription("") private Date created; /** @@ -147,6 +150,7 @@ public class Dmp implements Serializable * */ @JsonProperty("modified") + @JsonDeserialize(using = MultiDateDeserializer.class) @JsonPropertyDescription("Must be set each time DMP is modified. Indicates DMP version.") private Date modified; /** diff --git a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LanguageRDAMapper.java b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LanguageRDAMapper.java index 527ffe932..dd2343a2d 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LanguageRDAMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/models/rda/mapper/LanguageRDAMapper.java @@ -9,6 +9,7 @@ import java.io.IOException; import java.io.InputStreamReader; import java.nio.charset.StandardCharsets; import java.util.HashMap; +import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; @@ -17,13 +18,11 @@ public class LanguageRDAMapper { private static final Logger logger = LoggerFactory.getLogger(LanguageRDAMapper.class); static { - String json = null; try { ObjectMapper mapper = new ObjectMapper(); InputStreamReader isr = new InputStreamReader(LanguageRDAMapper.class.getClassLoader().getResource("internal/rda-lang-map.json").openStream(), StandardCharsets.UTF_8); - json = mapper.readValue(isr, String.class); + langMap.putAll(mapper.readValue(isr, LinkedHashMap.class)); isr.close(); - langMap.putAll(new org.json.JSONObject(json).toMap()); } catch (IOException e) { logger.error(e.getMessage(), e);