From fa66e5b6b8d79d139516fa0bb0758915186aca15 Mon Sep 17 00:00:00 2001 From: Claudio Atzori Date: Thu, 26 Nov 2020 13:09:19 +0100 Subject: [PATCH] ResultTypeComparator gives priority to Records collectedfrom Crossref --- .../dhp/schema/oaf/ResultTypeComparator.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/ResultTypeComparator.java b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/ResultTypeComparator.java index 6c11d1a85..eb54599ce 100644 --- a/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/ResultTypeComparator.java +++ b/dhp-common/src/main/java/eu/dnetlib/dhp/schema/oaf/ResultTypeComparator.java @@ -2,8 +2,14 @@ package eu.dnetlib.dhp.schema.oaf; import java.util.Comparator; +import java.util.HashSet; +import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.Stream; +import com.google.common.collect.Sets; import eu.dnetlib.dhp.schema.common.ModelConstants; +import static eu.dnetlib.dhp.schema.common.ModelConstants.CROSSREF_ID; public class ResultTypeComparator implements Comparator { @@ -17,6 +23,16 @@ public class ResultTypeComparator implements Comparator { if (right == null) return -1; + HashSet lCf = getCollectedFromIds(left); + HashSet rCf = getCollectedFromIds(right); + + if (lCf.contains(CROSSREF_ID) && !rCf.contains(CROSSREF_ID)) { + return -1; + } + if (!lCf.contains(CROSSREF_ID) && rCf.contains(CROSSREF_ID)) { + return 1; + } + String lClass = left.getResulttype().getClassid(); String rClass = right.getResulttype().getClassid(); @@ -46,4 +62,12 @@ public class ResultTypeComparator implements Comparator { // Else (but unlikely), lexicographical ordering will do. return lClass.compareTo(rClass); } + + protected HashSet getCollectedFromIds(Result left) { + return Optional.ofNullable(left.getCollectedfrom()) + .map(cf -> cf.stream() + .map(c -> c.getKey()) + .collect(Collectors.toCollection(HashSet::new))) + .orElse(new HashSet<>()); + } }