added some chances to upload the resource on the ckan as time series. Added a new @TimeSeries annotation

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@135199 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-01 22:21:37 +00:00
parent 291b542d61
commit f076015318
5 changed files with 51 additions and 29 deletions

View File

@ -0,0 +1,16 @@
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;
/**
* TimeSeries annotation
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface TimeSeries {
}

View File

@ -9,6 +9,7 @@ import javax.validation.constraints.Size;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.TimeSeries;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Fishery_Type;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Production_System_Type;
@ -77,6 +78,7 @@ public class FisheryRecord extends Common{
@JsonProperty("catches_or_landings")
@CustomField(key="Catches or landings")
@Tag
@TimeSeries
@Valid
private List<TimeSeriesBean<String, String>> catchesOrLandings;

View File

@ -9,6 +9,7 @@ import javax.validation.constraints.Size;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag;
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.TimeSeries;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Exploitation_Rate;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Stock_Type;
@ -63,22 +64,26 @@ public class StockRecord extends Common{
@JsonProperty("standard_exploitation_rate")
@CustomField(key="Standard Exploitation Rate")
@Tag
@TimeSeries
@Valid
private List<TimeSeriesBean<Exploitation_Rate, Void>> exploitationRateStandard;
@JsonProperty("exploitation_rate")
@CustomField(key="Exploitation Rate")
@TimeSeries
@Valid
private List<TimeSeriesBean<String, String>> exploitationRate;
@JsonProperty("standard_abundance_level")
@CustomField(key="Standard Abundance Level")
@Tag
@TimeSeries
@Valid
private List<TimeSeriesBean<Abundance_Level, Void>> abundanceLevelStandard;
@JsonProperty("abundance_level")
@CustomField(key="Abundance Level")
@TimeSeries
@Valid
private List<TimeSeriesBean<String, String>> abundanceLevel;

View File

@ -2,9 +2,6 @@ package org.gcube.data_catalogue.grsf_publish_ws.json.input;
import javax.validation.constraints.NotNull;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Abundance_Level;
import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Exploitation_Rate;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
@ -21,7 +18,7 @@ public class TimeSeriesBean<T, T1> implements Comparable<TimeSeriesBean<T, T1>>{
@JsonProperty("year")
@NotNull(message="year of a time series cannot be null")
private Long year;
@JsonProperty("value")
@NotNull(message="value of a time series cannot be null")
private T value;
@ -63,20 +60,15 @@ public class TimeSeriesBean<T, T1> implements Comparable<TimeSeriesBean<T, T1>>{
public void setUnit(T1 unit) {
this.unit = unit;
}
@Override
public String toString() {
Class<? extends Object> valueClass = value.getClass();
// when the value belongs to these classes annotated with @Tag..
if(valueClass.equals(Abundance_Level.class) || valueClass.equals(Exploitation_Rate.class))
return year + "-" + value;
else if(this.unit != null && !this.unit.getClass().equals(Void.class)) // e.g., catches and landings
if(this.unit != null && !this.unit.getClass().equals(Void.class)) // e.g., catches and landings
return year + "-" + value + "-" + unit;
else
return "TimeSeriesBean [value=" + value + ", unit=" + unit + ", year="
+ year + "]";
else
return year + "-" + value;
}
@Override

View File

@ -5,7 +5,6 @@ import java.beans.PropertyDescriptor;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.util.Collection;
import java.util.List;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
@ -22,10 +21,10 @@ 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.custom_annotations.TimeSeries;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
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;
import org.gcube.data_catalogue.grsf_publish_ws.utils.CSVHelpers;
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
@ -45,6 +44,7 @@ public class ManageTimeSeriesThread extends Thread{
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ManageTimeSeriesThread.class);
private static final int CANCHES = 3;
private Common record;
private String packageName;
@ -167,7 +167,7 @@ public class ManageTimeSeriesThread extends Thread{
do{
Field[] fields = current.getDeclaredFields();
for (Field field : fields) {
if (Collection.class.isAssignableFrom(field.getType())) {
if (field.isAnnotationPresent(TimeSeries.class)) {
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
if(f != null){
@ -175,19 +175,23 @@ public class ManageTimeSeriesThread extends Thread{
List asList = (List)f;
if(!asList.isEmpty()){
if(asList.get(0).getClass().equals(TimeSeriesBean.class)){
CustomField customAnnotation = field.getAnnotation(CustomField.class);
String resourceToAttachName = (productName + "_" + customAnnotation.key()).replaceAll("\\s", "_") + CSV_FILE_FORMAT;
String resourceToAttachDescription = productName + ":" + customAnnotation.key() + " time series";
CustomField customAnnotation = field.getAnnotation(CustomField.class);
String resourceToAttachName = (productName + "_" + customAnnotation.key()).replaceAll("\\s", "_") + CSV_FILE_FORMAT;
String resourceToAttachDescription = productName + ":" + customAnnotation.key() + " time series";
logger.debug("A time series has been just found (from field " + customAnnotation.key() + ")");
File csvFile = CSVHelpers.listToCSV(asList);
logger.debug("A time series has been just found (from field " + customAnnotation.key() + ")");
File csvFile = CSVHelpers.listToCSV(asList);
if(csvFile != null){
CkanResourceBase ckanResource = null;
if(csvFile != null){
for (int i = 0; i < CANCHES; i++) {
// upload this file on ckan
CkanResourceBase ckanResource = uploadFileOnCatalogue(csvFile, packageName, catalogue, username, resourceToAttachName, resourceToAttachDescription, apiKeyUser);
if(ckanResource == null)
ckanResource = uploadFileOnCatalogue(csvFile, packageName, catalogue, username, resourceToAttachName, resourceToAttachDescription, apiKeyUser);
//upload this file on the folder of the vre (under .catalogue) and change the url of the resource
if(ckanResource != null){
@ -199,15 +203,18 @@ public class ManageTimeSeriesThread extends Thread{
logger.info("going to patch the created resource with id " + ckanResource.getId() + " with url " + publicUrlToSetOnCkan);
boolean updated = catalogue.patchResource(ckanResource.getId(), publicUrlToSetOnCkan, resourceToAttachName, resourceToAttachDescription, "", apiKeyUser);
if(updated)
if(updated){
logger.info("Resource has been updated with the new url");
break;
}
}
}
// delete the file
csvFile.delete();
}
// delete the file
csvFile.delete();
}
}
}