121 lines
3.7 KiB
Java
121 lines
3.7 KiB
Java
package eu.eudat.models.rda.mapper;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
import eu.eudat.logic.utilities.helpers.MyStringUtils;
|
|
import eu.eudat.models.rda.Distribution;
|
|
|
|
import java.net.URI;
|
|
import java.util.*;
|
|
|
|
public class DistributionRDAMapper {
|
|
|
|
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();
|
|
Distribution rda = getRelative(rdaMap, node.get("numbering").asText());
|
|
if (!rdaMap.containsValue(rda)) {
|
|
rdaMap.put(node.get("numbering").asText(), rda);
|
|
}
|
|
for (PropertyName propertyName : PropertyName.values()) {
|
|
if (rdaProperty.contains(propertyName.getName())) {
|
|
switch (propertyName) {
|
|
case ACCESS_URL:
|
|
rda.setAccessUrl(rdaValue);
|
|
break;
|
|
case AVAILABLE_UTIL:
|
|
rda.setAvailableUntil(rdaValue);
|
|
break;
|
|
case DOWNLOAD_URL:
|
|
rda.setDownloadUrl(URI.create(rdaValue));
|
|
break;
|
|
case DESCRIPTION:
|
|
rda.setDescription(rdaValue);
|
|
break;
|
|
case DATA_ACCESS:
|
|
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
|
|
break;
|
|
case BYTE_SIZE:
|
|
rda.setByteSize(Integer.parseInt(rdaValue));
|
|
break;
|
|
case LICENSE:
|
|
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node)));
|
|
break;
|
|
case FORMAT:
|
|
rda.setFormat(Collections.singletonList(rdaValue));
|
|
break;
|
|
case TITLE:
|
|
rda.setTitle(rdaValue);
|
|
break;
|
|
case HOST:
|
|
rda.setHost(HostRDAMapper.toRDA(nodes, node.get("numbering").asText()));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new ArrayList<>(rdaMap.values());
|
|
}
|
|
|
|
public static Distribution toRDA(JsonNode node) {
|
|
Distribution rda = new Distribution();
|
|
String rdaProperty = node.get("rdaProperty").asText();
|
|
String rdaValue = node.get("value").asText();
|
|
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")) {
|
|
// rda.setHost(HostRDAMapper.toRDA(node));
|
|
} else if (rdaProperty.contains("license")) {
|
|
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node)));
|
|
} else if (rdaProperty.contains("title")) {
|
|
rda.setTitle(rdaValue);
|
|
}
|
|
|
|
|
|
return rda;
|
|
}
|
|
|
|
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());
|
|
}
|
|
|
|
private enum PropertyName {
|
|
ACCESS_URL("access_url"),
|
|
AVAILABLE_UTIL("available_util"),
|
|
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;
|
|
|
|
PropertyName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
}
|
|
}
|