Check for ITEM_URL field among the custom fields submitted and removed if it is the case. propagateUp property is now managed

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/data-catalogue/catalogue-ws@148900 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Costantino Perciante 2017-05-19 10:47:23 +00:00
parent 04792a90e4
commit 025c13a8ca
2 changed files with 47 additions and 9 deletions

View File

@ -5,10 +5,12 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
@ -132,6 +134,25 @@ public class CatalogueUtils {
}
/**
* Get the group hierarchy
* @param groupName
* @return
*/
public static List<String> getGroupHierarchyNames(String groupName, String username){
List<String> toReturn = new ArrayList<String>();
List<CkanGroup> ckanGroups = getCatalogue().getParentGroups(groupName, getCatalogue().getApiKeyFromUsername(username));
if(ckanGroups != null && !ckanGroups.isEmpty()){
for (CkanGroup ckanGroup : ckanGroups) {
toReturn.add(ckanGroup.getName());
}
}
return toReturn;
}
// =======================================================================
// METADATA PROFILE STUFF
// =======================================================================
@ -396,6 +417,8 @@ public class CatalogueUtils {
CustomField cf = new CustomField(object);
if(cf.getKey().equals(TYPE_KEY))
metadataTypeCF = cf;
else if(cf.getKey().equals(PackageCreatePostActions.ITEM_URL))
continue;
else
customFields.add(cf);
}
@ -468,7 +491,8 @@ public class CatalogueUtils {
fieldsMandatoryLowerBoundMap,
fieldsMandatoryUpperBoundMap,
numberFieldsMandatorySameKeyMap,
groupsToCreateAfterValidation);
groupsToCreateAfterValidation,
caller.getClient().getId());
validatedCustomFields.addAll(validCFs);
metadataIndex++;
@ -560,7 +584,8 @@ public class CatalogueUtils {
Map<String, Integer> fieldsMandatoryLowerBoundMap,
Map<String, Integer> fieldsMandatoryUpperBoundMap,
Map<String, Integer> numberFieldsMandatorySameKeyMap,
List<String> groupToCreate) throws Exception {
List<String> groupToCreate,
String username) throws Exception {
List<CustomField> toReturn = new ArrayList<CustomField>();
@ -576,7 +601,7 @@ public class CatalogueUtils {
fieldsFoundWithThisKey ++;
cf.setIndexCategory(categoryIndex);
cf.setIndexMetadataField(metadataIndex);
checkAsGroup(cf, metadataField, groupsArrayOriginal, groupToCreate);
checkAsGroup(cf, metadataField, groupsArrayOriginal, groupToCreate, username);
checkAsTag(cf, metadataField, tagsArrayOriginal);
toReturn.add(cf);
iterator.remove();
@ -670,7 +695,7 @@ public class CatalogueUtils {
* @param groupsArrayOriginal
*/
private static void checkAsGroup(CustomField fieldToValidate,
MetadataField metadataField, JSONArray groupsArrayOriginal, List<String> groupToCreate) {
MetadataField metadataField, JSONArray groupsArrayOriginal, List<String> groupToCreate, String username) {
logger.debug("Custom field is " + fieldToValidate);
logger.debug("MetadataField field is " + metadataField);
@ -679,7 +704,8 @@ public class CatalogueUtils {
MetadataGrouping grouping = metadataField.getGrouping();
if(grouping != null){
final List<String> groupNames = new ArrayList<String>();
boolean propagateUp = grouping.getPropagateUp();
final Set<String> groupNames = new HashSet<String>();
switch(grouping.getGroupingValue()){
case onFieldName:
@ -703,6 +729,14 @@ public class CatalogueUtils {
logger.debug("Adding group to which add this item " + UtilMethods.fromGroupTitleToName(title));
JSONObject group = new JSONObject();
group.put("name", UtilMethods.fromGroupTitleToName(title));
if(propagateUp){
List<String> parents = getGroupHierarchyNames(UtilMethods.fromGroupTitleToName(title), username);
for (String parent : parents) {
JSONObject groupP = new JSONObject();
groupP.put("name", parent);
groupsArrayOriginal.add(groupP);
}
}
groupsArrayOriginal.add(group);
}
@ -1053,10 +1087,10 @@ public class CatalogueUtils {
File file = upload.getValueAs(File.class);
long fileLenghtBytes = file.length();
long fileLenghtMegaByte = fileLenghtBytes >> 20;
logger.debug("File lenght in MegaByte is " + fileLenghtMegaByte);
logger.debug("File lenght in MegaByte is " + fileLenghtMegaByte);
if(fileLenghtMegaByte > MAX_UPLOADABLE_FILE_SIZE_MB)
throw new Exception("Exceeding maximum uploadable file size!");
if(fileLenghtMegaByte > MAX_UPLOADABLE_FILE_SIZE_MB)
throw new Exception("Exceeding maximum uploadable file size!");
}else
throw new Exception("No 'upload' field has been provided!");

View File

@ -23,6 +23,7 @@ import org.gcube.datacatalogue.ckanutillibrary.server.DataCatalogue;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.LoggerFactory;
@Path(Constants.ITEMS)
@ -109,7 +110,10 @@ public class Item {
}catch(Exception e){
logger.error("Something went wrong... ", e);
return CatalogueUtils.createJSONOnFailure(e.getMessage());
if(e instanceof ParseException)
return CatalogueUtils.createJSONOnFailure("Failed to parse incoming json!");
else
return CatalogueUtils.createJSONOnFailure(e.getMessage());
}
}