Fix issue with date formats on RDA import
This commit is contained in:
parent
fc7c39081a
commit
defac6afcc
|
@ -1,6 +1,5 @@
|
||||||
package eu.eudat.logic.managers;
|
package eu.eudat.logic.managers;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
|
||||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
import eu.eudat.data.entities.DMP;
|
import eu.eudat.data.entities.DMP;
|
||||||
|
@ -12,7 +11,6 @@ import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.Serializable;
|
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
|
@ -43,9 +41,8 @@ public class RDAManager {
|
||||||
|
|
||||||
public DMP convertToEntity(String json, String[] profiles) throws IOException {
|
public DMP convertToEntity(String json, String[] profiles) throws IOException {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
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);
|
return dmpRDAMapper.toEntity(rda, profiles);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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");
|
||||||
|
}
|
||||||
|
}
|
|
@ -9,6 +9,8 @@ import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.*;
|
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")
|
@JsonProperty("created")
|
||||||
|
@JsonDeserialize(using = MultiDateDeserializer.class)
|
||||||
@JsonPropertyDescription("")
|
@JsonPropertyDescription("")
|
||||||
private Date created;
|
private Date created;
|
||||||
/**
|
/**
|
||||||
|
@ -147,6 +150,7 @@ public class Dmp implements Serializable
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@JsonProperty("modified")
|
@JsonProperty("modified")
|
||||||
|
@JsonDeserialize(using = MultiDateDeserializer.class)
|
||||||
@JsonPropertyDescription("Must be set each time DMP is modified. Indicates DMP version.")
|
@JsonPropertyDescription("Must be set each time DMP is modified. Indicates DMP version.")
|
||||||
private Date modified;
|
private Date modified;
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,6 +9,7 @@ import java.io.IOException;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
|
@ -17,13 +18,11 @@ public class LanguageRDAMapper {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(LanguageRDAMapper.class);
|
private static final Logger logger = LoggerFactory.getLogger(LanguageRDAMapper.class);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
String json = null;
|
|
||||||
try {
|
try {
|
||||||
ObjectMapper mapper = new ObjectMapper();
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
InputStreamReader isr = new InputStreamReader(LanguageRDAMapper.class.getClassLoader().getResource("internal/rda-lang-map.json").openStream(), StandardCharsets.UTF_8);
|
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();
|
isr.close();
|
||||||
langMap.putAll(new org.json.JSONObject(json).toMap());
|
|
||||||
|
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
logger.error(e.getMessage(), e);
|
logger.error(e.getMessage(), e);
|
||||||
|
|
Loading…
Reference in New Issue