forked from D-Net/dnet-hadoop
manage merging of Relatation validation attributes
This commit is contained in:
parent
c9bbb8fcbd
commit
e5400b7408
|
@ -3,6 +3,9 @@ package eu.dnetlib.dhp.schema.common;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
@ -473,4 +476,25 @@ public class ModelSupport {
|
||||||
private static <T extends Oaf> String idFnForOafEntity(T t) {
|
private static <T extends Oaf> String idFnForOafEntity(T t) {
|
||||||
return ((OafEntity) t).getId();
|
return ((OafEntity) t).getId();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static final String ISO8601FORMAT = "yyyy-MM-dd'T'HH:mm:ssZ";
|
||||||
|
|
||||||
|
public static String oldest(String dateA, String dateB) throws ParseException {
|
||||||
|
|
||||||
|
if (StringUtils.isBlank(dateA)) {
|
||||||
|
return dateB;
|
||||||
|
}
|
||||||
|
if (StringUtils.isBlank(dateB)) {
|
||||||
|
return dateA;
|
||||||
|
}
|
||||||
|
if (StringUtils.isNotBlank(dateA) && StringUtils.isNotBlank(dateB)) {
|
||||||
|
|
||||||
|
final Date a = new SimpleDateFormat(ISO8601FORMAT).parse(dateA);
|
||||||
|
final Date b = new SimpleDateFormat(ISO8601FORMAT).parse(dateB);
|
||||||
|
|
||||||
|
return a.before(b) ? dateA : dateB;
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
|
||||||
package eu.dnetlib.dhp.schema.oaf;
|
package eu.dnetlib.dhp.schema.oaf;
|
||||||
|
|
||||||
|
import eu.dnetlib.dhp.schema.common.ModelSupport;
|
||||||
|
|
||||||
import static com.google.common.base.Preconditions.checkArgument;
|
import static com.google.common.base.Preconditions.checkArgument;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
@ -106,7 +109,7 @@ public class Relation extends Oaf {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Boolean getValidated() {
|
public Boolean getValidated() {
|
||||||
return validated;
|
return Objects.nonNull(validated) && validated;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValidated(Boolean validated) {
|
public void setValidated(Boolean validated) {
|
||||||
|
@ -130,6 +133,13 @@ public class Relation extends Oaf {
|
||||||
Objects.equals(getSubRelType(), r.getSubRelType()), "subRelType(s) must be equal");
|
Objects.equals(getSubRelType(), r.getSubRelType()), "subRelType(s) must be equal");
|
||||||
checkArgument(Objects.equals(getRelClass(), r.getRelClass()), "relClass(es) must be equal");
|
checkArgument(Objects.equals(getRelClass(), r.getRelClass()), "relClass(es) must be equal");
|
||||||
|
|
||||||
|
setValidated(getValidated() || r.getValidated());
|
||||||
|
try {
|
||||||
|
setValidationDate(ModelSupport.oldest(getValidationDate(), r.getValidationDate()));
|
||||||
|
} catch (ParseException e) {
|
||||||
|
throw new IllegalArgumentException(String.format("invalid validation date format in relation [s:%s, t:%s]: %s", getSource(), getTarget(), getValidationDate()));
|
||||||
|
}
|
||||||
|
|
||||||
super.mergeFrom(r);
|
super.mergeFrom(r);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -63,6 +63,51 @@ public class MergeTest {
|
||||||
assertEquals(3, a.getSubject().size());
|
assertEquals(3, a.getSubject().size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mergeRelationTest() {
|
||||||
|
|
||||||
|
Relation a = createRel(null, null);
|
||||||
|
Relation b = createRel(null, null);
|
||||||
|
a.mergeFrom(b);
|
||||||
|
assertEquals(a, b);
|
||||||
|
|
||||||
|
a = createRel(true, null);
|
||||||
|
b = createRel(null, null);
|
||||||
|
a.mergeFrom(b);
|
||||||
|
assertEquals(true, a.getValidated());
|
||||||
|
|
||||||
|
a = createRel(true, null);
|
||||||
|
b = createRel(false, null);
|
||||||
|
a.mergeFrom(b);
|
||||||
|
assertEquals(true, a.getValidated());
|
||||||
|
|
||||||
|
a = createRel(true, null);
|
||||||
|
b = createRel(true, "2016-04-05T12:41:19.202Z");
|
||||||
|
a.mergeFrom(b);
|
||||||
|
assertEquals("2016-04-05T12:41:19.202Z", a.getValidationDate());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void mergeRelationTestParseException() {
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> {
|
||||||
|
Relation a = createRel(true, "2016-04-05");
|
||||||
|
Relation b = createRel(true, "2016-04-05");
|
||||||
|
a.mergeFrom(b);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private Relation createRel(Boolean validated, String validationDate) {
|
||||||
|
Relation rel = new Relation();
|
||||||
|
rel.setSource("1");
|
||||||
|
rel.setTarget("2");
|
||||||
|
rel.setRelType("reltype");
|
||||||
|
rel.setSubRelType("subreltype");
|
||||||
|
rel.setRelClass("relclass");
|
||||||
|
rel.setValidated(validated);
|
||||||
|
rel.setValidationDate(validationDate);
|
||||||
|
return rel;
|
||||||
|
}
|
||||||
|
|
||||||
private KeyValue setKV(final String key, final String value) {
|
private KeyValue setKV(final String key, final String value) {
|
||||||
|
|
||||||
KeyValue k = new KeyValue();
|
KeyValue k = new KeyValue();
|
||||||
|
|
Loading…
Reference in New Issue