minor fixes

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@148344 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-05-08 08:26:06 +00:00
parent 82dd47b70c
commit 5d5b872f08
2 changed files with 33 additions and 32 deletions

View File

@ -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<NamespaceCategory> 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<NamespaceCategory> categories = getNamespaceCategories();
List<String> categoriesIds = new ArrayList<String>(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<String, Integer> fieldsMandatoryLowerBoundMap = new HashMap<String, Integer>(metadataFields.size());
Map<String, Integer> fieldsMandatoryUpperBoundMap = new HashMap<String, Integer>(metadataFields.size());
Map<String, Integer> numberFieldsSameKeyMap = new HashMap<String, Integer>(metadataFields.size());
Map<String, Integer> numberFieldsMandatorySameKeyMap = new HashMap<String, Integer>(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<String> groupsToCreateAfterValidation = new ArrayList<String>();
// 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<NamespaceCategory> categories,
Map<String, Integer> fieldsMandatoryLowerBoundMap,
Map<String, Integer> fieldsMandatoryUpperBoundMap,
Map<String, Integer> numberFieldsSameKeyMap,
Map<String, Integer> numberFieldsMandatorySameKeyMap,
List<String> groupToCreate) throws Exception {
List<CustomField> toReturn = new ArrayList<CustomField>();
String metadataFieldName = metadataField.getFieldName();
// keep track of the number of elements with this key
String metadataFieldName = metadataField.getFieldName();
int fieldsFoundWithThisKey = 0;
Iterator<CustomField> 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();

View File

@ -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