2016-11-24 17:53:50 +01:00
|
|
|
package org.gcube.data_catalogue.grsf_publish_ws.utils.threads;
|
|
|
|
|
2016-11-25 13:56:27 +01:00
|
|
|
import java.beans.IntrospectionException;
|
2016-11-27 12:33:41 +01:00
|
|
|
import java.beans.PropertyDescriptor;
|
|
|
|
import java.io.File;
|
|
|
|
import java.lang.reflect.Field;
|
2016-11-25 13:56:27 +01:00
|
|
|
import java.lang.reflect.InvocationTargetException;
|
2016-11-27 12:33:41 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.List;
|
2016-11-25 13:56:27 +01:00
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
|
2016-11-27 12:33:41 +01:00
|
|
|
import org.gcube.common.homelibrary.home.HomeLibrary;
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.HomeNotFoundException;
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.InternalErrorException;
|
|
|
|
import org.gcube.common.homelibrary.home.exceptions.UserNotFoundException;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.Workspace;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceFolder;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.WorkspaceSharedFolder;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.catalogue.WorkspaceCatalogue;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundException;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.exceptions.WorkspaceFolderNotFoundException;
|
|
|
|
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
|
2016-11-28 12:20:43 +01:00
|
|
|
import org.gcube.common.scope.api.ScopeProvider;
|
2016-11-27 12:33:41 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.CustomField;
|
2016-11-24 17:53:50 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common;
|
2016-11-27 12:33:41 +01:00
|
|
|
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;
|
2016-11-28 17:43:28 +01:00
|
|
|
import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods;
|
2016-11-24 17:53:50 +01:00
|
|
|
import org.gcube.datacatalogue.ckanutillibrary.DataCatalogue;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-11-27 12:33:41 +01:00
|
|
|
import eu.trentorise.opendata.jackan.model.CkanResourceBase;
|
|
|
|
|
2016-11-24 17:53:50 +01:00
|
|
|
/**
|
|
|
|
* Extract the time series present in the record, load them as resource on ckan and on the .catalogue
|
|
|
|
* folder under the vre folder.
|
|
|
|
* @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it)
|
|
|
|
*/
|
|
|
|
public class ManageTimeSeriesThread extends Thread{
|
|
|
|
|
2016-11-27 12:33:41 +01:00
|
|
|
private static final String CSV_FILE_FORMAT = ".csv";
|
|
|
|
private static final String PATH_SEPARATOR = "/";
|
|
|
|
|
2016-11-24 17:53:50 +01:00
|
|
|
// Logger
|
|
|
|
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(ManageTimeSeriesThread.class);
|
|
|
|
|
2016-11-25 13:56:27 +01:00
|
|
|
private Common record;
|
2016-11-25 14:35:00 +01:00
|
|
|
private String packageName;
|
2016-11-25 13:56:27 +01:00
|
|
|
private String username;
|
|
|
|
private DataCatalogue catalogue;
|
|
|
|
private String context;
|
2016-11-28 12:20:43 +01:00
|
|
|
private String token;
|
2016-11-25 13:56:27 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param record
|
|
|
|
* @param packageId
|
|
|
|
* @param username
|
|
|
|
* @param catalogue
|
|
|
|
* @param context
|
|
|
|
*/
|
2016-11-25 14:35:00 +01:00
|
|
|
public ManageTimeSeriesThread(Common record, String packageName,
|
2016-11-28 12:20:43 +01:00
|
|
|
String username, DataCatalogue catalogue, String context, String token) {
|
2016-11-25 13:56:27 +01:00
|
|
|
super();
|
|
|
|
this.record = record;
|
2016-11-25 14:35:00 +01:00
|
|
|
this.packageName = packageName;
|
2016-11-25 13:56:27 +01:00
|
|
|
this.username = username;
|
|
|
|
this.catalogue = catalogue;
|
|
|
|
this.context = context;
|
2016-11-28 12:20:43 +01:00
|
|
|
this.token = token;
|
2016-11-25 13:56:27 +01:00
|
|
|
}
|
2016-11-24 17:53:50 +01:00
|
|
|
|
2016-11-25 13:56:27 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
2016-11-24 17:53:50 +01:00
|
|
|
logger.info("Time series manager thread started");
|
2016-11-27 12:33:41 +01:00
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
ScopeProvider.instance.set(context);
|
|
|
|
SecurityTokenProvider.instance.set(token);
|
|
|
|
|
2016-11-24 17:53:50 +01:00
|
|
|
try {
|
2016-11-28 12:20:43 +01:00
|
|
|
manageTimeSeries(record, packageName, username, catalogue);
|
2016-11-25 13:56:27 +01:00
|
|
|
logger.info("The time series manager thread ended correctly");
|
2016-11-27 12:33:41 +01:00
|
|
|
return;
|
|
|
|
} catch (IllegalAccessException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (IllegalArgumentException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (InvocationTargetException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (WorkspaceFolderNotFoundException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (ItemNotFoundException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (IntrospectionException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (InternalErrorException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (HomeNotFoundException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
|
|
|
} catch (UserNotFoundException e) {
|
|
|
|
logger.error("Error was " + e.getMessage());
|
2016-11-28 12:20:43 +01:00
|
|
|
}finally{
|
|
|
|
ScopeProvider.instance.reset();
|
|
|
|
SecurityTokenProvider.instance.reset();
|
2016-11-27 12:33:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.error("Failed to attach csv files to the product...");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Manage the time series bean within a resource (e.g., catches or landings, exploitation rate and so on).
|
|
|
|
* The method save the time series as csv on ckan, and also save the file in the .catalogue area of the shared vre folder.
|
|
|
|
* @param record
|
|
|
|
* @throws IntrospectionException
|
|
|
|
* @throws InvocationTargetException
|
|
|
|
* @throws IllegalArgumentException
|
|
|
|
* @throws IllegalAccessException
|
|
|
|
* @throws UserNotFoundException
|
|
|
|
* @throws HomeNotFoundException
|
|
|
|
* @throws InternalErrorException
|
|
|
|
* @throws WorkspaceFolderNotFoundException
|
|
|
|
* @throws ItemNotFoundException
|
|
|
|
*/
|
2016-11-28 12:20:43 +01:00
|
|
|
public static void manageTimeSeries(Common record, String packageName, String username, DataCatalogue catalogue) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException, WorkspaceFolderNotFoundException, InternalErrorException, HomeNotFoundException, UserNotFoundException, ItemNotFoundException{
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
if(record == null)
|
|
|
|
throw new IllegalArgumentException("The given record is null!!");
|
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
Workspace ws = HomeLibrary.getHomeManagerFactory().getHomeManager().getHome().getWorkspace();
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
// Get a VRE folder by scope
|
2016-11-28 12:20:43 +01:00
|
|
|
WorkspaceSharedFolder vreFolder = ws.getVREFolderByScope(ScopeProvider.instance.get());
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
//Get the VRE Folder catalogue
|
|
|
|
WorkspaceCatalogue catalogueFolder = vreFolder.getVRECatalogue();
|
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
logger.debug("Catalogue folder in vre has path " + catalogueFolder.getPath());
|
|
|
|
|
2016-11-27 12:33:41 +01:00
|
|
|
// the structure under the .catalogue will be as follows:
|
|
|
|
// .catalogue:
|
|
|
|
// - stock:
|
|
|
|
// - first_letter_of_the_product
|
|
|
|
// - product_name
|
|
|
|
// - type of files (e.g., csv)
|
2016-11-28 12:20:43 +01:00
|
|
|
// -files (csv)
|
2016-11-27 12:33:41 +01:00
|
|
|
// - fishery
|
|
|
|
// - first_letter_of_the_product
|
|
|
|
// - product_name
|
|
|
|
// - type of files (e.g., csv)
|
2016-11-28 12:20:43 +01:00
|
|
|
// -files (csv)
|
|
|
|
|
2016-11-27 12:33:41 +01:00
|
|
|
String recordTypeFolderName = record.getProductType().toLowerCase();
|
|
|
|
String productName = record.getClass().equals(StockRecord.class) ? ((StockRecord)record).getStockName() : ((FisheryRecord)record).getFisheryName();
|
|
|
|
char firstLetter = productName.charAt(0);
|
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
// the whole path of the directory is going to be...
|
|
|
|
String csvDirectoryForThisProduct = recordTypeFolderName + PATH_SEPARATOR + firstLetter + PATH_SEPARATOR + productName + PATH_SEPARATOR + "csv";
|
|
|
|
logger.debug("The path under which the time series are going to be saved is " + csvDirectoryForThisProduct);
|
2016-11-28 17:43:28 +01:00
|
|
|
WorkspaceFolder csvFolder = HelperMethods.createOrGetSubFoldersByPath(catalogueFolder, csvDirectoryForThisProduct);
|
2016-11-28 12:20:43 +01:00
|
|
|
|
|
|
|
if(csvFolder != null){
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
String apiKeyUser = catalogue.getApiKeyFromUsername(username);
|
|
|
|
|
|
|
|
Class<?> current = record.getClass();
|
|
|
|
do{
|
|
|
|
Field[] fields = current.getDeclaredFields();
|
|
|
|
for (Field field : fields) {
|
|
|
|
if (Collection.class.isAssignableFrom(field.getType())) {
|
|
|
|
|
|
|
|
Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record);
|
|
|
|
if(f != null){
|
|
|
|
|
|
|
|
List asList = (List)f;
|
|
|
|
|
|
|
|
if(!asList.isEmpty()){
|
|
|
|
if(asList.get(0).getClass().equals(TimeSeriesBean.class)){
|
|
|
|
|
|
|
|
CustomField customAnnotation = field.getAnnotation(CustomField.class);
|
2016-11-28 14:14:23 +01:00
|
|
|
String resourceToAttachName = (productName + "_" + customAnnotation.key()).replaceAll("\\s", "_") + CSV_FILE_FORMAT;
|
|
|
|
String resourceToAttachDescription = productName + ":" + customAnnotation.key() + " time series";
|
2016-11-27 12:33:41 +01:00
|
|
|
|
2016-11-28 12:20:43 +01:00
|
|
|
logger.debug("A time series has been just found (from field " + customAnnotation.key() + ")");
|
2016-11-27 12:33:41 +01:00
|
|
|
File csvFile = CSVHelpers.listToCSV(asList);
|
|
|
|
|
|
|
|
if(csvFile != null){
|
|
|
|
|
|
|
|
// upload this file on ckan
|
2016-11-28 14:14:23 +01:00
|
|
|
CkanResourceBase ckanResource = uploadFileOnCatalogue(csvFile, packageName, catalogue, username, resourceToAttachName, resourceToAttachDescription, apiKeyUser);
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
//upload this file on the folder of the vre (under .catalogue) and change the url of the resource
|
|
|
|
if(ckanResource != null){
|
|
|
|
|
2016-11-28 17:43:28 +01:00
|
|
|
ExternalFile createdFileOnWorkspace = HelperMethods.uploadExternalFile(csvFolder, resourceToAttachName, resourceToAttachDescription, csvFile);
|
2016-11-27 12:33:41 +01:00
|
|
|
if(createdFileOnWorkspace != null){
|
|
|
|
|
|
|
|
String publicUrlToSetOnCkan = createdFileOnWorkspace.getPublicLink(true);
|
|
|
|
logger.info("going to patch the created resource with id " + ckanResource.getId() + " with url " + publicUrlToSetOnCkan);
|
2016-11-28 14:14:23 +01:00
|
|
|
boolean updated = catalogue.patchResource(ckanResource.getId(), publicUrlToSetOnCkan, resourceToAttachName, resourceToAttachDescription, "", apiKeyUser);
|
2016-11-27 12:33:41 +01:00
|
|
|
|
|
|
|
if(updated)
|
|
|
|
logger.info("Resource has been updated with the new url");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while((current = current.getSuperclass())!=null); // iterate from the inherited class up to the Object.class
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a resource on ckan
|
|
|
|
* @param csvFile
|
|
|
|
* @param packageName
|
|
|
|
* @param catalogue
|
|
|
|
* @param username
|
|
|
|
* @param resourceToAttachName
|
|
|
|
* @param description
|
|
|
|
* @return a ckan resource on success, null otherwise
|
|
|
|
*/
|
2016-11-28 12:20:43 +01:00
|
|
|
private static CkanResourceBase uploadFileOnCatalogue(File csvFile,
|
2016-11-27 12:33:41 +01:00
|
|
|
String packageName, DataCatalogue catalogue, String username,
|
2016-11-28 12:20:43 +01:00
|
|
|
String resourceToAttachName, String description, String apiKey) {
|
2016-11-27 12:33:41 +01:00
|
|
|
return catalogue.uploadResourceFile(
|
|
|
|
csvFile,
|
|
|
|
packageName,
|
2016-11-28 12:20:43 +01:00
|
|
|
apiKey,
|
2016-11-27 12:33:41 +01:00
|
|
|
resourceToAttachName,
|
|
|
|
description);
|
|
|
|
}
|
2016-11-24 17:53:50 +01:00
|
|
|
}
|