From db4e400a0b77e29f3ccc9e48286024263465925b Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Wed, 9 Dec 2020 18:01:45 +0100 Subject: [PATCH] introduced Oaf.mergeFrom method to allow merging of dataInfo(s), with prevalence based on datainfo.trust --- .../java/eu/dnetlib/dhp/schema/oaf/Oaf.java | 38 +++++++++++++++++-- .../eu/dnetlib/dhp/schema/oaf/OafEntity.java | 6 +-- .../eu/dnetlib/dhp/schema/oaf/Relation.java | 14 +------ 3 files changed, 36 insertions(+), 22 deletions(-) diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Oaf.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Oaf.java index 3496492e8..fef32c0c6 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Oaf.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Oaf.java @@ -2,8 +2,12 @@ package eu.dnetlib.dhp.schema.oaf; import java.io.Serializable; +import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; public abstract class Oaf implements Serializable { @@ -40,9 +44,35 @@ public abstract class Oaf implements Serializable { this.lastupdatetimestamp = lastupdatetimestamp; } - public void mergeOAFDataInfo(Oaf e) { - if (e.getDataInfo() != null && compareTrust(this, e) < 0) - dataInfo = e.getDataInfo(); + public void mergeFrom(Oaf o) { + if (Objects.isNull(o)) { + return; + } + setCollectedfrom( + Stream + .concat( + Optional + .ofNullable(getCollectedfrom()) + .map(Collection::stream) + .orElse(Stream.empty()), + Optional + .ofNullable(o.getCollectedfrom()) + .map(Collection::stream) + .orElse(Stream.empty())) + .distinct() // relies on KeyValue.equals + .collect(Collectors.toList())); + + mergeOAFDataInfo(o); + + setLastupdatetimestamp( + Math.max( + Optional.ofNullable(getLastupdatetimestamp()).orElse(0L), + Optional.ofNullable(o.getLastupdatetimestamp()).orElse(0L))); + } + + public void mergeOAFDataInfo(Oaf o) { + if (o.getDataInfo() != null && compareTrust(this, o) < 0) + dataInfo = o.getDataInfo(); } protected String extractTrust(Oaf e) { @@ -62,7 +92,7 @@ public abstract class Oaf implements Serializable { if (o == null || getClass() != o.getClass()) return false; Oaf oaf = (Oaf) o; - return Objects.equals(dataInfo, oaf.dataInfo) + return Objects.equals(getDataInfo(), oaf.getDataInfo()) && Objects.equals(lastupdatetimestamp, oaf.lastupdatetimestamp); } diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OafEntity.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OafEntity.java index 2823ee49d..17c3e6bdd 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OafEntity.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OafEntity.java @@ -78,14 +78,10 @@ public abstract class OafEntity extends Oaf implements Serializable { } public void mergeFrom(OafEntity e) { - - if (e == null) - return; + super.mergeFrom(e); originalId = mergeLists(originalId, e.getOriginalId()); - collectedfrom = mergeLists(collectedfrom, e.getCollectedfrom()); - pid = mergeLists(pid, e.getPid()); if (e.getDateofcollection() != null && compareTrust(this, e) < 0) diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java index 17a50d7ac..0de34dbec 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Relation.java @@ -130,19 +130,7 @@ public class Relation extends Oaf { Objects.equals(getSubRelType(), r.getSubRelType()), "subRelType(s) must be equal"); checkArgument(Objects.equals(getRelClass(), r.getRelClass()), "relClass(es) must be equal"); - setCollectedfrom( - Stream - .concat( - Optional - .ofNullable(getCollectedfrom()) - .map(Collection::stream) - .orElse(Stream.empty()), - Optional - .ofNullable(r.getCollectedfrom()) - .map(Collection::stream) - .orElse(Stream.empty())) - .distinct() // relies on KeyValue.equals - .collect(Collectors.toList())); + super.mergeFrom(r); } @Override