94 lines
3.3 KiB
Java
94 lines
3.3 KiB
Java
package org.opencdmp.filetransformer.rda.model.rda.mapper;
|
|
|
|
import org.opencdmp.commonmodels.models.descriptiotemplate.FieldModel;
|
|
import org.opencdmp.filetransformer.rda.model.rda.Cost;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.stream.Collectors;
|
|
|
|
@Component
|
|
public class CostRDAMapper{
|
|
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
|
|
|
|
public List<Cost> toRDA(Map<String, 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, Cost> rdaMap = new HashMap<>();
|
|
for(Map.Entry<String, List<FieldModel>> entry: nodes.entrySet()){
|
|
for(FieldModel node: entry.getValue()) {
|
|
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.cost")).findFirst().orElse("");
|
|
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;
|
|
}
|
|
String key = entry.getKey();
|
|
|
|
if (!key.contains("mult")) {
|
|
key = "0";
|
|
} else {
|
|
key = "" + key.charAt(4);
|
|
}
|
|
Cost rda;
|
|
if (rdaMap.containsKey(key)) {
|
|
rda = rdaMap.get(key);
|
|
} else {
|
|
rda = new Cost();
|
|
rdaMap.put(key, rda);
|
|
}
|
|
if (rdaProperty.contains("value")) {
|
|
try {
|
|
rda.setValue(Double.valueOf(rdaValue.getTextValue()));
|
|
} catch (NumberFormatException e) {
|
|
logger.warn("Dmp cost value " + rdaValue + " is not valid. Cost value will not be set.");
|
|
}
|
|
} else if (rdaProperty.contains("currency_code")) {
|
|
try {
|
|
// HashMap<String, String> result =
|
|
// new ObjectMapper().readValue(rdaValue, HashMap.class);
|
|
// rda.setCurrencyCode(Cost.CurrencyCode.fromValue(result.get("value")));
|
|
rda.setCurrencyCode(Cost.CurrencyCode.fromValue(rdaValue.getTextValue())); //TODO
|
|
} catch (Exception e) {
|
|
logger.warn("Dmp cost currency code is not valid and will not be set.");
|
|
}
|
|
} else if (rdaProperty.contains("title")) {
|
|
rda.setTitle(rdaValue.getTextValue());
|
|
|
|
// Iterator<JsonNode> iter = mapper.readTree(rdaValue).elements(); //TODO
|
|
// StringBuilder title = new StringBuilder();
|
|
// while(iter.hasNext()){
|
|
// String next = iter.next().asText();
|
|
// if(!next.equals("Other")) {
|
|
// title.append(next).append(", ");
|
|
// }
|
|
// }
|
|
// if(title.length() > 2){
|
|
// rda.setTitle(title.substring(0, title.length() - 2));
|
|
// }
|
|
// else{
|
|
// String t = rda.getTitle();
|
|
// if(t == null){ // only other as title
|
|
// rda.setTitle(rdaValue.getTextValue());
|
|
// }
|
|
// else{ // option + other
|
|
// rda.setTitle(t + ", " + rdaValue);
|
|
// }
|
|
// }
|
|
} else if (rdaProperty.contains("description")) {
|
|
rda.setDescription(rdaValue.getTextValue());
|
|
}
|
|
}
|
|
}
|
|
List<Cost> rdaList = rdaMap.values().stream()
|
|
.filter(cost -> cost.getTitle() != null)
|
|
.collect(Collectors.toList());
|
|
return rdaList;
|
|
|
|
}
|
|
}
|