Defined new annotation for custom fields to map them in extras fields. Defined methods to retrieve custom fields, tags and groups at run time.
git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@132956 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
e940cb5abb
commit
b7f35033c9
|
@ -0,0 +1,18 @@
|
||||||
|
package org.gcube.data_catalogue.grsf_publish_ws.custom_annotations;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom field annotation
|
||||||
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||||
|
*/
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
@Target(ElementType.FIELD)
|
||||||
|
public @interface CustomField {
|
||||||
|
|
||||||
|
public String key() default "";
|
||||||
|
|
||||||
|
}
|
|
@ -4,13 +4,13 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
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.Group;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
|
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.data_catalogue.grsf_publish_ws.utils.groups.Status;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type;
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type;
|
||||||
|
|
||||||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
|
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -25,11 +25,11 @@ public class Common {
|
||||||
@JsonProperty("license")
|
@JsonProperty("license")
|
||||||
private String license;
|
private String license;
|
||||||
|
|
||||||
@JsonIgnoreProperties(ignoreUnknown=true) // ignore in serialization/deserialization
|
@JsonProperty("author") // @JsonIgnoreProperties(ignoreUnknown=true) // ignore in serialization/deserialization
|
||||||
private String author; // this will be filled with the username of the token's owner
|
private String author;
|
||||||
|
|
||||||
@JsonProperty("version")
|
@JsonProperty("version")
|
||||||
private long version;
|
private Long version;
|
||||||
|
|
||||||
@JsonProperty("author_contact")
|
@JsonProperty("author_contact")
|
||||||
private String authorContact;
|
private String authorContact;
|
||||||
|
@ -41,34 +41,41 @@ public class Common {
|
||||||
private String maintainerContact;
|
private String maintainerContact;
|
||||||
|
|
||||||
@JsonProperty("catches_or_landings")
|
@JsonProperty("catches_or_landings")
|
||||||
|
@CustomField(key="Catches or landings")
|
||||||
private String catchesOrLandings;
|
private String catchesOrLandings;
|
||||||
|
|
||||||
@JsonProperty("database_sources")
|
@JsonProperty("database_sources")
|
||||||
@Group
|
@Group
|
||||||
|
@CustomField(key="Database sources")
|
||||||
private Source databaseSources;
|
private Source databaseSources;
|
||||||
|
|
||||||
@JsonProperty("source_of_information")
|
@JsonProperty("source_of_information")
|
||||||
|
@CustomField(key="Source of information")
|
||||||
private String sourceOfInformation;
|
private String sourceOfInformation;
|
||||||
|
|
||||||
@JsonProperty("data_owner")
|
@JsonProperty("data_owner")
|
||||||
|
@CustomField(key="Data owner")
|
||||||
private String dataOwner;
|
private String dataOwner;
|
||||||
|
|
||||||
@JsonProperty("type")
|
@JsonProperty("type")
|
||||||
@Tag
|
@Tag
|
||||||
@Group
|
@Group
|
||||||
|
@CustomField(key="Type")
|
||||||
private Type type;
|
private Type type;
|
||||||
|
|
||||||
@JsonProperty("status")
|
@JsonProperty("status")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Status")
|
||||||
private Status status;
|
private Status status;
|
||||||
|
|
||||||
@JsonProperty("resources")
|
@JsonProperty("resources")
|
||||||
private List<Resource> resources;
|
private List<Resource> resources;
|
||||||
|
|
||||||
@JsonProperty("extras")
|
@JsonProperty("extras")
|
||||||
private Map<String, Object> extras = new HashMap<>();
|
private Map<String, String> extras = new HashMap<>();
|
||||||
|
|
||||||
@JsonProperty("spatial")
|
@JsonProperty("spatial")
|
||||||
|
@CustomField(key="Spatial")
|
||||||
private String spatial;
|
private String spatial;
|
||||||
|
|
||||||
public Common() {
|
public Common() {
|
||||||
|
@ -76,11 +83,11 @@ public class Common {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Common(String description, String license, String author,
|
public Common(String description, String license, String author,
|
||||||
long version, String authorContact, String maintainer,
|
Long version, String authorContact, String maintainer,
|
||||||
String maintainerContact, String catchesOrLandings,
|
String maintainerContact, String catchesOrLandings,
|
||||||
Source databaseSources, String sourceOfInformation,
|
Source databaseSources, String sourceOfInformation,
|
||||||
String dataOwner, Type type, Status status,
|
String dataOwner, Type type, Status status,
|
||||||
List<Resource> resources, Map<String, Object> extras,
|
List<Resource> resources, Map<String, String> extras,
|
||||||
String spatial) {
|
String spatial) {
|
||||||
super();
|
super();
|
||||||
this.description = description;
|
this.description = description;
|
||||||
|
@ -133,11 +140,11 @@ public class Common {
|
||||||
this.author = author;
|
this.author = author;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getVersion() {
|
public Long getVersion() {
|
||||||
return version;
|
return version;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setVersion(long version) {
|
public void setVersion(Long version) {
|
||||||
this.version = version;
|
this.version = version;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,11 +228,11 @@ public class Common {
|
||||||
this.resources = resources;
|
this.resources = resources;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setProperties(Map<String, Object> extras) {
|
public void setProperties(Map<String, String> extras) {
|
||||||
this.extras = extras;
|
this.extras = extras;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Map<String, Object> getExtras() {
|
public Map<String, String> getExtras() {
|
||||||
return extras;
|
return extras;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
|
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
|
||||||
|
|
||||||
|
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.Group;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
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.Production_System_Type;
|
||||||
|
@ -16,35 +17,44 @@ public class FisheryRecord extends Common{
|
||||||
private String fisheryName;
|
private String fisheryName;
|
||||||
|
|
||||||
@JsonProperty("fishery_id")
|
@JsonProperty("fishery_id")
|
||||||
|
@CustomField(key="Fishery id")
|
||||||
private String fisheryId;
|
private String fisheryId;
|
||||||
|
|
||||||
@JsonProperty("scientific_name")
|
@JsonProperty("scientific_name")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Scientific name")
|
||||||
private String scientificName;
|
private String scientificName;
|
||||||
|
|
||||||
@JsonProperty("fishing_area")
|
@JsonProperty("fishing_area")
|
||||||
|
@CustomField(key="Fishing area")
|
||||||
private String fishingArea;
|
private String fishingArea;
|
||||||
|
|
||||||
@JsonProperty("exploiting_stocks")
|
@JsonProperty("exploiting_stocks")
|
||||||
|
@CustomField(key="Exploiting stocks")
|
||||||
private String exploitingStocks;
|
private String exploitingStocks;
|
||||||
|
|
||||||
@JsonProperty("management_entity")
|
@JsonProperty("management_entity")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Management entity")
|
||||||
private String managementEntity;
|
private String managementEntity;
|
||||||
|
|
||||||
@JsonProperty("jurisdiction_area")
|
@JsonProperty("jurisdiction_area")
|
||||||
|
@CustomField(key="Jurisdiction Area")
|
||||||
private String jurisdictionArea;
|
private String jurisdictionArea;
|
||||||
|
|
||||||
@JsonProperty("production_system_type")
|
@JsonProperty("production_system_type")
|
||||||
@Group
|
@Group
|
||||||
|
@CustomField(key="Production system type")
|
||||||
private Production_System_Type productionSystemType;
|
private Production_System_Type productionSystemType;
|
||||||
|
|
||||||
@JsonProperty("flag_state")
|
@JsonProperty("flag_state")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Flag state")
|
||||||
private String flagState;
|
private String flagState;
|
||||||
|
|
||||||
@JsonProperty("fishing_gear")
|
@JsonProperty("fishing_gear")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Fishing gear")
|
||||||
private String fishingGear;
|
private String fishingGear;
|
||||||
|
|
||||||
public FisheryRecord() {
|
public FisheryRecord() {
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
|
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
|
||||||
|
|
||||||
|
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.Group;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
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.Abundance_Level;
|
||||||
|
@ -17,46 +18,59 @@ public class StockRecord extends Common{
|
||||||
private String stockName;
|
private String stockName;
|
||||||
|
|
||||||
@JsonProperty("stock_id")
|
@JsonProperty("stock_id")
|
||||||
|
@CustomField(key="Stock Id")
|
||||||
private String stockID;
|
private String stockID;
|
||||||
|
|
||||||
@JsonProperty("species_scientific_name")
|
@JsonProperty("species_scientific_name")
|
||||||
@Tag
|
@Tag
|
||||||
|
@CustomField(key="Species scientific name")
|
||||||
private String speciesScientificName;
|
private String speciesScientificName;
|
||||||
|
|
||||||
@JsonProperty("area")
|
@JsonProperty("area")
|
||||||
|
@CustomField(key="Area")
|
||||||
private String area;
|
private String area;
|
||||||
|
|
||||||
@JsonProperty("exploiting_fishery")
|
@JsonProperty("exploiting_fishery")
|
||||||
|
@CustomField(key="Exploiting fishery")
|
||||||
private String exploitingFishery;
|
private String exploitingFishery;
|
||||||
|
|
||||||
@JsonProperty("management_entity")
|
@JsonProperty("management_entity")
|
||||||
|
@CustomField(key="Management entity")
|
||||||
private String managementEntity;
|
private String managementEntity;
|
||||||
|
|
||||||
@JsonProperty("assessment_methods")
|
@JsonProperty("assessment_methods")
|
||||||
|
@CustomField(key="Assessment methods")
|
||||||
private String assessmentMethods;
|
private String assessmentMethods;
|
||||||
|
|
||||||
@JsonProperty("state_of_marine_resource")
|
@JsonProperty("state_of_marine_resource")
|
||||||
|
@CustomField(key="State of marine resources")
|
||||||
private String stateOfMarineResource;
|
private String stateOfMarineResource;
|
||||||
|
|
||||||
@JsonProperty("exploitation_rate")
|
@JsonProperty("exploitation_rate")
|
||||||
|
@CustomField(key="Exploitation rate")
|
||||||
@Group
|
@Group
|
||||||
private Exploitation_Rate exploitationRate;
|
private Exploitation_Rate exploitationRate;
|
||||||
|
|
||||||
@JsonProperty("abundance_level")
|
@JsonProperty("abundance_level")
|
||||||
|
@CustomField(key="Abundance level")
|
||||||
@Group
|
@Group
|
||||||
private Abundance_Level abundanceLevel;
|
private Abundance_Level abundanceLevel;
|
||||||
|
|
||||||
@JsonProperty("narrative_state_and_trend")
|
@JsonProperty("narrative_state_and_trend")
|
||||||
|
@CustomField(key="Narrative state and trend")
|
||||||
private String narrativeStateAndTrend;
|
private String narrativeStateAndTrend;
|
||||||
|
|
||||||
@JsonProperty("scientific_advice")
|
@JsonProperty("scientific_advice")
|
||||||
|
@CustomField(key="Scientific advice")
|
||||||
private String scientificAdvice;
|
private String scientificAdvice;
|
||||||
|
|
||||||
@JsonProperty("reporting_entity")
|
@JsonProperty("reporting_entity")
|
||||||
|
@CustomField(key="Reporting entity")
|
||||||
private String reportingEntity;
|
private String reportingEntity;
|
||||||
|
|
||||||
@JsonProperty("reporting_year")
|
@JsonProperty("reporting_year")
|
||||||
private long reportingYear;
|
@CustomField(key="Reporting year")
|
||||||
|
private Long reportingYear;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
@ -87,7 +101,7 @@ public class StockRecord extends Common{
|
||||||
String assessmentMethods, String stateOfMarineResource,
|
String assessmentMethods, String stateOfMarineResource,
|
||||||
Exploitation_Rate exploitationRate, Abundance_Level abundanceLevel,
|
Exploitation_Rate exploitationRate, Abundance_Level abundanceLevel,
|
||||||
String narrativeStateAndTrend, String scientificAdvice,
|
String narrativeStateAndTrend, String scientificAdvice,
|
||||||
String reportingEntity, long reportingYear) {
|
String reportingEntity, Long reportingYear) {
|
||||||
super();
|
super();
|
||||||
this.stockName = stockName;
|
this.stockName = stockName;
|
||||||
this.stockID = stockID;
|
this.stockID = stockID;
|
||||||
|
@ -209,11 +223,11 @@ public class StockRecord extends Common{
|
||||||
this.reportingEntity = reportingEntity;
|
this.reportingEntity = reportingEntity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getReportingYear() {
|
public Long getReportingYear() {
|
||||||
return reportingYear;
|
return reportingYear;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setReportingYear(long reportingYear) {
|
public void setReportingYear(Long reportingYear) {
|
||||||
this.reportingYear = reportingYear;
|
this.reportingYear = reportingYear;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,23 @@
|
||||||
package org.gcube.data_catalogue.grsf_publish_ws.services;
|
package org.gcube.data_catalogue.grsf_publish_ws.services;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.ws.rs.Consumes;
|
import javax.ws.rs.Consumes;
|
||||||
import javax.ws.rs.POST;
|
import javax.ws.rs.POST;
|
||||||
import javax.ws.rs.Path;
|
import javax.ws.rs.Path;
|
||||||
import javax.ws.rs.Produces;
|
import javax.ws.rs.Produces;
|
||||||
import javax.ws.rs.core.MediaType;
|
import javax.ws.rs.core.MediaType;
|
||||||
import javax.ws.rs.core.Response;
|
import javax.ws.rs.core.Response;
|
||||||
|
import javax.ws.rs.core.Response.Status;
|
||||||
|
|
||||||
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
||||||
|
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.StockRecord;
|
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.datacatalogue.ckanutillibrary.DataCatalogue;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -16,21 +26,55 @@ import org.slf4j.LoggerFactory;
|
||||||
*/
|
*/
|
||||||
@Path("stock/")
|
@Path("stock/")
|
||||||
public class GrsfPublisherStockService {
|
public class GrsfPublisherStockService {
|
||||||
|
|
||||||
// Logger
|
// Logger
|
||||||
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
|
||||||
|
|
||||||
@POST
|
@POST
|
||||||
@Path("publish-product")
|
@Path("publish-product")
|
||||||
@Consumes(MediaType.APPLICATION_JSON)
|
@Consumes(MediaType.APPLICATION_JSON)
|
||||||
@Produces(MediaType.APPLICATION_JSON)
|
@Produces(MediaType.APPLICATION_JSON)
|
||||||
public Response publishStock(StockRecord record){
|
public Response publishStock(StockRecord record){
|
||||||
|
|
||||||
|
// retrieve context and username
|
||||||
|
Caller caller = AuthorizationProvider.instance.get();
|
||||||
|
String username = caller.getClient().getId();
|
||||||
|
String context = ScopeProvider.instance.get();
|
||||||
|
|
||||||
|
logger.info("Incoming request for creating a stock record by user with id " + username);
|
||||||
|
|
||||||
logger.info("Incoming request for creating a stock record");
|
ResponseCreationBean responseBean = new ResponseCreationBean();
|
||||||
|
Status status = Status.CREATED;
|
||||||
|
String id = "";
|
||||||
|
|
||||||
|
try{
|
||||||
|
|
||||||
|
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
|
||||||
|
|
||||||
|
// check the user has editor/admin role into the org TODO pending or accepted?
|
||||||
|
|
||||||
|
// check the record has a name, at least
|
||||||
|
|
||||||
|
// evaluate the tags of the product
|
||||||
|
|
||||||
|
// evaluate the resources
|
||||||
|
|
||||||
|
// evaluate tags and groups using reflection
|
||||||
|
List<String> tags = new ArrayList<String>();
|
||||||
|
List<String> groups = new ArrayList<String>();
|
||||||
|
|
||||||
|
// create the product
|
||||||
|
//catalogue.createCKanDataset(apiKey, title, organizationNameOrId, author, authorMail, maintainer, maintainerMail, version, description, licenseId, tags, customFields, resources, setPublic)
|
||||||
|
|
||||||
|
|
||||||
// TODO
|
}catch(Exception e){
|
||||||
return null;
|
logger.error("Failed to create stock record", e);
|
||||||
|
status = Status.INTERNAL_SERVER_ERROR;
|
||||||
|
responseBean.setError(e.toString());
|
||||||
|
responseBean.setId(id);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Response.status(status).entity(responseBean).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,66 @@
|
||||||
|
package org.gcube.data_catalogue.grsf_publish_ws.utils;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
||||||
|
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Associate the dataset to a group.
|
||||||
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
||||||
|
*/
|
||||||
|
public class AssociationToGroupThread extends Thread {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(AssociationToGroupThread.class);
|
||||||
|
|
||||||
|
private List<String> groupsTitles;
|
||||||
|
private String datasetId;
|
||||||
|
private String username;
|
||||||
|
private DataCatalogue catalogue;
|
||||||
|
private String organization;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param groupTitle
|
||||||
|
* @param datasetId
|
||||||
|
* @param username
|
||||||
|
* @param catalogue
|
||||||
|
*/
|
||||||
|
public AssociationToGroupThread(List<String> groupsTitles, String datasetId,
|
||||||
|
String username, DataCatalogue catalogue, String organization) {
|
||||||
|
this.groupsTitles = groupsTitles;
|
||||||
|
this.datasetId = datasetId;
|
||||||
|
this.username = username;
|
||||||
|
this.catalogue = catalogue;
|
||||||
|
this.organization = organization;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
|
||||||
|
logger.info("Association thread started to put the dataset with id="+ datasetId + " into group with title " + groupsTitles + " for user " + username);
|
||||||
|
|
||||||
|
// create the group
|
||||||
|
for (String groupTitle : groupsTitles) {
|
||||||
|
|
||||||
|
logger.debug("Group exists, going to add the user " + username + " as its admin...");
|
||||||
|
|
||||||
|
// retrieve the role to be assigned according the one the user has into the organization of the dataset
|
||||||
|
RolesCkanGroupOrOrg role = RolesCkanGroupOrOrg.valueOf(catalogue.getRoleOfUserInOrganization(username, organization, catalogue.getApiKeyFromUsername(username)).toUpperCase());
|
||||||
|
|
||||||
|
boolean assigned = catalogue.checkRoleIntoGroup(username, groupTitle, role);
|
||||||
|
|
||||||
|
if(assigned){
|
||||||
|
|
||||||
|
logger.debug("Admin/editor role was assigned for this group, going to associate the product to the group");
|
||||||
|
|
||||||
|
boolean putIntoGroup = catalogue.assignDatasetToGroup(groupTitle, datasetId, catalogue.getApiKeyFromUsername(username));
|
||||||
|
logger.info("Was product put into group? " + putIntoGroup);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,7 +1,17 @@
|
||||||
package org.gcube.data_catalogue.grsf_publish_ws.utils;
|
package org.gcube.data_catalogue.grsf_publish_ws.utils;
|
||||||
|
|
||||||
|
import java.beans.PropertyDescriptor;
|
||||||
|
import java.lang.reflect.Field;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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.datacatalogue.ckanutillibrary.DataCatalogue;
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
||||||
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -10,29 +20,107 @@ import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
|
||||||
*/
|
*/
|
||||||
public abstract class HelperMethods {
|
public abstract class HelperMethods {
|
||||||
|
|
||||||
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(HelperMethods.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert a group name to its id on ckan
|
* Convert a group name to its id on ckan
|
||||||
* @param origName
|
* @param origName
|
||||||
* @return
|
* @return
|
||||||
*/
|
*/
|
||||||
public static String getGroupNameOnCkan(String origName){
|
public static String getGroupNameOnCkan(String origName){
|
||||||
|
|
||||||
if(origName == null)
|
if(origName == null)
|
||||||
throw new IllegalArgumentException("origName cannot be null");
|
throw new IllegalArgumentException("origName cannot be null");
|
||||||
|
|
||||||
return origName.trim().toLowerCase().replaceAll("[^A-Za-z0-9_.]", "_");
|
return origName.trim().toLowerCase().replaceAll("[^A-Za-z0-9_.]", "_");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Retrieve the running instance of the data catalogue for this scope
|
* Retrieve the running instance of the data catalogue for this scope
|
||||||
* @return
|
* @return
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
|
public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
|
||||||
|
|
||||||
return DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
|
return DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the list of tags for this object
|
||||||
|
*/
|
||||||
|
public static void getTags(List<String> tags, Common record){
|
||||||
|
Class<?> current = record.getClass();
|
||||||
|
do{
|
||||||
|
Field[] fields = current.getDeclaredFields();
|
||||||
|
for (Field field : fields) {
|
||||||
|
if(field.isAnnotationPresent(Tag.class)){
|
||||||
|
try{
|
||||||
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
||||||
|
if(f != null){
|
||||||
|
|
||||||
|
tags.add(f.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while((current = current.getSuperclass())!=null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the list of groups for this object
|
||||||
|
*/
|
||||||
|
public static void getGroups(List<String> groups, Common record){
|
||||||
|
Class<?> current = record.getClass();
|
||||||
|
do{
|
||||||
|
Field[] fields = current.getDeclaredFields();
|
||||||
|
for (Field field : fields) {
|
||||||
|
if(field.isAnnotationPresent(Group.class)){
|
||||||
|
try{
|
||||||
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
||||||
|
if(f != null){
|
||||||
|
|
||||||
|
groups.add(getGroupNameOnCkan(f.toString()));
|
||||||
|
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while((current = current.getSuperclass())!=null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Retrieve the list of extras for this object
|
||||||
|
*/
|
||||||
|
public static void getExtras(Map<String, Object> extras, Common record){
|
||||||
|
Class<?> current = record.getClass();
|
||||||
|
do{
|
||||||
|
Field[] fields = current.getDeclaredFields();
|
||||||
|
for (Field field : fields) {
|
||||||
|
if(field.isAnnotationPresent(CustomField.class)){
|
||||||
|
try{
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
}catch(Exception e){
|
||||||
|
logger.error("Failed ot read value for field " + field.getName() + " skipping", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while((current = current.getSuperclass())!=null);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,5 +54,10 @@ public enum Abundance_Level {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,4 +54,9 @@ public enum Exploitation_Rate {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,5 +59,10 @@ public enum Production_System_Type {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -54,4 +54,9 @@ public enum Source {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -53,5 +53,10 @@ public enum Status {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,5 +56,10 @@ public enum Type {
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getOrigName();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,42 +4,87 @@ import java.beans.IntrospectionException;
|
||||||
import java.beans.PropertyDescriptor;
|
import java.beans.PropertyDescriptor;
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
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.Group;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord;
|
||||||
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level;
|
||||||
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Source;
|
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.data_catalogue.grsf_publish_ws.utils.groups.Type;
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Type;
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
public class JTests {
|
public class JTests {
|
||||||
|
|
||||||
@Test
|
//@Test
|
||||||
public void test() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException {
|
public void test() throws IllegalArgumentException, IllegalAccessException, InvocationTargetException, IntrospectionException {
|
||||||
|
|
||||||
// Common instance = new Common();
|
|
||||||
// instance.setType(Type.Assesment_Unit);
|
|
||||||
FisheryRecord recordFishery = new FisheryRecord();
|
FisheryRecord recordFishery = new FisheryRecord();
|
||||||
recordFishery.setType(Type.Fishing_Description);
|
recordFishery.setType(Type.Fishing_Description);
|
||||||
recordFishery.setDatabaseSources(Source.FIRMS);
|
recordFishery.setDatabaseSources(Source.FIRMS);
|
||||||
// Class<Common> commonClass = Common.class;
|
recordFishery.setStatus(Status.Pending);
|
||||||
Class<FisheryRecord> fisheryClass = FisheryRecord.class;
|
|
||||||
|
|
||||||
// bottom up, looks up for Tag fields
|
List<String> tags = new ArrayList<String>();
|
||||||
Class<?> current = fisheryClass;
|
List<String> groupsTitles = new ArrayList<String>();
|
||||||
|
Map<String, Object> extras = new HashMap<String, Object>();
|
||||||
|
|
||||||
|
// bottom up, looks up for Tag/Group fields
|
||||||
|
Class<?> current = recordFishery.getClass();
|
||||||
do{
|
do{
|
||||||
// do something with current's fields
|
|
||||||
System.out.println("Class is " + current.getCanonicalName());
|
System.out.println("Class is " + current.getCanonicalName());
|
||||||
Field[] fields = current.getDeclaredFields();
|
Field[] fields = current.getDeclaredFields();
|
||||||
|
|
||||||
for (Field field : fields) {
|
for (Field field : fields) {
|
||||||
if(field.isAnnotationPresent(Tag.class) || field.isAnnotationPresent(Group.class)){
|
if(field.isAnnotationPresent(Tag.class)){
|
||||||
System.out.println("Annotated field is " + field.getName());
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery);
|
||||||
Object f = new PropertyDescriptor(field.getName(), fisheryClass).getReadMethod().invoke(recordFishery);
|
if(f != null){
|
||||||
System.out.println("Field is " + field.getName() + " and its value for instance is " + f);
|
|
||||||
|
tags.add(f.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(field.isAnnotationPresent(Group.class)){
|
||||||
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery);
|
||||||
|
if(f != null){
|
||||||
|
|
||||||
|
groupsTitles.add(f.toString());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if(field.isAnnotationPresent(CustomField.class)){
|
||||||
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(recordFishery);
|
||||||
|
if(f != null){
|
||||||
|
|
||||||
|
// get the key to put into the map first
|
||||||
|
extras.put(field.getAnnotation(CustomField.class).key(), f);
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
while((current = current.getSuperclass())!=null);
|
while((current = current.getSuperclass())!=null);
|
||||||
|
|
||||||
|
// print
|
||||||
|
System.out.println("TAGS " + tags);
|
||||||
|
System.out.println("GROUPS " + groupsTitles);
|
||||||
|
System.out.println("EXTRAS " + extras);
|
||||||
|
}
|
||||||
|
|
||||||
|
//@Test
|
||||||
|
public void testJsonSerializer(){
|
||||||
|
//
|
||||||
|
// String pendingAsString = "pending";
|
||||||
|
// Status resSatus = Status.onDeserialize(pendingAsString);
|
||||||
|
// System.out.println("Res is " + resSatus);
|
||||||
|
//
|
||||||
|
// System.out.println("To string is " + resSatus.onSerialize());
|
||||||
|
|
||||||
|
Abundance_Level type = Abundance_Level.onDeserialize("Uncertain Not assessed");
|
||||||
|
System.out.println("Res is " + type.onSerialize());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue