From c1e20de7cf1857270629f9d4b4ebbb1503864764 Mon Sep 17 00:00:00 2001 From: "michele.artini" Date: Tue, 18 May 2021 14:00:14 +0200 Subject: [PATCH] fixed the deserialization of a json property --- .../ror/GenerateRorActionSetJob.java | 23 ++--------- .../ror/model/ExternalIdType.java | 20 +++++++--- .../ror/model/ExternalIdTypeDeserializer.java | 38 +++++++++++++++++++ .../actionmanager/ror/model/GeonamesCity.java | 2 +- 4 files changed, 57 insertions(+), 26 deletions(-) create mode 100644 dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdTypeDeserializer.java diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/GenerateRorActionSetJob.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/GenerateRorActionSetJob.java index 607fc014d..d6c17e415 100644 --- a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/GenerateRorActionSetJob.java +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/GenerateRorActionSetJob.java @@ -12,7 +12,6 @@ import static eu.dnetlib.dhp.schema.oaf.utils.OafMapperUtils.structuredProperty; import java.io.InputStream; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collection; import java.util.Date; import java.util.LinkedHashSet; import java.util.List; @@ -175,27 +174,13 @@ public class GenerateRorActionSetJob { for (final Map.Entry e : r.getExternalIds().entrySet()) { final String type = e.getKey(); - final Object all = e.getValue().getAll(); - if (all == null) { - // skip - } else { + final List all = e.getValue().getAll(); + if (all != null) { final Qualifier qualifier = qualifier( type, type, ModelConstants.DNET_PID_TYPES, ModelConstants.DNET_PID_TYPES); - if (all instanceof String) { + for (final String pid : all) { pids - .add(structuredProperty(all.toString(), qualifier, ROR_DATA_INFO)); - } else if (all instanceof Collection) { - for (final Object pid : (Collection) all) { - pids - .add(structuredProperty(pid.toString(), qualifier, ROR_DATA_INFO)); - } - } else if (all instanceof String[]) { - for (final String pid : (String[]) all) { - pids - .add(structuredProperty(pid, qualifier, ROR_DATA_INFO)); - } - } else { - log.warn("Invalid type for pid list: " + all.getClass()); + .add(structuredProperty(pid, qualifier, ROR_DATA_INFO)); } } } diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdType.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdType.java index 68c2d429b..406bfd82c 100644 --- a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdType.java +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdType.java @@ -2,24 +2,32 @@ package eu.dnetlib.dhp.actionmanager.ror.model; import java.io.Serializable; +import java.util.List; -import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +@JsonDeserialize(using = ExternalIdTypeDeserializer.class) public class ExternalIdType implements Serializable { - @JsonProperty("all") - private Object all; + private List all; - @JsonProperty("preferred") private String preferred; private final static long serialVersionUID = 2616688352998387611L; - public Object getAll() { + public ExternalIdType() { + } + + public ExternalIdType(final List all, final String preferred) { + this.all = all; + this.preferred = preferred; + } + + public List getAll() { return all; } - public void setAll(final Object all) { + public void setAll(final List all) { this.all = all; } diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdTypeDeserializer.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdTypeDeserializer.java new file mode 100644 index 000000000..3fd0c9250 --- /dev/null +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/ExternalIdTypeDeserializer.java @@ -0,0 +1,38 @@ + +package eu.dnetlib.dhp.actionmanager.ror.model; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.ObjectCodec; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonDeserializer; +import com.fasterxml.jackson.databind.JsonNode; + +public class ExternalIdTypeDeserializer extends JsonDeserializer { + + @Override + public ExternalIdType deserialize(final JsonParser p, final DeserializationContext ctxt) + throws IOException, JsonProcessingException { + final ObjectCodec oc = p.getCodec(); + final JsonNode node = oc.readTree(p); + + final JsonNode allNode = node.get("all"); + + final String preferred = node.get("preferred").asText(); + + final List all = new ArrayList<>(); + + if (allNode.isArray()) { + allNode.elements().forEachRemaining(x -> all.add(x.asText())); + } else { + all.add(allNode.asText()); + } + + return new ExternalIdType(all, preferred); + } + +} diff --git a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/GeonamesCity.java b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/GeonamesCity.java index 3fd539066..2b0487168 100644 --- a/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/GeonamesCity.java +++ b/dhp-workflows/dhp-aggregation/src/main/java/eu/dnetlib/dhp/actionmanager/ror/model/GeonamesCity.java @@ -28,7 +28,7 @@ public class GeonamesCity implements Serializable { @JsonProperty("nuts_level3") private NameAndCode nutsLevel3; - @JsonProperty("") + @JsonProperty("license") private License license; private final static long serialVersionUID = -8389480201526252955L;