From e6e214dab554a970e1e431a8c3db5644699bdcbd Mon Sep 17 00:00:00 2001 From: pjacewicz Date: Wed, 11 Mar 2020 16:58:17 +0100 Subject: [PATCH] Oaf merge and get strategy added --- .../dhp/actionmanager/OafMergeAndGet.java | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 dhp-workflows/dhp-actionmanager/src/main/java/eu/dnetlib/dhp/actionmanager/OafMergeAndGet.java diff --git a/dhp-workflows/dhp-actionmanager/src/main/java/eu/dnetlib/dhp/actionmanager/OafMergeAndGet.java b/dhp-workflows/dhp-actionmanager/src/main/java/eu/dnetlib/dhp/actionmanager/OafMergeAndGet.java new file mode 100644 index 000000000..dff82c0a5 --- /dev/null +++ b/dhp-workflows/dhp-actionmanager/src/main/java/eu/dnetlib/dhp/actionmanager/OafMergeAndGet.java @@ -0,0 +1,37 @@ +package eu.dnetlib.dhp.actionmanager; + +import eu.dnetlib.dhp.schema.oaf.Oaf; +import eu.dnetlib.dhp.schema.oaf.OafEntity; +import eu.dnetlib.dhp.schema.oaf.Relation; + +import java.util.function.BiFunction; + +public class OafMergeAndGet { + + public enum Strategy { + MERGE_FROM_AND_GET, SELECT_NEWER_AND_GET + } + + public static SerializableSupplier> functionFor(Strategy strategy) { + switch (strategy) { + case MERGE_FROM_AND_GET: + return () -> (x, y) -> { + if (x instanceof Relation) { + ((Relation) x).mergeFrom((Relation) y); + return x; + } + ((OafEntity) x).mergeFrom((OafEntity) y); + return x; + }; + case SELECT_NEWER_AND_GET: + return () -> (x, y) -> { + if (x.getLastupdatetimestamp() > y.getLastupdatetimestamp()) { + return x; + } + return y; + }; + } + throw new RuntimeException(); + } + +}