2020-03-20 17:20:22 +01:00
|
|
|
package eu.eudat.models.rda.mapper;
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2020-03-26 17:39:25 +01:00
|
|
|
import eu.eudat.data.entities.DatasetProfile;
|
2020-05-28 17:38:08 +02:00
|
|
|
import eu.eudat.elastic.entities.Tag;
|
2020-03-20 17:20:22 +01:00
|
|
|
import eu.eudat.logic.managers.DatasetManager;
|
2020-03-26 17:39:25 +01:00
|
|
|
import eu.eudat.logic.services.ApiContext;
|
2020-03-20 17:20:22 +01:00
|
|
|
import eu.eudat.logic.utilities.json.JsonSearcher;
|
|
|
|
import eu.eudat.models.data.datasetwizard.DatasetWizardModel;
|
2020-06-09 15:25:50 +02:00
|
|
|
import eu.eudat.models.rda.Contributor;
|
2020-03-20 17:20:22 +01:00
|
|
|
import eu.eudat.models.rda.Dataset;
|
2020-06-03 11:13:31 +02:00
|
|
|
import eu.eudat.models.rda.Language;
|
2020-03-26 17:39:25 +01:00
|
|
|
import org.json.JSONObject;
|
2020-03-20 17:20:22 +01:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.transaction.Transactional;
|
2020-03-26 17:39:25 +01:00
|
|
|
import java.util.*;
|
2020-03-20 17:20:22 +01:00
|
|
|
import java.util.stream.Collectors;
|
2020-03-26 17:39:25 +01:00
|
|
|
import java.util.stream.Stream;
|
2020-06-08 17:31:34 +02:00
|
|
|
import java.util.stream.StreamSupport;
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
@Component
|
|
|
|
public class DatasetRDAMapper {
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
|
|
|
|
|
|
|
|
private DatasetManager datasetManager;
|
2020-03-26 17:39:25 +01:00
|
|
|
private ApiContext apiContext;
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
@Autowired
|
2020-03-26 17:39:25 +01:00
|
|
|
public DatasetRDAMapper(DatasetManager datasetManager, ApiContext apiContext) {
|
2020-03-20 17:20:22 +01:00
|
|
|
this.datasetManager = datasetManager;
|
2020-03-26 17:39:25 +01:00
|
|
|
this.apiContext = apiContext;
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
2020-06-09 15:25:50 +02:00
|
|
|
public Dataset toRDA(eu.eudat.data.entities.Dataset dataset, List<Contributor> contributors) {
|
2020-03-20 17:20:22 +01:00
|
|
|
Dataset rda = new Dataset();
|
2020-05-28 17:38:08 +02:00
|
|
|
// rda.setDatasetId(DatasetIdRDAMapper.toRDA(dataset.getId()));
|
2020-03-20 17:20:22 +01:00
|
|
|
rda.setTitle(dataset.getLabel());
|
|
|
|
rda.setDescription(dataset.getDescription());
|
2020-03-26 17:39:25 +01:00
|
|
|
rda.setAdditionalProperty("template", dataset.getProfile().getId());
|
2020-03-20 17:20:22 +01:00
|
|
|
try {
|
2020-03-26 17:39:25 +01:00
|
|
|
JSONObject jObject = new JSONObject(dataset.getProperties());
|
|
|
|
Map<String, Object> templateIdsToValues = jObject.toMap();
|
2020-03-20 17:20:22 +01:00
|
|
|
DatasetWizardModel datasetWizardModel = new DatasetWizardModel().fromDataModel(dataset);
|
|
|
|
datasetWizardModel.setDatasetProfileDefinition(datasetManager.getPagedProfile(datasetWizardModel, dataset));
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String datasetDescriptionJson = mapper.writeValueAsString(datasetWizardModel.getDatasetProfileDefinition());
|
|
|
|
JsonNode datasetDescriptionObj = mapper.readTree(datasetDescriptionJson);
|
2020-05-28 17:38:08 +02:00
|
|
|
List<JsonNode> idNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.dataset_id");
|
|
|
|
if (!idNodes.isEmpty()) {
|
|
|
|
rda.setDatasetId(DatasetIdRDAMapper.toRDA(idNodes));
|
|
|
|
}
|
2020-03-20 17:20:22 +01:00
|
|
|
List<JsonNode> typeNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.type");
|
|
|
|
if (!typeNodes.isEmpty()) {
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setType(typeNodes.get(0).get("value").asText());
|
2020-05-28 17:38:08 +02:00
|
|
|
} else {
|
|
|
|
rda.setType(dataset.getLabel());
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> languageNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.language");
|
|
|
|
if (!languageNodes.isEmpty()) {
|
2020-06-03 11:13:31 +02:00
|
|
|
rda.setLanguage(Language.fromValue(languageNodes.get(0).get("value").asText()));
|
|
|
|
} else {
|
|
|
|
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(dataset.getProfile().getLanguage()));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> metadataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.metadata");
|
|
|
|
if (!metadataNodes.isEmpty()) {
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setMetadata(MetadataRDAMapper.toRDAList(metadataNodes));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> qaNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.data_quality_assurance");
|
|
|
|
if (!qaNodes.isEmpty()) {
|
2020-05-28 17:38:08 +02:00
|
|
|
/*rda.setDataQualityAssurance(qaNodes.stream().map(qaNode -> qaNode.get("value").asText()).collect(Collectors.toList()));
|
2020-03-26 17:39:25 +01:00
|
|
|
for (int i = 0; i < qaNodes.size(); i++) {
|
2020-03-27 09:50:37 +01:00
|
|
|
rda.setAdditionalProperty("qaId" + (i + 1), qaNodes.get(i).get("id").asText());
|
2020-05-28 17:38:08 +02:00
|
|
|
}*/
|
|
|
|
rda.setDataQualityAssurance(Collections.singletonList(qaNodes.get(0).get("value").asText()));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> preservationNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.preservation_statement");
|
|
|
|
if (!preservationNodes.isEmpty()) {
|
2020-03-24 10:26:17 +01:00
|
|
|
rda.setPreservationStatement(preservationNodes.get(0).get("value").asText());
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> distributionNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.distribution");
|
|
|
|
if (!distributionNodes.isEmpty()) {
|
2020-05-28 17:38:08 +02:00
|
|
|
rda.setDistribution(Collections.singletonList(DistributionRDAMapper.toRDA(distributionNodes)));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> keywordNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.keyword");
|
|
|
|
if (!keywordNodes.isEmpty()) {
|
2020-06-08 17:31:34 +02:00
|
|
|
rda.setKeyword(keywordNodes.stream().map(keywordNode -> {
|
|
|
|
JsonNode value = keywordNode.get("value");
|
|
|
|
if (value.isArray()) {
|
2020-06-09 15:25:50 +02:00
|
|
|
return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.asText())).collect(Collectors.toList());
|
2020-06-08 17:31:34 +02:00
|
|
|
} else {
|
2020-06-09 15:25:50 +02:00
|
|
|
return Collections.singletonList(KeywordRDAMapper.toRDA(keywordNode.get("value").asText()));
|
2020-06-08 17:31:34 +02:00
|
|
|
}
|
2020-06-09 15:25:50 +02:00
|
|
|
}).flatMap(Collection::stream).collect(Collectors.toList()));
|
2020-03-26 17:39:25 +01:00
|
|
|
for (int i = 0; i < keywordNodes.size(); i++) {
|
|
|
|
rda.setAdditionalProperty("keyword" + (i + 1), keywordNodes.get(i).get("id").asText());
|
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
} else {
|
|
|
|
List<String> tags = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(dataset.getId().toString()).getTags().stream().map(Tag::getName).collect(Collectors.toList());
|
|
|
|
rda.setKeyword(tags);
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> personalDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.personal_data");
|
|
|
|
if (!personalDataNodes.isEmpty()) {
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setPersonalData(personalDataNodes.stream().map(personalDataNode -> Dataset.PersonalData.fromValue(personalDataNode.get("value").asText())).findFirst().get());
|
2020-05-28 17:38:08 +02:00
|
|
|
} else {
|
|
|
|
rda.setPersonalData(Dataset.PersonalData.UNKNOWN);
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> securityAndPrivacyNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.security_and_privacy");
|
|
|
|
if (!securityAndPrivacyNodes.isEmpty()) {
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setSecurityAndPrivacy(SecurityAndPrivacyRDAMapper.toRDAList(securityAndPrivacyNodes));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> sensitiveDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.sensitive_data");
|
|
|
|
if (!sensitiveDataNodes.isEmpty()) {
|
2020-03-24 10:26:17 +01:00
|
|
|
rda.setSensitiveData(sensitiveDataNodes.stream().map(sensitiveDataNode -> Dataset.SensitiveData.fromValue(sensitiveDataNode.get("value").asText())).findFirst().get());
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
|
|
|
List<JsonNode> technicalResourceNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.technical_resource");
|
|
|
|
if (!technicalResourceNodes.isEmpty()) {
|
2020-03-23 17:09:31 +01:00
|
|
|
rda.setTechnicalResource(TechnicalResourceRDAMapper.toRDAList(technicalResourceNodes));
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|
2020-05-28 17:38:08 +02:00
|
|
|
List<JsonNode> issuedNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.issued");
|
|
|
|
if (!issuedNodes.isEmpty()) {
|
|
|
|
rda.setIssued(issuedNodes.get(0).get("value").asText());
|
|
|
|
}
|
2020-06-09 15:25:50 +02:00
|
|
|
List<JsonNode> contributorNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dmp.contributor");
|
|
|
|
if (!contributorNodes.isEmpty()) {
|
|
|
|
contributors.addAll(contributorNodes.stream().map(contributorNode -> {
|
|
|
|
JsonNode value = contributorNode.get("value");
|
|
|
|
if (value.isArray()) {
|
|
|
|
return StreamSupport.stream(value.spliterator(), false).map(node -> ContributorRDAMapper.toRDA(node.asText())).collect(Collectors.toList());
|
|
|
|
} else {
|
|
|
|
return Collections.singletonList(new Contributor());
|
|
|
|
}
|
|
|
|
}).flatMap(Collection::stream).collect(Collectors.toList()));
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
List<JsonNode> foundNodes = Stream.of(typeNodes, languageNodes, metadataNodes, qaNodes, preservationNodes, distributionNodes,
|
|
|
|
keywordNodes, personalDataNodes, securityAndPrivacyNodes, sensitiveDataNodes, technicalResourceNodes).flatMap(Collection::stream).collect(Collectors.toList());
|
|
|
|
templateIdsToValues.entrySet().forEach(entry -> {
|
|
|
|
boolean isFound = foundNodes.stream().anyMatch(node -> node.get("id").asText().equals(entry.getKey()));
|
|
|
|
if (!isFound && entry.getValue() != null && !entry.getValue().toString().isEmpty()) {
|
|
|
|
rda.setAdditionalProperty(entry.getKey(), entry.getValue());
|
|
|
|
}
|
|
|
|
});
|
2020-03-20 17:20:22 +01:00
|
|
|
|
|
|
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return rda;
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
|
|
|
|
|
2020-05-29 12:39:18 +02:00
|
|
|
public eu.eudat.data.entities.Dataset toEntity(Dataset rda, DatasetProfile defaultProfile) {
|
2020-03-26 17:39:25 +01:00
|
|
|
eu.eudat.data.entities.Dataset entity = new eu.eudat.data.entities.Dataset();
|
|
|
|
entity.setLabel(rda.getTitle());
|
|
|
|
entity.setDescription(rda.getDescription());
|
|
|
|
try {
|
|
|
|
DatasetProfile profile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(rda.getAdditionalProperties().get("template").toString()));
|
|
|
|
entity.setProfile(profile);
|
|
|
|
}catch(Exception e) {
|
|
|
|
logger.warn(e.getMessage(), e);
|
2020-05-29 12:39:18 +02:00
|
|
|
entity.setProfile(defaultProfile);
|
2020-03-26 17:39:25 +01:00
|
|
|
}
|
|
|
|
try {
|
|
|
|
Map<String, Object> properties = new HashMap<>();
|
|
|
|
DatasetWizardModel datasetWizardModel = new DatasetWizardModel();
|
|
|
|
datasetWizardModel.setProfile(entity.getProfile().getId());
|
|
|
|
datasetWizardModel.setDatasetProfileDefinition(datasetManager.getPagedProfile(datasetWizardModel, entity));
|
|
|
|
ObjectMapper mapper = new ObjectMapper();
|
|
|
|
String datasetDescriptionJson = mapper.writeValueAsString(datasetWizardModel.getDatasetProfileDefinition());
|
|
|
|
JsonNode datasetDescriptionObj = mapper.readTree(datasetDescriptionJson);
|
|
|
|
|
|
|
|
List<JsonNode> typeNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.type");
|
|
|
|
if (!typeNodes.isEmpty()) {
|
|
|
|
properties.put(typeNodes.get(0).get("id").asText(), rda.getType());
|
|
|
|
}
|
|
|
|
|
|
|
|
List<JsonNode> languageNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.language");
|
2020-05-29 13:42:21 +02:00
|
|
|
if (!languageNodes.isEmpty() && rda.getLanguage() != null) {
|
2020-03-26 17:39:25 +01:00
|
|
|
properties.put(languageNodes.get(0).get("id").asText(), rda.getLanguage().value());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rda.getMetadata() != null) {
|
|
|
|
properties.putAll(MetadataRDAMapper.toProperties(rda.getMetadata()));
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:38:08 +02:00
|
|
|
if (rda.getDatasetId() != null) {
|
|
|
|
properties.putAll(DatasetIdRDAMapper.toProperties(rda.getDatasetId(), datasetDescriptionObj));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*List <String> qaIds = rda.getAdditionalProperties().entrySet().stream().filter(entry -> entry.getKey().startsWith("qaId")).map(entry -> entry.getValue().toString()).collect(Collectors.toList());
|
2020-03-26 17:39:25 +01:00
|
|
|
for (int i = 0; i < qaIds.size(); i++) {
|
|
|
|
properties.put(qaIds.get(i), rda.getDataQualityAssurance().get(i));
|
2020-05-28 17:38:08 +02:00
|
|
|
}*/
|
|
|
|
List<JsonNode> qaNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.data_quality_assurance");
|
2020-05-29 12:39:18 +02:00
|
|
|
if (!qaNodes.isEmpty() && rda.getDataQualityAssurance() != null && !rda.getDataQualityAssurance().isEmpty()) {
|
2020-05-28 17:38:08 +02:00
|
|
|
properties.put(qaNodes.get(0).get("id").asText(), rda.getDataQualityAssurance().get(0));
|
2020-03-26 17:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
List<JsonNode> preservationNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.preservation_statement");
|
|
|
|
if (!preservationNodes.isEmpty()) {
|
|
|
|
properties.put(preservationNodes.get(0).get("id").asText(), rda.getPreservationStatement());
|
|
|
|
}
|
|
|
|
|
2020-05-28 17:38:08 +02:00
|
|
|
List<JsonNode> issuedNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.issued");
|
|
|
|
if (!issuedNodes.isEmpty()) {
|
|
|
|
properties.put(issuedNodes.get(0).get("id").asText(), rda.getIssued());
|
|
|
|
}
|
|
|
|
|
2020-03-26 17:39:25 +01:00
|
|
|
if (rda.getDistribution() != null) {
|
2020-05-28 17:38:08 +02:00
|
|
|
properties.putAll(DistributionRDAMapper.toProperties(rda.getDistribution().get(0), datasetDescriptionObj));
|
2020-03-26 17:39:25 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 12:30:47 +02:00
|
|
|
if (rda.getKeyword() != null) {
|
|
|
|
List<String> keywordIds = rda.getAdditionalProperties().entrySet().stream().filter(entry -> entry.getKey().startsWith("keyword")).map(entry -> entry.getValue().toString()).collect(Collectors.toList());
|
|
|
|
boolean takeAll = false;
|
|
|
|
if (keywordIds.size() < rda.getKeyword().size()) {
|
|
|
|
takeAll = true;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < keywordIds.size(); i++) {
|
|
|
|
if (takeAll) {
|
|
|
|
List<String> tags = new ArrayList<>();
|
|
|
|
for (String keyword : rda.getKeyword()) {
|
|
|
|
tags.add(mapper.writeValueAsString(toTagEntity(keyword)));
|
|
|
|
}
|
|
|
|
properties.put(keywordIds.get(i), tags);
|
|
|
|
} else {
|
|
|
|
properties.put(keywordIds.get(i), mapper.writeValueAsString(toTagEntity(rda.getKeyword().get(i))));
|
2020-06-09 15:25:50 +02:00
|
|
|
}
|
|
|
|
}
|
2020-03-26 17:39:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
List<JsonNode> personalDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.personal_data");
|
|
|
|
if (!personalDataNodes.isEmpty()) {
|
|
|
|
properties.put(personalDataNodes.get(0).get("id").asText(), rda.getPersonalData().value());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rda.getSecurityAndPrivacy() != null) {
|
|
|
|
properties.putAll(SecurityAndPrivacyRDAMapper.toProperties(rda.getSecurityAndPrivacy()));
|
|
|
|
}
|
|
|
|
|
|
|
|
List<JsonNode> sensitiveDataNodes = JsonSearcher.findNodes(datasetDescriptionObj, "rdaProperty", "dataset.sensitive_data");
|
|
|
|
if (!sensitiveDataNodes.isEmpty()) {
|
|
|
|
properties.put(sensitiveDataNodes.get(0).get("id").asText(), rda.getSensitiveData().value());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rda.getTechnicalResource() != null) {
|
|
|
|
properties.putAll(TechnicalResourceRDAMapper.toProperties(rda.getTechnicalResource()));
|
|
|
|
}
|
|
|
|
|
|
|
|
rda.getAdditionalProperties().entrySet().stream()
|
2020-03-27 09:50:37 +01:00
|
|
|
.filter(entry -> !entry.getKey().equals("template") && !entry.getKey().startsWith("qaId") && !entry.getKey().startsWith("keyword"))
|
2020-03-26 17:39:25 +01:00
|
|
|
.forEach(entry -> properties.put(entry.getKey(), entry.getValue()));
|
|
|
|
entity.setProperties(new ObjectMapper().writeValueAsString(properties));
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error(e.getMessage(), e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return entity;
|
|
|
|
}
|
2020-06-09 15:25:50 +02:00
|
|
|
|
|
|
|
private static Tag toTagEntity(String name) {
|
|
|
|
Tag tag = new Tag();
|
|
|
|
tag.setId("");
|
|
|
|
tag.setName(name);
|
|
|
|
return tag;
|
|
|
|
}
|
2020-03-20 17:20:22 +01:00
|
|
|
}
|