file-transformer-rda-json/core/src/main/java/eu/eudat/file/transformer/rda/mapper/CostRDAMapper.java

115 lines
3.5 KiB
Java

package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.Cost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CostRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static Cost toRDA(Map<String, Object> cost) throws JsonProcessingException {
Cost rda = new Cost();
Map<String, Object> code = mapper.readValue((String) cost.get("code"), HashMap.class);
rda.setCurrencyCode(Cost.CurrencyCode.fromValue((String) code.get("value")));
rda.setDescription((String) cost.get("description"));
if (cost.get("title") == null) {
throw new IllegalArgumentException("Cost Title is missing");
}
rda.setTitle((String) cost.get("title"));
rda.setValue(((Integer) cost.get("value")).doubleValue());
return rda;
}
public static List<Cost> toRDAList(List<FieldFileTransformerModel> nodes) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Cost> rdaMap = new HashMap<>();
for(FieldFileTransformerModel node: nodes){
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.cost")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
String key = node.getNumbering();
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));
}
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")));
}
catch (Exception e) {
logger.warn("Dmp cost currency code is not valid and will not be set.");
}
}
else if(rdaProperty.contains("title")){
Iterator<JsonNode> iter = mapper.readTree(rdaValue).elements();
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);
}
else{ // option + other
rda.setTitle(t + ", " + rdaValue);
}
}
}
else if(rdaProperty.contains("description")){
rda.setDescription(rdaValue);
}
}
List<Cost> rdaList = rdaMap.values().stream()
.filter(cost -> cost.getTitle() != null)
.collect(Collectors.toList());
return rdaList;
}
}