Fix issue with date formats on RDA import

This commit is contained in:
George Kalampokis 2021-09-28 14:17:03 +03:00
parent fc7c39081a
commit defac6afcc
4 changed files with 48 additions and 7 deletions

View File

@ -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,7 +41,6 @@ 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();
return dmpRDAMapper.toEntity(rda, profiles);

View File

@ -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<Date> {
private static final List<String> 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");
}
}

View File

@ -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;
/**

View File

@ -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);