Fixed various issues with DMP imports and exports (mostly RDA)

This commit is contained in:
George Kalampokis 2020-09-15 13:21:22 +03:00
parent 29c8ad8589
commit da7fc0def3
8 changed files with 85 additions and 31 deletions

View File

@ -57,8 +57,7 @@ import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.springframework.http.MediaType.APPLICATION_ATOM_XML;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.http.MediaType.*;
@RestController
@ -256,8 +255,10 @@ public class DMPs extends BaseController {
public ResponseEntity<ResponseItem> dmpUpload(@RequestParam("file") MultipartFile[] files, @RequestParam(name = "profiles", required = false)String[] profiles, Principal principal) throws Exception {
if (files[0].getContentType().equals(APPLICATION_JSON.toString())) {
this.dataManagementPlanManager.createFromRDA(files, principal, profiles);
} else if (files[0].getContentType().equals(APPLICATION_ATOM_XML.toString())) {
} else if (files[0].getContentType().equals(APPLICATION_ATOM_XML.toString()) || files[0].getContentType().equals(TEXT_XML.toString())) {
this.dataManagementPlanManager.createDmpFromXml(this.getApiContext(), files, principal);
} else {
return ResponseEntity.badRequest().body(new ResponseItem().status(ApiMessageCode.ERROR_MESSAGE).message("File format is not supported"));
}
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List>()
.status(ApiMessageCode.SUCCESS_MESSAGE));

View File

@ -0,0 +1,13 @@
package eu.eudat.logic.utilities.json;
public class JavaToJson {
public static String objectStringToJson(String object) {
String result = object.replaceAll("=", "\":\"")
.replaceAll("\\{", "{\"")
.replaceAll(", ", "\", \"")
.replaceAll("}", "\"}" ).
replaceAll("}\", \"\\{", "}, {");
return result;
}
}

View File

@ -53,6 +53,14 @@ public class DatasetId implements Serializable
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
private final static long serialVersionUID = -6295164005851378031L;
public DatasetId() {
}
public DatasetId(String identifier, Type type) {
this.identifier = identifier;
this.type = type;
}
/**
* The Dataset Identifier Schema
* <p>

View File

@ -52,15 +52,17 @@ public class DatasetIdRDAMapper {
}
private static void finalRDAMap(DatasetId rda, String property, String value) {
for (DatasetIdProperties datasetIdProperties : DatasetIdProperties.values()) {
if (property.contains(datasetIdProperties.getName())) {
switch (datasetIdProperties) {
case IDENTIFIER:
rda.setIdentifier(value);
break;
case TYPE:
rda.setType(DatasetId.Type.fromValue(value));
break;
if (value != null) {
for (DatasetIdProperties datasetIdProperties : DatasetIdProperties.values()) {
if (property.contains(datasetIdProperties.getName())) {
switch (datasetIdProperties) {
case IDENTIFIER:
rda.setIdentifier(value);
break;
case TYPE:
rda.setType(DatasetId.Type.fromValue(value));
break;
}
}
}
}

View File

@ -11,6 +11,7 @@ import eu.eudat.models.data.datasetprofile.DatasetProfileOverviewModel;
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
import eu.eudat.models.rda.Contributor;
import eu.eudat.models.rda.Dataset;
import eu.eudat.models.rda.DatasetId;
import eu.eudat.models.rda.Language;
import org.json.JSONObject;
import org.slf4j.Logger;
@ -56,11 +57,14 @@ public class DatasetRDAMapper {
if (!idNodes.isEmpty()) {
rda.setDatasetId(DatasetIdRDAMapper.toRDA(idNodes));
}
if (rda.getDatasetId() == null) {
rda.setDatasetId(new DatasetId(dataset.getId().toString(), DatasetId.Type.OTHER));
}
List<JsonNode> typeNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.type");
if (!typeNodes.isEmpty()) {
rda.setType(typeNodes.get(0).get("value").asText());
} else {
rda.setType(dataset.getLabel());
rda.setType("DMP Dataset");
}
List<JsonNode> languageNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.language");
if (!languageNodes.isEmpty()) {
@ -93,9 +97,9 @@ public class DatasetRDAMapper {
rda.setKeyword(keywordNodes.stream().map(keywordNode -> {
JsonNode value = keywordNode.get("value");
if (value.isArray()) {
return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.asText())).collect(Collectors.toList());
return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.asText())).flatMap(Collection::stream).collect(Collectors.toList());
} else {
return Collections.singletonList(KeywordRDAMapper.toRDA(keywordNode.get("value").asText()));
return KeywordRDAMapper.toRDA(keywordNode.get("value").asText());
}
}).flatMap(Collection::stream).collect(Collectors.toList()));
for (int i = 0; i < keywordNodes.size(); i++) {
@ -118,6 +122,8 @@ public class DatasetRDAMapper {
List<JsonNode> sensitiveDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.sensitive_data");
if (!sensitiveDataNodes.isEmpty()) {
rda.setSensitiveData(sensitiveDataNodes.stream().map(sensitiveDataNode -> Dataset.SensitiveData.fromValue(sensitiveDataNode.get("value").asText())).findFirst().get());
} else {
rda.setSensitiveData(Dataset.SensitiveData.UNKNOWN);
}
List<JsonNode> technicalResourceNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.technical_resource");
if (!technicalResourceNodes.isEmpty()) {

View File

@ -31,6 +31,18 @@ public class DmpRDAMapper {
@Transactional
public Dmp toRDA(DMP dmp) {
Map<String, Object> extraProperties;
if (dmp.getExtraProperties() == null) {
throw new IllegalArgumentException("DMP is missing required Data for RDA export");
} else {
extraProperties = new org.json.JSONObject(dmp.getExtraProperties()).toMap();
if (extraProperties.get("language") == null) {
throw new IllegalArgumentException("DMP must have it's language property defined");
}
if (extraProperties.get("contact") == null) {
throw new IllegalArgumentException("DMP must have it's contact property defined");
}
}
Dmp rda = new Dmp();
if (dmp.getDoi() != null && !dmp.getDoi().isEmpty()) {
rda.setDmpId(DmpIdRDAMapper.toRDA(dmp.getDoi()));
@ -42,11 +54,13 @@ public class DmpRDAMapper {
rda.setModified(dmp.getModified());
rda.setTitle(dmp.getLabel());
Map<String, Object> extraProperties = new org.json.JSONObject(dmp.getExtraProperties()).toMap();
if (!extraProperties.isEmpty()) {
if (extraProperties.get("language") != null) {
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(extraProperties.get("language").toString()));
if (extraProperties.get("ethicalIssues") != null) {
rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(extraProperties.get("ethicalIsses").toString()));
} else {
rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
}
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(extraProperties.get("language").toString()));
if (extraProperties.get("costs") != null) {
rda.setCost(new ArrayList<>());
((List) extraProperties.get("costs")).forEach(costl -> {
@ -59,10 +73,8 @@ public class DmpRDAMapper {
rda.getCost().add(cost);
});
}
if (extraProperties.get("contact") != null) {
UserInfo contact = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(UUID.fromString((String)extraProperties.get("contact")));
rda.setContact(ContactRDAMapper.toRDA(contact));
}
UserInfo contact = apiContext.getOperationsContext().getDatabaseRepository().getUserInfoDao().find(UUID.fromString((String) extraProperties.get("contact")));
rda.setContact(ContactRDAMapper.toRDA(contact));
}
/*UserInfo creator;
@ -101,7 +113,7 @@ public class DmpRDAMapper {
entity.getAssociatedDmps().add(exProfile);
}
}
if (rda.getContributor() != null && !rda.getContributor().isEmpty()) {
if (rda.getContributor() != null && !rda.getContributor().isEmpty() && rda.getContributor().get(0).getContributorId() != null) {
entity.setResearchers(rda.getContributor().stream().map(ContributorRDAMapper::toEntity).filter(StreamDistinctBy.distinctByKey(Researcher::getReference)).collect(Collectors.toSet()));
}
entity.setCreated(rda.getCreated());

View File

@ -1,25 +1,29 @@
package eu.eudat.models.rda.mapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.elastic.entities.Tag;
import eu.eudat.logic.utilities.json.JavaToJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
public class KeywordRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class);
public static String toRDA(String value) {
public static List<String> toRDA(String value) {
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> map = mapper.readValue(value, HashMap.class);
return (String) map.get("name");
value = JavaToJson.objectStringToJson(value);
List<Tag> tags = Arrays.asList(mapper.readValue(value, Tag[].class));
List<String> keywordNames = tags.stream().map(Tag::getName).collect(Collectors.toList());
return keywordNames;
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return value;
return new ArrayList<>();
}
}

View File

@ -346,8 +346,16 @@ export class FormFieldComponent extends BaseComponent implements OnInit {
parseTags() {
let stringValue = this.form.get('value').value;
stringValue = (<string>stringValue).replace(new RegExp('{', 'g'), '{"').replace(new RegExp('=', 'g'), '":"').replace(new RegExp(',', 'g'), '",').replace(new RegExp(', ', 'g'), ', "').replace(new RegExp('}', 'g'), '"}');
stringValue = stringValue.replace(new RegExp('}"', 'g'), '}').replace(new RegExp('"{', 'g'), '{');
if (typeof stringValue === 'string') {
stringValue = (<string>stringValue).replace(new RegExp('{', 'g'), '{"').replace(new RegExp('=', 'g'), '":"').replace(new RegExp(',', 'g'), '",').replace(new RegExp(', ', 'g'), ', "').replace(new RegExp('}', 'g'), '"}');
stringValue = stringValue.replace(new RegExp('}"', 'g'), '}').replace(new RegExp('"{', 'g'), '{');
} else if (stringValue instanceof Array) {
const tempArray = new Array();
for (let stringTag of stringValue) {
tempArray.push(JSON.parse(stringTag));
}
stringValue = JSON.stringify(tempArray);
}
const tagArray = JSON.parse(stringValue);
this.form.patchValue({'value': tagArray});
}