enable the possibility to extend the date formats used to parse Relation.validationDate
This commit is contained in:
parent
bf7acf268c
commit
e447ac68fa
|
@ -9,15 +9,19 @@ import java.security.NoSuchAlgorithmException;
|
|||
import java.text.ParseException;
|
||||
import java.time.Instant;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.time.format.DateTimeParseException;
|
||||
import java.time.temporal.ChronoField;
|
||||
import java.time.temporal.TemporalAccessor;
|
||||
import java.time.temporal.TemporalField;
|
||||
import java.util.*;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.apache.commons.codec.binary.Hex;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.lang3.time.DateUtils;
|
||||
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
|
||||
import eu.dnetlib.dhp.schema.oaf.*;
|
||||
|
@ -308,6 +312,11 @@ public class ModelSupport {
|
|||
|
||||
private static final String schemeTemplate = "dnet:%s_%s_relations";
|
||||
|
||||
public static final String[] DATE_TIME_FORMATS = {
|
||||
"yyyy-MM-dd'T'hh:mm:ss.SSS'Z'",
|
||||
"yyyy-MM-dd hh:mm:ss"
|
||||
};
|
||||
|
||||
private ModelSupport() {
|
||||
}
|
||||
|
||||
|
@ -501,12 +510,27 @@ public class ModelSupport {
|
|||
}
|
||||
if (StringUtils.isNotBlank(dateA) && StringUtils.isNotBlank(dateB)) {
|
||||
|
||||
final Date a = Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(dateA)));
|
||||
final Date b = Date.from(Instant.from(DateTimeFormatter.ISO_INSTANT.parse(dateB)));
|
||||
final Date a = tryParse(dateA);
|
||||
final Date b = tryParse(dateB);
|
||||
|
||||
return a.before(b) ? dateA : dateB;
|
||||
if (Objects.nonNull(a) && Objects.nonNull(b)) {
|
||||
return a.before(b) ? dateA : dateB;
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private static Date tryParse(String date) throws DateTimeParseException {
|
||||
try {
|
||||
return DateUtils.parseDate(date, DATE_TIME_FORMATS);
|
||||
} catch (ParseException e) {
|
||||
// ignore it, try another format
|
||||
}
|
||||
final String formats = String.join("\n", Arrays.asList(DATE_TIME_FORMATS));
|
||||
throw new DateTimeParseException(String.format("cannot parse %s, supported formats: \n%s", date, formats), date, 0);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -91,12 +91,18 @@ public class MergeTest {
|
|||
b = createRel(true, "2016-04-05T12:41:19.202Z");
|
||||
a.mergeFrom(b);
|
||||
assertEquals("2016-04-05T12:41:19.202Z", a.getValidationDate());
|
||||
|
||||
a = createRel(true, "2020-09-10 11:08:52");
|
||||
b = createRel(true, "2021-09-10 11:08:52");
|
||||
a.mergeFrom(b);
|
||||
assertEquals("2020-09-10 11:08:52", a.getValidationDate());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void mergeRelationTestParseException() {
|
||||
assertThrows(DateTimeParseException.class, () -> {
|
||||
Relation a = createRel(true, "2016-04-05");
|
||||
Relation a = createRel(true, "2016 April 05");
|
||||
Relation b = createRel(true, "2016-04-05");
|
||||
a.mergeFrom(b);
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue