211 lines
6.9 KiB
Java
211 lines
6.9 KiB
Java
package eu.eudat.file.transformer.model.rda.mapper;
|
|
|
|
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
|
|
import eu.eudat.file.transformer.model.rda.Distribution;
|
|
import eu.eudat.file.transformer.model.rda.License;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.net.URI;
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class DistributionRDAMapper {
|
|
private static final Logger logger = LoggerFactory.getLogger(DistributionRDAMapper.class);
|
|
private final LicenseRDAMapper licenseRDAMapper;
|
|
private final HostRDAMapper hostRDAMapper;
|
|
|
|
public DistributionRDAMapper(LicenseRDAMapper licenseRDAMapper, HostRDAMapper hostRDAMapper) {
|
|
this.licenseRDAMapper = licenseRDAMapper;
|
|
this.hostRDAMapper = hostRDAMapper;
|
|
}
|
|
|
|
public List<Distribution> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
|
|
if (nodes == null) return null;
|
|
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
|
|
Map<String, Distribution> rdaMap = new HashMap<>();
|
|
|
|
for (FieldModel node: nodes) {
|
|
String rdaProperty = getRdaDistributionProperty(node);
|
|
if(rdaProperty.isEmpty() || node.getData() == null){
|
|
continue;
|
|
}
|
|
|
|
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.getReferences() == null)){
|
|
continue;
|
|
}
|
|
String key = node.getNumbering();
|
|
if(!key.contains("mult")){
|
|
key = "0";
|
|
}
|
|
else{
|
|
key = "" + key.charAt(4);
|
|
}
|
|
Distribution rda;
|
|
if(rdaMap.containsKey(key)){
|
|
rda = rdaMap.get(key);
|
|
}
|
|
else {
|
|
rda = new Distribution();
|
|
rdaMap.put(key, rda);
|
|
}
|
|
for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
|
|
if (rdaProperty.contains(exportPropertyName.getName())) {
|
|
switch (exportPropertyName) {
|
|
case ACCESS_URL:
|
|
rda.setAccessUrl(rdaValue.getTextValue());
|
|
rda.setAdditionalProperty(ImportPropertyName.ACCESS_URL.getName(), node.getId());
|
|
break;
|
|
case AVAILABLE_UNTIL:
|
|
rda.setAvailableUntil(rdaValue.getTextValue());
|
|
rda.setAdditionalProperty(ImportPropertyName.AVAILABLE_UNTIL.getName(), node.getId());
|
|
break;
|
|
case DOWNLOAD_URL:
|
|
rda.setDownloadUrl(URI.create(rdaValue.getTextValue()));
|
|
rda.setAdditionalProperty(ImportPropertyName.DOWNLOAD_URL.getName(), node.getId());
|
|
break;
|
|
case DESCRIPTION:
|
|
if(!rdaProperty.contains("host")) {
|
|
rda.setDescription(rdaValue.getTextValue());
|
|
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
|
|
}
|
|
break;
|
|
case DATA_ACCESS:
|
|
try {
|
|
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue.getTextValue()));
|
|
rda.setAdditionalProperty(ImportPropertyName.DATA_ACCESS.getName(), node.getId());
|
|
}
|
|
catch (IllegalArgumentException e) {
|
|
logger.warn("Distribution data access " + rdaValue + " from semantic distribution.data_access is not valid. Data access will not be set set.");
|
|
}
|
|
break;
|
|
case BYTE_SIZE:
|
|
rda.setByteSize(Integer.parseInt(rdaValue.getTextValue()));
|
|
rda.setAdditionalProperty(ImportPropertyName.BYTE_SIZE.getName(), node.getId());
|
|
break;
|
|
case LICENSE:
|
|
List<FieldModel> licenseNodes = nodes.stream().filter(lnode -> {
|
|
for(String schematic: lnode.getSchematics()){
|
|
if(schematic.startsWith("rda.dataset.distribution.license")){
|
|
return true;
|
|
}
|
|
}
|
|
return false;
|
|
}).collect(Collectors.toList());
|
|
License license = licenseRDAMapper.toRDA(licenseNodes, valueFields);
|
|
rda.setLicense(license != null? Collections.singletonList(license): new ArrayList<>());
|
|
break;
|
|
case FORMAT:
|
|
//TODO
|
|
// try {
|
|
// JsonNode valueNode = mapper.readTree(node.getData().getValue());
|
|
// if(valueNode.isArray()){
|
|
// Iterator<JsonNode> iter = valueNode.elements();
|
|
// List<String> formats = new ArrayList<>();
|
|
// int i = 1;
|
|
// while(iter.hasNext()) {
|
|
// JsonNode current = iter.next();
|
|
// String format = current.toString();
|
|
//
|
|
// Map<String, String> result = mapper.readValue(format, HashMap.class);
|
|
// format = result.get("label");
|
|
// formats.add(format);
|
|
// rda.setAdditionalProperty("format" + i++, mapper.readTree(current.toString()));
|
|
//
|
|
// }
|
|
// rda.setFormat(formats);
|
|
// }
|
|
// else{
|
|
// if(rda.getFormat() == null || rda.getFormat().isEmpty()){
|
|
// rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(","))));
|
|
// }
|
|
// else{
|
|
// rda.getFormat().addAll(Arrays.asList(rdaValue.replace(" ", "").split(",")));
|
|
// }
|
|
// }
|
|
// rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.getId());
|
|
// }
|
|
// catch(JsonProcessingException e){
|
|
// logger.warn(e.getMessage());
|
|
// }
|
|
break;
|
|
case TITLE:
|
|
if(!rdaProperty.contains("host")) {
|
|
rda.setTitle(rdaValue.getTextValue());
|
|
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
|
|
}
|
|
break;
|
|
case HOST:
|
|
rda.setHost(hostRDAMapper.toRDA(nodes, valueFields, node.getNumbering()));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return rdaMap.values().stream()
|
|
.filter(distro -> distro.getTitle() != null).collect(Collectors.toList());
|
|
}
|
|
|
|
private static String getRdaDistributionProperty(FieldModel node) {
|
|
return node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution")).findFirst().orElse("");
|
|
}
|
|
|
|
private enum ExportPropertyName {
|
|
ACCESS_URL("access_url"),
|
|
AVAILABLE_UNTIL("available_until"),
|
|
BYTE_SIZE("byte_size"),
|
|
DATA_ACCESS("data_access"),
|
|
DESCRIPTION("description"),
|
|
DOWNLOAD_URL("download_url"),
|
|
FORMAT("format"),
|
|
HOST("host"),
|
|
LICENSE("license"),
|
|
TITLE("title");
|
|
|
|
private final String name;
|
|
|
|
ExportPropertyName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
}
|
|
|
|
private enum ImportPropertyName {
|
|
ACCESS_URL("accessurlId"),
|
|
AVAILABLE_UNTIL("availableUtilId"),
|
|
BYTE_SIZE("byteSizeId"),
|
|
DATA_ACCESS("dataAccessId"),
|
|
DESCRIPTION("descriptionId"),
|
|
DOWNLOAD_URL("downloadUrlId"),
|
|
FORMAT("formatId"),
|
|
/*HOST("host"),
|
|
LICENSE("license"),*/
|
|
TITLE("titleId");
|
|
|
|
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");
|
|
}
|
|
}
|
|
}
|