format prefilling fixes

This commit is contained in:
Bernaldo Mihasi 2022-02-02 14:15:33 +02:00
parent bd5041e4e3
commit 048189952c
2 changed files with 40 additions and 4 deletions

View File

@ -22,6 +22,11 @@ import eu.eudat.models.data.user.composite.PagedDatasetProfile;
import net.minidev.json.JSONValue;
import org.json.JSONArray;
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.Method;
@ -29,6 +34,7 @@ import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel> {
@ -371,11 +377,11 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
}
for (JsonNode node: nodes) {
String id = node.get(0) != null ? node.get(0).get("id").asText() : node.get("id").asText();
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format")){
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format") && !value.equals("null")){
JSONArray jsonArr = new JSONArray(value);
List<String> formats = new ArrayList<>();
String extension;
for (int i = 0; i < jsonArr.length(); i++){
for(int i = 0; i < jsonArr.length(); i++){
JSONObject jsonObj = jsonArr.getJSONObject(i);
String filename = (String) jsonObj.get("filename");
int index = filename.lastIndexOf('.');
@ -383,8 +389,37 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
formats.add(extension);
}
formats = formats.stream().distinct().collect(Collectors.toList());
String format = String.join(", ", formats);
properties.put(id, format);
List<Object> standardFormats = new ArrayList<>();
for(String format: formats) {
RestTemplate restTemplate = new RestTemplate();
String parsedUrl = "https://eestore.paas2.uninett.no/api/fileformat/?search=" + format;
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(new MediaType("application", "vnd.api+json")));
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")));
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();
cur.put("label", formatName.toString());
standardFormats.add(cur.toString());
found = true;
break;
}
}
if(found){
break;
}
}
}
properties.put(id, standardFormats);
}
else if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
value = value.replace("\"", "");

View File

@ -1040,6 +1040,7 @@
<mapping source="metadata.access_right" maDmpTarget="dataset.distribution.data_access"/>
<mapping source="metadata.publication_date" maDmpTarget="dataset.issued"/>
<mapping source="metadata.license.created" maDmpTarget="dataset.distribution.license.start_date"/>
<mapping source="metadata.embargo_date" maDmpTarget="dataset.distribution.license.start_date"/>
<mapping source="files" maDmpTarget="dataset.distribution.format"/>
</mappings>
<fixedMappings>