From 08cbe4dee0df6477bd15e21c5959b8b898295ea9 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Thu, 27 Oct 2016 09:40:19 +0000 Subject: [PATCH] Added the following fields : short_title, uuid, traceability_flag, water_area (as list), stock_uri git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@133639 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../custom_annotations/CustomField.java | 2 +- .../grsf_publish_ws/json/input/Common.java | 27 +++++++++++++++---- .../json/input/FisheryRecord.java | 23 +++------------- .../json/input/StockRecord.java | 27 +++---------------- .../services/GrsfPublisherFisheryService.java | 7 ++--- .../services/GrsfPublisherStockService.java | 7 ++--- .../grsf_publish_ws/utils/HelperMethods.java | 21 ++++++++++++--- .../grsf_publish_ws/utils/groups/Status.java | 4 --- .../grsf_publish_ws/JTests.java | 22 +++++++++++++-- 9 files changed, 76 insertions(+), 64 deletions(-) diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/CustomField.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/CustomField.java index d1a76ea..5e15a64 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/CustomField.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/custom_annotations/CustomField.java @@ -13,6 +13,6 @@ import java.lang.annotation.Target; @Target(ElementType.FIELD) public @interface CustomField { - public String key() default ""; // this will be the key of this custom field field on ckan + public String key() default ""; // this will be the key of this custom field on ckan } diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java index 511372b..97504f4 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/Common.java @@ -11,6 +11,7 @@ import javax.validation.constraints.Size; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; +import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; @@ -20,7 +21,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; * Information that both Stock and Fishery must contain * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -@JsonIgnoreProperties(value = {"author", "author_contact"}, ignoreUnknown = true) // ignore in serialization/deserialization +@JsonIgnoreProperties(value = {"author", "author_contact", "extras"}, ignoreUnknown = true) // ignore in serialization/deserialization public class Common { @JsonProperty("description") @@ -80,6 +81,7 @@ public class Common { @JsonProperty("uuid_knowledge_base") @CustomField(key="UUID Knowledge Base") @NotNull + @Size(min=1, message="uuid_knowledge_base cannot be empty") private String uuid; @JsonProperty("traceability_flag") @@ -88,7 +90,14 @@ public class Common { private boolean traceabilityFlag; @JsonProperty("extras") - private Map extras = new HashMap<>(); + private Map> extras = new HashMap<>(); + + @JsonProperty("status") + @CustomField(key="Status") + @Group + @Tag + @NotNull(message="status cannot be null") + private Status status; public Common() { super(); @@ -118,7 +127,7 @@ public class Common { List databaseSources, List sourceOfInformation, String dataOwner, Type type, String shortTitle, String uuid, boolean traceabilityFlag, - Map extras) { + Map> extras) { super(); this.description = description; this.license = license; @@ -234,11 +243,11 @@ public class Common { this.type = type; } - public void setExtras(Map extras) { + public void setExtras(Map> extras) { this.extras = extras; } - public Map getExtras() { + public Map> getExtras() { return extras; } @@ -265,6 +274,14 @@ public class Common { public void setTraceabilityFlag(boolean traceabilityFlag) { this.traceabilityFlag = traceabilityFlag; } + + public Status getStatus() { + return status; + } + + public void setStatus(Status status) { + this.status = status; + } @Override public String toString() { diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/FisheryRecord.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/FisheryRecord.java index 9413a7d..1db5065 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/FisheryRecord.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/FisheryRecord.java @@ -7,7 +7,6 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Production_System_Type; -import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -67,13 +66,6 @@ public class FisheryRecord extends Common{ @CustomField(key="Fishing gear") private String fishingGear; - @JsonProperty("status") - @CustomField(key="Status") - @Group - @Tag - @NotNull(message="status cannot be null") - private Status status; - @JsonProperty("environment") @CustomField(key="Environment") private String environment; @@ -100,7 +92,7 @@ public class FisheryRecord extends Common{ String scientificName, String fishingArea, String exploitingStocks, String managementEntity, String jurisdictionArea, Production_System_Type productionSystemType, String flagState, - String fishingGear, Status status, String environment) { + String fishingGear, String environment) { super(); this.fisheryName = fisheryName; this.fisheryId = fisheryId; @@ -112,7 +104,6 @@ public class FisheryRecord extends Common{ this.productionSystemType = productionSystemType; this.flagState = flagState; this.fishingGear = fishingGear; - this.status = status; this.environment = environment; } @@ -196,14 +187,6 @@ public class FisheryRecord extends Common{ this.fishingGear = fishingGear; } - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - public String getEnvironment() { return environment; } @@ -214,14 +197,14 @@ public class FisheryRecord extends Common{ @Override public String toString() { - return "FisheryRecord [" + super.toString() + ", fisheryName=" + fisheryName + ", fisheryId=" + return "FisheryRecord [" + super.toString() + ",fisheryName=" + fisheryName + ", fisheryId=" + fisheryId + ", scientificName=" + scientificName + ", fishingArea=" + fishingArea + ", exploitingStocks=" + exploitingStocks + ", managementEntity=" + managementEntity + ", jurisdictionArea=" + jurisdictionArea + ", productionSystemType=" + productionSystemType + ", flagState=" + flagState + ", fishingGear=" + fishingGear - + ", status=" + status + ", environment=" + environment + "]"; + + ", environment=" + environment + "]"; } } diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/StockRecord.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/StockRecord.java index 11f47c4..127f8fa 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/StockRecord.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/StockRecord.java @@ -10,7 +10,6 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Exploitation_Rate; -import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import com.fasterxml.jackson.annotation.JsonProperty; @@ -89,20 +88,11 @@ public class StockRecord extends Common{ @CustomField(key="Reporting year") private Long reportingYear; - @JsonProperty("status") - @Tag - @Group - @NotNull(message="status cannot be null") - @CustomField(key="Status") - private Status status; - @JsonProperty("stock_uri") - @NotNull(message="stock_uri cannot be null") @CustomField(key="Stock Uri") private String stockUri; @JsonProperty("water_area") - @NotNull(message="water_area cannot be null") @CustomField(key="Water Area") private List waterArea;// TODO check that multiple values are mapped to ckan @@ -138,7 +128,7 @@ public class StockRecord extends Common{ String assessmentMethods, String stateOfMarineResource, Exploitation_Rate exploitationRate, Abundance_Level abundanceLevel, String narrativeStateAndTrend, String scientificAdvice, - String reportingEntity, Long reportingYear, Status status, + String reportingEntity, Long reportingYear, String stockUri, List waterArea) { super(); this.stockName = stockName; @@ -155,7 +145,6 @@ public class StockRecord extends Common{ this.scientificAdvice = scientificAdvice; this.reportingEntity = reportingEntity; this.reportingYear = reportingYear; - this.status = status; this.stockUri = stockUri; this.waterArea = waterArea; } @@ -288,17 +277,9 @@ public class StockRecord extends Common{ this.reportingYear = reportingYear; } - public Status getStatus() { - return status; - } - - public void setStatus(Status status) { - this.status = status; - } - @Override public String toString() { - return "StockRecord [" + super.toString() + ",stockName=" + stockName + ", stockID=" + stockID + return "StockRecord [" + super.toString() + "stockName=" + stockName + ", stockID=" + stockID + ", speciesScientificName=" + speciesScientificName + ", area=" + area + ", exploitingFishery=" + exploitingFishery + ", managementEntity=" + managementEntity @@ -309,7 +290,7 @@ public class StockRecord extends Common{ + ", narrativeStateAndTrend=" + narrativeStateAndTrend + ", scientificAdvice=" + scientificAdvice + ", reportingEntity=" + reportingEntity + ", reportingYear=" - + reportingYear + ", status=" + status + ", stockUri=" - + stockUri + ", waterArea=" + waterArea + "]"; + + reportingYear + ", stockUri=" + stockUri + ", waterArea=" + + waterArea + "]"; } } diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java index 547a23f..1f97187 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java @@ -1,6 +1,7 @@ package org.gcube.data_catalogue.grsf_publish_ws.services; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -174,7 +175,7 @@ public class GrsfPublisherFisheryService { HelperMethods.getGroups(groups, record); // evaluate the custom fields - Map customFields = new HashMap(); + Map> customFields = new HashMap>(); if(record.getExtras() != null) customFields = record.getExtras(); @@ -183,7 +184,7 @@ public class GrsfPublisherFisheryService { HelperMethods.getExtras(customFields, record); // add the type - customFields.put(HelperMethods.PRODUCT_TYPE, THIS_TYPE); + customFields.put(HelperMethods.PRODUCT_TYPE, Arrays.asList(THIS_TYPE)); // retrieve the user's email and fullname String authorMail = HelperMethods.getUserEmail(context, SecurityTokenProvider.instance.get()); @@ -216,7 +217,7 @@ public class GrsfPublisherFisheryService { long version = record.getVersion() == null ? 1 : record.getVersion(); // create the product - id = catalogue.createCKanDataset( + id = catalogue.createCKanDatasetMultipleCustomFields( catalogue.getApiKeyFromUsername(username), futureTitle, organization, diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java index d66d08d..f099012 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java @@ -1,6 +1,7 @@ package org.gcube.data_catalogue.grsf_publish_ws.services; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -166,7 +167,7 @@ public class GrsfPublisherStockService { HelperMethods.getGroups(groups, record); // evaluate the custom fields - Map customFields = new HashMap(); + Map> customFields = new HashMap>(); if(record.getExtras() != null) customFields = record.getExtras(); @@ -175,7 +176,7 @@ public class GrsfPublisherStockService { HelperMethods.getExtras(customFields, record); // add the type - customFields.put(HelperMethods.PRODUCT_TYPE, THIS_TYPE); + customFields.put(HelperMethods.PRODUCT_TYPE, Arrays.asList(THIS_TYPE)); // retrieve the user's email and fullname String authorMail = HelperMethods.getUserEmail(context, SecurityTokenProvider.instance.get()); @@ -207,7 +208,7 @@ public class GrsfPublisherStockService { long version = record.getVersion() == null ? 1 : record.getVersion(); // create the product - id = catalogue.createCKanDataset( + id = catalogue.createCKanDatasetMultipleCustomFields( catalogue.getApiKeyFromUsername(username), futureTitle, organization, diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java index 54ad4b0..411cc4a 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java @@ -5,6 +5,7 @@ import java.io.BufferedReader; import java.io.InputStreamReader; import java.lang.reflect.Field; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -165,7 +166,7 @@ public abstract class HelperMethods { /** * Retrieve the list of extras for this object */ - public static void getExtras(Map extras, Common record){ + public static void getExtras(Map> extras, Common record){ Class current = record.getClass(); do{ @@ -176,8 +177,22 @@ public abstract class HelperMethods { Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record); if(f != null){ - // get the key to put into the map first - extras.put(field.getAnnotation(CustomField.class).key(), f.toString().trim()); + if(f.getClass().isArray()){ + if(Collection.class.isAssignableFrom(f.getClass())){ + + logger.debug("The object " + field.getName() + " is a collection"); + List res = (List)f; + extras.put(field.getAnnotation(CustomField.class).key(), res); + + }else + logger.error("The object " + field.getName() + " cannot be convert to a list/array"); + }else{ + List values = new ArrayList(); + if(extras.containsKey(field.getAnnotation(CustomField.class).key())) + values = extras.get(field.getAnnotation(CustomField.class).key()); + values.add(f.toString().trim()); + extras.put(field.getAnnotation(CustomField.class).key(), values); + } } }catch(Exception e){ diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/groups/Status.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/groups/Status.java index a1c4c6c..e824c90 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/groups/Status.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/groups/Status.java @@ -1,8 +1,5 @@ package org.gcube.data_catalogue.grsf_publish_ws.utils.groups; -import org.gcube.data_catalogue.grsf_publish_ws.filters.RequestsAuthAccountingFilter; -import org.slf4j.LoggerFactory; - import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonValue; @@ -45,7 +42,6 @@ public enum Status { */ @JsonCreator public static Status onDeserialize(String statusString) { - System.out.println("STATUS IS " + statusString); if(statusString != null) { for(Status status : Status.values()) { if (status.toString().equalsIgnoreCase(statusString.trim())) diff --git a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java index 04dd206..cac794a 100644 --- a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java +++ b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java @@ -8,6 +8,7 @@ import java.io.InputStreamReader; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -18,13 +19,12 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; import org.gcube.data_catalogue.grsf_publish_ws.json.input.DatabaseSource; import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord; import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord; -import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean; import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type; -import org.junit.Test; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse; @@ -237,4 +237,22 @@ public class JTests { // System.out.println(HelperMethods.retrieveOrgNameFromScope("/gcube/devNext/NextNext")); } + + //@Test + public void testCustomFieldAnnotation() throws JsonProcessingException{ + + StockRecord record = new StockRecord(); + record.setWaterArea(Arrays.asList("aa", "bb", "cc", "dd")); + Map> extras = new HashMap>(); + HelperMethods.getExtras(extras , record); + System.out.println("Extras = " + extras); + + ObjectMapper mapper = new ObjectMapper(); + + //Object to JSON in String + String jsonInString = mapper.writeValueAsString(record); + System.out.println(jsonInString); + + + } }