package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; import eu.eudat.logic.utilities.helpers.MyStringUtils; import eu.eudat.models.rda.SecurityAndPrivacy; import eu.eudat.models.rda.TechnicalResource; import java.util.*; public class TechnicalResourceRDAMapper { public static List toRDAList(List nodes) { Map rdaMap = new HashMap<>(); for (JsonNode node: nodes) { String rdaProperty = node.get("rdaProperty").asText(); String rdaValue = node.get("value").asText(); TechnicalResource rda = getRelative(rdaMap, node.get("numbering").asText()); if (!rdaMap.containsValue(rda)) { rdaMap.put(node.get("numbering").asText(), rda); } for (PropertyName propertyName: PropertyName.values()) { if (rdaProperty.contains(propertyName.getName())) { switch (propertyName) { case NAME: rda.setName(rdaValue); break; case DESCRIPTION: rda.setDescription(rdaValue); break; } } } } return new ArrayList<>(rdaMap.values()); } public static TechnicalResource toRDA(JsonNode node) { TechnicalResource rda = new TechnicalResource(); String rdaProperty = node.get("rdaProperty").asText(); String value = node.get("value").asText(); if (rdaProperty.contains("description")) { rda.setDescription(value); } else if (rdaProperty.contains("name")) { rda.setName(value); } return rda; } private static TechnicalResource getRelative(Map rdaMap, String numbering) { return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0) .max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new TechnicalResource()); } private enum PropertyName { NAME("name"), DESCRIPTION("description"); private String name; PropertyName(String name) { this.name = name; } public String getName() { return name; } } }