package eu.eudat.models.rda.mapper; import com.fasterxml.jackson.databind.JsonNode; import eu.eudat.logic.utilities.json.JsonSearcher; import eu.eudat.models.rda.License; import java.net.URI; import java.util.HashMap; import java.util.List; import java.util.Map; public class LicenseRDAMapper { public static License toRDA(List nodes) { License rda = new License(); for (JsonNode node: nodes) { String rdaProperty = node.get("rdaProperty").asText(); String value = node.get("value").asText(); for (LicenceProperties licenceProperties: LicenceProperties.values()) { if (rdaProperty.contains(licenceProperties.getName())) { switch (licenceProperties) { case LICENSE_REF: rda.setLicenseRef(URI.create(value)); break; case START_DATE: rda.setStartDate(value); break; } } } /*if (rdaProperty.contains("license_ref")) { rda.setLicenseRef(URI.create(value)); rda.setAdditionalProperty("license_refId", node.get("id").asText()); } else if (rdaProperty.contains("start_date")) { rda.setStartDate(value); rda.setAdditionalProperty("start_dateId", node.get("id").asText()); }*/ } return rda; } public static Map toProperties(List rdas) { Map properties = new HashMap<>(); rdas.forEach(rda -> { rda.getAdditionalProperties().entrySet().forEach(entry -> { switch (entry.getKey()) { case "license_refId": properties.put(entry.getValue().toString(), rda.getLicenseRef().toString()); break; case "start_dateId": properties.put(entry.getValue().toString(), rda.getStartDate()); break; } }); }); return properties; } public static Map toProperties(License rda, JsonNode root) { Map properties = new HashMap<>(); List licenseNodes = JsonSearcher.findNodes(root, "rdaProperty", "dataset.distribution.license"); for (JsonNode licenseNode: licenseNodes) { for (LicenceProperties licenceProperty: LicenceProperties.values()) { if (licenseNode.get("rdaProperty").asText().endsWith(licenceProperty.getName())) { switch (licenceProperty) { case LICENSE_REF: properties.put(licenseNode.get("id").asText(), rda.getLicenseRef().toString()); break; case START_DATE: properties.put(licenseNode.get("id").asText(), rda.getStartDate()); break; } } } } return properties; } public enum LicenceProperties { LICENSE_REF("license_ref"), START_DATE("start_date"); private String name; LicenceProperties(String name) { this.name = name; } public String getName() { return name; } } }