From 503a66596b6faeaa531e6cdb38076c9969e57cf6 Mon Sep 17 00:00:00 2001 From: Luca Frosini Date: Wed, 27 Apr 2022 13:12:13 +0200 Subject: [PATCH] REvisiting code to properly generate group name/id --- .../utils/CommonServiceUtils.java | 94 +++++++------------ 1 file changed, 35 insertions(+), 59 deletions(-) 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 b845029..d658dcd 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 @@ -36,7 +36,6 @@ import org.gcube.datacatalogue.common.Constants; import org.gcube.datacatalogue.common.enums.Product_Type; import org.gcube.datacatalogue.common.enums.Sources; import org.gcube.datacatalogue.common.enums.Status; -import org.gcube.datacatalogue.common.enums.Stock_Type; import org.json.simple.JSONObject; import org.slf4j.LoggerFactory; @@ -219,6 +218,36 @@ public class CommonServiceUtils { } } + private static void addGroup(Group group, Sources source, String value, Set groups) { + String conditionToCheck = group.condition(); + String groupNameOverValue = group.groupNameOverValue(); + boolean prependSource = group.prependSourceToGroupName(); + + boolean match = conditionToCheck.isEmpty() ? true + : value.matches(conditionToCheck); + + if(match) { + + StringBuffer stringBuffer = new StringBuffer(); + + if(prependSource) { + stringBuffer.append(source.toString().toLowerCase()); + stringBuffer.append("-"); + } + + if(groupNameOverValue.isEmpty()) { + stringBuffer.append(value); + }else { + stringBuffer.append(groupNameOverValue); + } + + String groupName = HelperMethods.getGroupNameOnCkan(stringBuffer.toString()); + + groups.add(groupName); + } + + } + /** * Retrieve the list of groups' names for this object */ @@ -226,74 +255,22 @@ public class CommonServiceUtils { Sources source) { if(field.isAnnotationPresent(Group.class)) { Group group = field.getAnnotation(Group.class); - String conditionToCheck = group.condition(); - String groupNameOverValue = group.groupNameOverValue(); - - - - // See https://support.d4science.org/issues/11832 - boolean assessmentUnit = false; - boolean prependSource = group.prependSourceToGroupName(); - if(record instanceof StockRecord) { - StockRecord stockRecord = (StockRecord) record; - Stock_Type stock_Type = stockRecord.getType(); - if(stock_Type != Stock_Type.Assessment_Unit) { - prependSource = false; - }else { - assessmentUnit = true; - } - } - // end patch for https://support.d4science.org/issues/11832 - try { Object f = new PropertyDescriptor(field.getName(), current).getReadMethod().invoke(record); if(f != null) { if(f instanceof List) { List asList = ((List) f); if(!asList.isEmpty()) { - logger.debug("The object annotated with @Group is a list. Adding ... "); - - // else add all the available elements + // add all the available elements for(int i = 0; i < asList.size(); i++) { - boolean match = conditionToCheck.isEmpty() ? true - : asList.get(i).toString().trim().matches(conditionToCheck); - if(match) { - String groupName = groupNameOverValue.isEmpty() - ? HelperMethods.getGroupNameOnCkan(source.toString().toLowerCase() + "-" - + asList.get(i).toString().trim()) - : source.toString().toLowerCase() + "-" + groupNameOverValue; - if(assessmentUnit && !prependSource) { - groups.add(groupNameOverValue); - }else { - groups.add(groupName); - } - } + String value = asList.get(i).toString().trim(); + addGroup(group, source, value, groups); } - } - } else { - - // also convert to the group name that should be on ckan - boolean match = conditionToCheck.isEmpty() ? true - : f.toString().trim().matches(conditionToCheck); - if(match) { - - String groupName = groupNameOverValue.isEmpty() - ? HelperMethods.getGroupNameOnCkan( - source.toString().toLowerCase() + "-" + f.toString().trim()) - : source.toString().toLowerCase() + "-" + groupNameOverValue; - - if(assessmentUnit && !prependSource) { - groups.add(groupNameOverValue); - }else { - groups.add(groupName); - } - - - - } + String value = f.toString().trim(); + addGroup(group, source, value, groups); } } @@ -301,7 +278,6 @@ public class CommonServiceUtils { logger.error("Failed to read value for field " + field.getName() + " skipping", e); } } - } /**