now the Source (Ram, GRSF, Fishsource, FIRMS) is specified in the path of the request. Other changes to help methods

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@135190 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-01 18:19:22 +00:00
parent 62d111688e
commit c216799898
18 changed files with 847 additions and 807 deletions

View File

@ -0,0 +1,207 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* The base class contains basic information needed to publish something in the data catalogue.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@JsonIgnoreProperties(value = {"author", "author_contact"})
public class Base {
private static Logger logger = LoggerFactory.getLogger(Base.class);
@JsonProperty("description")
private String description;
@JsonProperty("license_id")
private String license;
// filled automatically by the service
@JsonProperty("author")
private String author;
@JsonProperty("version")
private Long version;
// filled automatically by the service
@JsonProperty("author_contact")
private String authorContact;
@JsonProperty("maintainer")
private String maintainer;
@JsonProperty("maintainer_contact")
private String maintainerContact;
@JsonProperty("extras_fields")
private Map<String, List<String>> extrasFields = new HashMap<>();
@JsonProperty("extras_resources")
private List<ResourceBean> extrasResources = new ArrayList<ResourceBean>();
@JsonProperty("uuid_knowledge_base")
@CustomField(key="UUID Knowledge Base")
@NotNull(message="uuid_knowledge_base cannot be null")
@Size(min=1, message="uuid_knowledge_base cannot be empty")
private String uuid;
public Base() {
super();
}
/**
* @param description
* @param license
* @param author
* @param version
* @param authorContact
* @param maintainer
* @param maintainerContact
* @param extrasFields
* @param extrasResources
* @param uuid
*/
public Base(String description, String license, String author,
Long version, String authorContact, String maintainer,
String maintainerContact, Map<String, List<String>> extrasFields,
List<ResourceBean> extrasResources, String uuid) {
super();
this.description = description;
this.license = license;
this.author = author;
this.version = version;
this.authorContact = authorContact;
this.maintainer = maintainer;
this.maintainerContact = maintainerContact;
this.extrasFields = extrasFields;
this.extrasResources = extrasResources;
this.uuid = uuid;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getAuthorContact() {
return authorContact;
}
public void setAuthorContact(String authorContact) {
this.authorContact = authorContact;
}
public String getMaintainer() {
return maintainer;
}
public void setMaintainer(String maintainer) {
this.maintainer = maintainer;
}
public String getMaintainerContact() {
return maintainerContact;
}
public void setMaintainerContact(String maintainerContact) {
this.maintainerContact = maintainerContact;
}
public Map<String, List<String>> getExtrasFields() {
return extrasFields;
}
public void setExtrasFields(Map<String, List<String>> extrasFields) {
this.extrasFields = extrasFields;
}
public List<ResourceBean> getExtrasResources() {
return extrasResources;
}
public void setExtrasResources(List<ResourceBean> extrasResources) {
this.extrasResources = extrasResources;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
/**
* Use for generics object (unrecognized from Jackson) to be put into the map
* @param key
* @param value
*/
@JsonAnySetter
private void genericSetter(String key, Object value){
logger.info("Found extra property: [" + key + "," + value + "]");
List<String> values = new ArrayList<String>();
if(extrasFields.containsKey(key))
values = extrasFields.get(key);
else
values = new ArrayList<String>();
values.add(HelperMethods.removeHTML(value.toString()));
extrasFields.put(key, values);
}
@Override
public String toString() {
return "Base [description=" + description + ", license=" + license
+ ", author=" + author + ", version=" + version
+ ", authorContact=" + authorContact + ", maintainer="
+ maintainer + ", maintainerContact=" + maintainerContact
+ ", extrasFields=" + extrasFields + ", extrasResources="
+ extrasResources + ", uuid=" + uuid + "]";
}
}

View File

@ -1,352 +0,0 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CkanResource;
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.HelperMethods;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import org.gcube.datacatalogue.ckanutillibrary.models.ResourceBean;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
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", "product_type"}) // ignore in serialization/deserialization
public class Common {
private static Logger logger = LoggerFactory.getLogger(Common.class);
public static final String PRODUCT_TYPE_KEY = "Product type";
@JsonProperty("description")
private String description;
@JsonProperty("license_id")
private String license;
// filled automatically by the service
@JsonProperty("author")
private String author;
@JsonProperty("version")
private Long version;
// filled automatically by the service
@JsonProperty("author_contact")
private String authorContact;
@JsonProperty("maintainer")
private String maintainer;
@JsonProperty("maintainer_contact")
private String maintainerContact;
@JsonProperty("data_owner")
@CustomField(key="Data owner")
private String dataOwner;
@JsonProperty("database_sources")
@CkanResource
@NotNull(message="database_source cannot be null")
@Size(min=1, message="database_source cannot be empty")
@Valid
private List<Resource<Source>> databaseSources;
@JsonProperty("source_of_information")
@CkanResource
@Valid
private List<Resource<String>> sourceOfInformation;
@JsonProperty("short_title")
@CustomField(key="Short Title")
@NotNull(message="short_title cannot be null")
@Size(min=1, message="short_title cannot be empty")
private String shortTitle;
@JsonProperty("uuid_knowledge_base")
@CustomField(key="UUID Knowledge Base")
@NotNull(message="uuid_knowledge_base cannot be null")
@Size(min=1, message="uuid_knowledge_base cannot be empty")
// This will be the identifier of the product!!
private String uuid;
@JsonProperty("traceability_flag")
@CustomField(key="Traceability Flag")
@NotNull(message="traceability_flag cannot be null")
private boolean traceabilityFlag;
@JsonProperty("status")
@CustomField(key="Status")
@Group
@Tag
private Status status;
// automatically compiled
@JsonProperty("product_type")
@CustomField(key=PRODUCT_TYPE_KEY)
@Tag
@Group
private String productType;
@JsonProperty("reporting_year")
@CustomField(key="Reporting year")
private Long reportingYear;
@JsonProperty("extras_fields")
private Map<String, List<String>> extrasFields = new HashMap<>();
@JsonProperty("extras_resources")
private List<ResourceBean> extrasResources = new ArrayList<ResourceBean>();
public Common() {
super();
}
/**
* Create a common element.
* @param description
* @param license
* @param author
* @param version
* @param authorContact
* @param maintainer
* @param maintainerContact
* @param dataOwner
* @param databaseSources
* @param sourceOfInformation
* @param shortTitle
* @param uuid
* @param traceabilityFlag
* @param status
* @param productType
* @param reportingYear
* @param extrasFields
* @param extrasResources
*/
public Common(String description, String license, String author,
Long version, String authorContact, String maintainer,
String maintainerContact, String dataOwner,
List<Resource<Source>> databaseSources,
List<Resource<String>> sourceOfInformation, String shortTitle,
String uuid, boolean traceabilityFlag, Status status,
String productType, Long reportingYear,
Map<String, List<String>> extrasFields,
List<ResourceBean> extrasResources) {
super();
this.description = description;
this.license = license;
this.author = author;
this.version = version;
this.authorContact = authorContact;
this.maintainer = maintainer;
this.maintainerContact = maintainerContact;
this.dataOwner = dataOwner;
this.databaseSources = databaseSources;
this.sourceOfInformation = sourceOfInformation;
this.shortTitle = shortTitle;
this.uuid = uuid;
this.traceabilityFlag = traceabilityFlag;
this.status = status;
this.productType = productType;
this.reportingYear = reportingYear;
this.extrasFields = extrasFields;
this.extrasResources = extrasResources;
}
public String getProductType() {
return productType;
}
public Long getReportingYear() {
return reportingYear;
}
public void setReportingYear(Long reportingYear) {
this.reportingYear = reportingYear;
}
public Map<String, List<String>> getExtrasFields() {
return extrasFields;
}
public void setExtrasFields(Map<String, List<String>> extrasFields) {
this.extrasFields = extrasFields;
}
public List<ResourceBean> getExtrasResources() {
return extrasResources;
}
public void setExtrasResources(List<ResourceBean> extrasResources) {
this.extrasResources = extrasResources;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getLicense() {
return license;
}
public void setLicense(String license) {
this.license = license;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Long getVersion() {
return version;
}
public void setVersion(Long version) {
this.version = version;
}
public String getAuthorContact() {
return authorContact;
}
public void setAuthorContact(String authorContact) {
this.authorContact = authorContact;
}
public String getMaintainer() {
return maintainer;
}
public void setMaintainer(String maintainer) {
this.maintainer = maintainer;
}
public String getMaintainerContact() {
return maintainerContact;
}
public void setMaintainerContact(String maintainerContact) {
this.maintainerContact = maintainerContact;
}
public List<Resource<Source>> getDatabaseSources() {
return databaseSources;
}
public void setDatabaseSources(List<Resource<Source>> databaseSources) {
this.databaseSources = databaseSources;
}
public List<Resource<String>> getSourceOfInformation() {
return sourceOfInformation;
}
public void setSourceOfInformation(List<Resource<String>> sourceOfInformation) {
this.sourceOfInformation = sourceOfInformation;
}
public String getDataOwner() {
return dataOwner;
}
public void setDataOwner(String dataOwner) {
this.dataOwner = dataOwner;
}
public String getShortTitle() {
return shortTitle;
}
public void setShortTitle(String shortTitle) {
this.shortTitle = shortTitle;
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
}
public boolean isTraceabilityFlag() {
return traceabilityFlag;
}
public void setTraceabilityFlag(boolean traceabilityFlag) {
this.traceabilityFlag = traceabilityFlag;
}
public Status getStatus() {
return status;
}
public void setStatus(Status status) {
this.status = status;
}
@JsonAnySetter
/**
* Use for generics object passed into the map
* @param key
* @param value
*/
private void genericSetter(String key, Object value){
// found extra property
logger.info("Found extra property: [" + key + "," + value + "]");
// try to convert the value to a string and put into the hashmap
List<String> values = new ArrayList<String>();
if(extrasFields.containsKey(key))
values = extrasFields.get(key);
else
values = new ArrayList<String>();
values.add(HelperMethods.removeHTML(value.toString()));
extrasFields.put(key, values);
}
@Override
public String toString() {
return "Common [description=" + description + ", license=" + license
+ ", author=" + author + ", version=" + version
+ ", authorContact=" + authorContact + ", maintainer="
+ maintainer + ", maintainerContact=" + maintainerContact
+ ", dataOwner=" + dataOwner + ", databaseSources="
+ databaseSources + ", sourceOfInformation="
+ sourceOfInformation + ", shortTitle=" + shortTitle
+ ", uuid=" + uuid + ", traceabilityFlag=" + traceabilityFlag
+ ", status=" + status + ", productType=" + productType
+ ", reportingYear=" + reportingYear + ", extrasFields="
+ extrasFields + ", extrasResources=" + extrasResources + "]";
}
}

View File

@ -0,0 +1,190 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.List;
import javax.validation.Valid;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CkanResource;
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.Sources;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* Information that both Stock and Fishery records must contain.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@JsonIgnoreProperties(value = {"product_type"})
public class CommonAggregated extends Base{
public static final String PRODUCT_TYPE_KEY = "Product type";
@JsonProperty("data_owner")
@CustomField(key="Data owner")
private String dataOwner;
@JsonProperty("database_sources")
@CkanResource
// @NotNull(message="database_source cannot be null")
// @Size(min=1, message="database_source cannot be empty")
@Tag
@Valid
private List<Resource<Sources>> databaseSources;
@JsonProperty("source_of_information")
@CkanResource
@Valid
private List<Resource<String>> sourceOfInformation;
@JsonProperty("refers_to")
@CustomField(key="Refers to")
@Valid
private List<RefersToBean> refersTo;
@JsonProperty("short_title")
@CustomField(key="Short Title")
// @NotNull(message="short_title cannot be null")
// @Size(min=1, message="short_title cannot be empty")
private String shortTitle;
@JsonProperty("traceability_flag")
@CustomField(key="Traceability Flag")
// @NotNull(message="traceability_flag cannot be null")
private boolean traceabilityFlag;
@JsonProperty("status")
@CustomField(key="Status")
@Group
@Tag
private Status status;
@JsonProperty("reporting_year")
@CustomField(key="Reporting year")
private Long reportingYear;
// automatically compiled
@JsonProperty("product_type")
@CustomField(key=PRODUCT_TYPE_KEY)
@Tag
@Group
private String productType;
public CommonAggregated() {
super();
}
/**
* @param dataOwner
* @param databaseSources
* @param sourceOfInformation
* @param refersTo
* @param shortTitle
* @param traceabilityFlag
* @param status
* @param reportingYear
* @param productType
*/
public CommonAggregated(String dataOwner, List<Resource<Sources>> databaseSources,
List<Resource<String>> sourceOfInformation,
List<RefersToBean> refersTo, String shortTitle,
boolean traceabilityFlag, Status status, Long reportingYear,
String productType) {
super();
this.dataOwner = dataOwner;
this.databaseSources = databaseSources;
this.sourceOfInformation = sourceOfInformation;
this.refersTo = refersTo;
this.shortTitle = shortTitle;
this.traceabilityFlag = traceabilityFlag;
this.status = status;
this.reportingYear = reportingYear;
this.productType = productType;
}
public List<RefersToBean> getRefersTo() {
return refersTo;
}
public void setRefersTo(List<RefersToBean> refersTo) {
this.refersTo = refersTo;
}
public Long getReportingYear() {
return reportingYear;
}
public void setReportingYear(Long reportingYear) {
this.reportingYear = reportingYear;
}
public List<Resource<Sources>> getDatabaseSources() {
return databaseSources;
}
public void setDatabaseSources(List<Resource<Sources>> databaseSources) {
this.databaseSources = databaseSources;
}
public List<Resource<String>> getSourceOfInformation() {
return sourceOfInformation;
}
public void setSourceOfInformation(List<Resource<String>> sourceOfInformation) {
this.sourceOfInformation = sourceOfInformation;
}
public String getProductType() {
return productType;
}
public void setProductType(String productType) {
this.productType = productType;
}
public String getDataOwner() {
return dataOwner;
}
public void setDataOwner(String dataOwner) {
this.dataOwner = dataOwner;
}
public String getShortTitle() {
return shortTitle;
}
public void setShortTitle(String shortTitle) {
this.shortTitle = shortTitle;
}
public boolean isTraceabilityFlag() {
return traceabilityFlag;
}
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() {
return "Common [dataOwner=" + dataOwner + ", databaseSources="
+ databaseSources + ", sourceOfInformation="
+ sourceOfInformation + ", refersTo=" + refersTo
+ ", shortTitle=" + shortTitle + ", traceabilityFlag="
+ traceabilityFlag + ", status=" + status + ", reportingYear="
+ reportingYear + ", productType=" + productType + "]";
}
}

View File

@ -20,11 +20,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class FisheryRecord extends Common{
public class FisheryRecord extends CommonAggregated{
@JsonProperty("fishery_name")
@NotNull(message="fishery_name cannot be null")
@Size(min=1, message="fishery_name cannot be empty")
// @Size(min=1, message="fishery_name cannot be empty")
@CustomField(key="Fishery Name")
private String fisheryName;
@ -79,7 +79,7 @@ public class FisheryRecord extends Common{
@Tag
@Valid
private List<TimeSeriesBean<String, String>> catchesOrLandings;
@JsonProperty("type")
@CustomField(key="Type")
@Tag
@ -129,7 +129,7 @@ public class FisheryRecord extends Common{
this.catchesOrLandings = catchesOrLandings;
this.type = type;
}
public String getFisheryName() {
return fisheryName;
}
@ -238,5 +238,5 @@ public class FisheryRecord extends Common{
+ ", environment=" + environment + ", catchesOrLandings="
+ catchesOrLandings + ", type=" + type + "]";
}
}

View File

@ -0,0 +1,54 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import com.fasterxml.jackson.annotation.JsonProperty;
/**
* RefersToBean are used into aggregated records but not into the sources.
* To check if a product is a source or
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class RefersToBean {
@JsonProperty("url")
String url;
@JsonProperty("id")
String id;
public RefersToBean() {
super();
}
/** Create a refers to bean
* @param url
* @param id
*/
public RefersToBean(String url, String id) {
super();
this.url = url;
this.id = id;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public String toString() {
return "url=" + url + ", id=" + id;
}
}

View File

@ -3,7 +3,7 @@ package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -69,7 +69,7 @@ public class Resource<T> {
// in case of @Tag/@Group, we check the class of the element Name
Class<? extends Object> nameClass = name.getClass();
if(nameClass.equals(Source.class))
if(nameClass.equals(Sources.class))
return name.toString();
return "Resource [url=" + url + ", description=" + description

View File

@ -3,8 +3,6 @@ package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.List;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
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;
@ -21,11 +19,11 @@ import com.fasterxml.jackson.annotation.JsonProperty;
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class StockRecord extends Common{
public class StockRecord extends CommonAggregated{
@JsonProperty("stock_name")
@NotNull(message="stock_name cannot be null")
@Size(min=2, message="stock_name cannot be empty")
// @NotNull(message="stock_name cannot be null")
// @Size(min=2, message="stock_name cannot be empty")
@CustomField(key="Stock Name")
private String stockName;
@ -36,12 +34,12 @@ public class StockRecord extends Common{
@JsonProperty("species")
@CustomField(key="Species")
@Tag
@Size(min=1, message="species cannot be empty")
// @Size(min=1, message="species cannot be empty")
private List<String> species;
@JsonProperty("assessment_distribution_area")
@CustomField(key="Assessment distribution area")
@Size(min=1, message="assessment distribution area cannot be empty")
// @Size(min=1, message="assessment distribution area cannot be empty")
private List<String> area;
@JsonProperty("exploiting_fishery")
@ -309,7 +307,7 @@ public class StockRecord extends Common{
public void setReportingEntity(String reportingEntity) {
this.reportingEntity = reportingEntity;
}
public List<TimeSeriesBean<String, String>> getExploitationRate() {
return exploitationRate;
}

View File

@ -2,8 +2,7 @@ package org.gcube.data_catalogue.grsf_publish_ws.json.output;
/**
* Response bean to be used by the service.
* @author Costantino Perciante at ISTI-CNR
* (costantino.perciante@isti.cnr.it)
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class ResponseBean{

View File

@ -3,8 +3,10 @@ package org.gcube.data_catalogue.grsf_publish_ws.services;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.validation.Valid;
@ -15,6 +17,7 @@ import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@ -25,11 +28,13 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.CommonAggregated;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.DeleteProductBean;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
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.Record_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.ManageTimeSeriesThread;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
@ -43,15 +48,13 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
* Fishery web service methods
* @author Costantino Perciante at ISTI-CNR
*/
@Path("fishery/")
@Path("{source}/fishery/")
public class GrsfPublisherFisheryService {
private static final String DEFAULT_FISHERY_LICENSE = "CC-BY-SA-4.0";
private static final String THIS_TYPE = "Fishery";
// the context
@Context
ServletContext contextServlet;
@Context ServletContext contextServlet;
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class);
@ -89,7 +92,8 @@ public class GrsfPublisherFisheryService {
@Produces(MediaType.APPLICATION_JSON)
public Response publishFishery(
@NotNull(message="record cannot be null")
@Valid FisheryRecord record)
@Valid FisheryRecord record,
@PathParam("source") String source)
throws ValidationException{
// retrieve context and username
@ -107,165 +111,171 @@ public class GrsfPublisherFisheryService {
try{
// determine the organization in which this product should be put
String contextInWhichPublish = HelperMethods.getContextFromStatus(record.getStatus(), contextServlet);
// Cast the source to the accepted ones
Sources sourceInPath = Sources.valueOf(source);
if(contextInWhichPublish == null || !contextInWhichPublish.equals(context)){
if(sourceInPath == null)
throw new Exception("The specified source in the path is unknown");
// stop, this value must be defined
status = Status.BAD_REQUEST;
throw new IllegalArgumentException("Status attribute is not defined or the Token you are using is not correct to perform such request!");
// // determine the organization in which this product should be put
// String contextInWhichPublish = HelperMethods.getContextFromStatus(record.getStatus(), contextServlet);
//
// if(contextInWhichPublish == null || !contextInWhichPublish.equals(context)){
//
// // stop, this value must be defined
// status = Status.BAD_REQUEST;
// throw new IllegalArgumentException("Status attribute is not defined or the Token you are using is not correct to perform such request!");
//
// }else{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
}else{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
// // in this case, the status is mandatory
// if(record.getStatus() == null && contextInWhichPublish.equals(HelperMethods.PENDING_CONTEX_KEY)){
//
// status = Status.BAD_REQUEST;
// throw new Exception("Status information is missing");
//
// }
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
// check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(context);
String role = catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username));
logger.info("***************************Role of the user " + username + " is " + role);
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
status = Status.FORBIDDEN;
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-admin role!");
}
// The name of the product will be the uuid of the kb. The title will be the fishery's fishery_name. Fishery has also the constraint that
// fishing area and jurisdiction area cannot be empty at the same time
String futureName = record.getUuid();
String futureTitle = record.getFisheryName();
List<String> fishingArea = record.getFishingArea();
List<String> jurisdictionArea = record.getJurisdictionArea();
if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST;
throw new Exception("The name requested for the product is not correct! It should contain only alphanumeric characters, and symbols like '.' or '_', '-'");
}else if((fishingArea == null || fishingArea.isEmpty()) && (jurisdictionArea == null || jurisdictionArea.isEmpty()) && sourceInPath.equals(Sources.GRSF)){
status = Status.BAD_REQUEST;
throw new Exception("fishing_area and jurisdiction_area cannot be null/empty at the same time!");
}else{
// in this case, the status is mandatory
if(record.getStatus() == null && contextInWhichPublish.equals(HelperMethods.PENDING_CONTEX_KEY)){
status = Status.BAD_REQUEST;
throw new Exception("Status information is missing");
}
// check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish);
String role = catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username));
logger.info("***************************Role of the user " + username + " is " + role);
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet...");
boolean alreadyExist = catalogue.existProductWithNameOrId(futureName);
status = Status.FORBIDDEN;
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-admin role!");
if(alreadyExist){
}
// The name of the product will be the uuid of the kb. The title will be the fishery's fishery_name. Fishery has also the constraint that
// fishing area and jurisdiction area cannot be empty at the same time
String futureName = record.getUuid();
String futureTitle = record.getFisheryName();
List<String> fishingArea = record.getFishingArea();
List<String> jurisdictionArea = record.getJurisdictionArea();
if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST;
throw new Exception("The name requested for the product is not correct! It should contain only alphanumeric characters, and symbols like '.' or '_', '-'");
}else if((fishingArea == null || fishingArea.isEmpty()) && (jurisdictionArea == null || jurisdictionArea.isEmpty())){
status = Status.BAD_REQUEST;
throw new Exception("fishing_area and jurisdiction_area cannot be null/empty at the same time!");
logger.debug("A product with name " + futureName + " already exists");
status = Status.CONFLICT;
throw new Exception("A product with name " + futureName + " already exists");
}else{
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet...");
boolean alreadyExist = catalogue.existProductWithNameOrId(futureName);
// set the type
record.setProductType(Record_Type.FISHERY.getOrigName());
if(alreadyExist){
// evaluate the tags of the product
Set<String> tags = new HashSet<String>();
HelperMethods.getTags(tags, record);
logger.debug("A product with name " + futureName + " already exists");
status = Status.CONFLICT;
throw new Exception("A product with name " + futureName + " already exists");
// evaluate the groups
Set<String> groups = new HashSet<String>();
HelperMethods.getGroups(groups, record);
// evaluate the custom fields
Map<String, List<String>> customFields = record.getExtrasFields();
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
if(authorMail == null || authorFullname == null){
logger.debug("Author fullname or mail missing, cannot continue");
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
}else{
// set the type
record.setProductType(THIS_TYPE);
// evaluate the resources
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// evaluate the tags of the product
List<String> tags = new ArrayList<String>();
HelperMethods.getTags(tags, record);
// check the license id
String license = null;
if(record.getLicense() == null || record.getLicense().isEmpty())
license = DEFAULT_FISHERY_LICENSE;
else
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
license = record.getLicense();
else throw new Exception("Please check the license id!");
// evaluate the groups
List<String> groups = new ArrayList<String>();
HelperMethods.getGroups(groups, record);
long version = record.getVersion() == null ? 1 : record.getVersion();
// evaluate the custom fields
Map<String, List<String>> customFields = record.getExtrasFields();
// create the product
id = catalogue.createCKanDatasetMultipleCustomFields(
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),
record.getMaintainerContact(),
version,
HelperMethods.removeHTML(record.getDescription()),
license,
new ArrayList<String>(tags),
customFields,
resources,
false);
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
if(id != null){
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
logger.info("Product created! Id is " + id);
responseBean.setId(id);
status = Status.CREATED;
responseBean.setError(null);
responseBean.setProductUrl(catalogue.getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + futureName, "UTF-8"));
responseBean.setKbUuid(record.getUuid());
if(authorMail == null || authorFullname == null){
if(!groups.isEmpty()){
logger.info("Launching thread for association to the list of groups " + groups);
AssociationToGroupThread threadGroups = new AssociationToGroupThread(new ArrayList<String>(groups), id, organization, username, catalogue);
threadGroups.start();
logger.info("Waiting association thread to die..");
threadGroups.join();
logger.debug("Ok, it died");
}
logger.debug("Author fullname or mail missing, cannot continue");
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
// manage time series
logger.info("Launching thread for time series handling");
new ManageTimeSeriesThread(record, futureName, username, catalogue, context, token).start();
}else{
}else
throw new Exception("There was an error during the product generation, sorry");
// evaluate the resources
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// check the license id
String license = null;
if(record.getLicense() == null || record.getLicense().isEmpty())
license = DEFAULT_FISHERY_LICENSE;
else
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
license = record.getLicense();
else throw new Exception("Please check the license id!");
long version = record.getVersion() == null ? 1 : record.getVersion();
// create the product
id = catalogue.createCKanDatasetMultipleCustomFields(
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),
record.getMaintainerContact(),
version,
HelperMethods.removeHTML(record.getDescription()),
license,
tags,
customFields,
resources,
false);
if(id != null){
logger.info("Product created! Id is " + id);
responseBean.setId(id);
status = Status.CREATED;
responseBean.setError(null);
responseBean.setProductUrl(catalogue.getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + futureName, "UTF-8"));
responseBean.setKbUuid(record.getUuid());
if(!groups.isEmpty()){
logger.info("Launching thread for association to the list of groups " + groups);
AssociationToGroupThread threadGroups = new AssociationToGroupThread(groups, id, organization, username, catalogue);
threadGroups.start();
logger.info("Waiting association thread to die..");
threadGroups.join();
logger.debug("Ok, it died");
}
// manage time series
logger.info("Launching thread for time series handling");
new ManageTimeSeriesThread(record, futureName, username, catalogue, context, token).start();
}else
throw new Exception("There was an error during the product generation, sorry");
}
}
}
}
}
// }
}catch(Exception e){
logger.error("Failed to create fishery record", e);
responseBean.setError(e.getMessage());
@ -313,7 +323,7 @@ public class GrsfPublisherFisheryService {
}
// get extras and check there is the product type
if(fisheryInCkan.getExtrasAsHashMap().get(Common.PRODUCT_TYPE_KEY).equals(THIS_TYPE)){
if(fisheryInCkan.getExtrasAsHashMap().get(CommonAggregated.PRODUCT_TYPE_KEY).equals(Record_Type.FISHERY.getOrigName())){
logger.warn("Ok, this is a fishery, removing it");
boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), catalogue.getApiKeyFromUsername(username), true);
if(deleted){

View File

@ -3,8 +3,10 @@ package org.gcube.data_catalogue.grsf_publish_ws.services;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.validation.Valid;
@ -15,6 +17,7 @@ import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
@ -25,11 +28,13 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.authorization.library.utils.Caller;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.CommonAggregated;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.DeleteProductBean;
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.Record_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.ManageTimeSeriesThread;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
@ -43,15 +48,13 @@ import eu.trentorise.opendata.jackan.model.CkanDataset;
* Stock web service methods
* @author Costantino Perciante at ISTI-CNR
*/
@Path("stock/")
@Path("{source}/stock/")
public class GrsfPublisherStockService {
private static final String DEFAULT_STOCK_LICENSE = "CC-BY-SA-4.0";
private static final String THIS_TYPE = "Stock";
// the context
@Context
ServletContext contextServlet;
@Context ServletContext contextServlet;
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
@ -88,7 +91,7 @@ public class GrsfPublisherStockService {
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public Response publishStock(
@NotNull(message="record cannot be null") @Valid StockRecord record) throws ValidationException{
@NotNull(message="record cannot be null") @Valid StockRecord record, @PathParam("source") String source) throws ValidationException{
// retrieve context and username
Caller caller = AuthorizationProvider.instance.get();
@ -104,155 +107,163 @@ public class GrsfPublisherStockService {
try{
// determine the organization in which this product should be put
String contextInWhichPublish = HelperMethods.getContextFromStatus(record.getStatus(), contextServlet);
// Cast the source to the accepted ones
Sources sourceInPath = Sources.valueOf(source);
if(contextInWhichPublish == null || !contextInWhichPublish.equals(context)){
if(sourceInPath == null)
throw new Exception("The specified source in the path is unknown");
// stop, this value must be defined
status = Status.BAD_REQUEST;
throw new IllegalArgumentException("Status attribute is not defined or the Token you are using is not correct to perform such request!");
logger.info("The request is to create a stock object of source " + sourceInPath);
// // determine the organization in which this product should be put
// String contextInWhichPublish = HelperMethods.getContextFromStatus(record.getStatus(), contextServlet);
//
// if(contextInWhichPublish == null || !contextInWhichPublish.equals(context)){
//
// // stop, this value must be defined
// status = Status.BAD_REQUEST;
// throw new IllegalArgumentException("Status attribute is not defined or the Token you are using is not correct to perform such request!");
//
// }else{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
}else{
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){
// // in this case, the status is mandatory
// if(record.getStatus() == null && contextInWhichPublish.equals(HelperMethods.PENDING_CONTEX_KEY)){
//
// status = Status.BAD_REQUEST;
// throw new Exception("Status information is missing");
//
// }
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
// check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(context);
String role = catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username));
logger.info("Role of the user " + username + " is " + role);
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
status = Status.FORBIDDEN;
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-Administrator role!");
}
// check the record has a name, at least
String futureName = record.getUuid();
String futureTitle = record.getStockName();
if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST;
throw new Exception("The name requested for the product is not correct! It should contain only alphanumeric characters, and symbols like '.' or '_', '-'");
}else{
// in this case, the status is mandatory
if(record.getStatus() == null && contextInWhichPublish.equals(HelperMethods.PENDING_CONTEX_KEY)){
status = Status.BAD_REQUEST;
throw new Exception("Status information is missing");
}
// check the user has editor/admin role into the org
String organization = HelperMethods.retrieveOrgNameFromScope(contextInWhichPublish);
String role = catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username));
logger.info("***************************Role of the user " + username + " is " + role);
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet...");
boolean alreadyExist = catalogue.existProductWithNameOrId(futureName);
status = Status.FORBIDDEN;
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-Administrator role!");
if(alreadyExist){
}
// check the record has a name, at least
String futureName = record.getUuid();
String futureTitle = record.getStockName();
if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST;
throw new Exception("The name requested for the product is not correct! It should contain only alphanumeric characters, and symbols like '.' or '_', '-'");
logger.debug("A product with name " + futureName + " already exists");
status = Status.CONFLICT;
throw new Exception("Sorry but a product with such name already exists!");
}else{
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet...");
boolean alreadyExist = catalogue.existProductWithNameOrId(futureName);
// set the type
record.setProductType(Record_Type.STOCK.getOrigName());
if(alreadyExist){
// evaluate the tags of the product
Set<String> tags = new HashSet<String>();
HelperMethods.getTags(tags, record);
logger.debug("A product with name " + futureName + " already exists");
status = Status.CONFLICT;
throw new Exception("Sorry but a product with such name already exists!");
// evaluate the groups
Set<String> groups = new HashSet<String>();
HelperMethods.getGroups(groups, record);
// evaluate the custom fields
Map<String, List<String>> customFields = record.getExtrasFields();
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
if(authorMail == null || authorFullname == null){
logger.debug("Author fullname or mail missing, cannot continue");
responseBean.setId(null);
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("Sorry but was not possible to retrieve your fullname/email!");
}else{
// set the type
record.setProductType(THIS_TYPE);
// evaluate the resources
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// evaluate the tags of the product
List<String> tags = new ArrayList<String>();
HelperMethods.getTags(tags, record);
// check the license id
String license = null;
if(record.getLicense() == null || record.getLicense().isEmpty())
license = DEFAULT_STOCK_LICENSE;
else
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
license = record.getLicense();
else throw new Exception("Please check the license id!");
// evaluate the groups
List<String> groups = new ArrayList<String>();
HelperMethods.getGroups(groups, record);
long version = record.getVersion() == null ? 1 : record.getVersion();
// evaluate the custom fields
Map<String, List<String>> customFields = record.getExtrasFields();
// create the product
String id = catalogue.createCKanDatasetMultipleCustomFields(
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),
record.getMaintainerContact(),
version,
HelperMethods.removeHTML(record.getDescription()),
license,
new ArrayList<String>(tags),
customFields,
resources,
false);
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
if(id != null){
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
logger.info("Product created! Id is " + id);
responseBean.setId(id);
status = Status.CREATED;
responseBean.setProductUrl(catalogue.getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + futureName, "UTF-8"));
responseBean.setKbUuid(record.getUuid());
if(authorMail == null || authorFullname == null){
if(!groups.isEmpty()){
logger.info("Launching thread for association to the list of groups " + groups);
AssociationToGroupThread threadGroups = new AssociationToGroupThread(new ArrayList<String>(groups), id, organization, username, catalogue);
threadGroups.start();
logger.info("Waiting association thread to die..");
threadGroups.join();
logger.debug("Ok, it died");
}
logger.debug("Author fullname or mail missing, cannot continue");
responseBean.setId(null);
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("Sorry but was not possible to retrieve your fullname/email!");
}else{
// evaluate the resources
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// check the license id
String license = null;
if(record.getLicense() == null || record.getLicense().isEmpty())
license = DEFAULT_STOCK_LICENSE;
else
if(HelperMethods.existsLicenseId(record.getLicense(), catalogue))
license = record.getLicense();
else throw new Exception("Please check the license id!");
long version = record.getVersion() == null ? 1 : record.getVersion();
// create the product
String id = catalogue.createCKanDatasetMultipleCustomFields(
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),
record.getMaintainerContact(),
version,
HelperMethods.removeHTML(record.getDescription()),
license,
tags,
customFields,
resources,
false);
if(id != null){
logger.info("Product created! Id is " + id);
responseBean.setId(id);
status = Status.CREATED;
responseBean.setProductUrl(catalogue.getPortletUrl() + "?" + URLEncoder.encode("path=/dataset/" + futureName, "UTF-8"));
responseBean.setKbUuid(record.getUuid());
if(!groups.isEmpty()){
logger.info("Launching thread for association to the list of groups " + groups);
AssociationToGroupThread threadGroups = new AssociationToGroupThread(groups, id, organization, username, catalogue);
threadGroups.start();
logger.info("Waiting association thread to die..");
threadGroups.join();
logger.debug("Ok, it died");
}
// manage time series
logger.info("Launching thread for time series handling");
new ManageTimeSeriesThread(record, futureName, username, catalogue, context, token).start();
}else
throw new Exception("There was an error during the product generation, sorry");
}
// manage time series
logger.info("Launching thread for time series handling");
new ManageTimeSeriesThread(record, futureName, username, catalogue, context, token).start();
}else
throw new Exception("There was an error during the product generation, sorry");
}
}
}
}
// }
}catch(Exception e){
logger.error("Failed to create stock record", e);
responseBean.setError(e.getMessage());
@ -300,7 +311,7 @@ public class GrsfPublisherStockService {
}
// get extras and check there is the product type
if(stockInCkan.getExtrasAsHashMap().get(Common.PRODUCT_TYPE_KEY).equals(THIS_TYPE)){
if(stockInCkan.getExtrasAsHashMap().get(CommonAggregated.PRODUCT_TYPE_KEY).equals(Record_Type.STOCK.getOrigName())){
logger.warn("Ok, this is a stock, removing it");
boolean deleted = catalogue.deleteProduct(stockInCkan.getId(), catalogue.getApiKeyFromUsername(username), true);

View File

@ -58,10 +58,8 @@ public class CSVHelpers {
headerLine.append(CSV_SEPARATOR);
headerLine.append("unit");
}else{
// ....nothing
}
bw.write(headerLine.toString());
bw.newLine();
bw.flush();

View File

@ -1,76 +0,0 @@
/**
*
*/
package org.gcube.data_catalogue.grsf_publish_ws.utils;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;
import java.util.List;
import org.gcube.common.resources.gcore.GCoreEndpoint;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.resources.discovery.client.api.DiscoveryClient;
import org.gcube.resources.discovery.client.queries.api.SimpleQuery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Retrieve the endpoint for the Social Networking service running on Smartgears.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public class GcoreEndpointReaderSocialWS {
private static final String resource = "jersey-servlet";
private static final String serviceName = "SocialNetworking";
private static final String serviceClass = "Portal";
private static Logger logger = LoggerFactory.getLogger(GcoreEndpointReaderSocialWS.class);
/**
* Instantiates a new gcore endpoint reader.
* @param scope the scope
* @throws Exception the exception
*/
public static String getUrlSocialWS(String scope) throws Exception {
if(scope == null || scope.isEmpty())
throw new IllegalArgumentException();
String oldScope = ScopeProvider.instance.get();
try{
logger.info("set scope "+scope);
ScopeProvider.instance.set(scope);
SimpleQuery query = queryFor(GCoreEndpoint.class);
query.addCondition(String.format("$resource/Profile/ServiceClass/text() eq '%s'",serviceClass));
query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'");
query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName));
query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+resource+"\"]/text()");
logger.debug("submitting quey "+query.toString());
DiscoveryClient<String> client = client();
List<String> endpoints = client.submit(query);
if (endpoints == null || endpoints.isEmpty()) throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope);
String urlFound = endpoints.get(0);
if(urlFound==null)
throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope);
logger.info("found entyname "+urlFound+" for ckanResource: "+resource);
return urlFound;
}catch(Exception e){
String error = "An error occurred during GCoreEndpoint discovery, serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+scope +".";
logger.error(error, e);
throw new Exception(error);
}finally{
if(oldScope != null && !oldScope.equals(scope))
ScopeProvider.instance.set(oldScope);
}
}
}

