keyword-tags exported-imported fixes, prefilling more mapping

This commit is contained in:
Bernaldo Mihasi 2022-01-28 14:55:52 +02:00
parent 70d37cead1
commit 125a97baed
5 changed files with 73 additions and 24 deletions

View File

@ -1054,14 +1054,10 @@ public class DatasetManager {
if (!value.toString().equals("\"\"")) { if (!value.toString().equals("\"\"")) {
String stringValue = value.toString().replaceAll("=", ":"); String stringValue = value.toString().replaceAll("=", ":");
JSONArray values = new JSONArray(stringValue); JSONArray values = new JSONArray(stringValue);
if (values != null) { values.iterator().forEachRemaining(element -> {
values.iterator().forEachRemaining(element -> { Map<String, Object> data = ((JSONObject) element).toMap();
Map<String, Object> data = ((JSONObject) element).toMap(); this.addTag(tags, wizardModel.getTags(), data.get("id").toString(), data.get("name").toString());
this.addTag(tags, wizardModel.getTags(), data.get("id").toString(), data.get("name").toString()); });
});
} else {
this.addTag(tags, wizardModel.getTags(), "", value.asText());
}
} }
}); });
} }

View File

@ -20,9 +20,13 @@ 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.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.JSONObject;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -346,7 +350,7 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
Class<?>[] params = setterMethod.getParameterTypes(); Class<?>[] params = setterMethod.getParameterTypes();
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
//GK: Tags Special logic //GK: Tags Special logic
if (prefillingMapping.getTarget().equals("tags")) { if (!value.equals("null") && prefillingMapping.getTarget().equals("tags")) {
List<Object> rawTags = (List<Object>) mapper.readValue(value, params[0]); List<Object> rawTags = (List<Object>) mapper.readValue(value, params[0]);
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());
@ -359,9 +363,44 @@ public class DatasetWizardModel implements DataModel<Dataset, DatasetWizardModel
} }
} else { } else {
List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget()); List<JsonNode> nodes = JsonSearcher.findNodes(parentNode, "rdaProperty", prefillingMapping.getMaDmpTarget());
if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.available_until") && !value.equals("null")){
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("uuuu-MM-dd");
LocalDate date = LocalDate.parse(value.replace("\"", ""), formatter);
date = date.plusYears(20);
value = date.toString();
}
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();
properties.put(id, value); if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.format")){
JSONArray jsonArr = new JSONArray(value);
List<String> formats = new ArrayList<>();
String extension;
for (int i = 0; i < jsonArr.length(); i++){
JSONObject jsonObj = jsonArr.getJSONObject(i);
String filename = (String) jsonObj.get("filename");
int index = filename.lastIndexOf('.');
extension = (index > 0) ? filename.substring(index+1) : filename;
formats.add(extension);
}
formats = formats.stream().distinct().collect(Collectors.toList());
String format = String.join(", ", formats);
properties.put(id, format);
}
else if(prefillingMapping.getMaDmpTarget().equals("dataset.distribution.data_access")){
value = value.replace("\"", "");
if(value.equals("open")){
properties.put(id, value);
}
else if(value.equals("restricted")){
properties.put(id, "shared");
}
else{
properties.put(id, "closed");
}
}
else{
properties.put(id, value.replace("\"", ""));
}
} }
} }
} }

View File

