73 lines
2.0 KiB
Java
73 lines
2.0 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.SecurityAndPrivacy;
|
|
|
|
import java.util.*;
|
|
|
|
public class SecurityAndPrivacyRDAMapper {
|
|
|
|
public static List<SecurityAndPrivacy> toRDAList(List<JsonNode> nodes) {
|
|
Map<String, SecurityAndPrivacy> rdaMap = new HashMap<>();
|
|
|
|
for (JsonNode node: nodes) {
|
|
String rdaProperty = node.get("rdaProperty").asText();
|
|
String rdaValue = node.get("value").asText();
|
|
|
|
SecurityAndPrivacy 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 TITLE:
|
|
rda.setTitle(rdaValue);
|
|
break;
|
|
case DESCRIPTION:
|
|
rda.setDescription(rdaValue);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return new ArrayList<>(rdaMap.values());
|
|
}
|
|
|
|
public static SecurityAndPrivacy toRDA(JsonNode node) {
|
|
SecurityAndPrivacy rda = new SecurityAndPrivacy();
|
|
String rdaProperty = node.get("rdaProperty").asText();
|
|
String value =node.get("value").asText();
|
|
|
|
if (rdaProperty.contains("description")) {
|
|
rda.setDescription(value);
|
|
} else if (rdaProperty.contains("title")) {
|
|
rda.setTitle(value);
|
|
}
|
|
|
|
return rda;
|
|
}
|
|
|
|
private static SecurityAndPrivacy getRelative(Map<String, SecurityAndPrivacy> 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 SecurityAndPrivacy());
|
|
}
|
|
|
|
private enum PropertyName {
|
|
TITLE("title"),
|
|
DESCRIPTION("description");
|
|
|
|
private String name;
|
|
|
|
PropertyName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
}
|
|
}
|