Several changes to the model

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@135170 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-01 14:37:26 +00:00
parent 41c877cda7
commit 62d111688e
14 changed files with 326 additions and 244 deletions

View File

@ -135,6 +135,12 @@
<artifactId>jersey-bean-validation</artifactId>
<version>${version.jersey}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>3.0-alpha-1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.test-framework.providers</groupId>
<artifactId>jersey-test-framework-provider-jetty</artifactId>

View File

@ -13,6 +13,4 @@ import java.lang.annotation.Target;
@Target({ElementType.FIELD, ElementType.TYPE})
public @interface Group {
Class parentGroup() default Class.class;
}

View File

@ -1,5 +1,6 @@
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;
@ -12,9 +13,14 @@ 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;
@ -22,9 +28,10 @@ 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", "extras", "product_type"}, ignoreUnknown = true) // ignore in serialization/deserialization
@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")
@ -58,15 +65,11 @@ public class Common {
@CkanResource
@NotNull(message="database_source cannot be null")
@Size(min=1, message="database_source cannot be empty")
@Tag
@Group
@Valid
private List<Resource<Source>> databaseSources;
@JsonProperty("source_of_information")
@CkanResource
@NotNull(message="source_of_information cannot be null")
@Size(min=1, message="source_of_information cannot be empty")
@Valid
private List<Resource<String>> sourceOfInformation;
@ -101,8 +104,15 @@ public class Common {
@Group
private String productType;
@JsonProperty("extras")
private Map<String, List<String>> extras = new HashMap<>();
@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();
@ -117,26 +127,27 @@ public class Common {
* @param authorContact
* @param maintainer
* @param maintainerContact
* @param catchesOrLandings
* @param dataOwner
* @param databaseSources
* @param sourceOfInformation
* @param dataOwner
* @param type
* @param shortTitle
* @param uuid
* @param traceabilityFlag
* @param extras
* @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 maintainerContact, String dataOwner,
List<Resource<Source>> databaseSources,
List<Resource<String>> sourceOfInformation, String dataOwner,
String shortTitle, String uuid,
boolean traceabilityFlag, Map<String, List<String>> extras,
Status status, String productType) {
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;
@ -145,21 +156,47 @@ public class Common {
this.authorContact = authorContact;
this.maintainer = maintainer;
this.maintainerContact = maintainerContact;
this.dataOwner = dataOwner;
this.databaseSources = databaseSources;
this.sourceOfInformation = sourceOfInformation;
this.dataOwner = dataOwner;
this.shortTitle = shortTitle;
this.uuid = uuid;
this.traceabilityFlag = traceabilityFlag;
this.extras = extras;
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;
}
@ -244,14 +281,6 @@ public class Common {
this.dataOwner = dataOwner;
}
public void setExtras(Map<String, List<String>> extras) {
this.extras = extras;
}
public Map<String, List<String>> getExtras() {
return extras;
}
public String getShortTitle() {
return shortTitle;
}
@ -284,6 +313,28 @@ public class Common {
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
@ -292,9 +343,10 @@ public class Common {
+ maintainer + ", maintainerContact=" + maintainerContact
+ ", dataOwner=" + dataOwner + ", databaseSources="
+ databaseSources + ", sourceOfInformation="
+ sourceOfInformation + ", shortTitle="
+ shortTitle + ", uuid=" + uuid + ", traceabilityFlag="
+ traceabilityFlag + ", extras=" + extras + ", status="
+ status + ", productType=" + productType + "]";
+ sourceOfInformation + ", shortTitle=" + shortTitle
+ ", uuid=" + uuid + ", traceabilityFlag=" + traceabilityFlag
+ ", status=" + status + ", productType=" + productType
+ ", reportingYear=" + reportingYear + ", extrasFields="
+ extrasFields + ", extrasResources=" + extrasResources + "]";
}
}

View File

@ -1,6 +1,5 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.Collections;
import java.util.List;
import javax.validation.Valid;
@ -40,11 +39,11 @@ public class FisheryRecord extends Common{
@JsonProperty("fishing_area")
@CustomField(key="Fishing area")
private String fishingArea;
private List<String> fishingArea;
@JsonProperty("exploited_stocks")
@CustomField(key="Exploited stocks")
private String exploitingStocks;
private List<String> exploitingStocks;
@JsonProperty("management_entity")
@Tag
@ -53,7 +52,7 @@ public class FisheryRecord extends Common{
@JsonProperty("jurisdiction_area")
@CustomField(key="Jurisdiction Area")
private String jurisdictionArea;
private List<String> jurisdictionArea;
@JsonProperty("production_system_type")
@Group
@ -108,8 +107,9 @@ public class FisheryRecord extends Common{
* @param type
*/
public FisheryRecord(String fisheryName, String fisheryId,
String scientificName, String fishingArea, String exploitingStocks,
String managementEntity, String jurisdictionArea,
String scientificName, List<String> fishingArea,
List<String> exploitingStocks, String managementEntity,
List<String> jurisdictionArea,
Production_System_Type productionSystemType, String flagState,
String fishingGear, String environment,
List<TimeSeriesBean<String, String>> catchesOrLandings,
@ -154,22 +154,14 @@ public class FisheryRecord extends Common{
this.scientificName = scientificName;
}
public String getFishingArea() {
public List<String> getFishingArea() {
return fishingArea;
}
public void setFishingArea(String fishingArea) {
public void setFishingArea(List<String> fishingArea) {
this.fishingArea = fishingArea;
}
public String getExploitingStocks() {
return exploitingStocks;
}
public void setExploitingStocks(String exploitingStocks) {
this.exploitingStocks = exploitingStocks;
}
public String getManagementEntity() {
return managementEntity;
}
@ -178,11 +170,11 @@ public class FisheryRecord extends Common{
this.managementEntity = managementEntity;
}
public String getJurisdictionArea() {
public List<String> getJurisdictionArea() {
return jurisdictionArea;
}
public void setJurisdictionArea(String jurisdictionArea) {
public void setJurisdictionArea(List<String> jurisdictionArea) {
this.jurisdictionArea = jurisdictionArea;
}
@ -224,10 +216,6 @@ public class FisheryRecord extends Common{
public void setCatchesOrLandings(List<TimeSeriesBean<String, String>> catchesOrLandings) {
this.catchesOrLandings = catchesOrLandings;
if(catchesOrLandings != null){
Collections.sort(catchesOrLandings);
}
}
public Fishery_Type getType() {
@ -250,4 +238,5 @@ public class FisheryRecord extends Common{
+ ", environment=" + environment + ", catchesOrLandings="
+ catchesOrLandings + ", type=" + type + "]";
}
}

View File

@ -66,7 +66,7 @@ public class Resource<T> {
@Override
public String toString() {
// in case of @Tag, we check the class of the element Name
// in case of @Tag/@Group, we check the class of the element Name
Class<? extends Object> nameClass = name.getClass();
if(nameClass.equals(Source.class))

View File

@ -1,6 +1,5 @@
package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import java.util.Collections;
import java.util.List;
import javax.validation.Valid;
@ -34,22 +33,20 @@ public class StockRecord extends Common{
@CustomField(key="Stock Id")
private String stockID;
@JsonProperty("species_scientific_name")
@CustomField(key="Species scientific name")
@JsonProperty("species")
@CustomField(key="Species")
@Tag
@NotNull(message="species_scientific_name cannot be null")
@Size(min=1, message="species_scientific_name cannot be empty")
private String speciesScientificName;
@Size(min=1, message="species cannot be empty")
private List<String> species;
@JsonProperty("assessment_distribution_area")
@CustomField(key="Assessment distribution area")
@NotNull(message="assessment distribution cannot be null")
@Size(min=1, message="assessment distribution area cannot be empty")
private String area;
private List<String> area;
@JsonProperty("exploiting_fishery")
@CustomField(key="Exploiting fishery")
private String exploitingFishery;
private List<String> exploitingFishery;
@JsonProperty("management_entity")
@CustomField(key="Management entity")
@ -63,25 +60,27 @@ public class StockRecord extends Common{
@CustomField(key="State of marine resources")
private String stateOfMarineResource;
@JsonProperty("exploitation_rate")
@CustomField(key="Exploitation Rate")
@JsonProperty("standard_exploitation_rate")
@CustomField(key="Standard Exploitation Rate")
@Tag
@Valid
private List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRate;
private List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRateStandard;
@JsonProperty("exploitation_rate")
@CustomField(key="Exploitation Rate")
@Valid
private List<TimeSeriesBean<String, String>> exploitationRate;
@JsonProperty("standard_abundance_level")
@CustomField(key="Standard Abundance Level")
@Tag
@Valid
private List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevelStandard;
@JsonProperty("abundance_level")
@CustomField(key="Abundance Level")
@Tag
@Valid
private List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevel;
@JsonProperty("exploitation_rate_for_grouping")
@Group
private Exploitation_Rate exploitationRateForGrouping;
@JsonProperty("abundance_level_for_grouping")
@Group
private Abundance_Level abundanceLevelForGrouping;
private List<TimeSeriesBean<String, String>> abundanceLevel;
@JsonProperty("narrative_state_and_trend")
@CustomField(key="Narrative state and trend")
@ -95,10 +94,6 @@ public class StockRecord extends Common{
@CustomField(key="Reporting entity")
private String reportingEntity;
@JsonProperty("reporting_year")
@CustomField(key="Reporting year")
private Long reportingYear;
@JsonProperty("stock_uri")
@CustomField(key="Stock Uri")
private String stockUri;
@ -121,16 +116,16 @@ public class StockRecord extends Common{
* Create a Stock element.
* @param stockName
* @param stockID
* @param speciesScientificName
* @param species
* @param area
* @param exploitingFishery
* @param managementEntity
* @param assessmentMethods
* @param stateOfMarineResource
* @param exploitationRateStandard
* @param exploitationRate
* @param abundanceLevelStandard
* @param abundanceLevel
* @param exploitationRateForGrouping
* @param abundanceLevelForGrouping
* @param narrativeStateAndTrend
* @param scientificAdvice
* @param reportingEntity
@ -139,34 +134,38 @@ public class StockRecord extends Common{
* @param waterArea
* @param type
*/
public StockRecord(String stockName, String stockID,
String speciesScientificName, String area,
String exploitingFishery, String managementEntity,
String assessmentMethods, String stateOfMarineResource,
List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRate,
List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevel,
Exploitation_Rate exploitationRateForGrouping,
Abundance_Level abundanceLevelForGrouping,
public StockRecord(
String stockName,
String stockID,
List<String> species,
List<String> area,
List<String> exploitingFishery,
String managementEntity,
String assessmentMethods,
String stateOfMarineResource,
List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRateStandard,
List<TimeSeriesBean<String, String>> exploitationRate,
List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevelStandard,
List<TimeSeriesBean<String, String>> abundanceLevel,
String narrativeStateAndTrend, String scientificAdvice,
String reportingEntity, Long reportingYear, String stockUri,
List<String> waterArea, Stock_Type type) {
super();
this.stockName = stockName;
this.stockID = stockID;
this.speciesScientificName = speciesScientificName;
this.species = species;
this.area = area;
this.exploitingFishery = exploitingFishery;
this.managementEntity = managementEntity;
this.assessmentMethods = assessmentMethods;
this.stateOfMarineResource = stateOfMarineResource;
this.exploitationRateStandard = exploitationRateStandard;
this.exploitationRate = exploitationRate;
this.abundanceLevelStandard = abundanceLevelStandard;
this.abundanceLevel = abundanceLevel;
this.exploitationRateForGrouping = exploitationRateForGrouping;
this.abundanceLevelForGrouping = abundanceLevelForGrouping;
this.narrativeStateAndTrend = narrativeStateAndTrend;
this.scientificAdvice = scientificAdvice;
this.reportingEntity = reportingEntity;
this.reportingYear = reportingYear;
this.stockUri = stockUri;
this.waterArea = waterArea;
this.type = type;
@ -180,24 +179,6 @@ public class StockRecord extends Common{
this.type = type;
}
public Exploitation_Rate getExploitationRateForGrouping() {
return exploitationRateForGrouping;
}
public void setExploitationRateForGrouping(
Exploitation_Rate exploitationRateForGrouping) {
this.exploitationRateForGrouping = exploitationRateForGrouping;
}
public Abundance_Level getAbundanceLevelForGrouping() {
return abundanceLevelForGrouping;
}
public void setAbundanceLevelForGrouping(
Abundance_Level abundanceLevelForGrouping) {
this.abundanceLevelForGrouping = abundanceLevelForGrouping;
}
public String getStockUri() {
return stockUri;
}
@ -230,27 +211,27 @@ public class StockRecord extends Common{
this.stockID = stockID;
}
public String getSpeciesScientificName() {
return speciesScientificName;
public List<String> getSpecies() {
return species;
}
public void setSpeciesScientificName(String speciesScientificName) {
this.speciesScientificName = speciesScientificName;
public void setSpecies(List<String> species) {
this.species = species;
}
public String getArea() {
public List<String> getArea() {
return area;
}
public void setArea(String area) {
public void setArea(List<String> area) {
this.area = area;
}
public String getExploitingFishery() {
public List<String> getExploitingFishery() {
return exploitingFishery;
}
public void setExploitingFishery(String exploitingFishery) {
public void setExploitingFishery(List<String> exploitingFishery) {
this.exploitingFishery = exploitingFishery;
}
@ -278,28 +259,31 @@ public class StockRecord extends Common{
this.stateOfMarineResource = stateOfMarineResource;
}
public List<TimeSeriesBean<Exploitation_Rate, Void>> getExploitationRate() {
return exploitationRate;
public List<TimeSeriesBean<Exploitation_Rate, Void>> getExploitationRateStandard() {
return exploitationRateStandard;
}
public void setExploitationRate(List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRate) {
this.exploitationRate = exploitationRate;
if(exploitationRate != null){
Collections.sort(exploitationRate);
}
public void setExploitationRateStandard(
List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRateStandard) {
this.exploitationRateStandard = exploitationRateStandard;
}
public List<TimeSeriesBean<Abundance_Level, Void>> getAbundanceLevel() {
public List<TimeSeriesBean<Abundance_Level, Void>> getAbundanceLevelStandard() {
return abundanceLevelStandard;
}
public void setAbundanceLevelStandard(
List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevelStandard) {
this.abundanceLevelStandard = abundanceLevelStandard;
}
public List<TimeSeriesBean<String, String>> getAbundanceLevel() {
return abundanceLevel;
}
public void setAbundanceLevel(List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevel) {
public void setAbundanceLevel(
List<TimeSeriesBean<String, String>> abundanceLevel) {
this.abundanceLevel = abundanceLevel;
if(abundanceLevel != null){
Collections.sort(abundanceLevel);
}
}
public String getNarrativeStateAndTrend() {
@ -326,31 +310,32 @@ public class StockRecord extends Common{
this.reportingEntity = reportingEntity;
}
public Long getReportingYear() {
return reportingYear;
public List<TimeSeriesBean<String, String>> getExploitationRate() {
return exploitationRate;
}
public void setReportingYear(Long reportingYear) {
this.reportingYear = reportingYear;
public void setExploitationRate(
List<TimeSeriesBean<String, String>> exploitationRate) {
this.exploitationRate = exploitationRate;
}
@Override
public String toString() {
return "StockRecord [stockName=" + stockName + ", stockID=" + stockID
+ ", speciesScientificName=" + speciesScientificName
+ ", area=" + area + ", exploitingFishery=" + exploitingFishery
+ ", species=" + species + ", area=" + area
+ ", exploitingFishery=" + exploitingFishery
+ ", managementEntity=" + managementEntity
+ ", assessmentMethods=" + assessmentMethods
+ ", stateOfMarineResource=" + stateOfMarineResource
+ ", exploitationRateStandard=" + exploitationRateStandard
+ ", exploitationRate=" + exploitationRate
+ ", abundanceLevelStandard=" + abundanceLevelStandard
+ ", abundanceLevel=" + abundanceLevel
+ ", exploitationRateForGrouping="
+ exploitationRateForGrouping + ", abundanceLevelForGrouping="
+ abundanceLevelForGrouping + ", narrativeStateAndTrend="
+ narrativeStateAndTrend + ", scientificAdvice="
+ scientificAdvice + ", reportingEntity=" + reportingEntity
+ ", reportingYear=" + reportingYear + ", stockUri=" + stockUri
+ ", waterArea=" + waterArea + ", type=" + type + "]";
+ ", narrativeStateAndTrend=" + narrativeStateAndTrend
+ ", scientificAdvice=" + scientificAdvice
+ ", reportingEntity=" + reportingEntity + ", stockUri="
+ stockUri + ", waterArea=" + waterArea + ", type=" + type
+ "]";
}
}

View File

@ -149,8 +149,8 @@ public class GrsfPublisherFisheryService {
// fishing area and jurisdiction area cannot be empty at the same time
String futureName = record.getUuid();
String futureTitle = record.getFisheryName();
String fishingArea = record.getFishingArea();
String jurisdictionArea = record.getJurisdictionArea();
List<String> fishingArea = record.getFishingArea();
List<String> jurisdictionArea = record.getJurisdictionArea();
if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST;
@ -186,10 +186,7 @@ public class GrsfPublisherFisheryService {
HelperMethods.getGroups(groups, record);
// evaluate the custom fields
Map<String, List<String>> customFields = new HashMap<String, List<String>>();
if(record.getExtras() != null)
customFields = record.getExtras();
Map<String, List<String>> customFields = record.getExtrasFields();
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
@ -207,7 +204,8 @@ public class GrsfPublisherFisheryService {
}else{
// evaluate the resources
List<ResourceBean> resources = HelperMethods.getResourcesFromBean(record, username, tags, groups);
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// check the license id
String license = null;
@ -225,7 +223,7 @@ public class GrsfPublisherFisheryService {
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),

View File

@ -175,10 +175,7 @@ public class GrsfPublisherStockService {
HelperMethods.getGroups(groups, record);
// evaluate the custom fields
Map<String, List<String>> customFields = new HashMap<String, List<String>>();
if(record.getExtras() != null)
customFields = record.getExtras();
Map<String, List<String>> customFields = record.getExtrasFields();
// automatically retrieve the other ones
HelperMethods.getExtras(customFields, record);
@ -197,7 +194,8 @@ public class GrsfPublisherStockService {
}else{
// evaluate the resources
List<ResourceBean> resources = HelperMethods.getResourcesFromBean(record, username, tags, groups);
List<ResourceBean> resources = record.getExtrasResources();
HelperMethods.getResourcesFromBean(record, username, tags, groups, resources);
// check the license id
String license = null;
@ -215,7 +213,7 @@ public class GrsfPublisherStockService {
catalogue.getApiKeyFromUsername(username),
futureTitle,
futureName,
organization,
organization,//"grsf", //TO DELETE TODO
authorFullname,
authorMail,
record.getMaintainer(),

View File

@ -169,11 +169,28 @@ public abstract class HelperMethods {
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
if(f != null){
if(f instanceof List<?>){
List asList = ((List) f);
if(!asList.isEmpty()){
logger.debug("The object annotated with @Tag/@Group is a list. Adding ... ");
// else add all the available elements
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);
}
}
}else{
// also convert to the group name that should be on ckan
String groupName = getGroupNameOnCkan(f.toString().trim());
if(!groups.contains(groupName))
groups.add(groupName);
}
}
// check if the field is an enumerator, and the enum class is also annotated with @Group
@ -187,14 +204,6 @@ public abstract class HelperMethods {
if(!groups.contains(groupName))
groups.add(groupName);
// check if this class has a father too (@Group annotation allows it)
Group groupAnnotationOnEnum = field.getType().getAnnotation(Group.class);
if(!groupAnnotationOnEnum.parentGroup().equals(Class.class)){
String groupNameFather = getGroupNameOnCkan(groupAnnotationOnEnum.parentGroup().getSimpleName());
if(!groups.contains(groupNameFather))
groups.add(groupNameFather);
}
}
}catch(Exception e){
@ -427,11 +436,11 @@ public abstract class HelperMethods {
* @param username
* @param tags
* @param groups
* @param resources
* @return
*/
public static List<ResourceBean> getResourcesFromBean(Common record, String username, List<String> tags, List<String> groups){
public static void getResourcesFromBean(Common record, String username, List<String> tags, List<String> groups, List<ResourceBean> resources){
List<ResourceBean> toReturn = new ArrayList<ResourceBean>();
Class<?> current = record.getClass();
do{
Field[] fields = current.getDeclaredFields();
@ -446,13 +455,13 @@ public abstract class HelperMethods {
List<Resource> listOfResources = (List<Resource>)f;
for (Resource resource : listOfResources) {
toReturn.add(new ResourceBean(resource.getUrl(), resource.getName().toString(), resource.getDescription(), null, username, null, null));
resources.add(new ResourceBean(resource.getUrl(), resource.getName().toString(), resource.getDescription(), null, username, null, null));
}
}else{
Resource res = (Resource)f;
toReturn.add(new ResourceBean(res.getUrl(), res.getName().toString(), res.getDescription(), null, username, null, null));
resources.add(new ResourceBean(res.getUrl(), res.getName().toString(), res.getDescription(), null, username, null, null));
}
}
@ -464,8 +473,7 @@ public abstract class HelperMethods {
}
while((current = current.getSuperclass())!=null); // iterate from the inherited class up to the Object.class
logger.info("Returning resources " + toReturn);
return toReturn;
logger.info("Returning resources " + resources);
}
/**
@ -563,29 +571,12 @@ public abstract class HelperMethods {
if(html == null || html.isEmpty())
return html;
logger.info("Incoming text is " + html);
// remove html and clean
String withoutHTML = Jsoup.parse(html).text();
withoutHTML = Jsoup.clean(withoutHTML, Whitelist.basic());
// TODO ...
// could contain non ascii chars ... try to convert them to question marks and then remove them
// try{
// String strippedWithQuestionMarks = Normalizer.normalize(withoutHTML, Normalizer.Form.NFD);
// String regex = "[\\p{InCombiningDiacriticalMarks}\\p{IsLm}\\p{IsSk}]+";
// strippedWithQuestionMarks = new String(strippedWithQuestionMarks.replaceAll(regex, "").getBytes("ascii"), "ascii");
// strippedWithQuestionMarks = strippedWithQuestionMarks.replaceAll("[?]+", " ");
// withoutHTML = strippedWithQuestionMarks;
// }catch(UnsupportedEncodingException e){
// logger.warn("Unable to convert to question marks non ascii chars..", e);
// // remove non ascii directly
// withoutHTML = withoutHTML.replaceAll("[^\\x00-\\x7F]", " ");
// }
// remove non ascii chars ...
withoutHTML = withoutHTML.replaceAll("[^\\p{ASCII}]", " ");
logger.info("Without html is " + withoutHTML);
return withoutHTML;
}
}

View File

@ -9,7 +9,7 @@ import com.fasterxml.jackson.annotation.JsonValue;
* Abundance_Level for Stock records
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@Group(parentGroup=Indicator.class)
@Group
public enum Abundance_Level {
Intermediate_Abundance("Intermediate abundance"),

View File

@ -8,7 +8,7 @@ import com.fasterxml.jackson.annotation.JsonCreator;
* Exploitation_Rate for Stock records
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@Group(parentGroup=Indicator.class)
@Group
public enum Exploitation_Rate {
Moderate_Fishing_Mortality("Moderate fishing mortality"),

View File

@ -1,12 +0,0 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils.groups;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group;
/**
* Indicator group: father of Abundance level and Exploitation Rate
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@Group
public enum Indicator {
}

View File

@ -1,12 +1,19 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils.threads;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Set;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.models.RolesCkanGroupOrOrg;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanGroup;
/**
* Associate the dataset to a group.
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
@ -40,17 +47,26 @@ public class AssociationToGroupThread extends Thread {
@Override
public void run() {
try{
logger.info("Association thread started to put the dataset with id="+ datasetId + " into group with title(s) " + groupsTitles + " for user " + username);
// find parents' groups
String userApiKey = catalogue.getApiKeyFromUsername(username);
findHierarchy(groupsTitles, catalogue, userApiKey);
Set<String> uniqueGroups = new HashSet<String>(groupsTitles);
logger.info("Full set of groups is " + groupsTitles);
// 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, organizationId, catalogue.getApiKeyFromUsername(username)).toUpperCase());
RolesCkanGroupOrOrg role = RolesCkanGroupOrOrg.valueOf(catalogue.getRoleOfUserInOrganization(username, organizationId, userApiKey).toUpperCase());
if(!role.equals(RolesCkanGroupOrOrg.ADMIN))
role = RolesCkanGroupOrOrg.MEMBER; // decrease the role to member if it is not an admin
for (String groupTitle : groupsTitles) {
for (String groupTitle : uniqueGroups) {
logger.debug("Setting role " + role + " into group " + groupTitle + " to user " + username);
boolean assigned = catalogue.checkRoleIntoGroup(username, groupTitle, role);
@ -65,6 +81,42 @@ public class AssociationToGroupThread extends Thread {
}
}
logger.info("The Association Group thread ended correctly");
}catch(Exception e){
logger.error("Exception follows ", e);
}
}
/**
* Find the hierarchy of trees
* @param uniqueGroups
* @param catalogue
* @param user's api key
*/
public static void findHierarchy(
List<String> groupsTitles,
DataCatalogue catalogue,
String apiKey) {
ListIterator<String> iterator = groupsTitles.listIterator();
while (iterator.hasNext()) {
String group = (String) iterator.next();
List<CkanGroup> parents = catalogue.getParentGroups(group, apiKey);
if(parents == null || parents.isEmpty())
return;
for (CkanGroup ckanGroup : parents) {
List<String> parentsList = new ArrayList<String>(Arrays.asList(ckanGroup.getName()));
findHierarchy(parentsList, catalogue, apiKey);
for (String parent : parentsList) {
iterator.add(parent);
}
}
}
}
}

View File

@ -13,8 +13,10 @@ 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;
@ -41,9 +43,9 @@ 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.Status;
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogueFactory;
import org.junit.Test;
import org.slf4j.LoggerFactory;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -555,4 +557,27 @@ public class JTests {
}
//@Test
public void testHierarchy(){
String name = "low-abundance";
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance("/gcube/devNext/NextNext");
List<String> uniqueGroups = new ArrayList<String>();
uniqueGroups.add(name);
uniqueGroups.add(name);
AssociationToGroupThread.findHierarchy(uniqueGroups, catalogue, catalogue.getApiKeyFromUsername("costantino_perciante"));
logger.debug("Hierarchy is " + uniqueGroups);
}
//@Test
public void testAssociationThread() throws InterruptedException{
String name = "low-abundance";
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance("/gcube/devNext/NextNext");
AssociationToGroupThread threadGroups = new AssociationToGroupThread(Arrays.asList(name), "another-test-test-please-ignore", "grsf", "costantino_perciante", catalogue);
threadGroups.start();
threadGroups.join();
logger.info("Thread stopped!");
}
}