@ -1,13 +1,13 @@
package eu.eudat.models.rda.mapper; package eu.eudat.models.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.data.entities.DatasetProfile; import eu.eudat.data.entities.DatasetProfile;
import eu.eudat.elastic.criteria.DatasetCriteria;
import eu.eudat.elastic.entities.Tag; import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.managers.DatasetManager; import eu.eudat.logic.managers.DatasetManager;
import eu.eudat.logic.services.ApiContext; import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.json.JavaToJson; import eu.eudat.logic.utilities.helpers.StreamDistinctBy;
import eu.eudat.logic.utilities.json.JsonSearcher; import eu.eudat.logic.utilities.json.JsonSearcher;
import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel; import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel; import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
@ -19,7 +19,6 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import javax.transaction.Transactional; import javax.transaction.Transactional;
import java.net.URI;
import java.time.Instant; import java.time.Instant;
import java.time.ZoneId; import java.time.ZoneId;
import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatter;
@ -131,7 +130,7 @@ public class DatasetRDAMapper {
rda.setKeyword(keywordNodes.stream().map(keywordNode -> { rda.setKeyword(keywordNodes.stream().map(keywordNode -> {
JsonNode value = keywordNode.get("value"); JsonNode value = keywordNode.get("value");
if (value.isArray()) { if (value.isArray()) {
return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.asText())).flatMap(Collection::stream).collect(Collectors.toList()); return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.toString())).flatMap(Collection::stream).collect(Collectors.toList());
} else { } else {
return KeywordRDAMapper.toRDA(keywordNode.get("value").asText()); return KeywordRDAMapper.toRDA(keywordNode.get("value").asText());
} }
@ -331,9 +330,14 @@ public class DatasetRDAMapper {
// if (keywordIds.size() < rda.getKeyword().size()) { // if (keywordIds.size() < rda.getKeyword().size()) {
// takeAll = true; // takeAll = true;
// } // }
DatasetCriteria criteria = new DatasetCriteria();
criteria.setHasTags(true);
List<Tag> tags = this.apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().query(criteria).stream().map(eu.eudat.elastic.entities.Dataset::getTags).flatMap(Collection::stream).filter(StreamDistinctBy.distinctByKey(Tag::getId)).collect(Collectors.toList());
if(!rda.getKeyword().isEmpty()){ if(!rda.getKeyword().isEmpty()){
for (int i = 0; i < keywordIds.size(); i++) { List<Tag> templateTags = tags.stream().filter(tag -> rda.getKeyword().contains(tag.getName())).collect(Collectors.toList());
// if (takeAll) { properties.put(keywordIds.get(0), mapper.writeValueAsString(templateTags));
// for (int i = 0; i < keywordIds.size(); i++) {
// //if (takeAll) {
// List<String> tags = new ArrayList<>(); // List<String> tags = new ArrayList<>();
// for (String keyword : rda.getKeyword()) { // for (String keyword : rda.getKeyword()) {
// tags.add(mapper.writeValueAsString(toTagEntity(keyword))); // tags.add(mapper.writeValueAsString(toTagEntity(keyword)));
@ -342,8 +346,8 @@ public class DatasetRDAMapper {
// } else { // } else {
// properties.put(keywordIds.get(i), mapper.writeValueAsString(toTagEntity(rda.getKeyword().get(i)))); // properties.put(keywordIds.get(i), mapper.writeValueAsString(toTagEntity(rda.getKeyword().get(i))));
// } // }
properties.put(keywordIds.get(i), rda.getKeyword().get(i)); // properties.put(keywordIds.get(i), rda.getKeyword().get(i));
} // }
} }
} }

View File

@ -2,7 +2,9 @@ package eu.eudat.models.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.elastic.entities.Dataset;
import eu.eudat.elastic.entities.Tag; import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.services.ApiContext;
import eu.eudat.logic.utilities.json.JavaToJson; import eu.eudat.logic.utilities.json.JavaToJson;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -15,15 +17,15 @@ public class KeywordRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class); private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class);
public static List<String> toRDA(String value) { public static List<String> toRDA(String value) {
if (!value.isEmpty()) { if (!value.isEmpty() && !value.equals("null")) {
try { try {
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
String valueJson = JavaToJson.objectStringToJson(value); String valueJson = JavaToJson.objectStringToJson(value);
List<Tag> tags = Arrays.asList(mapper.readValue(valueJson, Tag[].class)); Tag tag = mapper.readValue(valueJson, Tag.class);
return tags.stream().map(Tag::getName).collect(Collectors.toList()); return new ArrayList<>(Collections.singletonList(tag.getName()));
} catch (JsonProcessingException e) { } catch (JsonProcessingException e) {
logger.warn(e.getMessage() + ". Attempting to parse it as a String list."); logger.warn(e.getMessage() + ". Attempting to parse it as a String since its a new tag.");
return new ArrayList<>(Arrays.asList(value.replace(" ", "").split(","))); return new ArrayList<>(Collections.singletonList(value));
} }
} }

View File

@ -1039,11 +1039,19 @@
<mapping source="metadata.dates.valid" maDmpTarget="dataset.distribution.available_until"/> <mapping source="metadata.dates.valid" maDmpTarget="dataset.distribution.available_until"/>
<mapping source="metadata.access_right" maDmpTarget="dataset.distribution.data_access"/> <mapping source="metadata.access_right" maDmpTarget="dataset.distribution.data_access"/>
<mapping source="metadata.publication_date" maDmpTarget="dataset.issued"/> <mapping source="metadata.publication_date" maDmpTarget="dataset.issued"/>
<mapping source="metadata.license.id" maDmpTarget="dataset.distribution.license.license_ref"/>
<mapping source="metadata.license.created" maDmpTarget="dataset.distribution.license.start_date"/> <mapping source="metadata.license.created" maDmpTarget="dataset.distribution.license.start_date"/>
<mapping source="files" maDmpTarget="dataset.distribution.format"/>
</mappings> </mappings>
<fixedMappings> <fixedMappings>
<fixedMapping maDmpTarget="dataset.distribution.host.title" value="Zenodo" /> <fixedMapping maDmpTarget="dataset.distribution.host.title" value="Zenodo" />
<fixedMapping maDmpTarget="dataset.metadata.metadata_standard_id.identifier" value="https://schema.datacite.org/meta/kernel-4.4/" />
<fixedMapping maDmpTarget="dataset.metadata.metadata_standard_id.type" value="url" />
<fixedMapping maDmpTarget="dataset.distribution.host.description" value="Repository hosted by Zenodo" />
<fixedMapping maDmpTarget="dataset.distribution.host.url" value="https://zenodo.org" />
<fixedMapping maDmpTarget="dataset.distribution.host.pid_system" value="doi" />
<fixedMapping maDmpTarget="dataset.security_and_privacy.title" value="TBD" />
<fixedMapping maDmpTarget="dataset.security_and_privacy.description" value="TBD" />
<fixedMapping maDmpTarget="dataset.preservation_statement" value="TBD" />
</fixedMappings> </fixedMappings>
</prefillingGet> </prefillingGet>
</config> </config>