Commenting code which is related to action already covered by gCat

This commit is contained in:
Luca Frosini 2020-06-09 18:05:34 +02:00
parent f1a81e071e
commit 013e54c070
6 changed files with 380 additions and 330 deletions

View File

@ -1,4 +1,4 @@
package org.gcube.data_catalogue.grsf_publish_ws.utils;
package org.gcube.data_catalogue.grsf_publish_ws.AAA_UNNEEDED;
import static org.gcube.resources.discovery.icclient.ICFactory.client;
import static org.gcube.resources.discovery.icclient.ICFactory.queryFor;

View File

@ -61,33 +61,33 @@ public class GrsfPublisherFisheryService {
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherFisheryService.class);
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public Response hello() {
return Response.ok("Hello.. Fishery service is here").build();
}
@GET
@Path("get-licenses")
@Produces(MediaType.APPLICATION_JSON)
public Response getLicenses() {
Status status = Status.OK;
String context = ScopeProvider.instance.get();
DataCatalogue catalogue;
try {
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
if(licenses == null)
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(licenses).build();
} catch(Exception e) {
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status)
.entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
.build();
}
}
// @GET
// @Path("hello")
// @Produces(MediaType.TEXT_PLAIN)
// public Response hello() {
// return Response.ok("Hello.. Fishery service is here").build();
// }
//
// @GET
// @Path("get-licenses")
// @Produces(MediaType.APPLICATION_JSON)
// public Response getLicenses() {
// Status status = Status.OK;
// String context = ScopeProvider.instance.get();
// DataCatalogue catalogue;
// try {
//// catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
// if(licenses == null)
// status = Status.INTERNAL_SERVER_ERROR;
// return Response.status(status).entity(licenses).build();
// } catch(Exception e) {
// status = Status.INTERNAL_SERVER_ERROR;
// return Response.status(status)
// .entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
// .build();
// }
// }
@POST
@Path("publish-product")
@ -113,26 +113,33 @@ public class GrsfPublisherFisheryService {
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
} else {
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
// } else {
String apiKey = catalogue.getApiKeyFromUsername(username);
String organization = HelperMethods.retrieveOrgNameFromScope(context);
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
String apiKey = "";
String organization = "";
// extend this role to the other organizations in this context
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
RolesCkanGroupOrOrg.ADMIN);
// String apiKey = catalogue.getApiKeyFromUsername(username);
// String organization = HelperMethods.retrieveOrgNameFromScope(context);
// CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
//
// // extend this role to the other organizations in this context
// CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
// RolesCkanGroupOrOrg.ADMIN);
String authorMail = "";
String authorFullname = "";
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
if(authorMail == null || authorFullname == null) {
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
}
// String authorMail = HelperMethods.getUserEmail(context, token);
// String authorFullname = HelperMethods.getUserFullname(context, token);
//
// if(authorMail == null || authorFullname == null) {
// throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
// }
// 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
@ -140,7 +147,7 @@ public class GrsfPublisherFisheryService {
String futureTitle = record.getFisheryName();
// check name
CommonServiceUtils.checkName(futureName, catalogue);
// CommonServiceUtils.checkName(futureName, catalogue);
Map<String,List<String>> customFields = record.getExtrasFields();
Set<String> tags = new HashSet<String>();
@ -206,7 +213,7 @@ public class GrsfPublisherFisheryService {
} else {
throw new Exception("There was an error during the record generation, sorry");
}
}
// }
} catch(Exception e) {
logger.error("Failed to create fishery record" + e);
status = Status.INTERNAL_SERVER_ERROR;
@ -236,7 +243,8 @@ public class GrsfPublisherFisheryService {
"Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a fishery");
try {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
@ -298,11 +306,12 @@ public class GrsfPublisherFisheryService {
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception("There was a problem while serving your request");
}
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception("There was a problem while serving your request");
// }
//
// if it is a request for GRSF records, we have Fishery - Stock groups, so it is easy.
// For other cases, records needs to be parsed
if(sourceInPath.equals(Sources.GRSF))
@ -345,10 +354,13 @@ public class GrsfPublisherFisheryService {
logger.info("Received call to get the catalogue identifier for the product with name " + name);
try {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception("There was a problem while serving your request");
}
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception("There was a problem while serving your request");
// }
//
DataCatalogue catalogue = null;
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
if(dataset != null) {
Map<String,String> result = new HashMap<String,String>();
@ -395,33 +407,40 @@ public class GrsfPublisherFisheryService {
throw new Exception("Please specify the '" + Constants.CATALOG_ID + "' property");
}
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception(
"There was a problem while serving your request. No catalogue instance was found in this context!");
} else {
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception(
// "There was a problem while serving your request. No catalogue instance was found in this context!");
// } else {
// get already published record and modify it
String apiKey = catalogue.getApiKeyFromUsername(username);
String apiKey = "";
// String apiKey = catalogue.getApiKeyFromUsername(username);
CkanDataset recordPublished = catalogue.getDataset(catalogId, apiKey);
if(recordPublished == null)
throw new Exception("A record with catalogue id " + catalogId + " does not exist!");
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
String authorMail = "";
String authorFullname = "";
if(authorMail == null || authorFullname == null) {
logger.debug("Author fullname or mail missing, cannot continue");
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
}
// // 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");
// throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
// }
String organization = HelperMethods.retrieveOrgNameFromScope(context);
String organization = "";
// String organization = HelperMethods.retrieveOrgNameFromScope(context);
// check he/she has admin role
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// name, product url and are going to remain unchanged (so we keep them from the already published record);
String name = recordPublished.getName();
@ -502,7 +521,7 @@ public class GrsfPublisherFisheryService {
} else {
throw new Exception("There was an error during the item updated, sorry");
}
}
// }
} catch(Exception e) {
logger.error("Failed to update fishery record" + e);
responseBean.setError(e.getMessage());
@ -528,8 +547,9 @@ public class GrsfPublisherFisheryService {
Status status = Status.INTERNAL_SERVER_ERROR;
try {
DataCatalogue catalogue = null;
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception(

View File

@ -61,33 +61,33 @@ public class GrsfPublisherStockService {
// Logger
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(GrsfPublisherStockService.class);
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public Response hello() {
return Response.ok("Hello.. Stock service is here").build();
}
@GET
@Path("get-licenses")
@Produces(MediaType.APPLICATION_JSON)
public Response getLicenses() {
Status status = Status.OK;
String context = ScopeProvider.instance.get();
DataCatalogue catalogue;
try {
catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
if(licenses == null)
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status).entity(licenses).build();
} catch(Exception e) {
status = Status.INTERNAL_SERVER_ERROR;
return Response.status(status)
.entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
.build();
}
}
// @GET
// @Path("hello")
// @Produces(MediaType.TEXT_PLAIN)
// public Response hello() {
// return Response.ok("Hello.. Stock service is here").build();
// }
//
// @GET
// @Path("get-licenses")
// @Produces(MediaType.APPLICATION_JSON)
// public Response getLicenses() {
// Status status = Status.OK;
// String context = ScopeProvider.instance.get();
// DataCatalogue catalogue;
// try {
// catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// Map<String,String> licenses = CommonServiceUtils.getLicenses(catalogue);
// if(licenses == null)
// status = Status.INTERNAL_SERVER_ERROR;
// return Response.status(status).entity(licenses).build();
// } catch(Exception e) {
// status = Status.INTERNAL_SERVER_ERROR;
// return Response.status(status)
// .entity(new ResponseBean(false, "Unable to retrieve license list " + e.getLocalizedMessage(), null))
// .build();
// }
// }
@POST
@Path("publish-product")
@ -113,35 +113,46 @@ public class GrsfPublisherStockService {
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
} else {
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception("There was a problem while serving your request. No catalogue instance was found!");
// } else {
//
String apiKey = catalogue.getApiKeyFromUsername(username);
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
String apiKey = "";
String organization = "";
// String apiKey = catalogue.getApiKeyFromUsername(username);
// String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
// check it has admin role or throw exception
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// extend this role to the other organizations in this context
CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
RolesCkanGroupOrOrg.ADMIN);
// To support this gCat must be modified according to the following ticket
// https://support.d4science.org/issues/19365
// CommonServiceUtils.extendRoleToOtherOrganizations(username, catalogue, organization,
// RolesCkanGroupOrOrg.ADMIN);
String authorMail = "";
String authorFullname = "";
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
if(authorMail == null || authorFullname == null) {
throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
}
// String authorMail = HelperMethods.getUserEmail(context, token);
// String authorFullname = HelperMethods.getUserFullname(context, token);
//
// if(authorMail == null || authorFullname == null) {
// throw new Exception("Sorry but it was not possible to retrieve your fullname/email!");
// }
// check the record has a name, at least
String futureName = record.getUuid();
String futureTitle = record.getStockName();
// check name and throws exception
CommonServiceUtils.checkName(futureName, catalogue);
// CommonServiceUtils.checkName(futureName, catalogue);
// load other information
Map<String,List<String>> customFields = record.getExtrasFields();
@ -209,7 +220,7 @@ public class GrsfPublisherStockService {
} else
throw new Exception(
"There was an error during the product generation, sorry! Unable to create the dataset");
}
// }
} catch(Exception e) {
logger.error("Failed to create stock record", e);
status = Status.INTERNAL_SERVER_ERROR;
@ -238,11 +249,13 @@ public class GrsfPublisherStockService {
"Received call to delete product with id " + recordToDelete.getId() + ", checking if it is a stock");
try {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
}
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// status = Status.INTERNAL_SERVER_ERROR;
// throw new Exception("There was a problem while serving your request");
// }
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
@ -304,11 +317,13 @@ public class GrsfPublisherStockService {
// Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source);
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request");
}
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// status = Status.INTERNAL_SERVER_ERROR;
// throw new Exception("There was a problem while serving your request");
// }
// if it is a request for GRSF records, we have Fishery - Stock groups, so it is easy.
// For other cases, records needs to be parsed
@ -348,10 +363,13 @@ public class GrsfPublisherStockService {
Status status = Status.INTERNAL_SERVER_ERROR;
logger.info("Received call to get the catalogue identifier for the product with name " + name);
try {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception("There was a problem while serving your request");
}
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
// if(catalogue == null) {
// throw new Exception("There was a problem while serving your request");
// }
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
if(dataset != null) {
Map<String,String> result = new HashMap<String,String>();
@ -398,32 +416,40 @@ public class GrsfPublisherStockService {
throw new Exception("Please specify the 'catalog_id' property");
}
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception(
"There was a problem while serving your request. No catalogue instance was found in this context!");
} else {
// if(catalogue == null) {
// throw new Exception(
// "There was a problem while serving your request. No catalogue instance was found in this context!");
// } else {
// get already published record and modify it
String apiKey = catalogue.getApiKeyFromUsername(username);
String apiKey = "";
// String apiKey = catalogue.getApiKeyFromUsername(username);
CkanDataset recordPublished = catalogue.getDataset(catalogId, apiKey);
if(recordPublished == null)
throw new Exception("A record with id " + catalogId + " does not exist!");
// retrieve the user's email and fullname
String authorMail = HelperMethods.getUserEmail(context, token);
String authorFullname = HelperMethods.getUserFullname(context, token);
String authorMail = "";
String authorFullname = "";
if(authorMail == null || authorFullname == null) {
logger.debug("Author fullname or mail missing, cannot continue");
throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
}
String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
// 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");
// throw new Exception("Sorry but there was not possible to retrieve your fullname/email!");
// }
String organization = "";
// String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin";
// check he/she has admin role
CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization);
// name, product url and are going to remain unchanged (so we keep them from the publisher record);
String name = recordPublished.getName();
@ -506,7 +532,7 @@ public class GrsfPublisherStockService {
} else {
throw new Exception("There was an error during the item updated, sorry");
}
}
// }
} catch(Exception e) {
logger.error("Failed to update stock record", e);
responseBean.setError(e.getMessage());
@ -534,12 +560,13 @@ public class GrsfPublisherStockService {
try {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null) {
throw new Exception(
"There was a problem while serving your request. No catalogue instance was found in this context!");
} else {
DataCatalogue catalogue = null;
// DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
//
// if(catalogue == null) {
// throw new Exception(
// "There was a problem while serving your request. No catalogue instance was found in this context!");
// } else {
// catalog id must be reported
String uuid = bean.getUuid();
@ -574,7 +601,7 @@ public class GrsfPublisherStockService {
responseBean.setKbUuid(uuid);
responseBean.setId(record.getId());
responseBean.setItemUrl(record.getExtrasAsHashMap().get(Constants.ITEM_URL_FIELD));
}
// }
} catch(Exception e) {
logger.error("Failed to update stock record's status", e);
responseBean.setError(e.getMessage());

View File

@ -31,7 +31,6 @@ import org.gcube.data_catalogue.grsf_publish_ws.utils.csv.ManageTimeSeriesThread
import org.gcube.data_catalogue.grsf_publish_ws.utils.threads.AssociationToGroupThread;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.shared.ResourceBean;
import org.gcube.datacatalogue.ckanutillibrary.shared.RolesCkanGroupOrOrg;
import org.gcube.datacatalogue.common.AAA_PORTED.Constants;
import org.gcube.datacatalogue.common.AAA_PORTED.Product_Type;
import org.gcube.datacatalogue.common.AAA_PORTED.Sources;
@ -40,9 +39,6 @@ import org.gcube.datacatalogue.common.AAA_PORTED.Stock_Type;
import org.json.simple.JSONObject;
import org.slf4j.LoggerFactory;
import eu.trentorise.opendata.jackan.model.CkanDataset;
import eu.trentorise.opendata.jackan.model.CkanLicense;
/**
* Services common utils.
* @author Costantino Perciante (ISTI - CNR)
@ -55,20 +51,20 @@ public class CommonServiceUtils {
private static final int TAG_MAX_SIZE = 100;
private static Map<String,Boolean> extensionsCheck = new ConcurrentHashMap<>();
/**
* Retrieve the list of licenses for stocks and fisheries
* @return
*/
public static Map<String,String> getLicenses(DataCatalogue catalogue) {
logger.info("Requested licenses...");
Map<String,String> toReturn = new HashMap<String,String>();
List<CkanLicense> licenses = catalogue.getLicenses();
for(CkanLicense ckanLicense : licenses) {
toReturn.put(ckanLicense.getId(), ckanLicense.getTitle());
}
return toReturn;
}
// /**
// * Retrieve the list of licenses for stocks and fisheries
// * @return
// */
// public static Map<String,String> getLicenses(DataCatalogue catalogue) {
// logger.info("Requested licenses...");
// Map<String,String> toReturn = new HashMap<String,String>();
// List<CkanLicense> licenses = catalogue.getLicenses();
//
// for(CkanLicense ckanLicense : licenses) {
// toReturn.put(ckanLicense.getId(), ckanLicense.getTitle());
// }
// return toReturn;
// }
/**
* Validate an aggregated GRSF record. TODO use @Valid tags
@ -421,45 +417,46 @@ public class CommonServiceUtils {
}
}
/**
* Evaluate if the user has the admin role
* Throws exception if he/she doesn't
*/
public static void hasAdminRole(String username, DataCatalogue catalogue, String apiKey, String organization)
throws Exception {
String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey);
logger.info("Role of the user " + username + " is " + role + " in " + organization);
if(role == null || role.isEmpty() || !role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString()))
throw new Exception(
"You are not authorized to create a product. Please check you have the Catalogue-Administrator role!");
}
/**
* Check this record's name
* @param futureName
* @param catalogue
* @throws Exception on name check
*/
public static void checkName(String futureName, DataCatalogue catalogue) throws Exception {
if(!HelperMethods.isNameValid(futureName)) {
throw new Exception(
"The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'");
} else {
logger.debug("Checking if such name [" + futureName + "] doesn't exist ...");
boolean alreadyExists = catalogue.existProductWithNameOrId(futureName);
if(alreadyExists) {
logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists");
throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists");
}
}
}
// /**
// * Evaluate if the user has the admin role
// * Throws exception if he/she doesn't
// */
// public static void hasAdminRole(String username, DataCatalogue catalogue, String apiKey, String organization)
// throws Exception {
//
// String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey);
// logger.info("Role of the user " + username + " is " + role + " in " + organization);
//
// if(role == null || role.isEmpty() || !role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString()))
// throw new Exception(
// "You are not authorized to create a product. Please check you have the Catalogue-Administrator role!");
//
// }
// /**
// * Check this record's name
// * @param futureName
// * @param catalogue
// * @throws Exception on name check
// */
// public static void checkName(String futureName, DataCatalogue catalogue) throws Exception {
//
// if(!HelperMethods.isNameValid(futureName)) {
// throw new Exception(
// "The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'");
// } else {
//
// logger.debug("Checking if such name [" + futureName + "] doesn't exist ...");
// boolean alreadyExists = catalogue.existProductWithNameOrId(futureName);
//
// if(alreadyExists) {
// logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists");
// throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists");
//
// }
// }
// }
/**
* Validate and check sources
@ -495,7 +492,11 @@ public class CommonServiceUtils {
String databaseSource = "";
// we have the id within the catalog of this record. This means that we can retrieve the record and its system:type
for(RefersToBean refersToBean : refersTo) {
String sourceOrganization = getRecordOrganization(refersToBean.getId(), apiKey, context);
String sourceOrganization = "";
// String sourceOrganization = getRecordOrganization(refersToBean.getId(), apiKey, context);
resources.add(new ResourceBean(refersToBean.getUrl(), sourceOrganization, "", null, username, null,
null));
sourcesList.add(sourceOrganization.toLowerCase());
@ -563,35 +564,35 @@ public class CommonServiceUtils {
groups.add(groupName);
}
/**
* Fetch the system:type property from a record
* @param itemIdOrName
* @param apiKey
* @return null on error
* @throws Exception
*/
public static String getSystemTypeValue(String itemIdOrName, String apiKey, String context) throws Exception {
DataCatalogue catalog = HelperMethods.getDataCatalogueRunningInstance(context);
CkanDataset dataset = catalog.getDataset(itemIdOrName, apiKey);
if(dataset == null)
throw new Exception("Unable to find record with id or name " + itemIdOrName);
String systemTypeValue = dataset.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY);
if(systemTypeValue == null || systemTypeValue.isEmpty())
throw new Exception(Constants.SYSTEM_TYPE_CUSTOM_KEY + " property not set in record " + itemIdOrName);
else
return systemTypeValue;
}
public static String getRecordOrganization(String itemIdOrName, String apiKey, String context) throws Exception {
DataCatalogue catalog = HelperMethods.getDataCatalogueRunningInstance(context);
CkanDataset dataset = catalog.getDataset(itemIdOrName, apiKey);
if(dataset == null)
throw new Exception("Unable to find record with id or name " + itemIdOrName);
else
return dataset.getOrganization().getTitle();
}
// /**
// * Fetch the system:type property from a record
// * @param itemIdOrName
// * @param apiKey
// * @return null on error
// * @throws Exception
// */
// public static String getSystemTypeValue(String itemIdOrName, String apiKey, String context) throws Exception {
//
// DataCatalogue catalog = HelperMethods.getDataCatalogueRunningInstance(context);
// CkanDataset dataset = catalog.getDataset(itemIdOrName, apiKey);
// if(dataset == null)
// throw new Exception("Unable to find record with id or name " + itemIdOrName);
// String systemTypeValue = dataset.getExtrasAsHashMap().get(Constants.SYSTEM_TYPE_CUSTOM_KEY);
// if(systemTypeValue == null || systemTypeValue.isEmpty())
// throw new Exception(Constants.SYSTEM_TYPE_CUSTOM_KEY + " property not set in record " + itemIdOrName);
// else
// return systemTypeValue;
//
// }
//
// public static String getRecordOrganization(String itemIdOrName, String apiKey, String context) throws Exception {
// DataCatalogue catalog = HelperMethods.getDataCatalogueRunningInstance(context);
// CkanDataset dataset = catalog.getDataset(itemIdOrName, apiKey);
// if(dataset == null)
// throw new Exception("Unable to find record with id or name " + itemIdOrName);
// else
// return dataset.getOrganization().getTitle();
// }
/**
* Actions to execute once the dataset has been updated or created.
@ -670,25 +671,25 @@ public class CommonServiceUtils {
}).start();
}
/**
* Extend roles to other organization
* @param username
* @param catalogue
* @param organization
* @param admin
*/
public static void extendRoleToOtherOrganizations(String username, DataCatalogue catalogue, String organization,
RolesCkanGroupOrOrg admin) {
logger.debug("Checking if role extension is needed here");
if(extensionsCheck.containsKey(username) && extensionsCheck.get(username))
return;
else {
catalogue.assignRolesOtherOrganization(username, organization, admin);
extensionsCheck.put(username, true);
}
}
// /**
// * Extend roles to other organization
// * @param username
// * @param catalogue
// * @param organization
// * @param admin
// */
// public static void extendRoleToOtherOrganizations(String username, DataCatalogue catalogue, String organization,
// RolesCkanGroupOrOrg admin) {
//
// logger.debug("Checking if role extension is needed here");
// if(extensionsCheck.containsKey(username) && extensionsCheck.get(username))
// return;
// else {
// catalogue.assignRolesOtherOrganization(username, organization, admin);
// extensionsCheck.put(username, true);
// }
//
// }
/**
* Evaluate in which organization a record has to be published. The only exception is when grsf_admin is involved.

View File

@ -29,6 +29,7 @@ import org.gcube.common.homelibrary.home.workspace.exceptions.ItemNotFoundExcept
import org.gcube.common.homelibrary.home.workspace.folder.items.ExternalFile;
import org.gcube.common.resources.gcore.utils.XPathHelper;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.data_catalogue.grsf_publish_ws.AAA_UNNEEDED.GcoreEndPointReaderSocial;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogueFactory;
import org.gcube.datacatalogue.common.AAA_UNNEEDED.CacheImpl;
@ -103,38 +104,38 @@ public abstract class HelperMethods {
return modified;
}
/**
* Retrieve the running instance of the data catalogue for this scope
* @return
* @throws Exception
*/
public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
// /**
// * Retrieve the running instance of the data catalogue for this scope
// * @return
// * @throws Exception
// */
// public static DataCatalogue getDataCatalogueRunningInstance(String scope) throws Exception{
//
// if(catalogueCache.get(scope) != null)
// return catalogueCache.get(scope);
// else{
// try{
// DataCatalogue instance = DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
// catalogueCache.insert(scope, instance);
// return instance;
// }catch(Exception e){
// logger.error("Failed to instantiate data catalogue lib", e);
// throw new Exception("Failed to retrieve catalogue information");
// }
// }
// }
if(catalogueCache.get(scope) != null)
return catalogueCache.get(scope);
else{
try{
DataCatalogue instance = DataCatalogueFactory.getFactory().getUtilsPerScope(scope);
catalogueCache.insert(scope, instance);
return instance;
}catch(Exception e){
logger.error("Failed to instantiate data catalogue lib", e);
throw new Exception("Failed to retrieve catalogue information");
}
}
}
/**
* Retrieve the organization name in which the user wants to publish starting from the scope
* @param contextInWhichPublish
* @return
*/
public static String retrieveOrgNameFromScope(String scope) {
String[] splittedScope = scope.split("/");
return splittedScope[splittedScope.length - 1].toLowerCase();
}
// /**
// * Retrieve the organization name in which the user wants to publish starting from the scope
// * @param contextInWhichPublish
// * @return
// */
// public static String retrieveOrgNameFromScope(String scope) {
//
// String[] splittedScope = scope.split("/");
// return splittedScope[splittedScope.length - 1].toLowerCase();
//
// }
/**
* Validate the name the product will have
@ -150,13 +151,14 @@ public abstract class HelperMethods {
}
}
/**
/* *//**
* Retrieve the user's email given his/her username
* @param context
* @param token
* @return
* @throws Exception
*/
*//*
public static String getUserEmail(String context, String token){
// check in cache
@ -174,13 +176,13 @@ public abstract class HelperMethods {
return result;
}
/**
*//**
* Retrieve the user's fullname given his/her username
* @param context
* @param token
* @return
* @throws Exception
*/
*//*
public static String getUserFullname(String context, String token){
// check in cache
@ -198,10 +200,10 @@ public abstract class HelperMethods {
return result;
}
/**
*//**
* Execute the GET http request at this url, and return the result as string
* @return
*/
*//*
private static String executGETHttpRequest(String url, int expectedCodeOnSuccess){
try(CloseableHttpClient client = HttpClientBuilder.create().build();){
@ -231,7 +233,7 @@ public abstract class HelperMethods {
return null;
}
}*/
/**
* Check that the given license id is in CKAN

View File

@ -378,39 +378,39 @@ public class JTests {
}
//@Test
public void testHierarchy() throws Exception{
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 Exception{
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, "apiKey");
threadGroups.start();
threadGroups.join();
logger.info("Thread stopped!");
}
//@Test
public void testCaches(){
String context = "/gcube/devNext/NextNext";
String token = "";
for (int i = 0; i < 1000; i++) {
logger.debug(HelperMethods.getUserEmail(context, token));
}
}
// //@Test
// public void testHierarchy() throws Exception{
// 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 Exception{
// 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, "apiKey");
// threadGroups.start();
// threadGroups.join();
// logger.info("Thread stopped!");
//
//
// }
//
// //@Test
// public void testCaches(){
//
// String context = "/gcube/devNext/NextNext";
// String token = "";
// for (int i = 0; i < 1000; i++) {
// logger.debug(HelperMethods.getUserEmail(context, token));
// }
//
// }
//@Test
public void testMatch(){