From 4d9c4888b29f2f2571bfc77db8f4c812b0289c5f Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Fri, 27 Oct 2017 14:04:47 +0000 Subject: [PATCH] minor fixes to csv git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/grsf-publisher-ws@157715 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../json/input/others/TimeSeriesBean.java | 6 +- .../services/GrsfPublisherFisheryService.java | 122 +++++++++--------- .../services/GrsfPublisherStockService.java | 10 +- .../utils/CommonServiceUtils.java | 3 +- .../utils/GcoreEndPointReaderSocial.java | 6 +- .../grsf_publish_ws/utils/csv/CSVUtils.java | 9 +- 6 files changed, 78 insertions(+), 78 deletions(-) diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/others/TimeSeriesBean.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/others/TimeSeriesBean.java index 69da699..258f833 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/others/TimeSeriesBean.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/json/input/others/TimeSeriesBean.java @@ -82,7 +82,7 @@ public class TimeSeriesBean implements Comparable>{ } public void setYear(Long year) { - this.year = year; + this.year = year == null? -1 : year; } public T1 getUnit() { @@ -132,7 +132,7 @@ public class TimeSeriesBean implements Comparable>{ @Override public int compareTo(TimeSeriesBean o) { - return (int) (this.year - o.year); // ascending.. low to highest + return Long.compare(this.year, o.year); } @Override @@ -142,7 +142,7 @@ public class TimeSeriesBean implements Comparable>{ String unit = (this.unit != null ? " " + this.unit : ""); String databaseSource = (this.databaseSource != null ? " (" + this.databaseSource + ")" : ""); String dataOwner = (this.dataOwner != null ? " " + this.dataOwner : ""); - String referenceYear = " Ref. Year " + year; + String referenceYear = year >= 0 ? " Ref. Year " + year : ""; String reportingYearOrAssessment = (assessment != null ? " and Rep. Year or Assessment Id " + assessment + "" : ""); return value + unit + databaseSource + dataOwner + referenceYear + reportingYearOrAssessment; 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 05c069d..4f30991 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 @@ -49,7 +49,7 @@ import eu.trentorise.opendata.jackan.model.CkanDataset; * Fishery web service methods. * @author Costantino Perciante at ISTI-CNR (costantino.perciante@isti.cnr.it) */ -@Path("{source:firms|FIRMS|ram|RAM|grsf|GRSF|FishSource|fishsource}/fishery/") +@Path("{source:firms|FIRMS|grsf|GRSF|FishSource|fishsource}/fishery/") public class GrsfPublisherFisheryService { // the context @@ -108,9 +108,9 @@ public class GrsfPublisherFisheryService { }else{ String apiKey = catalogue.getApiKeyFromUsername(username); - String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin"; + 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); @@ -136,7 +136,19 @@ public class GrsfPublisherFisheryService { List resources = record.getExtrasResources(); // validate end set sources - CommonServiceUtils.validateRecordAndMapFields(apiKey, context, contextServlet, sourceInPath, record, Product_Type.FISHERY, tags, customFields, groups, resources, username, futureTitle); + CommonServiceUtils.validateRecordAndMapFields( + apiKey, + context, + contextServlet, + sourceInPath, + record, + Product_Type.FISHERY, + tags, + customFields, + groups, + resources, + username, + futureTitle); // check the license id String license = null; @@ -145,7 +157,8 @@ public class GrsfPublisherFisheryService { else if(HelperMethods.existsLicenseId(record.getLicense(), catalogue)) license = record.getLicense(); - else throw new Exception("Please check the license id!"); + else + throw new Exception("Please check the license id!"); long version = record.getVersion() == null ? 1 : record.getVersion(); @@ -159,7 +172,7 @@ public class GrsfPublisherFisheryService { throw new Exception("Failed to retrieve the namespaces for the key fields!"); customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF)); - + String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath); logger.info("Invoking create method.."); @@ -186,16 +199,16 @@ public class GrsfPublisherFisheryService { if(id != null){ logger.info("Created record with identifier " + id); - String description = "Short Name: " + record.getShortName() + "\n"; - + String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n"; + if(sourceInPath.equals(Sources.GRSF)) - description += ", GRSF Semantic Identifier: " + record.getFisheryId() + "\n"; - + description += ", " + Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getFisheryId() + "\n"; + CommonServiceUtils.actionsPostCreateOrUpdate( id, futureName, record, apiKey, username, organization, null, responseBean, catalogue, namespaces, groups, context, token, futureTitle, authorFullname, contextServlet, false, description); - + status = Status.CREATED; }else{ @@ -261,10 +274,6 @@ public class GrsfPublisherFisheryService { status = Status.OK; responseBean.setId(fisheryInCkan.getId()); } - else{ - status = Status.INTERNAL_SERVER_ERROR; - throw new Exception("Request failed, sorry. Unable to delete/purge the fishery"); - } }else{ status = Status.BAD_REQUEST; throw new Exception("The id you are using doesn't belong to a Fishery item having source " + source + "!"); @@ -394,7 +403,7 @@ public class GrsfPublisherFisheryService { if(catalogId == null || catalogId.isEmpty()){ status = Status.BAD_REQUEST; - throw new Exception("Please specify the 'catalog_id' property"); + throw new Exception("Please specify the '" + Constants.CATALOG_ID + "' property"); } DataCatalogue catalogue = HelperMethods.getDataCatalogueRunningInstance(context); @@ -419,13 +428,12 @@ public class GrsfPublisherFisheryService { throw new Exception("Sorry but there was not possible to retrieve your fullname/email!"); } - - String organization = HelperMethods.retrieveOrgNameFromScope(context); //"grsf_admin"; + String organization = HelperMethods.retrieveOrgNameFromScope(context); // check he/she has admin role CommonServiceUtils.hasAdminRole(username, catalogue, apiKey, organization); - // name, title, product url and are going to remain unchanged (so we keep them from the publisher record); + // name, title, product url and are going to remain unchanged (so we keep them from the already published record); String name = recordPublished.getName(); String title = recordPublished.getTitle(); @@ -467,62 +475,58 @@ public class GrsfPublisherFisheryService { // set the visibility of the datatest according the context boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY)); - // add the SYSTEM_TYPE - customFields.put(Constants.SYSTEM_TYPE_CUSTOM_KEY, Arrays.asList(sourceInPath.getOrigName())); - // convert extras' keys to keys with namespace Map namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_FISHERY); if(namespaces == null) throw new Exception("Failed to retrieve the namespaces for the key fields!"); - // retrieve the url + // retrieve the already generated url String modifiedUUIDKey = namespaces.containsKey(Constants.ITEM_URL_FIELD) ? namespaces.get(Constants.ITEM_URL_FIELD) : Constants.ITEM_URL_FIELD; - String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey); - customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl)); + String itemUrl = recordPublished.getExtrasAsHashMap().get(modifiedUUIDKey); + customFields.put(Constants.ITEM_URL_FIELD, Arrays.asList(itemUrl)); - // replace fields - customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF)); - - String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath); + // replace fields + customFields = HelperMethods.replaceFieldsKey(customFields, namespaces, !sourceInPath.equals(Sources.GRSF)); - logger.info("Invoking update method.."); + String publishInOrganization = CommonServiceUtils.evaluateOrganization(organization, sourceInPath); - // update the product - String id = catalogue.updateCKanDataset( - apiKey, - catalogId, - title, name, - publishInOrganization, - authorFullname, - authorMail, - record.getMaintainer(), - record.getMaintainerContact(), - version, - HelperMethods.removeHTML(record.getDescription()), - license, - new ArrayList(tags), - null, // remove any previous group - customFields, - resources, - publicDataset); + logger.info("Invoking update method.."); - if(id != null){ - logger.info("Item updated!"); - CommonServiceUtils.actionsPostCreateOrUpdate( - id, name, record, apiKey, username, organization, - itemUrl, responseBean, catalogue, namespaces, groups, context, token, title, authorFullname, - contextServlet, false, null); - status = Status.OK; - }else{ - throw new Exception("There was an error during the item updated, sorry"); - } + // update the product + String id = catalogue.updateCKanDataset( + apiKey, + catalogId, + title, name, + publishInOrganization, + authorFullname, + authorMail, + record.getMaintainer(), + record.getMaintainerContact(), + version, + HelperMethods.removeHTML(record.getDescription()), + license, + new ArrayList(tags), + null, // remove any previous group + customFields, + resources, + publicDataset); + + if(id != null){ + logger.info("Item updated!"); + CommonServiceUtils.actionsPostCreateOrUpdate( + id, name, record, apiKey, username, organization, + itemUrl, responseBean, catalogue, namespaces, groups, context, token, title, authorFullname, + contextServlet, false, null); + status = Status.OK; + }else{ + throw new Exception("There was an error during the item updated, sorry"); + } } }catch(Exception e){ logger.error("Failed to update fishery record" + e); - status = Status.INTERNAL_SERVER_ERROR; responseBean.setError(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 2625640..9693ef4 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 @@ -200,15 +200,16 @@ public class GrsfPublisherStockService { if(id != null){ logger.info("Product created! Id is " + id); - String description = "Short Name: " + record.getShortName() + "\n"; + String description = Constants.SHORT_NAME_CUSTOM_KEY + ": " + record.getShortName() + "\n"; if(sourceInPath.equals(Sources.GRSF)) - description += ", GRSF Semantic Identifier: " + record.getStockId() + "\n"; + description += ", " + Constants.GRSF_SEMANTIC_IDENTIFIER_CUSTOM_KEY + ": " + record.getStockId() + "\n"; CommonServiceUtils.actionsPostCreateOrUpdate( id, futureName, record, apiKey, username, organization, null, responseBean, catalogue, namespaces, groups, context, token, futureTitle, authorFullname, contextServlet, false, description); + status = Status.CREATED; }else @@ -323,7 +324,6 @@ public class GrsfPublisherStockService { for (String id : fullGroupListIds) { CkanDataset dataset = catalogue.getDataset(id, catalogue.getApiKeyFromUsername(username)); if(dataset != null){ - String type = dataset.getExtrasAsHashMap().get(Constants.DOMAIN_CUSTOM_KEY); if(Product_Type.STOCK.getOrigName().equals(type)) datasetsIds.add(id); @@ -474,9 +474,6 @@ public class GrsfPublisherStockService { // set the visibility of the datatest according the context boolean publicDataset = context.equals((String)contextServlet.getInitParameter(HelperMethods.PUBLIC_CONTEX_KEY)); - // add the SYSTEM_TYPE - customFields.put(Constants.SYSTEM_TYPE_CUSTOM_KEY, Arrays.asList(sourceInPath.getOrigName())); - // convert extras' keys to keys with namespace Map namespaces = HelperMethods.getFieldToFieldNameSpaceMapping(Constants.GENERIC_RESOURCE_NAME_MAP_KEY_NAMESPACES_STOCK); @@ -529,7 +526,6 @@ public class GrsfPublisherStockService { } }catch(Exception e){ logger.error("Failed to update stock record", e); - status = Status.INTERNAL_SERVER_ERROR; responseBean.setError(e.getMessage()); } return Response.status(status).entity(responseBean).build(); 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 6d04442..f2c18ec 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 @@ -61,7 +61,6 @@ public class CommonServiceUtils { licenses = HelperMethods.getLicenses(HelperMethods.getDataCatalogueRunningInstance(ScopeProvider.instance.get())); }catch(Exception e){ logger.error("Failed to retrieve the list of licenses"); - return null; } return licenses; } @@ -583,7 +582,7 @@ public class CommonServiceUtils { } /** - * Evaluate in which organization a record has to be published. The only expcetion is when grsf_admin is involved. + * Evaluate in which organization a record has to be published. The only exception is when grsf_admin is involved. * @param organization * @param sourceInPath * @return 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/utils/GcoreEndPointReaderSocial.java index bf1edbe..d6f9433 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/utils/GcoreEndPointReaderSocial.java @@ -41,17 +41,17 @@ public class GcoreEndPointReaderSocial { query.addCondition("$resource/Profile/DeploymentData/Status/text() eq 'ready'"); query.addCondition(String.format("$resource/Profile/ServiceName/text() eq '%s'",serviceName)); query.setResult("$resource/Profile/AccessPoint/RunningInstanceInterfaces//Endpoint[@EntryName/string() eq \""+resource+"\"]/text()"); - + DiscoveryClient client = client(); List endpoints = client.submit(query); - if (endpoints == null || endpoints.isEmpty()) throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); + if (endpoints == null || endpoints.isEmpty()) + throw new Exception("Cannot retrieve the GCoreEndpoint serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); this.basePath = endpoints.get(0); if(basePath==null) throw new Exception("Endpoint:"+resource+", is null for serviceName: "+serviceName +", serviceClass: " +serviceClass +", in scope: "+context); - logger.info("found entyname "+basePath+" for ckanResource: "+resource); }catch(Exception e){ logger.error("Unable to retrieve such service endpoint information!", e); diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/csv/CSVUtils.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/csv/CSVUtils.java index 6f581e2..cc26298 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/csv/CSVUtils.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/csv/CSVUtils.java @@ -108,7 +108,7 @@ public class CSVUtils { for (TimeSeriesBean bean : timeSeries) { StringBuffer oneLine = new StringBuffer(); - oneLine.append(bean.getYear()); + oneLine.append(bean.getYear() > 0 ? bean.getYear() : ""); if(isValuePresent){ oneLine.append(CSV_SEPARATOR); @@ -144,9 +144,6 @@ public class CSVUtils { bw.close(); - // file created - logger.debug("CSV file created correctly on this machine!"); - // Add names of the sources to the file's name for (String source : sources) { relevantSources[0] += source + "_"; @@ -155,6 +152,10 @@ public class CSVUtils { // on exit delete it... file.deleteOnExit(); + + // file created + logger.debug("CSV file created correctly on this machine!"); + return file; } catch(Exception e){