file-transformer-rda-json/core/src/main/java/eu/eudat/file/transformer/model/rda/mapper/HostRDAMapper.java

201 lines
7.5 KiB
Java

package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.Host;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.*;
@Component
public class HostRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(HostRDAMapper.class);
public Host toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields, String numbering){
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Host rda = new Host();
for (FieldModel 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) {
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
for (ExportPropertyName propertyName: ExportPropertyName.values()) {
if (rdaProperty.contains(propertyName.getName())) {
switch (propertyName) {
case AVAILABILITY:
rda.setAvailability(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.AVAILABILITY.getName(), node.getId());
break;
case BACKUP_FREQUENCY:
rda.setBackupFrequency(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.BACKUP_FREQUENCY.getName(), node.getId());
break;
case BACKUP_TYPE:
rda.setBackupType(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.BACKUP_TYPE.getName(), node.getId());
break;
case CERTIFIED_WITH:
try {
rda.setCertifiedWith(Host.CertifiedWith.fromValue(rdaValue.getTextValue()));
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.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
case GEO_LOCATION:
// if (rdaValue.getTextValue().startsWith("{")) {
// try {
// rdaValue = mapper.readValue(rdaValue.getTextValue(), 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.getTextValue()));
// rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId());
// }
// catch (IllegalArgumentException e) {
// logger.warn("Distribution host geo location " + rdaValue.getTextValue() + "from semantic distribution.host.geo_location is not valid. Geo location will not be set set.");
// }
//TODO
rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId());
break;
case PID_SYSTEM:
// try{
// JsonNode valueNode = mapper.readTree(rdaValue.getTextValue());
// Iterator<JsonNode> iter = valueNode.elements();
// List<String> pList = new ArrayList<>();
// while(iter.hasNext()) {
// pList.add(iter.next().asText());
// }
// List<PidSystem> 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);
// }
//TODO
break;
case STORAGE_TYPE:
rda.setStorageType(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.STORAGE_TYPE.getName(), node.getId());
break;
case SUPPORT_VERSIONING:
try {
rda.setSupportVersioning(Host.SupportVersioning.fromValue(rdaValue.getTextValue()));
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.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
break;
case URL:
try {
rda.setUrl(URI.create(rdaValue.getTextValue()));
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;
}
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");
}
}
}