REvisiting code to properly generate group name/id

This commit is contained in:
Luca Frosini 2022-04-27 13:12:13 +02:00
parent 51c91a3878
commit 503a66596b
1 changed files with 35 additions and 59 deletions

View File

@ -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<String> 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);
}
}
}
/**