From 487ba3a00ce79e5d1fdb710cf6a950cd52f5141b Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Sat, 10 Dec 2016 10:24:47 +0000 Subject: [PATCH] minor fixes git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@139877 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../services/CommonServiceUtils.java | 13 ++++++ .../services/GrsfPublisherFisheryService.java | 24 +++-------- .../services/GrsfPublisherStockService.java | 41 ++++++------------- 3 files changed, 31 insertions(+), 47 deletions(-) diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/CommonServiceUtils.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/CommonServiceUtils.java index 6568d4c..76a11bd 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/CommonServiceUtils.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/services/CommonServiceUtils.java @@ -15,6 +15,7 @@ import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Group; import org.gcube.data_catalogue.grsf_publish_ws.custom_annotations.Tag; import org.gcube.data_catalogue.grsf_publish_ws.json.input.Base; import org.gcube.data_catalogue.grsf_publish_ws.json.input.Common; +import org.gcube.data_catalogue.grsf_publish_ws.json.input.FisheryRecord; import org.gcube.data_catalogue.grsf_publish_ws.json.input.RefersToBean; import org.gcube.data_catalogue.grsf_publish_ws.json.input.Resource; import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord; @@ -88,6 +89,18 @@ public class CommonServiceUtils { throw new Exception("species cannot be null/empty in a GRSF record"); } + // check if it is a stock and perform related checks + if(record.getClass().equals(FisheryRecord.class)){ + + FisheryRecord fishery = (FisheryRecord) record; + + List fishingArea = fishery.getFishingArea(); + List jurisdictionArea = fishery.getJurisdictionArea(); + + if((fishingArea == null || fishingArea.isEmpty()) && (jurisdictionArea == null || jurisdictionArea.isEmpty())) + throw new Exception("fishing_area and jurisdiction_area cannot be null/empty at the same time!"); + } + } 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 8f927c4..d4b8240 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 @@ -111,15 +111,15 @@ public class GrsfPublisherFisheryService { // Cast the source to the accepted ones Sources sourceInPath = Sources.onDeserialize(source); - if(sourceInPath == null) + if(sourceInPath == null){ + status = Status.BAD_REQUEST; throw new Exception("The specified source in the path is unrecognized. Values accepted are " + Sources.getAsList()); + } DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); if(catalogue == null){ - status = Status.INTERNAL_SERVER_ERROR; throw new Exception("There was a problem while serving your request"); - }else{ String apiKey = catalogue.getApiKeyFromUsername(username); @@ -137,16 +137,9 @@ public class GrsfPublisherFisheryService { // fishing area and jurisdiction area cannot be empty at the same time String futureName = record.getUuid(); String futureTitle = record.getFisheryName(); - List fishingArea = record.getFishingArea(); - List jurisdictionArea = record.getJurisdictionArea(); if(!HelperMethods.isNameValid(futureName)){ - status = Status.BAD_REQUEST; throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'"); - - }else if((fishingArea == null || fishingArea.isEmpty()) && (jurisdictionArea == null || jurisdictionArea.isEmpty()) && sourceInPath.equals(Sources.GRSF)){ - status = Status.BAD_REQUEST; - throw new Exception("fishing_area and jurisdiction_area cannot be null/empty at the same time!"); }else{ logger.debug("Checking if such name [" + futureName + "] doesn't exist yet..."); @@ -277,9 +270,9 @@ public class GrsfPublisherFisheryService { logger.info("Thread to write a post about the new product has been launched"); } - }else + }else{ throw new Exception("There was an error during the product generation, sorry"); - + } } } } @@ -430,7 +423,7 @@ public class GrsfPublisherFisheryService { String username = caller.getClient().getId(); ResponseBean responseBean = new ResponseBean(); - Status status = Status.INTERNAL_SERVER_ERROR; + Status status = Status.OK; logger.info("Received call to get the catalogue identifier for the product with name " + name); @@ -438,10 +431,7 @@ public class GrsfPublisherFisheryService { DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); if(catalogue == null){ - - status = Status.INTERNAL_SERVER_ERROR; throw new Exception("There was a problem while serving your request"); - } CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username)); @@ -449,8 +439,6 @@ public class GrsfPublisherFisheryService { responseBean.setResult(dataset.getId()); responseBean.setSuccess(true); }else{ - responseBean.setResult(null); - responseBean.setSuccess(false); responseBean.setMessage("Unable to retrieve a catalogue product with name " + name); } 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 b80e56c..00afa4f 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 @@ -115,10 +115,7 @@ public class GrsfPublisherStockService { DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); if(catalogue == null){ - - status = Status.INTERNAL_SERVER_ERROR; throw new Exception("There was a problem while serving your request"); - }else{ String apiKey = catalogue.getApiKeyFromUsername(username); @@ -128,31 +125,25 @@ public class GrsfPublisherStockService { String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey); logger.info("Role of the user " + username + " is " + role); if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){ - status = Status.FORBIDDEN; throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-Administrator role!"); - } // check the record has a name, at least String futureName = record.getUuid(); String futureTitle = record.getStockName(); if(!HelperMethods.isNameValid(futureName)){ - status = Status.BAD_REQUEST; throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'"); - }else{ logger.debug("Checking if such 'uuid_knowledge_base' [" + futureName + "] doesn't exist yet..."); boolean alreadyExist = catalogue.existProductWithNameOrId(futureName); if(alreadyExist){ - logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists"); status = Status.CONFLICT; throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists"); - }else{ // validate the record if it is a GRSF one and set the record type and in manage context @@ -307,28 +298,26 @@ public class GrsfPublisherStockService { 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); - if(sourceInPath == null) + if(sourceInPath == null){ + status = Status.BAD_REQUEST; throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]"); - + } + 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)); if(stockInCkan == null){ - status = Status.NOT_FOUND; 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 @@ -353,7 +342,6 @@ public class GrsfPublisherStockService { }catch(Exception e){ logger.error("Failed to delete this ", e); - status = Status.INTERNAL_SERVER_ERROR; responseBean.setError(e.getMessage()); } @@ -370,7 +358,7 @@ public class GrsfPublisherStockService { String context = ScopeProvider.instance.get(); ResponseBean responseBean = new ResponseBean(); - Status status = Status.INTERNAL_SERVER_ERROR; + Status status = Status.OK; // check it is a stock ... logger.info("Received call to get stocks with source " + source); @@ -382,15 +370,15 @@ public class GrsfPublisherStockService { // Cast the source to the accepted ones Sources sourceInPath = Sources.onDeserialize(source); - if(sourceInPath == null) + if(sourceInPath == null){ + status = Status.BAD_REQUEST; 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"); @@ -403,15 +391,14 @@ public class GrsfPublisherStockService { responseBean.setSuccess(true); }catch(Exception e){ - logger.error("Failed to delete this ", e); - status = Status.INTERNAL_SERVER_ERROR; + logger.error("Failed to fetch stocks ids of source " + source, e); responseBean.setSuccess(false); responseBean.setMessage(e.getMessage()); } return Response.status(status).entity(responseBean).build(); } - + @GET @Path("get-catalogue-id-from-name") @Produces(MediaType.APPLICATION_JSON) @@ -424,7 +411,7 @@ public class GrsfPublisherStockService { String username = caller.getClient().getId(); ResponseBean responseBean = new ResponseBean(); - Status status = Status.INTERNAL_SERVER_ERROR; + Status status = Status.OK; logger.info("Received call to get the catalogue identifier for the product with name " + name); @@ -432,10 +419,8 @@ public class GrsfPublisherStockService { DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); if(catalogue == null){ - status = Status.INTERNAL_SERVER_ERROR; throw new Exception("There was a problem while serving your request"); - } CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username)); @@ -443,14 +428,12 @@ public class GrsfPublisherStockService { responseBean.setResult(dataset.getId()); responseBean.setSuccess(true); }else{ - responseBean.setResult(null); - responseBean.setSuccess(false); + status = Status.INTERNAL_SERVER_ERROR; responseBean.setMessage("Unable to retrieve a catalogue product with name " + name); } }catch(Exception e){ logger.error("Failed to retrieve this product", e); - status = Status.INTERNAL_SERVER_ERROR; responseBean.setSuccess(false); responseBean.setMessage(e.getMessage()); }