Improve tags pre-filling mapping

This commit is contained in:
George Kalampokis 2022-02-22 14:15:51 +02:00
parent 0bd08c2fac
commit 3c42ee3531
1 changed files with 43 additions and 45 deletions

View File

@ -60,6 +60,36 @@ public class PrefillingMapper {
} }
private static void setValue(PrefillingMapping prefillingMapping, String value, DatasetWizardModel datasetWizardModel, JsonNode parentNode, Map<String, Object> properties) throws InvocationTargetException, IllegalAccessException, JsonProcessingException { private static void setValue(PrefillingMapping prefillingMapping, String value, DatasetWizardModel datasetWizardModel, JsonNode parentNode, Map<String, Object> properties) throws InvocationTargetException, IllegalAccessException, JsonProcessingException {
String trimRegex = prefillingMapping.getTrimRegex() != null ? prefillingMapping.getTrimRegex() : "";
if (!value.startsWith("\"") && !value.startsWith("[") && !value.equals("null")) {
value = "\"" + value + "\"";
}
JsonNode valueNode = mapper.readTree(value);
List<String> parsedValues = new ArrayList<>();
if (valueNode.isArray() && (!valueNode.get(0).isTextual())) {
if (prefillingMapping.getSubSource() == null || prefillingMapping.getSubSource().isEmpty()) {
throw new IllegalArgumentException("Source value is an array but no subSource field have been set");
}
String parsedValue;
for(int i = 0; i < valueNode.size(); i++){
JsonNode jsonObj = valueNode.get(i);
String subSource = jsonObj.get(prefillingMapping.getSubSource()).asText();
parsedValue = subSource.replaceAll(trimRegex, "");
parsedValues.add(parsedValue);
}
parsedValues = parsedValues.stream().distinct().collect(Collectors.toList());
}
String parsedValue = null;
if (valueNode.isTextual()) {
parsedValue = valueNode.textValue().replace(trimRegex, "");
}else if (valueNode.isArray() && valueNode.get(0).isTextual()) {
List<String> values = new LinkedList<>();
for (int i = 0; i < valueNode.size(); i++) {
values.add(valueNode.get(i).textValue().replace(trimRegex, ""));
}
parsedValue = String.join(", ", values);
}
if (prefillingMapping.getTarget() != null) { if (prefillingMapping.getTarget() != null) {
try { try {
String methodName = "set" + prefillingMapping.getTarget().substring(0, 1).toUpperCase(Locale.ROOT) + prefillingMapping.getTarget().substring(1); String methodName = "set" + prefillingMapping.getTarget().substring(0, 1).toUpperCase(Locale.ROOT) + prefillingMapping.getTarget().substring(1);
@ -67,44 +97,17 @@ public class PrefillingMapper {
.filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()).get(0); .filter(method -> method.getName().equals(methodName)).collect(Collectors.toList()).get(0);
Class<?>[] params = setterMethod.getParameterTypes(); Class<?>[] params = setterMethod.getParameterTypes();
//GK: Tags Special logic //GK: Tags Special logic
if (!value.equals("null") && prefillingMapping.getTarget().equals("tags")) { if (parsedValue != null && !parsedValue.equals("null") && prefillingMapping.getTarget().equals("tags")) {
value = mapper.valueToTree(parseTags(value)).toString(); parsedValue = mapper.valueToTree(parseTags(parsedValue)).toString();
} else {
parsedValue = mapper.valueToTree(parsedValue).toString();
} }
setterMethod.invoke(datasetWizardModel, mapper.readValue(value, params[0])); setterMethod.invoke(datasetWizardModel, mapper.readValue(parsedValue, params[0]));
}catch (InvocationTargetException | IllegalAccessException | JsonProcessingException e) { }catch (InvocationTargetException | IllegalAccessException | JsonProcessingException e) {
throw e; throw e;
} }
} else { } else {
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget()); List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget());
String trimRegex = prefillingMapping.getTrimRegex() != null ? prefillingMapping.getTrimRegex() : "";
if (!value.startsWith("\"") && !value.startsWith("[") && !value.equals("null")) {
value = "\"" + value + "\"";
}
JsonNode valueNode = mapper.readTree(value);
List<String> parsedValues = new ArrayList<>();
if (valueNode.isArray() && (!valueNode.get(0).isTextual())) {
if (prefillingMapping.getSubSource() == null || prefillingMapping.getSubSource().isEmpty()) {
throw new IllegalArgumentException("Source value is an array but no subSource field have been set");
}
String parsedValue;
for(int i = 0; i < valueNode.size(); i++){
JsonNode jsonObj = valueNode.get(i);
String subSource = jsonObj.get(prefillingMapping.getSubSource()).asText();
parsedValue = subSource.replaceAll(trimRegex, "");
parsedValues.add(parsedValue);
}
parsedValues = parsedValues.stream().distinct().collect(Collectors.toList());
}
String parsedValue = null;
if (valueNode.isTextual()) {
parsedValue = valueNode.textValue().replace(trimRegex, "");
}else if (valueNode.isArray() && valueNode.get(0).isTextual()) {
List<String> values = new LinkedList<>();
for (int i = 0; i < valueNode.size(); i++) {
values.add(valueNode.get(i).textValue().replace(trimRegex, ""));
}
parsedValue = String.join(", ", values);
}
//GK: This is not generic and it will crash if the dataset.issued is not available on the template //GK: This is not generic and it will crash if the dataset.issued is not available on the template
/*if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){ /*if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
@ -129,10 +132,10 @@ public class PrefillingMapper {
}*/ }*/
if (prefillingMapping.getMaDmpTarget().equals("dataset.distribution.available_until") && parsedValue != null && !parsedValue.equals("null")) { if (prefillingMapping.getMaDmpTarget().equals("dataset.distribution.available_until") && parsedValue != null && !parsedValue.equals("null")) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd"); DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(parsedValue, formatter); LocalDate date = LocalDate.parse(parsedValue, formatter);
date = date.plusYears(20); date = date.plusYears(20);
parsedValue = date.toString(); parsedValue = date.toString();
} }
for (JsonNode node: nodes) { for (JsonNode node: nodes) {
@ -146,7 +149,7 @@ public class PrefillingMapper {
properties.put(id, parseComboBoxValues(node, parsedValues)); properties.put(id, parseComboBoxValues(node, parsedValues));
break; break;
case TAGS: case TAGS:
properties.put(id, mapper.valueToTree(parseTags(value)).toString()); properties.put(id, mapper.valueToTree(parseTags(parsedValue)).toString());
break; break;
default: default:
if (!parsedValues.isEmpty()) if (!parsedValues.isEmpty())
@ -202,15 +205,10 @@ public class PrefillingMapper {
private static List<Tag> parseTags(String value) throws JsonProcessingException { private static List<Tag> parseTags(String value) throws JsonProcessingException {
if (value == null || value.isEmpty()) if (value == null || value.isEmpty())
return new LinkedList<>(); return new LinkedList<>();
JsonNode rawTags = mapper.readTree(value); String[] rawTags = value.split(", ");
List<Tag> parsedTags = new LinkedList<>(); List<Tag> parsedTags = new LinkedList<>();
if (rawTags.isArray()) { for (String rawTag : rawTags) {
for (int i = 0; i < rawTags.size(); i++) { parsedTags.add(new Tag(rawTag, rawTag));
parsedTags.add(new Tag(rawTags.get(i).textValue(), rawTags.get(i).textValue()));
}
} else if (rawTags.isTextual()) {
List<String> tags = Arrays.asList(rawTags.textValue().split(", "));
parsedTags.addAll(tags.stream().map(s -> new Tag(s, s)).collect(Collectors.toList()));
} }
return parsedTags; return parsedTags;
} }