2020-03-20 17:20:22 +01:00
|
|
|
package eu.eudat.models.rda.mapper;
|
|
|
|
|
2021-12-29 14:24:46 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2021-12-30 10:28:15 +01:00
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
2020-03-20 17:20:22 +01:00
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2021-12-29 14:24:46 +01:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-03-23 17:09:31 +01:00
|
|
|
import eu.eudat.logic.utilities.helpers.MyStringUtils;
|
2021-12-29 14:24:46 +01:00
|
|
|
import eu.eudat.logic.utilities.json.JavaToJson;
|
2020-05-28 17:38:08 +02:00
|
|
|
import eu.eudat.logic.utilities.json.JsonSearcher;
|
2020-03-20 17:20:22 +01:00
|
|
|
import eu.eudat.models.rda.Distribution;
|
2021-12-17 11:59:10 +01:00
|
|
|
import eu.eudat.models.rda.License;
|
2020-03-26 17:39:25 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
import java.net.URI;
|
2020-03-23 17:09:31 +01:00
|
|
|
import java.util.*;
|
2020-05-28 17:38:08 +02:00
|
|
|
import java.util.stream.Collectors;
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
public class DistributionRDAMapper {
|
2020-03-26 17:39:25 +01:00
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DistributionRDAMapper.class);
|
2020-03-20 17:20:22 +01:00
|
|
|
|
2020-03-23 17:09:31 +01:00
|
|
|
public static List<Distribution> toRDAList(List<JsonNode> nodes) {
|
|
|
|
Map<String, Distribution> rdaMap = new HashMap<>();
|
|
|
|
|
|
|
|
for (JsonNode node: nodes) {
|
|
|
|
String rdaProperty = node.get("rdaProperty").asText();
|
|
|
|
String rdaValue = node.get("value").asText();
|
2021-12-30 10:28:15 +01:00
|
|
|
//if(rdaValue == null || rdaValue.isEmpty()){
|
|
|
|
if(rdaValue == null || (rdaValue.isEmpty() && !node.get("value").isArray())){
|
2021-12-29 14:24:46 +01:00
|
|
|
continue;
|
2020-03-23 17:09:31 +01:00
|
|
|
}
|
2021-12-29 14:24:46 +01:00
|
|
|
String key = node.get("numbering").asText();
|
|
|
|
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);
|
|
|
|
}
|
2021-12-30 10:28:15 +01:00
|
|
|
/* Distribution rda = getRelative(rdaMap, node.get("numbering").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
if (!rdaMap.containsValue(rda)) {
|
|
|
|
rdaMap.put(node.get("numbering").asText(), rda);
|
2021-12-30 10:28:15 +01:00
|
|
|
} */
|
2020-03-26 17:39:25 +01:00
|
|
|
for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
|
|
|
|
if (rdaProperty.contains(exportPropertyName.getName())) {
|
|
|
|
switch (exportPropertyName) {
|
2020-03-23 17:09:31 +01:00
|
|
|
case ACCESS_URL:
|
|
|
|
rda.setAccessUrl(rdaValue);
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.ACCESS_URL.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
2021-12-16 10:44:34 +01:00
|
|
|
case AVAILABLE_UNTIL:
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setAvailableUntil(rdaValue);
|
2021-12-16 10:44:34 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.AVAILABLE_UNTIL.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case DOWNLOAD_URL:
|
|
|
|
rda.setDownloadUrl(URI.create(rdaValue));
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.DOWNLOAD_URL.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case DESCRIPTION:
|
2021-12-29 14:24:46 +01:00
|
|
|
if(!rdaProperty.contains("host")) {
|
|
|
|
rda.setDescription(rdaValue);
|
|
|
|
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.get("id").asText());
|
|
|
|
}
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case DATA_ACCESS:
|
|
|
|
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.DATA_ACCESS.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case BYTE_SIZE:
|
|
|
|
rda.setByteSize(Integer.parseInt(rdaValue));
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.BYTE_SIZE.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case LICENSE:
|
2020-05-28 17:38:08 +02:00
|
|
|
List<JsonNode> licenseNodes = nodes.stream().filter(lnode -> lnode.get("rdaProperty").asText().toLowerCase().contains("license")).collect(Collectors.toList());
|
2021-12-17 11:59:10 +01:00
|
|
|
License license = LicenseRDAMapper.toRDA(licenseNodes);
|
|
|
|
rda.setLicense(license != null? Collections.singletonList(license): new ArrayList<>());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case FORMAT:
|
2021-12-29 14:24:46 +01:00
|
|
|
if(node.get("value").isArray()){
|
|
|
|
Iterator<JsonNode> iter = node.get("value").elements();
|
|
|
|
List<String> formats = new ArrayList<>();
|
|
|
|
while(iter.hasNext()) {
|
|
|
|
String format = JavaToJson.objectStringToJson(iter.next().asText());
|
2021-12-30 10:28:15 +01:00
|
|
|
try {
|
|
|
|
Map<String, String> result = new ObjectMapper().readValue(format, HashMap.class);
|
|
|
|
format = result.get("label");
|
|
|
|
formats.add(format);
|
|
|
|
}
|
|
|
|
catch(JsonProcessingException e){
|
|
|
|
logger.warn(e.getMessage());
|
|
|
|
}
|
2021-12-29 14:24:46 +01:00
|
|
|
}
|
|
|
|
rda.setFormat(formats);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(","))));
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.get("id").asText());
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case TITLE:
|
2021-12-29 14:24:46 +01:00
|
|
|
if(!rdaProperty.contains("host")) {
|
|
|
|
rda.setTitle(rdaValue);
|
|
|
|
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.get("id").asText());
|
|
|
|
}
|
2020-03-23 17:09:31 +01:00
|
|
|
break;
|
|
|
|
case HOST:
|
|
|
|
rda.setHost(HostRDAMapper.toRDA(nodes, node.get("numbering").asText()));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-12-17 11:59:10 +01:00
|
|
|
return rdaMap.values().stream()
|
|
|
|
.filter(distro -> distro.getTitle() != null).collect(Collectors.toList());
|
2020-03-23 17:09:31 +01:00
|
|
|
}
|
|
|
|
|
2020-03-26 17:39:25 +01:00
|
|
|
public static Map<String, String> toProperties(List<Distribution> rdas) {
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
|
|
|
|
rdas.forEach(rda -> {
|
|
|
|
rda.getAdditionalProperties().entrySet().forEach(entry -> {
|
|
|
|
try {
|
|
|
|
ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
|
|
|
|
switch (importPropertyName) {
|
|
|
|
case ACCESS_URL:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getAccessUrl());
|
|
|
|
break;
|
|
|
|
case TITLE:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getTitle());
|
|
|
|
break;
|
|
|
|
case DESCRIPTION:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getDescription());
|
|
|
|
break;
|
|
|
|
case FORMAT:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getFormat().get(0));
|
|
|
|
break;
|
|
|
|
case BYTE_SIZE:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getByteSize().toString());
|
|
|
|
break;
|
|
|
|
case DATA_ACCESS:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getDataAccess().value());
|
|
|
|
break;
|
|
|
|
case DOWNLOAD_URL:
|
|
|
|
properties.put(entry.getValue().toString(), rda.getDownloadUrl().toString());
|
|
|
|
break;
|
2021-12-16 10:44:34 +01:00
|
|
|
case AVAILABLE_UNTIL:
|
2020-03-26 17:39:25 +01:00
|
|
|
properties.put(entry.getValue().toString(), rda.getAvailableUntil());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
});
|
2020-03-27 13:31:06 +01:00
|
|
|
if (rda.getHost() != null) {
|
|
|
|
properties.putAll(HostRDAMapper.toProperties(rda.getHost()));
|
|
|
|
}
|
|
|
|
if (rda.getLicense() != null && !rda.getLicense().isEmpty()) {
|
|
|
|
properties.putAll(LicenseRDAMapper.toProperties(rda.getLicense()));
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:38:08 +02:00
|
|
|
public static Map<String, String> toProperties(Distribution rda, JsonNode root) {
|
|
|
|
Map<String, String> properties = new HashMap<>();
|
|
|
|
|
|
|
|
List<JsonNode> distributionNodes = JsonSearcher.findNodes(root, "rdaProperty", "dataset.distribution");
|
|
|
|
|
|
|
|
for (JsonNode distributionNode: distributionNodes) {
|
|
|
|
for (ExportPropertyName exportPropertyName: ExportPropertyName.values()) {
|
2020-05-29 15:08:40 +02:00
|
|
|
if (distributionNode.get("rdaProperty").asText().contains(exportPropertyName.getName())) {
|
2020-05-28 17:38:08 +02:00
|
|
|
switch (exportPropertyName) {
|
|
|
|
case ACCESS_URL:
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getAccessUrl());
|
|
|
|
break;
|
|
|
|
case DESCRIPTION:
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getDescription());
|
|
|
|
break;
|
|
|
|
case TITLE:
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getTitle());
|
|
|
|
break;
|
2021-12-16 10:44:34 +01:00
|
|
|
case AVAILABLE_UNTIL:
|
2020-05-28 17:38:08 +02:00
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getAvailableUntil());
|
|
|
|
break;
|
|
|
|
case DOWNLOAD_URL:
|
2020-05-29 13:42:21 +02:00
|
|
|
if (rda.getDownloadUrl() != null) {
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getDownloadUrl().toString());
|
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
break;
|
|
|
|
case DATA_ACCESS:
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getDataAccess().value());
|
|
|
|
break;
|
|
|
|
case BYTE_SIZE:
|
2020-05-29 13:42:21 +02:00
|
|
|
if (rda.getByteSize() != null) {
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getByteSize().toString());
|
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
break;
|
|
|
|
case FORMAT:
|
2020-05-29 13:42:21 +02:00
|
|
|
if (rda.getFormat() != null && !rda.getFormat().isEmpty()) {
|
|
|
|
properties.put(distributionNode.get("id").asText(), rda.getFormat().get(0));
|
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
break;
|
|
|
|
case LICENSE:
|
2020-05-29 13:42:21 +02:00
|
|
|
if (rda.getLicense() != null && !rda.getLicense().isEmpty()) {
|
|
|
|
properties.putAll(LicenseRDAMapper.toProperties(rda.getLicense().get(0), root));
|
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
break;
|
|
|
|
case HOST:
|
2020-05-29 13:42:21 +02:00
|
|
|
if (rda.getHost() != null) {
|
|
|
|
properties.putAll(HostRDAMapper.toProperties(rda.getHost()));
|
|
|
|
}
|
|
|
|
break;
|
2020-05-28 17:38:08 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Distribution toRDA(List<JsonNode> nodes) {
|
2020-03-20 17:20:22 +01:00
|
|
|
Distribution rda = new Distribution();
|
2020-05-28 17:38:08 +02:00
|
|
|
for (JsonNode node: nodes) {
|
|
|
|
String rdaProperty = node.get("rdaProperty").asText();
|
|
|
|
String rdaValue = node.get("value").asText();
|
|
|
|
for (ExportPropertyName exportPropertyName: ExportPropertyName.values()) {
|
|
|
|
if (rdaProperty.contains(exportPropertyName.getName())) {
|
|
|
|
switch (exportPropertyName) {
|
|
|
|
case ACCESS_URL:
|
|
|
|
rda.setAccessUrl(rdaValue);
|
|
|
|
break;
|
|
|
|
case DESCRIPTION:
|
|
|
|
rda.setDescription(rdaValue);
|
|
|
|
break;
|
|
|
|
case TITLE:
|
|
|
|
rda.setTitle(rdaValue);
|
|
|
|
break;
|
2021-12-16 10:44:34 +01:00
|
|
|
case AVAILABLE_UNTIL:
|
2020-05-28 17:38:08 +02:00
|
|
|
rda.setAvailableUntil(rdaValue);
|
|
|
|
break;
|
|
|
|
case DOWNLOAD_URL:
|
|
|
|
rda.setDownloadUrl(URI.create(rdaValue));
|
|
|
|
break;
|
|
|
|
case DATA_ACCESS:
|
|
|
|
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
|
|
|
|
break;
|
|
|
|
case BYTE_SIZE:
|
|
|
|
rda.setByteSize(Integer.parseInt(rdaValue));
|
|
|
|
break;
|
|
|
|
case FORMAT:
|
|
|
|
rda.setFormat(Collections.singletonList(rdaValue));
|
|
|
|
break;
|
|
|
|
case LICENSE:
|
|
|
|
List<JsonNode> licenseNodes = nodes.stream().filter(lnode -> lnode.get("rdaProperty").asText().toLowerCase().contains("license")).collect(Collectors.toList());
|
|
|
|
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(licenseNodes)));
|
|
|
|
break;
|
|
|
|
case HOST:
|
|
|
|
List<JsonNode> hostNodes = nodes.stream().filter(lnode -> lnode.get("rdaProperty").asText().toLowerCase().contains("host")).collect(Collectors.toList());
|
|
|
|
rda.setHost(HostRDAMapper.toRDA(hostNodes, "0"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/*if (rdaProperty.contains("access_url")) {
|
|
|
|
rda.setAccessUrl(rdaValue);
|
|
|
|
} else if (rdaProperty.contains("available_util")) {
|
|
|
|
rda.setAvailableUntil(rdaValue);
|
|
|
|
} else if (rdaProperty.contains("byte_size")) {
|
|
|
|
rda.setByteSize(Integer.parseInt(rdaValue));
|
|
|
|
} else if (rdaProperty.contains("data_access")) {
|
|
|
|
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
|
|
|
|
} else if (rdaProperty.contains("description")) {
|
|
|
|
rda.setDescription(rdaValue);
|
|
|
|
} else if (rdaProperty.contains("download_url")) {
|
|
|
|
rda.setDownloadUrl(URI.create(rdaValue));
|
|
|
|
} else if (rdaProperty.contains("format")) {
|
|
|
|
rda.setFormat(Collections.singletonList(rdaValue));
|
|
|
|
} else if (rdaProperty.contains("host")) {
|
2020-03-23 17:09:31 +01:00
|
|
|
// rda.setHost(HostRDAMapper.toRDA(node));
|
2020-05-28 17:38:08 +02:00
|
|
|
} else if (rdaProperty.contains("license")) {
|
|
|
|
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node)));
|
|
|
|
} else if (rdaProperty.contains("title")) {
|
|
|
|
rda.setTitle(rdaValue);
|
|
|
|
}*/
|
|
|
|
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
2020-11-13 17:26:09 +01:00
|
|
|
|
|
|
|
if (rda.getTitle() == null) {
|
|
|
|
throw new IllegalArgumentException("Distribution title is missing");
|
|
|
|
}
|
2020-03-20 17:20:22 +01:00
|
|
|
|
2020-11-13 17:26:09 +01:00
|
|
|
if (rda.getDataAccess() == null) {
|
|
|
|
throw new IllegalArgumentException("Distribution Data Access is missing");
|
|
|
|
}
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
return rda;
|
|
|
|
}
|
2020-03-23 17:09:31 +01:00
|
|
|
|
|
|
|
private static Distribution getRelative( Map<String, Distribution> 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 Distribution());
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:39:25 +01:00
|
|
|
private enum ExportPropertyName {
|
2020-03-23 17:09:31 +01:00
|
|
|
ACCESS_URL("access_url"),
|
2021-12-16 10:44:34 +01:00
|
|
|
AVAILABLE_UNTIL("available_until"),
|
2020-03-23 17:09:31 +01:00
|
|
|
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;
|
|
|
|
|
2020-03-26 17:39:25 +01:00
|
|
|
ExportPropertyName(String name) {
|
2020-03-23 17:09:31 +01:00
|
|
|
this.name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getName() {
|
|
|
|
return name;
|
|
|
|
}
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
|
|
|
|
private enum ImportPropertyName {
|
|
|
|
ACCESS_URL("accessurlId"),
|
2021-12-16 10:44:34 +01:00
|
|
|
AVAILABLE_UNTIL("availableUtilId"),
|
2020-03-26 17:39:25 +01:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
}
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|