minor fixes

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@139877 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2016-12-10 10:24:47 +00:00
parent ae56c46861
commit 487ba3a00c
3 changed files with 31 additions and 47 deletions

View File

@ -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.custom_annotations.Tag;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Base; 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.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.RefersToBean;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.Resource; import org.gcube.data_catalogue.grsf_publish_ws.json.input.Resource;
import org.gcube.data_catalogue.grsf_publish_ws.json.input.StockRecord; 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"); 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<String> fishingArea = fishery.getFishingArea();
List<String> 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!");
}
} }

View File

@ -111,15 +111,15 @@ public class GrsfPublisherFisheryService {
// Cast the source to the accepted ones // Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source); 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()); throw new Exception("The specified source in the path is unrecognized. Values accepted are " + Sources.getAsList());
}
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
}else{ }else{
String apiKey = catalogue.getApiKeyFromUsername(username); String apiKey = catalogue.getApiKeyFromUsername(username);
@ -137,16 +137,9 @@ public class GrsfPublisherFisheryService {
// fishing area and jurisdiction area cannot be empty at the same time // fishing area and jurisdiction area cannot be empty at the same time
String futureName = record.getUuid(); String futureName = record.getUuid();
String futureTitle = record.getFisheryName(); String futureTitle = record.getFisheryName();
List<String> fishingArea = record.getFishingArea();
List<String> jurisdictionArea = record.getJurisdictionArea();
if(!HelperMethods.isNameValid(futureName)){ if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST; status = Status.BAD_REQUEST;
throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'"); 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{ }else{
logger.debug("Checking if such name [" + futureName + "] doesn't exist yet..."); 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"); 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"); throw new Exception("There was an error during the product generation, sorry");
}
} }
} }
} }
@ -430,7 +423,7 @@ public class GrsfPublisherFisheryService {
String username = caller.getClient().getId(); String username = caller.getClient().getId();
ResponseBean responseBean = new ResponseBean(); 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); 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); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
} }
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username)); CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
@ -449,8 +439,6 @@ public class GrsfPublisherFisheryService {
responseBean.setResult(dataset.getId()); responseBean.setResult(dataset.getId());
responseBean.setSuccess(true); responseBean.setSuccess(true);
}else{ }else{
responseBean.setResult(null);
responseBean.setSuccess(false);
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name); responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
} }

View File

