rda export import tag bug, format preffiling fixed

This commit is contained in:
Bernaldo Mihasi 2022-02-17 10:11:34 +02:00
parent bf77a85acb
commit da5ba952e1
4 changed files with 86 additions and 70 deletions

View File

@ -164,6 +164,10 @@ public class DataManagementProfileManager {
} }
private List<Tuple<String, String>> externalAutocompleteRequest(DmpProfileExternalAutoComplete data, String like) { private List<Tuple<String, String>> externalAutocompleteRequest(DmpProfileExternalAutoComplete data, String like) {
return externalAutocompleteRequest(data.getUrl(), data.getOptionsRoot(), data.getLabel(), data.getValue(), like);
}
public static List<Tuple<String, String>> externalAutocompleteRequest(String url, String optionsRoot, String label, String value, String like) {
List<Tuple<String, String>> result = new LinkedList<>(); List<Tuple<String, String>> result = new LinkedList<>();
RestTemplate restTemplate = new RestTemplate(); RestTemplate restTemplate = new RestTemplate();
HttpHeaders headers = new HttpHeaders(); HttpHeaders headers = new HttpHeaders();
@ -171,11 +175,12 @@ public class DataManagementProfileManager {
headers.setContentType(MediaType.APPLICATION_JSON); headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<>("parameters", headers); HttpEntity<String> entity = new HttpEntity<>("parameters", headers);
ResponseEntity<Object> response = restTemplate.exchange(data.getUrl() + "?search=" + like, HttpMethod.GET, entity, Object.class); ResponseEntity<Object> response = restTemplate.exchange(url + "?search=" + like, HttpMethod.GET, entity, Object.class);
DocumentContext jsonContext = JsonPath.parse(response.getBody()); DocumentContext jsonContext = JsonPath.parse(response.getBody());
List<Map<String, String>> jsonItems = jsonContext.read(data.getOptionsRoot() + "['" + data.getLabel() + "','" + data.getValue() + "']"); List<Map<String, String>> jsonItems = jsonContext.read(optionsRoot + "['" + label + "','" + value + "']");
jsonItems.forEach(item -> result.add(new Tuple<>(item.get(data.getValue()), item.get(data.getLabel())))); jsonItems.forEach(item -> result.add(new Tuple<>(item.get(value), item.get(label))));
return result; return result;
} }
} }

View File

