From 51ff3b4e819cd726134eb5900d52c67924f095c8 Mon Sep 17 00:00:00 2001 From: pjacewicz Date: Wed, 1 Apr 2020 18:28:23 +0200 Subject: [PATCH] [dhp-schemas] added safeguard against casting exception in mergeFrom methods and null-safe handling of collectedfrom collection for relation --- .../java/eu/dnetlib/dhp/schema/oaf/Dataset.java | 5 +++++ .../java/eu/dnetlib/dhp/schema/oaf/Datasource.java | 4 ++++ .../eu/dnetlib/dhp/schema/oaf/Organization.java | 5 +++++ .../dhp/schema/oaf/OtherResearchProduct.java | 4 ++++ .../java/eu/dnetlib/dhp/schema/oaf/Project.java | 5 +++++ .../java/eu/dnetlib/dhp/schema/oaf/Publication.java | 4 ++++ .../java/eu/dnetlib/dhp/schema/oaf/Relation.java | 13 +++++++------ .../main/java/eu/dnetlib/dhp/schema/oaf/Result.java | 4 ++++ .../java/eu/dnetlib/dhp/schema/oaf/Software.java | 5 +++++ 9 files changed, 43 insertions(+), 6 deletions(-) diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Dataset.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Dataset.java index f33b11d21a..70a6d1f316 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Dataset.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Dataset.java @@ -79,6 +79,11 @@ public class Dataset extends Result implements Serializable { @Override public void mergeFrom(OafEntity e) { super.mergeFrom(e); + + if (!Dataset.class.isAssignableFrom(e.getClass())){ + return; + } + final Dataset d = (Dataset) e; storagedate = d.getStoragedate() != null && compareTrust(this, e)<0? d.getStoragedate() : storagedate; diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Datasource.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Datasource.java index 08936cc297..c8d9736c5a 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Datasource.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Datasource.java @@ -367,6 +367,10 @@ public class Datasource extends OafEntity implements Serializable { public void mergeFrom(OafEntity e) { super.mergeFrom(e); + if (!Datasource.class.isAssignableFrom(e.getClass())){ + return; + } + Datasource d = (Datasource)e; datasourcetype = d.getDatasourcetype() != null && compareTrust(this, e)<0? d.getDatasourcetype() : datasourcetype; diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Organization.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Organization.java index ab3730f527..a73a7f845b 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Organization.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Organization.java @@ -170,6 +170,11 @@ public class Organization extends OafEntity implements Serializable { @Override public void mergeFrom(OafEntity e) { super.mergeFrom(e); + + if (!Organization.class.isAssignableFrom(e.getClass())){ + return; + } + final Organization o = (Organization) e; legalshortname = o.getLegalshortname() != null && compareTrust(this, e)<0? o.getLegalshortname() : legalshortname; legalname = o.getLegalname() != null && compareTrust(this, e)<0 ? o.getLegalname() : legalname; diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OtherResearchProduct.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OtherResearchProduct.java index 9d93d73d31..cc36817bbf 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OtherResearchProduct.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/OtherResearchProduct.java @@ -40,6 +40,10 @@ public class OtherResearchProduct extends Result implements Serializable { public void mergeFrom(OafEntity e) { super.mergeFrom(e); + if (!OtherResearchProduct.class.isAssignableFrom(e.getClass())){ + return; + } + OtherResearchProduct o = (OtherResearchProduct)e; contactperson = mergeLists(contactperson, o.getContactperson()); diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Project.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Project.java index 8d9b8fd2d2..023be8fc05 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Project.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Project.java @@ -270,6 +270,11 @@ public class Project extends OafEntity implements Serializable { @Override public void mergeFrom(OafEntity e) { super.mergeFrom(e); + + if (!Project.class.isAssignableFrom(e.getClass())){ + return; + } + Project p = (Project)e; websiteurl= p.getWebsiteurl()!= null && compareTrust(this,e)<0?p.getWebsiteurl():websiteurl; diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Publication.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Publication.java index 0e3291e169..20a0b2121a 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Publication.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Publication.java @@ -20,6 +20,10 @@ public class Publication extends Result implements Serializable { public void mergeFrom(OafEntity e) { super.mergeFrom(e); + if (!Publication.class.isAssignableFrom(e.getClass())){ + return; + } + Publication p = (Publication) e; if (p.getJournal() != 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 6738b86938..768fea19f8 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 @@ -1,8 +1,6 @@ package eu.dnetlib.dhp.schema.oaf; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -78,9 +76,12 @@ public class Relation extends Oaf { checkArgument(Objects.equals(getSubRelType(), r.getSubRelType()),"subRelType(s) must be equal"); checkArgument(Objects.equals(getRelClass(), r.getRelClass()),"relClass(es) must be equal"); - setCollectedFrom(Stream.concat(getCollectedFrom().stream(), r.getCollectedFrom().stream()) - .distinct() // relies on KeyValue.equals - .collect(Collectors.toList())); + 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())); } @Override diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Result.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Result.java index d04272d522..f98bcec936 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Result.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Result.java @@ -223,6 +223,10 @@ public class Result extends OafEntity implements Serializable { public void mergeFrom(OafEntity e) { super.mergeFrom(e); + if (!Result.class.isAssignableFrom(e.getClass())){ + return; + } + Result r = (Result) e; instance = mergeLists(instance, r.getInstance()); diff --git a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Software.java b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Software.java index 655ea16656..6b51a6adae 100644 --- a/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Software.java +++ b/dhp-schemas/src/main/java/eu/dnetlib/dhp/schema/oaf/Software.java @@ -49,6 +49,11 @@ public class Software extends Result implements Serializable { @Override public void mergeFrom(OafEntity e) { super.mergeFrom(e); + + if (!Software.class.isAssignableFrom(e.getClass())){ + return; + } + final Software s = (Software) e; documentationUrl = mergeLists(documentationUrl, s.getDocumentationUrl());