From 5d5b872f08b37522c1600531c625ebc7ee255aa6 Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Mon, 8 May 2017 08:26:06 +0000 Subject: [PATCH] minor fixes git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@148344 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../catalogue/utils/CatalogueUtils.java | 61 ++++++++++--------- .../datacatalogue/catalogue/ws/Item.java | 4 +- 2 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java b/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java index 123de95..9228deb 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java @@ -93,6 +93,8 @@ public class CatalogueUtils { public static final short MAX_UPLOADABLE_FILE_SIZE_MB = 100; public static final String EMAIL_IN_PROFILE_KEY = "email"; public static final String FULLNAME_IN_PROFILE_KEY = "fullname"; + private static final Object RESOURCE_NAME_KEY = "name"; + private static final Object RESOURCE_URL_KEY = "url"; // ======================================================================= @@ -206,7 +208,7 @@ public class CatalogueUtils { * @throws Exception */ public static List getNamespaceCategories() throws Exception{ - + Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); String context = ScopeProvider.instance.get(); @@ -217,7 +219,7 @@ public class CatalogueUtils { reader = new DataCalogueMetadataFormatReader(); profilesCache.put(new Element(context, reader)); } - + return reader.getListOfNamespaceCategories(); } @@ -310,8 +312,8 @@ public class CatalogueUtils { while (it.hasNext()) { JSONObject resource = (JSONObject) it.next(); - String name = (String)resource.get("name"); - String url = (String)resource.get("url"); + String name = (String)resource.get(RESOURCE_NAME_KEY); + String url = (String)resource.get(RESOURCE_URL_KEY); if(url == null || name == null || url.isEmpty() || name.isEmpty()) throw new Exception("Resources must have at least a name and an url field set!"); @@ -341,7 +343,7 @@ public class CatalogueUtils { String licenseId = (String)dataset.get(LICENSE_KEY); if(licenseId == null || licenseId.isEmpty()) - throw new Exception("You must specify a license identifier to be attached to the item. License list can be retrieved invoking license methods"); + throw new Exception("You must specify a license identifier to be attached to the item. License list can be retrieved invoking license methods"); // set author and author email JSONObject profileValues = (JSONObject)profile.get(RESULT_KEY); @@ -399,7 +401,7 @@ public class CatalogueUtils { } if(metadataTypeCF == null) - throw new Exception("'" + TYPE_KEY + "' field is missing in context where metadata profile(s) are defined!"); + throw new Exception("'" + TYPE_KEY + "' extra field is missing in context where metadata profile(s) are defined!"); if(groupsArrayOriginal == null) groupsArrayOriginal = new JSONArray(); @@ -418,8 +420,7 @@ public class CatalogueUtils { } if(profile == null) - throw new Exception("'" + TYPE_KEY + "' specified as custom field doesn't match any of the profiles defined in this context!"); - + throw new Exception("'" + TYPE_KEY + "' extra field's value specified as custom field doesn't match any of the profiles defined in this context!"); else{ JSONArray extrasArrayUpdated = null; @@ -433,7 +434,7 @@ public class CatalogueUtils { List categories = getNamespaceCategories(); List categoriesIds = new ArrayList(categories == null ? 0 : categories.size()); if(categoriesIds.isEmpty()) - logger.warn("No category defined"); + logger.warn("No category defined in context " + ScopeProvider.instance.get()); else{ for (NamespaceCategory metadataCategory : categories) categoriesIds.add(metadataCategory.getId()); // save them later for matching with custom fields @@ -445,9 +446,9 @@ public class CatalogueUtils { // keep track of mandatory fields and their cardinality Map fieldsMandatoryLowerBoundMap = new HashMap(metadataFields.size()); Map fieldsMandatoryUpperBoundMap = new HashMap(metadataFields.size()); - Map numberFieldsSameKeyMap = new HashMap(metadataFields.size()); + Map numberFieldsMandatorySameKeyMap = new HashMap(metadataFields.size()); - // keep track of the groups that must be created after validation but before item creation. + // keep track of the groups that must be created AFTER validation but BEFORE item creation. List groupsToCreateAfterValidation = new ArrayList(); // now validate fields @@ -466,7 +467,7 @@ public class CatalogueUtils { categories, fieldsMandatoryLowerBoundMap, fieldsMandatoryUpperBoundMap, - numberFieldsSameKeyMap, + numberFieldsMandatorySameKeyMap, groupsToCreateAfterValidation); validatedCustomFields.addAll(validCFs); metadataIndex++; @@ -480,7 +481,7 @@ public class CatalogueUtils { .next(); int lowerBound = entry.getValue(); int upperBound = fieldsMandatoryUpperBoundMap.get(entry.getKey()); - int inserted = numberFieldsSameKeyMap.get(entry.getKey()); + int inserted = numberFieldsMandatorySameKeyMap.get(entry.getKey()); logger.info("Field with key '" + entry.getKey() + "' has been found " + inserted + " times and its lower bound is " + lowerBound + " and upper bound " + upperBound); @@ -497,7 +498,7 @@ public class CatalogueUtils { for(CustomField cf : customFields) validatedCustomFields.add(cf); - // retransform to json + // convert back to json for (CustomField customField : validatedCustomFields) { JSONObject jsonObj = new JSONObject(); jsonObj.put(EXTRA_KEY, customField.getQualifiedKey()); @@ -512,14 +513,19 @@ public class CatalogueUtils { extrasArrayUpdated.add(metadataTypeJSON); // create groups - for (String title : groupsToCreateAfterValidation) + for (String title : groupsToCreateAfterValidation){ try { createGroupAsSysAdmin(title, title, ""); } catch (Exception e) { logger.error("Failed to create group with title " + title, e); } + } } + // if there are no tags, throw an exception + if(tagsArrayOriginal.isEmpty()) + throw new Exception("Please define at least one tag for this item!"); + obj.put(TAGS_KEY, tagsArrayOriginal); obj.put(GROUPS_KEY, groupsArrayOriginal); obj.put(EXTRAS_KEY, extrasArrayUpdated); @@ -553,13 +559,12 @@ public class CatalogueUtils { List categories, Map fieldsMandatoryLowerBoundMap, Map fieldsMandatoryUpperBoundMap, - Map numberFieldsSameKeyMap, + Map numberFieldsMandatorySameKeyMap, List groupToCreate) throws Exception { List toReturn = new ArrayList(); - String metadataFieldName = metadataField.getFieldName(); - // keep track of the number of elements with this key + String metadataFieldName = metadataField.getFieldName(); int fieldsFoundWithThisKey = 0; Iterator iterator = customFields.iterator(); @@ -590,22 +595,18 @@ public class CatalogueUtils { // upper bound boolean hasVocabulary = metadataField.getVocabulary() != null; - int upperBound = hasVocabulary ? (metadataField.getVocabulary().isMultiSelection() ? Integer.MAX_VALUE : 1) : 1; + int upperBound = hasVocabulary ? (metadataField.getVocabulary().isMultiSelection() ? metadataField.getVocabulary().getVocabularyFields().size() : 1) : 1; + + if(fieldsMandatoryUpperBoundMap.containsKey(metadataFieldName)) + upperBound += fieldsMandatoryUpperBoundMap.get(metadataFieldName); - if(fieldsMandatoryUpperBoundMap.containsKey(metadataFieldName)){ - long tempSum = upperBound + fieldsMandatoryUpperBoundMap.get(metadataFieldName); - if(tempSum > Integer.MAX_VALUE) - upperBound = Integer.MAX_VALUE; - else - upperBound = (int) tempSum; - } fieldsMandatoryUpperBoundMap.put(metadataFieldName, upperBound); // fields with this same key int countPerFields = fieldsFoundWithThisKey; - if(numberFieldsSameKeyMap.containsKey(metadataFieldName)) - countPerFields += numberFieldsSameKeyMap.get(metadataFieldName); - numberFieldsSameKeyMap.put(metadataFieldName, countPerFields); + if(numberFieldsMandatorySameKeyMap.containsKey(metadataFieldName)) + countPerFields += numberFieldsMandatorySameKeyMap.get(metadataFieldName); + numberFieldsMandatorySameKeyMap.put(metadataFieldName, countPerFields); } @@ -970,7 +971,7 @@ public class CatalogueUtils { JSONParser parser = new JSONParser(); JSONObject obj = (JSONObject) parser.parse(jsonRes); obj.put(HELP_KEY, HELP_URL_GCUBE_CATALOGUE); - + logger.debug("replaced information " + obj); return obj.toJSONString(); diff --git a/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java b/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java index 2d898f5..5785782 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java @@ -46,6 +46,7 @@ public class Item { } + @SuppressWarnings("unchecked") @POST @Path(Constants.CREATE_METHOD) @Consumes(MediaType.APPLICATION_JSON) @@ -76,11 +77,10 @@ public class Item { obj = (JSONObject)parser.parse(CatalogueUtils.delegatePost(caller, context, Constants.ITEM_CREATE, obj.toJSONString(), uriInfo)); - // after creation, if it was ok ... + // after creation, if it is ok ... if((boolean)obj.get(CatalogueUtils.SUCCESS_KEY)){ JSONObject result = (JSONObject)obj.get(CatalogueUtils.RESULT_KEY); - DataCatalogue utils = CatalogueUtils.getCatalogue(); // add also this information as custom field