From d409a4dd108c129527a23ebae11b45e5337af121 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Tue, 6 Dec 2016 16:06:51 +0000 Subject: [PATCH] added methods to retrieve products of type stock/fishery for the different sources. At most 1000 datasets'ids are returned git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@139725 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../services/GrsfPublisherFisheryService.java | 62 ++++++++++++++-- .../services/GrsfPublisherStockService.java | 71 ++++++++++++++++--- 2 files changed, 119 insertions(+), 14 deletions(-) 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 dfb2b5c..35f9251 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 @@ -27,10 +27,10 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common; import org.gcube.data_catalogue.grsf_publish_ws.json.input.DeleteProductBean; import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord; import org.gcube.data_catalogue.grsf_publish_ws.json.input.RefersToBean; +import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean; import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean; import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Product_Type; @@ -164,7 +164,7 @@ public class GrsfPublisherFisheryService { + " The current Manage context is " + (String)contextServlet.getInitParameter(HelperMethods.MANAGE_CONTEX_KEY) + " The current Public context is " + (String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY) ); - + if(contextFromStatus.equals(HelperMethods.MANAGE_CONTEX_KEY)){ if(sourceInPath.equals(Sources.GRSF)){ record.setRecordType(Record_Type.AGGREGATED); @@ -307,7 +307,7 @@ public class GrsfPublisherFisheryService { Sources sourceInPath = Sources.onDeserialize(source); if(sourceInPath == null) - throw new Exception("The specified source in the path is unrecognized. Values accepted are [rams, firms, fishsource, grsf]"); + throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]"); logger.info("The request is to create a stock object of source " + sourceInPath); @@ -322,7 +322,7 @@ public class GrsfPublisherFisheryService { } // get extras and check there is the product type - if(fisheryInCkan.getExtrasAsHashMap().get(Common.PRODUCT_TYPE_KEY).equals(Product_Type.FISHERY.getOrigName())){ + if(catalogue.isDatasetInGroup(source + "-" + "fishery", recordToDelete.getId())){ logger.warn("Ok, this is a fishery, removing it"); boolean deleted = catalogue.deleteProduct(fisheryInCkan.getId(), catalogue.getApiKeyFromUsername(username), true); if(deleted){ @@ -337,7 +337,7 @@ public class GrsfPublisherFisheryService { }else{ status = Status.BAD_REQUEST; - throw new Exception("The id you are using doesn't belong to a Fishery product!"); + throw new Exception("The id you are using doesn't belong to a Fishery product having source " + source + "!"); } }catch(Exception e){ logger.error("Failed to delete this "); @@ -348,4 +348,56 @@ public class GrsfPublisherFisheryService { return Response.status(status).entity(responseBean).build(); } + @GET + @Path("get-fisheries-ids") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response getFisheriesIds( + @PathParam("source") String source){ + + // retrieve context and username + String context = ScopeProvider.instance.get(); + + ResponseBean responseBean = new ResponseBean(); + Status status = Status.INTERNAL_SERVER_ERROR; + + logger.info("Received call to get fisheries with source " + source); + + List datasetsIds = new ArrayList(); + + try{ + + // Cast the source to the accepted ones + Sources sourceInPath = Sources.onDeserialize(source); + + if(sourceInPath == null) + throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]"); + + DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); + if(catalogue == null){ + + status = Status.INTERNAL_SERVER_ERROR; + throw new Exception("There was a problem while serving your request"); + + } + + List datasets = catalogue.getProductsInGroup(source + "-" + "fishery"); + + for (CkanDataset ckanDataset : datasets) { + datasetsIds.add(ckanDataset.getId()); + } + + responseBean.setResult(datasetsIds); + responseBean.setSuccess(true); + + }catch(Exception e){ + logger.error("Failed to delete this ", e); + status = Status.INTERNAL_SERVER_ERROR; + responseBean.setSuccess(false); + responseBean.setMessage(e.getMessage()); + } + + return Response.status(status).entity(responseBean).build(); + } + } 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 074e0a7..b837611 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 @@ -27,10 +27,10 @@ import org.gcube.common.authorization.library.provider.AuthorizationProvider; import org.gcube.common.authorization.library.provider.SecurityTokenProvider; import org.gcube.common.authorization.library.utils.Caller; import org.gcube.common.scope.api.ScopeProvider; -import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common; import org.gcube.data_catalogue.grsf_publish_ws.json.input.DeleteProductBean; import org.gcube.data_catalogue.grsf_publish_ws.json.input.RefersToBean; import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord; +import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseBean; import org.gcube.data_catalogue.grsf_publish_ws.json.output.ResponseCreationBean; import org.gcube.data_catalogue.grsf_publish_ws.utils.HelperMethods; import org.gcube.data_catalogue.grsf_publish_ws.utils.groups.Product_Type; @@ -106,7 +106,7 @@ public class GrsfPublisherStockService { Sources sourceInPath = Sources.onDeserialize(source); if(sourceInPath == null) - throw new Exception("The specified source in the path is unrecogized. Values accepted are [rams, firms, fishsource, grsf]"); + throw new Exception("The specified source in the path is unrecogized. Values accepted are [ram, firms, fishsource, grsf]"); logger.info("The request is to create a stock object of source " + sourceInPath); @@ -300,9 +300,9 @@ public class GrsfPublisherStockService { Sources sourceInPath = Sources.onDeserialize(source); if(sourceInPath == null) - throw new Exception("The specified source in the path is unrecognized. Values accepted are [rams, firms, fishsource, grsf]"); + throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]"); - logger.info("The request is to create a stock object of source " + sourceInPath); + logger.info("The request is to delete a stock object of source " + sourceInPath); // retrieve the catalogue instance CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), catalogue.getApiKeyFromUsername(username)); @@ -313,11 +313,11 @@ public class GrsfPublisherStockService { throw new Exception("There was a problem while serving your request. This product was not found"); } - + // get extras and check there is the product type - if(stockInCkan.getExtrasAsHashMap().get(Common.PRODUCT_TYPE_KEY).equals(Product_Type.STOCK.getOrigName())){ - - logger.warn("Ok, this is a stock, removing it"); + if(catalogue.isDatasetInGroup(source + "-" + "stock", recordToDelete.getId())){ + + logger.warn("Ok, this is a stock of the right type, removing it"); boolean deleted = catalogue.deleteProduct(stockInCkan.getId(), catalogue.getApiKeyFromUsername(username), true); if(deleted){ logger.info("Stock DELETED AND PURGED!"); @@ -331,7 +331,7 @@ public class GrsfPublisherStockService { }else{ status = Status.BAD_REQUEST; - throw new Exception("The id you are using doesn't belong to a Stock product!"); + throw new Exception("The id you are using doesn't belong to a Stock product having source " + source + "!"); } }catch(Exception e){ @@ -342,5 +342,58 @@ public class GrsfPublisherStockService { return Response.status(status).entity(responseBean).build(); } + + @GET + @Path("get-stocks-ids") + @Consumes(MediaType.APPLICATION_JSON) + @Produces(MediaType.APPLICATION_JSON) + public Response getStocksIds( + @PathParam("source") String source){ + + // retrieve context and username + String context = ScopeProvider.instance.get(); + + ResponseBean responseBean = new ResponseBean(); + Status status = Status.INTERNAL_SERVER_ERROR; + + // check it is a stock ... + logger.info("Received call to get stocks with source " + source); + + List datasetsIds = new ArrayList(); + + try{ + + // Cast the source to the accepted ones + Sources sourceInPath = Sources.onDeserialize(source); + + if(sourceInPath == null) + throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]"); + + DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); + if(catalogue == null){ + + status = Status.INTERNAL_SERVER_ERROR; + throw new Exception("There was a problem while serving your request"); + + } + + List datasets = catalogue.getProductsInGroup(source + "-" + "stock"); + + for (CkanDataset ckanDataset : datasets) { + datasetsIds.add(ckanDataset.getId()); + } + + responseBean.setResult(datasetsIds); + responseBean.setSuccess(true); + + }catch(Exception e){ + logger.error("Failed to delete this ", e); + status = Status.INTERNAL_SERVER_ERROR; + responseBean.setSuccess(false); + responseBean.setMessage(e.getMessage()); + } + + return Response.status(status).entity(responseBean).build(); + } }