View File

@ -10,6 +10,7 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.ServletContext;
@ -23,7 +24,7 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CkanResource;
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.json.input.Common;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Base;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Resource;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.TimeSeriesBean;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
@ -100,7 +101,7 @@ public abstract class HelperMethods {
/**
* Retrieve the list of tags for this object
*/
public static void getTags(List<String> tags, Common record){
public static void getTags(Set<String> tags, Base record){
Class<?> current = record.getClass();
do{
@ -124,14 +125,13 @@ public abstract class HelperMethods {
for (int i = (asList.size() - elementsToConsider); i < asList.size(); i++) {
String finalTag = asList.get(i).toString().trim().replaceAll(REGEX_TAGS, "");
logger.debug(finalTag);
tags.add(finalTag);
}
}else{
// else add all the available elements
for (int i = 0; i < elementsToConsider; i++) {
String finalTag = asList.get(i).toString().trim().replaceAll(REGEX_TAGS, "");
logger.debug(finalTag);
tags.add(finalTag);
}
}
@ -158,7 +158,7 @@ public abstract class HelperMethods {
/**
* Retrieve the list of groups' names for this object
*/
public static void getGroups(List<String> groups, Common record){
public static void getGroups(Set<String> groups, Base record){
Class<?> current = record.getClass();
do{
@ -179,8 +179,7 @@ public abstract class HelperMethods {
for (int i = 0; i < asList.size(); i++) {
String groupName = getGroupNameOnCkan(asList.get(i).toString().trim());
logger.debug(groupName);
if(!groups.contains(groupName))
groups.add(groupName);
groups.add(groupName);
}
}
@ -188,8 +187,7 @@ public abstract class HelperMethods {
// also convert to the group name that should be on ckan
String groupName = getGroupNameOnCkan(f.toString().trim());
if(!groups.contains(groupName))
groups.add(groupName);
groups.add(groupName);
}
}
@ -201,8 +199,7 @@ public abstract class HelperMethods {
// extract the name from the enum class and add it to the groups
// also convert to the group name that should be on ckan
String groupName = getGroupNameOnCkan(field.getType().getSimpleName());
if(!groups.contains(groupName))
groups.add(groupName);
groups.add(groupName);
}
@ -220,7 +217,7 @@ public abstract class HelperMethods {
/**
* Retrieve the list of extras for this object
*/
public static void getExtras(Map<String, List<String>> extras, Common record){
public static void getExtras(Map<String, List<String>> extras, Base record){
Class<?> current = record.getClass();
do{
@ -251,14 +248,12 @@ public abstract class HelperMethods {
for (int i = (asList.size() - elementsToConsider); i < asList.size(); i++) {
// trim and remove html
String clean = HelperMethods.removeHTML(asList.get(i).toString().trim());
logger.debug(clean);
valuesForKey.add(clean);
}
}else{
for (int i = 0; i < elementsToConsider; i++) {
String clean = HelperMethods.removeHTML(asList.get(i).toString().trim());
logger.debug(clean);
valuesForKey.add(clean);
}
}
@ -294,28 +289,28 @@ public abstract class HelperMethods {
}
/**
* Return the context in which the user wants to publish by the status information
* @param status
* @param contextServlet
* @return
*/
public static String getContextFromStatus(Status status, ServletContext contextServlet) {
String toReturn = null;
switch(status){
case Confirmed :
toReturn = (String)contextServlet.getInitParameter(CONFIRMED_CONTEX_KEY);
break;
case Pending:
toReturn = (String)contextServlet.getInitParameter(PENDING_CONTEX_KEY);
break;
default: break;
}
logger.debug("Context evaluated is " + toReturn);
return toReturn;
}
// /**
// * Return the context in which the user wants to publish by the status information
// * @param status
// * @param contextServlet
// * @return
// */
// public static String getContextFromStatus(Status status, ServletContext contextServlet) {
//
// String toReturn = null;
// switch(status){
// case Confirmed :
// toReturn = (String)contextServlet.getInitParameter(CONFIRMED_CONTEX_KEY);
// break;
// case Pending:
// toReturn = (String)contextServlet.getInitParameter(PENDING_CONTEX_KEY);
// break;
// default: break;
//
// }
// logger.debug("Context evaluated is " + toReturn);
// return toReturn;
// }
/**
* Validate the name the product will have
@ -439,7 +434,7 @@ public abstract class HelperMethods {
* @param resources
* @return
*/
public static void getResourcesFromBean(Common record, String username, List<String> tags, List<String> groups, List<ResourceBean> resources){
public static void getResourcesFromBean(Base record, String username, Set<String> tags, Set<String> groups, List<ResourceBean> resources){
Class<?> current = record.getClass();
do{

View File

@ -0,0 +1,49 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils.groups;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
/**
* The type of record, i.e. Stock or Fishery
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
public enum Record_Type {
FISHERY("Fishery"),
STOCK("Stock");
private String subGroupNameOrig;
private Record_Type(String origName) {
this.subGroupNameOrig = origName;
}
/**
* Return the original name
* @return
*/
public String getOrigName(){
return subGroupNameOrig;
}
@JsonValue
public String onSerialize(){
return subGroupNameOrig.toLowerCase();
}
@JsonCreator
public static Record_Type onDeserialize(String recordTypeString) {
if(recordTypeString != null) {
for(Record_Type source : Record_Type.values()) {
if (source.toString().equalsIgnoreCase(recordTypeString.trim()))
return source;
}
}
return null;
}
@Override
public String toString() {
return getOrigName();
}
}

View File

@ -10,15 +10,16 @@ import com.fasterxml.jackson.annotation.JsonValue;
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@Group
public enum Source {
public enum Sources {
FIRMS("FIRMS"),
RAM("RAM"),
FISHSOURCE("FishSource");
FISHSOURCE("FishSource"),
GRSF("GRSF");
private String subGroupNameOrig;
private Source(String origName) {
private Sources(String origName) {
this.subGroupNameOrig = origName;
}
@ -36,9 +37,9 @@ public enum Source {
}
@JsonCreator
public static Source onDeserialize(String sourceString) {
public static Sources onDeserialize(String sourceString) {
if(sourceString != null) {
for(Source source : Source.values()) {
for(Sources source : Sources.values()) {
if (source.toString().equalsIgnoreCase(sourceString.trim()))
return source;
}

View File

@ -22,7 +22,7 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNot
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.CommonAggregated;
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.input.TimeSeriesBean;
@ -46,7 +46,7 @@ public class ManageTimeSeriesThread extends Thread{
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ManageTimeSeriesThread.class);
private Common record;
private CommonAggregated record;
private String packageName;
private String username;
private DataCatalogue catalogue;
@ -60,7 +60,7 @@ public class ManageTimeSeriesThread extends Thread{
* @param catalogue
* @param context
*/
public ManageTimeSeriesThread(Common record, String packageName,
public ManageTimeSeriesThread(CommonAggregated record, String packageName,
String username, DataCatalogue catalogue, String context, String token) {
super();
this.record = record;
@ -122,7 +122,7 @@ public class ManageTimeSeriesThread extends Thread{
* @throws WorkspaceFolderNotFoundException
* @throws ItemNotFoundException
*/
public static void manageTimeSeries(Common record, String packageName, String username, DataCatalogue catalogue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, WorkspaceFolderNotFoundException, InternalErrorException, HomeNotFoundException, UserNotFoundException, ItemNotFoundException{
public static void manageTimeSeries(CommonAggregated record, String packageName, String username, DataCatalogue catalogue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, WorkspaceFolderNotFoundException, InternalErrorException, HomeNotFoundException, UserNotFoundException, ItemNotFoundException{
if(record == null)
throw new IllegalArgumentException("The given record is null!!");

View File

@ -13,7 +13,7 @@ import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord;
import org.gcube.data_catalogue.grsf_publish_ws.services.GrsfPublisherFisheryService;
import org.gcube.data_catalogue.grsf_publish_ws.services.GrsfPublisherStockService;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Fishery_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Stock_Type;
import org.glassfish.jersey.server.ResourceConfig;
@ -36,7 +36,7 @@ public class JJerseyTest extends JerseyTest{
recordFishery.setLicense("a caso una lincense");
recordFishery.setDataOwner("data owner");
recordFishery.setType(Fishery_Type.Fishing_Description);
recordFishery.setDatabaseSources(new ArrayList<Resource<Source>>(1));
recordFishery.setDatabaseSources(new ArrayList<Resource<Sources>>(1));
recordFishery.setSourceOfInformation(new ArrayList<Resource<String>>(1));
recordFishery.setStatus(Status.Pending);
Response res = target("fishery").path("/publish-product").request().post(Entity.entity(recordFishery, MediaType.APPLICATION_JSON));

View File

@ -2,10 +2,8 @@ package org.gcube.data_catalogue.grsf_publish_ws;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
@ -13,10 +11,8 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.homelibrary.home.HomeLibrary;
@ -32,7 +28,7 @@ import org.gcube.common.scope.api.ScopeProvider;
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.json.input.Common;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.CommonAggregated;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Resource;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord;
@ -41,7 +37,7 @@ import org.gcube.data_catalogue.grsf_publish_ws.utils.CSVHelpers;
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.Fishery_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Sources;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Status;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
@ -51,14 +47,8 @@ import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.trentorise.opendata.jackan.internal.org.apache.http.HttpResponse;
import eu.trentorise.opendata.jackan.internal.org.apache.http.client.methods.HttpGet;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.CloseableHttpClient;
import eu.trentorise.opendata.jackan.internal.org.apache.http.impl.client.HttpClientBuilder;
public class JTests {
private static final String TEST_TOKEN = "";
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(JTests.class);
//@Test
@ -66,7 +56,7 @@ public class JTests {
FisheryRecord recordFishery = new FisheryRecord();
recordFishery.setType(Fishery_Type.Fishing_Description);
recordFishery.setDatabaseSources(new ArrayList<Resource<Source>>());
recordFishery.setDatabaseSources(new ArrayList<Resource<Sources>>());
recordFishery.setStatus(Status.Pending);
List<String> tags = new ArrayList<String>();
@ -143,7 +133,7 @@ public class JTests {
// @Test
public void testJSONMapping() throws IOException{
FisheryRecord record = new FisheryRecord();
// FisheryRecord record = new FisheryRecord();
// record.setType(Type.Assessment_Unit);
// record.setFisheryId("sajhdskajda");
// record.setScientificName("assadsadada");
@ -221,40 +211,6 @@ public class JTests {
// System.out.println(converted);
}
//@Test
public void instanciateGCoreReader() throws Exception{
String baseUrl = org.gcube.data_catalogue.grsf_publish_ws.utils.GcoreEndpointReaderSocialWS.getUrlSocialWS("/gcube/devNext/NextNext");
System.out.println("Base url is " + baseUrl);
try(CloseableHttpClient client = HttpClientBuilder.create().build();){
String url = baseUrl.replace("80", "") + "/users/getUserEmail?gcube-token=" + TEST_TOKEN;
System.out.println("Request url is " + url);
HttpGet getRequest = new HttpGet(url);
HttpResponse response = client.execute(getRequest);
if (response.getStatusLine().getStatusCode() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatusLine().getStatusCode());
}
BufferedReader br = new BufferedReader(
new InputStreamReader((response.getEntity().getContent())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
System.out.println(response.toString());
}catch(Exception e){
System.err.println("error while performing post method " + e.toString());
}
}
//@Test
public void testFromScopeToOrgName(){
@ -528,7 +484,7 @@ public class JTests {
//@Test
public void fatherGroupAnnotation(){
Common commonRecord = new Common();
CommonAggregated commonRecord = new CommonAggregated();
commonRecord.setStatus(Status.Confirmed);
commonRecord.getStatus();
@ -567,7 +523,7 @@ public class JTests {
AssociationToGroupThread.findHierarchy(uniqueGroups, catalogue, catalogue.getApiKeyFromUsername("costantino_perciante"));
logger.debug("Hierarchy is " + uniqueGroups);
}
//@Test
public void testAssociationThread() throws InterruptedException{
String name = "low-abundance";
@ -577,7 +533,7 @@ public class JTests {
threadGroups.join();
logger.info("Thread stopped!");
}
}