97 lines
3.1 KiB
Java
97 lines
3.1 KiB
Java
package eu.eudat.file.transformer.model.rda.mapper;
|
|
|
|
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
|
|
import eu.eudat.file.transformer.model.rda.SecurityAndPrivacy;
|
|
import eu.eudat.file.transformer.utils.string.MyStringUtils;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.*;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class SecurityAndPrivacyRDAMapper {
|
|
public List<SecurityAndPrivacy> 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, SecurityAndPrivacy> rdaMap = new HashMap<>();
|
|
|
|
for (FieldModel node: nodes) {
|
|
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.security_and_privacy")).findFirst().orElse("");
|
|
if (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.getTextValue().isBlank()){
|
|
continue;
|
|
}
|
|
SecurityAndPrivacy rda = getRelative(rdaMap, node.getNumbering());
|
|
if (!rdaMap.containsValue(rda)) {
|
|
rdaMap.put(node.getNumbering(), rda);
|
|
}
|
|
for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
|
|
if (rdaProperty.contains(exportPropertyName.getName())) {
|
|
switch (exportPropertyName) {
|
|
case TITLE:
|
|
rda.setTitle(rdaValue.getTextValue());
|
|
rda.getAdditionalProperties().put(ImportPropertyName.TITLE.getName(), node.getId());
|
|
break;
|
|
case DESCRIPTION:
|
|
rda.setDescription(rdaValue.getTextValue());
|
|
rda.getAdditionalProperties().put(ImportPropertyName.DESCRIPTION.getName(), node.getId());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return rdaMap.values().stream()
|
|
.filter(sap -> sap.getTitle() != null)
|
|
.collect(Collectors.toList());
|
|
}
|
|
|
|
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 ExportPropertyName {
|
|
TITLE("title"),
|
|
DESCRIPTION("description");
|
|
|
|
private String name;
|
|
|
|
ExportPropertyName(String name) {
|
|
this.name = name;
|
|
}
|
|
|
|
public String getName() {
|
|
return name;
|
|
}
|
|
}
|
|
|
|
private enum ImportPropertyName {
|
|
TITLE("titleId"),
|
|
DESCRIPTION("descriptionId");
|
|
|
|
private 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("Property not available");
|
|
}
|
|
}
|
|
}
|