@ -115,10 +115,7 @@ public class GrsfPublisherStockService {
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
}else{ }else{
String apiKey = catalogue.getApiKeyFromUsername(username); String apiKey = catalogue.getApiKeyFromUsername(username);
@ -128,31 +125,25 @@ public class GrsfPublisherStockService {
String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey); String role = catalogue.getRoleOfUserInOrganization(username, organization, apiKey);
logger.info("Role of the user " + username + " is " + role); logger.info("Role of the user " + username + " is " + role);
if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){ if(!role.equalsIgnoreCase(RolesCkanGroupOrOrg.ADMIN.toString())){
status = Status.FORBIDDEN; status = Status.FORBIDDEN;
throw new Exception("You are not authorized to create a product. Please check you have the Catalogue-Administrator role!"); 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 // check the record has a name, at least
String futureName = record.getUuid(); String futureName = record.getUuid();
String futureTitle = record.getStockName(); String futureTitle = record.getStockName();
if(!HelperMethods.isNameValid(futureName)){ if(!HelperMethods.isNameValid(futureName)){
status = Status.BAD_REQUEST; status = Status.BAD_REQUEST;
throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'"); throw new Exception("The 'uuid_knowledge_base' must contain only alphanumeric characters, and symbols like '.' or '_', '-'");
}else{ }else{
logger.debug("Checking if such 'uuid_knowledge_base' [" + futureName + "] doesn't exist yet..."); logger.debug("Checking if such 'uuid_knowledge_base' [" + futureName + "] doesn't exist yet...");
boolean alreadyExist = catalogue.existProductWithNameOrId(futureName); boolean alreadyExist = catalogue.existProductWithNameOrId(futureName);
if(alreadyExist){ if(alreadyExist){
logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists"); logger.debug("A product with 'uuid_knowledge_base' " + futureName + " already exists");
status = Status.CONFLICT; status = Status.CONFLICT;
throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists"); throw new Exception("A product with 'uuid_knowledge_base' " + futureName + " already exists");
}else{ }else{
// validate the record if it is a GRSF one and set the record type and in manage context // 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); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
} }
// Cast the source to the accepted ones // Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source); 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]"); 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); logger.info("The request is to delete a stock object of source " + sourceInPath);
// retrieve the catalogue instance // retrieve the catalogue instance
CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), catalogue.getApiKeyFromUsername(username)); CkanDataset stockInCkan = catalogue.getDataset(recordToDelete.getId(), catalogue.getApiKeyFromUsername(username));
if(stockInCkan == null){ if(stockInCkan == null){
status = Status.NOT_FOUND; status = Status.NOT_FOUND;
throw new Exception("There was a problem while serving your request. This product was 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 // get extras and check there is the product type
@ -353,7 +342,6 @@ public class GrsfPublisherStockService {
}catch(Exception e){ }catch(Exception e){
logger.error("Failed to delete this ", e); logger.error("Failed to delete this ", e);
status = Status.INTERNAL_SERVER_ERROR;
responseBean.setError(e.getMessage()); responseBean.setError(e.getMessage());
} }
@ -370,7 +358,7 @@ public class GrsfPublisherStockService {
String context = ScopeProvider.instance.get(); String context = ScopeProvider.instance.get();
ResponseBean responseBean = new ResponseBean(); ResponseBean responseBean = new ResponseBean();
Status status = Status.INTERNAL_SERVER_ERROR; Status status = Status.OK;
// check it is a stock ... // check it is a stock ...
logger.info("Received call to get stocks with source " + source); logger.info("Received call to get stocks with source " + source);
@ -382,15 +370,15 @@ public class GrsfPublisherStockService {
// Cast the source to the accepted ones // Cast the source to the accepted ones
Sources sourceInPath = Sources.onDeserialize(source); 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]"); throw new Exception("The specified source in the path is unrecognized. Values accepted are [ram, firms, fishsource, grsf]");
}
DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
} }
List<CkanDataset> datasets = catalogue.getProductsInGroup(source + "-" + "stock"); List<CkanDataset> datasets = catalogue.getProductsInGroup(source + "-" + "stock");
@ -403,15 +391,14 @@ public class GrsfPublisherStockService {
responseBean.setSuccess(true); responseBean.setSuccess(true);
}catch(Exception e){ }catch(Exception e){
logger.error("Failed to delete this ", e); logger.error("Failed to fetch stocks ids of source " + source, e);
status = Status.INTERNAL_SERVER_ERROR;
responseBean.setSuccess(false); responseBean.setSuccess(false);
responseBean.setMessage(e.getMessage()); responseBean.setMessage(e.getMessage());
} }
return Response.status(status).entity(responseBean).build(); return Response.status(status).entity(responseBean).build();
} }
@GET @GET
@Path("get-catalogue-id-from-name") @Path("get-catalogue-id-from-name")
@Produces(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON)
@ -424,7 +411,7 @@ public class GrsfPublisherStockService {
String username = caller.getClient().getId(); String username = caller.getClient().getId();
ResponseBean responseBean = new ResponseBean(); 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); 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); DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context);
if(catalogue == null){ if(catalogue == null){
status = Status.INTERNAL_SERVER_ERROR; status = Status.INTERNAL_SERVER_ERROR;
throw new Exception("There was a problem while serving your request"); throw new Exception("There was a problem while serving your request");
} }
CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username)); CkanDataset dataset = catalogue.getDataset(name, catalogue.getApiKeyFromUsername(username));
@ -443,14 +428,12 @@ public class GrsfPublisherStockService {
responseBean.setResult(dataset.getId()); responseBean.setResult(dataset.getId());
responseBean.setSuccess(true); responseBean.setSuccess(true);
}else{ }else{
responseBean.setResult(null); status = Status.INTERNAL_SERVER_ERROR;
responseBean.setSuccess(false);
responseBean.setMessage("Unable to retrieve a catalogue product with name " + name); responseBean.setMessage("Unable to retrieve a catalogue product with name " + name);
} }
}catch(Exception e){ }catch(Exception e){
logger.error("Failed to retrieve this product", e); logger.error("Failed to retrieve this product", e);
status = Status.INTERNAL_SERVER_ERROR;
responseBean.setSuccess(false); responseBean.setSuccess(false);
responseBean.setMessage(e.getMessage()); responseBean.setMessage(e.getMessage());
} }