@ -1050,10 +1050,13 @@ public class DatasetManager {
Set<JsonNode> tagNodes = new HashSet<>(); Set<JsonNode> tagNodes = new HashSet<>();
tagNodes.addAll(JsonSearcher.findNodes(propertiesJson, "renderStyle", "tags", true)); tagNodes.addAll(JsonSearcher.findNodes(propertiesJson, "renderStyle", "tags", true));
tagNodes.addAll(JsonSearcher.findNodes(propertiesJson, "rdaProperty", "dataset.keyword")); tagNodes.addAll(JsonSearcher.findNodes(propertiesJson, "rdaProperty", "dataset.keyword"));
if(wizardModel.getTags() == null){
wizardModel.setTags(new ArrayList<>());
}
if (!tagNodes.isEmpty()) { if (!tagNodes.isEmpty()) {
tagNodes.forEach(node -> { tagNodes.forEach(node -> {
JsonNode value = node.get("value"); JsonNode value = node.get("value");
if (!value.toString().equals("\"\"")) { if (!value.toString().equals("\"\"") && !value.toString().equals("null")) {
String stringValue = value.toString().replaceAll("=", ":"); String stringValue = value.toString().replaceAll("=", ":");
JSONArray values = new JSONArray(stringValue); JSONArray values = new JSONArray(stringValue);
values.iterator().forEachRemaining(element -> { values.iterator().forEachRemaining(element -> {

View File

@ -5,6 +5,7 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.*; import eu.eudat.data.entities.*;
import eu.eudat.elastic.entities.Tag; import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.managers.DataManagementProfileManager;
import eu.eudat.logic.managers.DatasetManager; import eu.eudat.logic.managers.DatasetManager;
import eu.eudat.logic.proxy.config.entities.PrefillingFixedMapping; import eu.eudat.logic.proxy.config.entities.PrefillingFixedMapping;
import eu.eudat.logic.proxy.config.entities.PrefillingGet; import eu.eudat.logic.proxy.config.entities.PrefillingGet;
@ -18,15 +19,11 @@ import eu.eudat.models.data.dataset.Service;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel; import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.dmp.DataManagementPlan; import eu.eudat.models.data.dmp.DataManagementPlan;
import eu.eudat.models.data.externaldataset.ExternalDatasetListingModel; import eu.eudat.models.data.externaldataset.ExternalDatasetListingModel;
import eu.eudat.models.data.helpermodels.Tuple;
import eu.eudat.models.data.user.composite.PagedDatasetProfile; import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import net.minidev.json.JSONValue; import net.minidev.json.JSONValue;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -34,7 +31,6 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel> { public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel> {
@ -361,6 +357,11 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
if (rawTags.get(0) instanceof String) { if (rawTags.get(0) instanceof String) {
List<Tag> parsedTags = rawTags.stream().map(rawTag -> new Tag((String) rawTag, (String) rawTag)).collect(Collectors.toList()); List<Tag> parsedTags = rawTags.stream().map(rawTag -> new Tag((String) rawTag, (String) rawTag)).collect(Collectors.toList());
value = mapper.writeValueAsString(parsedTags); value = mapper.writeValueAsString(parsedTags);
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", "dataset.keyword");
for (JsonNode node: nodes) {
String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText();
properties.put(id, value);
}
} }
} }
setterMethod.invoke(datasetWizardModel, mapper.readValue(value, params[0])); setterMethod.invoke(datasetWizardModel, mapper.readValue(value, params[0]));
@ -375,6 +376,7 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
date = date.plusYears(20); date = date.plusYears(20);
value = date.toString(); value = date.toString();
} }
StringBuilder freeTextFormat = new StringBuilder();
for (JsonNode node: nodes) { for (JsonNode node: nodes) {
String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText(); String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText();
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format") && !value.equals("null")) { if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format") && !value.equals("null")) {
@ -389,53 +391,46 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
formats.add(extension); formats.add(extension);
} }
formats = formats.stream().distinct().collect(Collectors.toList()); formats = formats.stream().distinct().collect(Collectors.toList());
List<Object> standardFormats = new ArrayList<>(); List<Object> standardFormats = new ArrayList<>();
StringBuilder freeTextFormat = new StringBuilder();
String renderStyle = node.get(0) != null ? node.get(0).get("viewStyle").get("renderStyle").asText() : node.get("viewStyle").get("renderStyle").asText();
if(renderStyle.equals("combobox")){
String autocomplete = node.get(0) != null ? node.get(0).get("data").get("type").asText() : node.get("data").get("type").asText();
if(autocomplete.equals("autocomplete")) {
JsonNode urlNode = node.get(0) != null ? node.get(0).get("data").get("autoCompleteSingleDataList") : node.get("data").get("autoCompleteSingleDataList");
String url = urlNode.get(0).get("url").asText();
String optionsRoot = urlNode.get(0).get("optionsRoot").asText();
String label = urlNode.get(0).get("autoCompleteOptions").get("label").asText();
String val = urlNode.get(0).get("autoCompleteOptions").get("value").asText();
for (String format : formats) { for (String format : formats) {
RestTemplate restTemplate = new RestTemplate(); List<Tuple<String, String>> result = DataManagementProfileManager.externalAutocompleteRequest(url, optionsRoot, label, val, format);
String parsedUrl = "https://eestore.paas2.uninett.no/api/fileformat/?search=" + format; result = result.stream().distinct().collect(Collectors.toList());
HttpHeaders headers = new HttpHeaders(); if(!result.isEmpty()){
headers.setAccept(Collections.singletonList(new MediaType("application", "vnd.api+json"))); for (Tuple<String, String> f : result) {
HttpEntity<String> entity = new HttpEntity("", headers);
Map<String, Object> data = restTemplate.exchange(parsedUrl, HttpMethod.GET, entity, LinkedHashMap.class).getBody();
jsonArr = new JSONArray(new ObjectMapper().writeValueAsString(data.get("data")));
if(jsonArr.length() > 0) {
for (int i = 0; i < jsonArr.length(); i++) {
JSONObject jsonObj = jsonArr.getJSONObject(i);
jsonObj = jsonObj.getJSONObject("attributes");
JSONArray extensions = jsonObj.getJSONArray("extensions");
Object formatName = jsonObj.get("name");
boolean found = false;
for (int j = 0; j < extensions.length(); j++) {
if (extensions.getString(j).equals(format)) {
JSONObject cur = new JSONObject(); JSONObject cur = new JSONObject();
cur.put("label", formatName.toString()); cur.put("label", f.getLabel());
standardFormats.add(cur.toString()); standardFormats.add(cur.toString());
found = true; freeTextFormat.append(f.getLabel()).append(", ");
break;
}
}
if (found) {
break;
}
} }
} }
else{ else{
freeTextFormat.append(format).append(", "); freeTextFormat.append(format).append(", ");
} }
} }
String renderStyle = node.get(0) != null ? node.get(0).get("viewStyle").get("renderStyle").asText() : node.get("viewStyle").get("renderStyle").asText();
if(renderStyle.equals("freetext")){
if (freeTextFormat.length() > 0) {
freeTextFormat.setLength(freeTextFormat.length() - 2);
}
properties.put(id, freeTextFormat.toString());
}
else{
properties.put(id, standardFormats); properties.put(id, standardFormats);
} }
} }
else if(renderStyle.equals("freetext")){
if (freeTextFormat.length() == 0) {
for (String format : formats) {
freeTextFormat.append(format).append(", ");
}
}
freeTextFormat.setLength(freeTextFormat.length() - 2);
properties.put(id, freeTextFormat.toString());
}
}
else if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){ else if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
value = value.replace("\"", ""); value = value.replace("\"", "");
if(value.equals("open")){ if(value.equals("open")){

View File

@ -89,12 +89,12 @@ public class DistributionRDAMapper {
int i = 1; int i = 1;
while(iter.hasNext()) { while(iter.hasNext()) {
JsonNode current = iter.next(); JsonNode current = iter.next();
String format = JavaToJson.objectStringToJson(current.asText()); String format = JavaToJson.objectStringToJson(current.toString());
try { try {
Map<String, String> result = new ObjectMapper().readValue(format, HashMap.class); Map<String, String> result = new ObjectMapper().readValue(format, HashMap.class);
format = result.get("label"); format = result.get("label");
formats.add(format); formats.add(format);
rda.setAdditionalProperty("format" + i++, new ObjectMapper().readTree(current.asText())); rda.setAdditionalProperty("format" + i++, new ObjectMapper().readTree(current.toString()));
} }
catch(JsonProcessingException e){ catch(JsonProcessingException e){
logger.warn(e.getMessage()); logger.warn(e.getMessage());
@ -103,8 +103,13 @@ public class DistributionRDAMapper {
rda.setFormat(formats); rda.setFormat(formats);
} }
else{ else{
if(rda.getFormat() == null || rda.getFormat().isEmpty()){
rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(",")))); rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(","))));
} }
else{
rda.getFormat().addAll(Arrays.asList(rdaValue.replace(" ", "").split(",")));
}
}
rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.get("id").asText()); rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.get("id").asText());
break; break;
case TITLE: case TITLE:
@ -209,6 +214,9 @@ public class DistributionRDAMapper {
break; break;
case FORMAT: case FORMAT:
if (rda.getFormat() != null && !rda.getFormat().isEmpty()) { if (rda.getFormat() != null && !rda.getFormat().isEmpty()) {
String style = distributionNode.get("viewStyle").get("renderStyle").asText();
if(style.equals("combobox")) {
if (distributionNode.get("data").get("type").asText().equals("autocomplete")) {
Map<String, Object> additionalProperties = rda.getAdditionalProperties(); Map<String, Object> additionalProperties = rda.getAdditionalProperties();
List<Object> standardFormats = new ArrayList<>(); List<Object> standardFormats = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
@ -223,6 +231,11 @@ public class DistributionRDAMapper {
} }
}); });
} }
}
else if(style.equals("freetext")){
properties.put(distributionNode.get("id").asText(), String.join(", ", rda.getFormat()));
}
}
break; break;
case LICENSE: case LICENSE:
if (rda.getLicense() != null && !rda.getLicense().isEmpty()) { if (rda.getLicense() != null && !rda.getLicense().isEmpty()) {