From 013e54c070cc7e9ade5af0aabfaef45642bc69f8 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Tue, 9 Jun 2020 18:05:34 +0200 Subject: [PATCH] Commenting code which is related to action already covered by gCat --- .../GcoreEndPointReaderSocial.java | 2 +- .../services/GrsfPublisherFisheryService.java | 164 ++++++++------ .../services/GrsfPublisherStockService.java | 189 +++++++++------- .../utils/CommonServiceUtils.java | 211 +++++++++--------- .../grsf_publish_ws/utils/HelperMethods.java | 78 +++---- .../grsf_publish_ws/JTests.java | 66 +++--- 6 files changed, 380 insertions(+), 330 deletions(-) rename src/main/java/org/gcube/data_catalogue/grsf_publish_ws/{utils => AAA_UNNEEDED}/GcoreEndPointReaderSocial.java (97%) diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/GcoreEndPointReaderSocial.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/AAA_UNNEEDED/GcoreEndPointReaderSocial.java similarity index 97% rename from src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/GcoreEndPointReaderSocial.java rename to src/main/java/org/gcube/data_catalogue/grsf_publish_ws/AAA_UNNEEDED/GcoreEndPointReaderSocial.java index da6b5be..6756e90 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/GcoreEndPointReaderSocial.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/AAA_UNNEEDED/GcoreEndPointReaderSocial.java @@ -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; diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java index d390983..7b1ecfb 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherFisheryService.java @@ -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 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 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> customFields = record.getExtrasFields(); Set tags = new HashSet(); @@ -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 result = new HashMap(); @@ -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( diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java index d867bb3..6d600d0 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/GrsfPublisherStockService.java @@ -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 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 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> 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 result = new HashMap(); @@ -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()); diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java index 2af1e74..4d75b3f 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java @@ -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 extensionsCheck = new ConcurrentHashMap<>(); - /** - * Retrieve the list of licenses for stocks and fisheries - * @return - */ - public static Map getLicenses(DataCatalogue catalogue) { - logger.info("Requested licenses..."); - Map toReturn = new HashMap(); - List 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 getLicenses(DataCatalogue catalogue) { +// logger.info("Requested licenses..."); +// Map toReturn = new HashMap(); +// List 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. diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java index b345701..ae12c0c 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java @@ -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 diff --git a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java index bece3f6..b387c76 100644 --- a/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java +++ b/src/test/java/org/gcube/data_catalogue/grsf_publish_ws/JTests.java @@ -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 uniqueGroups = new ArrayList(); - 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 uniqueGroups = new ArrayList(); +// 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(){