diff --git a/dmp-backend/web/src/main/java/eu/eudat/logic/mapper/prefilling/PrefillingMapper.java b/dmp-backend/web/src/main/java/eu/eudat/logic/mapper/prefilling/PrefillingMapper.java index 183e27918..b8ef8a335 100644 --- a/dmp-backend/web/src/main/java/eu/eudat/logic/mapper/prefilling/PrefillingMapper.java +++ b/dmp-backend/web/src/main/java/eu/eudat/logic/mapper/prefilling/PrefillingMapper.java @@ -60,6 +60,36 @@ public class PrefillingMapper { } private static void setValue(PrefillingMapping prefillingMapping, String value, DatasetWizardModel datasetWizardModel, JsonNode parentNode, Map 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 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 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) { try { 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); Class[] params = setterMethod.getParameterTypes(); //GK: Tags Special logic - if (!value.equals("null") && prefillingMapping.getTarget().equals("tags")) { - value = mapper.valueToTree(parseTags(value)).toString(); + if (parsedValue != null && !parsedValue.equals("null") && prefillingMapping.getTarget().equals("tags")) { + 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) { throw e; } } else { List 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 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 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 /*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")) { - DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd"); - LocalDate date = LocalDate.parse(parsedValue, formatter); - date = date.plusYears(20); - parsedValue = date.toString(); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd"); + LocalDate date = LocalDate.parse(parsedValue, formatter); + date = date.plusYears(20); + parsedValue = date.toString(); } for (JsonNode node: nodes) { @@ -146,7 +149,7 @@ public class PrefillingMapper { properties.put(id, parseComboBoxValues(node, parsedValues)); break; case TAGS: - properties.put(id, mapper.valueToTree(parseTags(value)).toString()); + properties.put(id, mapper.valueToTree(parseTags(parsedValue)).toString()); break; default: if (!parsedValues.isEmpty()) @@ -202,15 +205,10 @@ public class PrefillingMapper { private static List parseTags(String value) throws JsonProcessingException { if (value == null || value.isEmpty()) return new LinkedList<>(); - JsonNode rawTags = mapper.readTree(value); + String[] rawTags = value.split(", "); List parsedTags = new LinkedList<>(); - if (rawTags.isArray()) { - for (int i = 0; i < rawTags.size(); i++) { - parsedTags.add(new Tag(rawTags.get(i).textValue(), rawTags.get(i).textValue())); - } - } else if (rawTags.isTextual()) { - List tags = Arrays.asList(rawTags.textValue().split(", ")); - parsedTags.addAll(tags.stream().map(s -> new Tag(s, s)).collect(Collectors.toList())); + for (String rawTag : rawTags) { + parsedTags.add(new Tag(rawTag, rawTag)); } return parsedTags; }