file-transformer-rda-json/core/src/main/java/org/opencdmp/filetransformer/rda/model/rda/mapper/TechnicalResourceRDAMapper....

97 lines
3.1 KiB
Java

package org.opencdmp.filetransformer.rda.model.rda.mapper;
import org.opencdmp.commonmodels.models.descriptiotemplate.FieldModel;
import org.opencdmp.filetransformer.rda.model.rda.TechnicalResource;
import org.opencdmp.filetransformer.rda.utils.string.MyStringUtils;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class TechnicalResourceRDAMapper {
public List<TechnicalResource> toRDA(List<FieldModel> nodes, List<org.opencdmp.commonmodels.models.description.FieldModel> valueFields ) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, TechnicalResource> rdaMap = new HashMap<>();
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.technical_resource")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
org.opencdmp.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;
}
TechnicalResource 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 NAME:
rda.setName(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.NAME.getName(), node.getId());
break;
case DESCRIPTION:
rda.setDescription(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
}
}
}
}
return rdaMap.values().stream()
.filter(tr -> tr.getName() != null)
.collect(Collectors.toList());
}
private static TechnicalResource getRelative(Map<String, TechnicalResource> 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 TechnicalResource());
}
private enum ExportPropertyName {
NAME("name"),
DESCRIPTION("description");
private String name;
ExportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private enum ImportPropertyName {
NAME("nameId"),
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 name not available");
}
}
}