diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java index 7b3a761..4f91a1a 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/CommonServiceUtils.java @@ -218,6 +218,43 @@ public class CommonServiceUtils { } } + public static final String GROUP_SUFFIX = "_group"; + + /** + * Convert a group name to its id on ckan + * @param origName + * @return + */ + private static String getGroupIDOnCkan(String origName){ + if(origName == null) { + throw new IllegalArgumentException("origName cannot be null"); + } + String modified = origName.replaceAll("\\(", ""); + modified = modified.replaceAll("\\)", ""); + modified = modified.trim().toLowerCase().replaceAll("[^A-Za-z0-9-]", "-"); + if(modified.startsWith("-")) { + modified = modified.substring(1); + } + if(modified.endsWith("-")) { + modified = modified.substring(0, modified.length() -1); + } + return modified; + } + + public static String getGroupId(String groupName) { + StringBuffer stringBuffer = new StringBuffer(); + stringBuffer.append(groupName); + /* + * The "_group" suffix is added to all groups to + * avoid issues on groups and organizations having the same name + * e.g. RAM organization (id=ram) and RAM group (id=ram_group) + */ + if(!groupName.endsWith(GROUP_SUFFIX)) { + stringBuffer.append(GROUP_SUFFIX); + } + return getGroupIDOnCkan(stringBuffer.toString()); + } + private static void addGroup(Group group, Sources source, String value, Set groups) { String conditionToCheck = group.condition(); String groupNameOverValue = group.groupNameOverValue(); @@ -227,27 +264,44 @@ public class CommonServiceUtils { : value.matches(conditionToCheck); if(match) { - StringBuffer stringBuffer = new StringBuffer(); - if(prependSource) { stringBuffer.append(source.toString()); stringBuffer.append(" "); } - if(groupNameOverValue.isEmpty()) { stringBuffer.append(value); }else { stringBuffer.append(groupNameOverValue); } - - String groupName = HelperMethods.getGroupNameOnCkan(stringBuffer.toString()); - - groups.add(groupName); + String groupId = getGroupId(stringBuffer.toString()); + groups.add(groupId); } } + /** + * Add the record to the group of sources + * @param groups + * @param sourcesList + * @param productType + * @param sourceInPath + */ + private static void addRecordToGroups(Set groups, Set sourcesList, Product_Type productType, Sources sourceInPath) { + if(sourceInPath == Sources.GRSF) { + groups.add(getGroupId(Sources.GRSF.getOrigName())); // i.e. grsf_group + }else { + groups.add(getGroupId(Constants.SYSTEM_TYPE_LEGACY_RECORD)); // i.e. legacy_group + } + + // evaluate the custom fields/tags, resources and groups + groups.add(getGroupId(productType.getOrigName())); //i.e. stock_group or fishery_group + + for(String source : sourcesList) { + groups.add(getGroupId(source)); // i.e. firms_group, fishsource_group, ram_group + } + } + /** * Retrieve the list of groups' names for this object */ @@ -522,28 +576,6 @@ public class CommonServiceUtils { } - /** - * Add the record to the group of sources - * @param groups - * @param sourcesList - * @param productType - * @param sourceInPath - */ - private static void addRecordToGroups(Set groups, Set sourcesList, Product_Type productType, Sources sourceInPath) { - if(sourceInPath == Sources.GRSF) { - groups.add(HelperMethods.getGroupNameOnCkan(Sources.GRSF.getOrigName())); // i.e. grsf - }else { - groups.add(HelperMethods.getGroupNameOnCkan(Constants.SYSTEM_TYPE_LEGACY_RECORD)); // i.e. legacy - } - - // evaluate the custom fields/tags, resources and groups - groups.add(HelperMethods.getGroupNameOnCkan(productType.getOrigName())); //i.e. stock or fishery - - for(String source : sourcesList) { - groups.add(HelperMethods.getGroupNameOnCkan(source)); // i.e. FIRMS, FishSource, RAM - } - } - // /** // * Fetch the system:type property from a record // * @param itemIdOrName diff --git a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java index 32e3956..f1f01ce 100644 --- a/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java +++ b/src/main/java/org/gcube/data_catalogue/grsf_publish_ws/utils/HelperMethods.java @@ -80,27 +80,6 @@ public abstract class HelperMethods { private static CacheInterface> namespacesCache = new CacheImpl>(1000 * 60 * 60 * 24); private static CacheInterface catalogueCache = new CacheImpl(1000 * 60 * 60 * 24); - /** - * Convert a group name to its id on ckan - * @param origName - * @return - */ - public static String getGroupNameOnCkan(String origName){ - if(origName == null) { - throw new IllegalArgumentException("origName cannot be null"); - } - String modified = origName.replaceAll("\\(", ""); - modified = modified.replaceAll("\\)", ""); - modified = modified.trim().toLowerCase().replaceAll("[^A-Za-z0-9-]", "-"); - if(modified.startsWith("-")) { - modified = modified.substring(1); - } - if(modified.endsWith("-")) { - modified = modified.substring(0, modified.length() -1); - } - return modified; - } - /** * Retrieve the running instance of the data catalogue for this scope * @return