From 82dd47b70cb4a6e2b457887f0a488907323effdc Mon Sep 17 00:00:00 2001 From: Costantino Perciante Date: Fri, 5 May 2017 16:51:22 +0000 Subject: [PATCH] item url is returned even if it has not been added yet to the product. Minor fixes on case of errors coming from ckan itself. Ehcache configuration fixed (explicitly set the tmp path for disk area) git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@148339 82a268e6-3cf1-43bd-a215-b396298e98cf --- .../catalogue/utils/CatalogueUtils.java | 51 ++++++++++++------- .../utils/PackageCreatePostActions.java | 10 ++-- .../datacatalogue/catalogue/ws/Item.java | 20 ++++++-- src/main/resources/ehcache.xml | 7 +-- 4 files changed, 62 insertions(+), 26 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 6e038eb..123de95 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/utils/CatalogueUtils.java @@ -206,8 +206,18 @@ public class CatalogueUtils { * @throws Exception */ public static List getNamespaceCategories() throws Exception{ + + Cache profilesCache = CachesManager.getCache(CachesManager.PROFILES_READERS_CACHE); + String context = ScopeProvider.instance.get(); - DataCalogueMetadataFormatReader reader = new DataCalogueMetadataFormatReader(); + DataCalogueMetadataFormatReader reader; + if(profilesCache.isKeyInCache(context)) + reader = (DataCalogueMetadataFormatReader) profilesCache.get(context).getObjectValue(); + else{ + reader = new DataCalogueMetadataFormatReader(); + profilesCache.put(new Element(context, reader)); + } + return reader.getListOfNamespaceCategories(); } @@ -437,6 +447,9 @@ public class CatalogueUtils { Map fieldsMandatoryUpperBoundMap = new HashMap(metadataFields.size()); Map numberFieldsSameKeyMap = new HashMap(metadataFields.size()); + // keep track of the groups that must be created after validation but before item creation. + List groupsToCreateAfterValidation = new ArrayList(); + // now validate fields int metadataIndex = 0; for (MetadataField metadataField : metadataFields) { @@ -453,7 +466,8 @@ public class CatalogueUtils { categories, fieldsMandatoryLowerBoundMap, fieldsMandatoryUpperBoundMap, - numberFieldsSameKeyMap); + numberFieldsSameKeyMap, + groupsToCreateAfterValidation); validatedCustomFields.addAll(validCFs); metadataIndex++; @@ -471,7 +485,7 @@ public class CatalogueUtils { logger.info("Field with key '" + entry.getKey() + "' has been found " + inserted + " times and its lower bound is " + lowerBound + " and upper bound " + upperBound); if(inserted < lowerBound || inserted > upperBound) - throw new Exception("Field with key '" + entry.getKey() + "' is mandatory, but it's not present among the provided fields or its cardinality is not respected ([" + lowerBound + ", " + upperBound +"])."); + throw new Exception("Field with key '" + entry.getKey() + "' is mandatory, but it's not present among the provided fields or its cardinality is not respected ([min = " + lowerBound + ", max=" + upperBound +"])."); } // sort validated custom fields and add to the extrasArrayUpdated json array @@ -496,11 +510,20 @@ public class CatalogueUtils { metadataTypeJSON.put(EXTRA_KEY, metadataTypeCF.getKey()); metadataTypeJSON.put(EXTRA_VALUE, metadataTypeCF.getValue()); extrasArrayUpdated.add(metadataTypeJSON); + + // create groups + for (String title : groupsToCreateAfterValidation) + try { + createGroupAsSysAdmin(title, title, ""); + } catch (Exception e) { + logger.error("Failed to create group with title " + title, e); + } } obj.put(TAGS_KEY, tagsArrayOriginal); obj.put(GROUPS_KEY, groupsArrayOriginal); obj.put(EXTRAS_KEY, extrasArrayUpdated); + } } @@ -530,7 +553,8 @@ public class CatalogueUtils { List categories, Map fieldsMandatoryLowerBoundMap, Map fieldsMandatoryUpperBoundMap, - Map numberFieldsSameKeyMap) throws Exception { + Map numberFieldsSameKeyMap, + List groupToCreate) throws Exception { List toReturn = new ArrayList(); String metadataFieldName = metadataField.getFieldName(); @@ -547,7 +571,7 @@ public class CatalogueUtils { fieldsFoundWithThisKey ++; cf.setIndexCategory(categoryIndex); cf.setIndexMetadataField(metadataIndex); - checkAsGroup(cf, metadataField, groupsArrayOriginal); + checkAsGroup(cf, metadataField, groupsArrayOriginal, groupToCreate); checkAsTag(cf, metadataField, tagsArrayOriginal); toReturn.add(cf); iterator.remove(); @@ -638,7 +662,7 @@ public class CatalogueUtils { * @param groupsArrayOriginal */ private static void checkAsGroup(CustomField fieldToValidate, - MetadataField metadataField, JSONArray groupsArrayOriginal) { + MetadataField metadataField, JSONArray groupsArrayOriginal, List groupToCreate) { logger.debug("Custom field is " + fieldToValidate); logger.debug("MetadataField field is " + metadataField); @@ -673,17 +697,8 @@ public class CatalogueUtils { // force group creation if needed if(grouping.getCreate()){ - new Thread() { - @Override - public void run() { - for (String title : groupNames) - try { - createGroupAsSysAdmin(title, title, ""); - } catch (Exception e) { - logger.error("Failed to create group with title " + title, e); - } - } - }.start(); + for (String title : groupNames) + groupToCreate.add(title); } } @@ -955,6 +970,8 @@ 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(); }catch(Exception e){ diff --git a/src/main/java/org/gcube/datacatalogue/catalogue/utils/PackageCreatePostActions.java b/src/main/java/org/gcube/datacatalogue/catalogue/utils/PackageCreatePostActions.java index fc8c011..fc09ba2 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/utils/PackageCreatePostActions.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/utils/PackageCreatePostActions.java @@ -23,11 +23,12 @@ public class PackageCreatePostActions extends Thread { private String packageId; private String context; + private String datasetUrl; private String token; private String username; private JSONArray tags; private String title; - private static final String ITEM_URL = "Item URL"; + public static final String ITEM_URL = "Item URL"; private static Logger logger = LoggerFactory.getLogger(PackageCreatePostActions.class); /** @@ -38,6 +39,7 @@ public class PackageCreatePostActions extends Thread { */ public PackageCreatePostActions( String username, + String datasetUrl, String packageId, String context, String token, @@ -45,6 +47,7 @@ public class PackageCreatePostActions extends Thread { String title) { super(); this.packageId = packageId; + this.datasetUrl = datasetUrl; this.context = context; this.token = token; this.username = username; @@ -65,14 +68,15 @@ public class PackageCreatePostActions extends Thread { utils.setSearchableField(packageId, true); // add also this information as custom field - String datasetUrl = utils.getUnencryptedUrlFromDatasetIdOrName(packageId); + if(datasetUrl == null) + datasetUrl = utils.getUnencryptedUrlFromDatasetIdOrName(packageId); Map> addField = new HashMap>(); addField.put(ITEM_URL, Arrays.asList(datasetUrl)); utils.patchProductCustomFields(packageId, apiKey, addField); JSONObject profile = CatalogueUtils.getUserProfile(username); - // set author and author email + // get author and author email JSONObject profileValues = (JSONObject)profile.get(CatalogueUtils.RESULT_KEY); String fullnameUser = (String) profileValues.get(CatalogueUtils.FULLNAME_IN_PROFILE_KEY); 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 ca18cf8..2d898f5 100644 --- a/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java +++ b/src/main/java/org/gcube/datacatalogue/catalogue/ws/Item.java @@ -19,6 +19,7 @@ import org.gcube.common.scope.api.ScopeProvider; import org.gcube.datacatalogue.catalogue.utils.CatalogueUtils; import org.gcube.datacatalogue.catalogue.utils.Constants; import org.gcube.datacatalogue.catalogue.utils.PackageCreatePostActions; +import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue; import org.json.simple.JSONArray; import org.json.simple.JSONObject; import org.json.simple.parser.JSONParser; @@ -73,21 +74,34 @@ public class Item { if(profiles != null && !profiles.isEmpty()) CatalogueUtils.validateAgainstProfile(obj, caller, profiles); - CatalogueUtils.delegatePost(caller, context, Constants.ITEM_CREATE, obj.toJSONString(), uriInfo); + obj = (JSONObject)parser.parse(CatalogueUtils.delegatePost(caller, context, Constants.ITEM_CREATE, obj.toJSONString(), uriInfo)); // after creation, if it was ok ... if((boolean)obj.get(CatalogueUtils.SUCCESS_KEY)){ JSONObject result = (JSONObject)obj.get(CatalogueUtils.RESULT_KEY); - new PackageCreatePostActions( + DataCatalogue utils = CatalogueUtils.getCatalogue(); + + // add also this information as custom field + String datasetUrl = utils.getUnencryptedUrlFromDatasetIdOrName((String)(result.get(CatalogueUtils.DATASET_KEY))); + if(datasetUrl != null){ + JSONObject itemUrl = new JSONObject(); + itemUrl.put(CatalogueUtils.EXTRA_KEY, PackageCreatePostActions.ITEM_URL); + itemUrl.put(CatalogueUtils.EXTRA_VALUE, datasetUrl); + ((JSONArray)((JSONObject)obj.get(CatalogueUtils.RESULT_KEY)).get(CatalogueUtils.EXTRAS_KEY)).add(itemUrl); + } + PackageCreatePostActions packagePostActions = new PackageCreatePostActions( username, + datasetUrl, (String)(result.get(CatalogueUtils.DATASET_KEY)), context, SecurityTokenProvider.instance.get(), (JSONArray)(result.get(CatalogueUtils.TAGS_KEY)), (String)(result.get(CatalogueUtils.TITLE_KEY)) - ).start(); + ); + + packagePostActions.start(); } diff --git a/src/main/resources/ehcache.xml b/src/main/resources/ehcache.xml index be8fbbd..f1f2ab1 100644 --- a/src/main/resources/ehcache.xml +++ b/src/main/resources/ehcache.xml @@ -3,11 +3,12 @@ xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true" monitoring="autodetect" dynamicConfig="true"> - + + - +