package eu.eudat.file.transformer.rda.mapper; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel; import eu.eudat.file.transformer.rda.Host; import eu.eudat.file.transformer.rda.PidSystem; import eu.eudat.file.transformer.utils.string.MyStringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.net.URI; import java.util.*; import java.util.stream.Collectors; public class HostRDAMapper { private static final Logger logger = LoggerFactory.getLogger(HostRDAMapper.class); public static Host toRDA(List nodes, String numbering) { ObjectMapper mapper = new ObjectMapper(); Host rda = new Host(); for (FieldFileTransformerModel node: nodes) { String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution.host")).findFirst().orElse(""); if (rdaProperty.contains("host")) { int firstDiff = MyStringUtils.getFirstDifference(numbering, node.getNumbering()); if (firstDiff == -1 || firstDiff >= 2) { if (node.getData() == null) { continue; } String rdaValue = node.getData().getValue(); if(rdaValue == null || rdaValue.isEmpty()){ continue; } for (ExportPropertyName propertyName: ExportPropertyName.values()) { if (rdaProperty.contains(propertyName.getName())) { switch (propertyName) { case AVAILABILITY: rda.setAvailability(rdaValue); rda.setAdditionalProperty(ImportPropertyName.AVAILABILITY.getName(), node.getId()); break; case BACKUP_FREQUENCY: rda.setBackupFrequency(rdaValue); rda.setAdditionalProperty(ImportPropertyName.BACKUP_FREQUENCY.getName(), node.getId()); break; case BACKUP_TYPE: rda.setBackupType(rdaValue); rda.setAdditionalProperty(ImportPropertyName.BACKUP_TYPE.getName(), node.getId()); break; case CERTIFIED_WITH: try { rda.setCertifiedWith(Host.CertifiedWith.fromValue(rdaValue)); rda.setAdditionalProperty(ImportPropertyName.CERTIFIED_WITH.getName(), node.getId()); } catch (IllegalArgumentException e) { logger.warn("Distribution host certified with " + rdaValue + "from semantic distribution.host.certified_with is not valid. Certified_with will not be set set."); } break; case DESCRIPTION: rda.setDescription(rdaValue); rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId()); break; case GEO_LOCATION: if (rdaValue.startsWith("{")) { try { rdaValue = mapper.readValue(rdaValue, Map.class).get("id").toString(); } catch (JsonProcessingException e) { logger.warn(e.getLocalizedMessage() + ". Try to pass value as is"); } } try { rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue)); rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId()); } catch (IllegalArgumentException e) { logger.warn("Distribution host geo location " + rdaValue + "from semantic distribution.host.geo_location is not valid. Geo location will not be set set."); } break; case PID_SYSTEM: try{ JsonNode valueNode = mapper.readTree(rdaValue); Iterator iter = valueNode.elements(); List pList = new ArrayList<>(); while(iter.hasNext()) { pList.add(iter.next().asText()); } List pidList; if(pList.size() == 0){ pidList = Arrays.stream(rdaValue.replaceAll("[\\[\"\\]]","").split(",")) .map(PidSystem::fromValue).collect(Collectors.toList()); } else{ pidList = pList.stream().map(PidSystem::fromValue).collect(Collectors.toList()); } rda.setPidSystem(pidList); rda.setAdditionalProperty(ImportPropertyName.PID_SYSTEM.getName(), node.getId()); } catch (IllegalArgumentException e){ rda.setPidSystem(new ArrayList<>()); break; } catch (JsonProcessingException e) { throw new RuntimeException(e); } break; case STORAGE_TYPE: rda.setStorageType(rdaValue); rda.setAdditionalProperty(ImportPropertyName.STORAGE_TYPE.getName(), node.getId()); break; case SUPPORT_VERSIONING: try { rda.setSupportVersioning(Host.SupportVersioning.fromValue(rdaValue)); rda.setAdditionalProperty(ImportPropertyName.SUPPORT_VERSIONING.getName(), node.getId()); } catch (IllegalArgumentException e) { logger.warn("Distribution host support versioning " + rdaValue + "from semantic distribution.host.support_versioning is not valid. Support versioning will not be set set."); } break; case TITLE: rda.setTitle(rdaValue); rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId()); break; case URL: try { rda.setUrl(URI.create(rdaValue)); rda.setAdditionalProperty(ImportPropertyName.URL.getName(), node.getId()); } catch (IllegalArgumentException e) { logger.warn(e.getLocalizedMessage() + ". Skipping url parsing"); } break; } } } } } } if(rda.getTitle() == null || rda.getUrl() == null){ return null; } return rda; } //TODO /* public static List toProperties(Host rda) { List properties = new ArrayList<>(); rda.getAdditionalProperties().entrySet().forEach(entry -> { try { ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey()); Field field = new Field(); field.setKey(entry.getValue().toString()); switch (importPropertyName) { case AVAILABILITY: field.setValue(rda.getAvailability()); break; case TITLE: field.setValue(rda.getTitle()); break; case DESCRIPTION: field.setValue(rda.getDescription()); break; case BACKUP_FREQUENCY: field.setValue(rda.getBackupFrequency()); break; case BACKUP_TYPE: field.setValue(rda.getBackupType()); break; case CERTIFIED_WITH: field.setValue(rda.getCertifiedWith().value()); break; case GEO_LOCATION: field.setValue(rda.getGeoLocation().value()); break; case PID_SYSTEM: List pids = new ArrayList<>(); ObjectMapper mapper = new ObjectMapper(); for(PidSystem pid: rda.getPidSystem()){ pids.add(pid.value()); } if(!pids.isEmpty()){ field.setValue(mapper.writeValueAsString(pids)); } break; case STORAGE_TYPE: field.setValue(rda.getStorageType()); break; case SUPPORT_VERSIONING: field.setValue(rda.getSupportVersioning().value()); break; case URL: field.setValue(rda.getUrl().toString()); break; } properties.add(field); } catch (Exception e) { logger.error(e.getMessage(), e); } }); return properties; } */ private enum ExportPropertyName { AVAILABILITY("availability"), BACKUP_FREQUENCY("backup_frequency"), BACKUP_TYPE("backup_type"), CERTIFIED_WITH("certified_with"), DESCRIPTION("description"), GEO_LOCATION("geo_location"), PID_SYSTEM("pid_system"), STORAGE_TYPE("storage_type"), SUPPORT_VERSIONING("support_versioning"), TITLE("title"), URL("url"); private final String name; ExportPropertyName(String name) { this.name = name; } public String getName() { return name; } } private enum ImportPropertyName { AVAILABILITY("availabilityId"), BACKUP_FREQUENCY("backup_frequencyId"), BACKUP_TYPE("backup_typeId"), CERTIFIED_WITH("certified_withId"), DESCRIPTION("descriptionId"), GEO_LOCATION("geo_locationId"), PID_SYSTEM("pid_systemId"), STORAGE_TYPE("storage_typeId"), SUPPORT_VERSIONING("support_versioningId"), TITLE("titleId"), URL("urlId"); private final String name; ImportPropertyName(String name) { this.name = name; } public String getName() { return name; } public static ImportPropertyName fromString(String name) throws Exception { for (ImportPropertyName importPropertyName: ImportPropertyName.values()) { if (importPropertyName.getName().equals(name)) { return importPropertyName; } } throw new Exception("No name available"); } } }