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
This commit is contained in:
Costantino Perciante 2017-05-05 16:51:22 +00:00
parent 106e71e779
commit 82dd47b70c
4 changed files with 62 additions and 26 deletions

View File

@ -206,8 +206,18 @@ public class CatalogueUtils {
* @throws Exception
*/
public static List<NamespaceCategory> 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<String, Integer> fieldsMandatoryUpperBoundMap = new HashMap<String, Integer>(metadataFields.size());
Map<String, Integer> numberFieldsSameKeyMap = new HashMap<String, Integer>(metadataFields.size());
// keep track of the groups that must be created after validation but before item creation.
List<String> groupsToCreateAfterValidation = new ArrayList<String>();
// 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<NamespaceCategory> categories,
Map<String, Integer> fieldsMandatoryLowerBoundMap,
Map<String, Integer> fieldsMandatoryUpperBoundMap,
Map<String, Integer> numberFieldsSameKeyMap) throws Exception {
Map<String, Integer> numberFieldsSameKeyMap,
List<String> groupToCreate) throws Exception {
List<CustomField> toReturn = new ArrayList<CustomField>();
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<String> 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){

View File

@ -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<String, List<String>> addField = new HashMap<String, List<String>>();
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);

View File

@ -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();
}

View File

@ -3,11 +3,12 @@
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">
<cache name="profile_readers" maxEntriesLocalHeap="10"
maxEntriesLocalDisk="100" diskSpoolBufferSizeMB="20" eternal="false"
<diskStore path="/home/gcube/tomcat/tmp" />
<cache name="profile_readers" maxEntriesLocalHeap="10" eternal="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600"
memoryStoreEvictionPolicy="LFU" transactionalMode="off">
<persistence strategy="localTempSwap" />
<persistence strategy="none" />
</cache>
<cache name="profile_users" maxEntriesLocalHeap="1000"