use new models

This commit is contained in:
Efstratios Giannopoulos 2024-03-08 18:35:10 +02:00
parent 97eea9f5f7
commit 7d2fb8437f
91 changed files with 2463 additions and 2811 deletions

View File

@ -18,15 +18,20 @@
<maven.compiler.target>21</maven.compiler.target>
<maven.compiler.release>21</maven.compiler.release>
<revision>1.0.0-SNAPSHOT</revision>
<transformer-base.version>0.0.4</transformer-base.version>
<transformer-base.version>0.0.5</transformer-base.version>
</properties>
<dependencies>
<dependency>
<groupId>gr.cite.opendmp</groupId>
<artifactId>file-transformer-base</artifactId>
<artifactId>file-transformer-base</artifactId>
<version>${transformer-base.version}</version>
</dependency>
<dependency>
<groupId>gr.cite.opendmp</groupId>
<artifactId>common-models</artifactId>
<version>0.0.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>

View File

@ -1,9 +0,0 @@
package eu.eudat.file.transformer.configuration;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(FileStorageProperties.class)
public class FileStorageConfiguration {
}

View File

@ -1,93 +0,0 @@
package eu.eudat.file.transformer.executor;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.misc.FileEnvelope;
import eu.eudat.file.transformer.models.misc.FileFormat;
import eu.eudat.file.transformer.rda.Dataset;
import eu.eudat.file.transformer.rda.Dmp;
import eu.eudat.file.transformer.rda.RDAModel;
import eu.eudat.file.transformer.rda.mapper.DatasetRDAMapper;
import eu.eudat.file.transformer.rda.mapper.DmpRDAMapper;
import eu.eudat.file.transformer.utils.service.storage.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.List;
@Service
public class RdaFileTransformer implements FileTransformerClient {
private final DmpRDAMapper dmpRDAMapper;
private final DatasetRDAMapper descriptionRDAMapper;
private final ObjectMapper mapper;
private final FileStorageService fileStorageService;
@Autowired
public RdaFileTransformer(DmpRDAMapper dmpRDAMapper, DatasetRDAMapper descriptionRDAMapper, FileStorageService fileStorageService) {
this.dmpRDAMapper = dmpRDAMapper;
this.descriptionRDAMapper = descriptionRDAMapper;
this.fileStorageService = fileStorageService;
mapper = new ObjectMapper();
mapper.registerModule(new JavaTimeModule());
mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
}
@Override
public FileEnvelope exportDmp(DmpFileTransformerModel dmpFileTransformerModel) throws InvalidApplicationException, IOException {
Dmp dmp = this.dmpRDAMapper.toRDA(dmpFileTransformerModel);
RDAModel rdaModel = new RDAModel();
rdaModel.setDmp(dmp);
String dmpJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(rdaModel);
FileEnvelope result = new FileEnvelope();
result.setFilename(dmpFileTransformerModel.getLabel() + ".json");
result.setFile(this.fileStorageService.storeFile(dmpJson.getBytes(StandardCharsets.UTF_8)));
return result;
}
@Override
public FileEnvelope exportDescription(DescriptionFileTransformerModel descriptionFileTransformerModel, String format) throws InvalidApplicationException, IOException {
Dataset dataset = this.descriptionRDAMapper.toRDA(descriptionFileTransformerModel, this.dmpRDAMapper.toRDA(descriptionFileTransformerModel.getDmp()));
String dmpJson = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(dataset);
FileEnvelope result = new FileEnvelope();
result.setFilename(descriptionFileTransformerModel.getLabel() + ".json");
result.setFile(this.fileStorageService.storeFile(dmpJson.getBytes(StandardCharsets.UTF_8)));
return result;
}
@Override
public DmpFileTransformerModel importDmp(FileEnvelope fileEnvelope) {
/*try { //TODO
String jsonString = String.valueOf(this.fileStorageService.readFile(fileEnvelope.getFile()));
RDAModel rda = mapper.readValue(jsonString, RDAModel.class);
DmpFileTransformerModel model = this.dmpRDAMapper.toEntity(rda.getDmp(), )
} catch (JsonProcessingException e) {
}*/
return null;
}
@Override
public DescriptionFileTransformerModel importDescription(FileEnvelope fileEnvelope) {
return null;
}
@Override
public FileTransformerConfiguration getConfiguration() {
List<FileFormat> supportedFormats = List.of(new FileFormat("json", false, null));
FileTransformerConfiguration configuration = new FileTransformerConfiguration();
configuration.setFileTransformerId("json");
configuration.setExportVariants(supportedFormats);
configuration.setImportVariants(supportedFormats);
return configuration;
}
}

View File

@ -0,0 +1,25 @@
package eu.eudat.file.transformer.model;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
public class FundingModel{
private ReferenceModel grant;
private ReferenceModel funder;
public ReferenceModel getGrant() {
return grant;
}
public void setGrant(ReferenceModel grant) {
this.grant = grant;
}
public ReferenceModel getFunder() {
return funder;
}
public void setFunder(ReferenceModel funder) {
this.funder = funder;
}
}

View File

@ -0,0 +1,33 @@
package eu.eudat.file.transformer.model;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
public class ProjectModel{
private ReferenceModel project;
private ReferenceModel grant;
private ReferenceModel funder;
public ReferenceModel getGrant() {
return grant;
}
public void setGrant(ReferenceModel grant) {
this.grant = grant;
}
public ReferenceModel getFunder() {
return funder;
}
public void setFunder(ReferenceModel funder) {
this.funder = funder;
}
public ReferenceModel getProject() {
return project;
}
public void setProject(ReferenceModel project) {
this.project = project;
}
}

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,4 +1,4 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -1,5 +1,5 @@
package eu.eudat.file.transformer.rda;
package eu.eudat.file.transformer.model.rda;
import com.fasterxml.jackson.annotation.*;

View File

@ -0,0 +1,22 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.ContactId;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
public class ContactIdRDAMapper {
public ContactId toRDA(UUID id) {
if (id == null) return null;
ContactId rda = new ContactId();
rda.setIdentifier(id.toString());
rda.setType(ContactId.Type.OTHER);
return rda;
}
public UUID toModel(ContactId rda) {
return rda == null ? null : UUID.fromString(rda.getIdentifier());
}
}

View File

@ -0,0 +1,48 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.enums.ContactInfoType;
import eu.eudat.commonmodels.models.UserContactInfoModel;
import eu.eudat.commonmodels.models.UserModel;
import eu.eudat.file.transformer.model.rda.Contact;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class ContactRDAMapper{
private final ContactIdRDAMapper contactIdRDAMapper;
public ContactRDAMapper(ContactIdRDAMapper contactIdRDAMapper) {
this.contactIdRDAMapper = contactIdRDAMapper;
}
public Contact toRDA(UserModel model) {
if (model == null) return null;
if (model.getName() == null) throw new IllegalArgumentException("Contact Name is missing");
UserContactInfoModel emailContact = model.getContacts() != null ? model.getContacts().stream().filter(userContactInfo -> userContactInfo.getType().equals(ContactInfoType.Email)).findFirst().orElse(null) : null;
if (emailContact == null) throw new IllegalArgumentException("Contact Email is missing");
Contact rda = new Contact();
rda.setName(model.getName());
rda.setMbox(emailContact.getValue());
rda.setContactId(contactIdRDAMapper.toRDA(emailContact.getId()));
return rda;
}
public UserModel toEntity(Contact rda) {
if (rda == null) return null;
UserModel entity = new UserModel();
entity.setName(rda.getName());
UserContactInfoModel emailContactInfo = new UserContactInfoModel();
emailContactInfo.setId(contactIdRDAMapper.toModel(rda.getContactId()));
emailContactInfo.setType(ContactInfoType.Email);
emailContactInfo.setValue(rda.getMbox());
entity.setContacts(List.of(emailContactInfo));
return entity;
}
}

View File

@ -0,0 +1,44 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.ContributorId;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class ContributorIdRDAMapper{
public ContributorId toRDA(String id) {
if (id == null || id.isBlank()) return null;
ContributorId rda = new ContributorId();
String[] idParts = id.split(":");
String prefix = idParts.length > 1 ? idParts[0] : id;
if (prefix.equals("orcid")) {
String finalId = id.replace(prefix + ":", "");
rda.setIdentifier("http://orcid.org/" + finalId);
rda.setType(ContributorId.Type.ORCID);
} else {
rda.setIdentifier(id);
rda.setType(ContributorId.Type.OTHER);
}
return rda;
}
public String toEntity(ContributorId rda) {
if (rda.getIdentifier() != null) return null;
String referenceString;
if (rda.getType() == ContributorId.Type.ORCID) {
String id = rda.getIdentifier().replace("http://orcid.org/", "");
referenceString = "orcid:" + id;
} else {
String[] idParts = rda.getIdentifier().split(":");
if (idParts.length == 1) {
referenceString = "dmp:" + rda.getIdentifier();
} else {
referenceString = rda.getIdentifier();
}
}
return referenceString;
}
}

View File

@ -0,0 +1,98 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.Cost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Component
public class CostRDAMapper{
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
public List<Cost> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields ) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, Cost> rdaMap = new HashMap<>();
for(FieldModel node: nodes){
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.cost")).findFirst().orElse("");
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
String key = node.getNumbering();
if(!key.contains("mult")){
key = "0";
}
else{
key = "" + key.charAt(4);
}
Cost rda;
if(rdaMap.containsKey(key)){
rda = rdaMap.get(key);
}
else{
rda = new Cost();
rdaMap.put(key, rda);
}
if(rdaProperty.contains("value")){
try {
rda.setValue(Double.valueOf(rdaValue.getTextValue()));
}
catch (NumberFormatException e) {
logger.warn("Dmp cost value " + rdaValue + " is not valid. Cost value will not be set.");
}
}
else if(rdaProperty.contains("currency_code")){
try {
// HashMap<String, String> result =
// new ObjectMapper().readValue(rdaValue, HashMap.class);
// rda.setCurrencyCode(Cost.CurrencyCode.fromValue(result.get("value")));
rda.setCurrencyCode(Cost.CurrencyCode.fromValue(rdaValue.getTextValue())); //TODO
}
catch (Exception e) {
logger.warn("Dmp cost currency code is not valid and will not be set.");
}
}
else if(rdaProperty.contains("title")){
rda.setTitle(rdaValue.getTextValue());
// Iterator<JsonNode> iter = mapper.readTree(rdaValue).elements(); //TODO
// StringBuilder title = new StringBuilder();
// while(iter.hasNext()){
// String next = iter.next().asText();
// if(!next.equals("Other")) {
// title.append(next).append(", ");
// }
// }
// if(title.length() > 2){
// rda.setTitle(title.substring(0, title.length() - 2));
// }
// else{
// String t = rda.getTitle();
// if(t == null){ // only other as title
// rda.setTitle(rdaValue.getTextValue());
// }
// else{ // option + other
// rda.setTitle(t + ", " + rdaValue);
// }
// }
}
else if(rdaProperty.contains("description")){
rda.setDescription(rdaValue.getTextValue());
}
}
List<Cost> rdaList = rdaMap.values().stream()
.filter(cost -> cost.getTitle() != null)
.collect(Collectors.toList());
return rdaList;
}
}

View File

@ -0,0 +1,92 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.DatasetId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
@Component
public class DatasetIdRDAMapper{
private static final Logger logger = LoggerFactory.getLogger(DatasetIdRDAMapper.class);
public DatasetId toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
DatasetId data = new DatasetId();
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.dataset_id")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
finalRDAMap(data, rdaProperty, rdaValue.getTextValue());
// try { //TODO
// Map<String, Object> values = mapper.readValue(rdaValue, HashMap.class); //TODO
// if (!values.isEmpty()) {
// values.entrySet().forEach(entry -> finalRDAMap(data, entry.getKey(), (String) entry.getValue()));
// } else {
// finalRDAMap(data, rdaProperty, rdaValue.getTextValue());
// }
// } catch (IOException e) {
// logger.warn(e.getMessage() + ".Passing value as is");
// finalRDAMap(data, rdaProperty, rdaValue.getTextValue());
//
// }
}
if (data.getIdentifier() != null && data.getType() != null) {
return data;
}
return null;
}
private static void finalRDAMap(DatasetId rda, String property, String value) {
if (value != null) {
for (DatasetIdProperties datasetIdProperties : DatasetIdProperties.values()) {
if (property.contains(datasetIdProperties.getName())) {
switch (datasetIdProperties) {
case IDENTIFIER:
rda.setIdentifier(value);
break;
case TYPE:
try {
rda.setType(DatasetId.Type.fromValue(value));
}
catch (IllegalArgumentException e){
logger.warn("Type " + value + " from semantic rda.dataset.dataset_id.type was not found. Setting type to OTHER.");
rda.setType(DatasetId.Type.OTHER);
}
break;
}
}
}
}
}
private enum DatasetIdProperties {
IDENTIFIER("identifier"),
TYPE("type");
private final String name;
DatasetIdProperties(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -0,0 +1,346 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.description.PropertyDefinitionFieldSetItemModel;
import eu.eudat.commonmodels.models.description.PropertyDefinitionFieldSetModel;
import eu.eudat.commonmodels.models.description.PropertyDefinitionModel;
import eu.eudat.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.Dataset;
import eu.eudat.file.transformer.model.rda.DatasetId;
import eu.eudat.file.transformer.model.rda.Dmp;
import eu.eudat.file.transformer.model.rda.Language;
import eu.eudat.file.transformer.service.descriptiontemplatesearcher.TemplateFieldSearcherService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class DatasetRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
private final TemplateFieldSearcherService templateFieldSearcherService;
private final LanguageRDAMapper languageRDAMapper;
private final DatasetIdRDAMapper datasetIdRDAMapper;
private final MetadataRDAMapper metadataRDAMapper;
private final DistributionRDAMapper distributionRDAMapper;
private final TechnicalResourceRDAMapper technicalResourceRDAMapper;
private final CostRDAMapper costRDAMapper;
private final SecurityAndPrivacyRDAMapper securityAndPrivacyRDAMapper;
private final KeywordRDAMapper keywordRDAMapper;
@Autowired
public DatasetRDAMapper(TemplateFieldSearcherService templateFieldSearcherService, LanguageRDAMapper languageRDAMapper, DatasetIdRDAMapper datasetIdRDAMapper, MetadataRDAMapper metadataRDAMapper, DistributionRDAMapper distributionRDAMapper, TechnicalResourceRDAMapper technicalResourceRDAMapper, CostRDAMapper costRDAMapper, SecurityAndPrivacyRDAMapper securityAndPrivacyRDAMapper, KeywordRDAMapper keywordRDAMapper) {
this.templateFieldSearcherService = templateFieldSearcherService;
this.languageRDAMapper = languageRDAMapper;
this.datasetIdRDAMapper = datasetIdRDAMapper;
this.metadataRDAMapper = metadataRDAMapper;
this.distributionRDAMapper = distributionRDAMapper;
this.technicalResourceRDAMapper = technicalResourceRDAMapper;
this.costRDAMapper = costRDAMapper;
this.securityAndPrivacyRDAMapper = securityAndPrivacyRDAMapper;
this.keywordRDAMapper = keywordRDAMapper;
}
private List<eu.eudat.commonmodels.models.description.FieldModel> findValueField(FieldModel fieldModel, PropertyDefinitionModel descriptionTemplateModel){
List<eu.eudat.commonmodels.models.description.FieldModel> items = new ArrayList<>();
if (descriptionTemplateModel == null || descriptionTemplateModel.getFieldSets() == null) return items;
for (PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel : descriptionTemplateModel.getFieldSets().values()){
if (propertyDefinitionFieldSetModel.getItems() == null) continue;
for (PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel : propertyDefinitionFieldSetModel.getItems()){
if (propertyDefinitionFieldSetItemModel.getFields() == null) continue;
eu.eudat.commonmodels.models.description.FieldModel valueField = propertyDefinitionFieldSetItemModel.getFields().getOrDefault(fieldModel.getId(), null);
if (valueField != null) items.add(valueField);
}
}
return items;
}
private List<eu.eudat.commonmodels.models.description.FieldModel> getAllValueFields(PropertyDefinitionModel descriptionTemplateModel){
List<eu.eudat.commonmodels.models.description.FieldModel> items = new ArrayList<>();
if (descriptionTemplateModel == null || descriptionTemplateModel.getFieldSets() == null) return items;
for (PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel : descriptionTemplateModel.getFieldSets().values()){
if (propertyDefinitionFieldSetModel.getItems() == null) continue;
for (PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel : propertyDefinitionFieldSetModel.getItems()){
if (propertyDefinitionFieldSetItemModel.getFields() == null) continue;
items.addAll(propertyDefinitionFieldSetItemModel.getFields().values());
}
}
return items;
}
public Dataset toRDA(DescriptionModel descriptionEntity, Map<String, Object> extraData) {
if (descriptionEntity == null) return null;
if (descriptionEntity.getLabel() == null) throw new IllegalArgumentException("Dataset Label is missing");
if (extraData == null) throw new IllegalArgumentException("extraData is missing");
Object dmpObject = extraData.getOrDefault("dmp", null);
if (dmpObject == null) throw new IllegalArgumentException("Dmp is missing");
Dmp dmp = (Dmp)dmpObject;
Dataset rda = new Dataset();
rda.setTitle(descriptionEntity.getLabel());
rda.setDescription(descriptionEntity.getDescription());
rda.setAdditionalProperty("template", descriptionEntity.getDescriptionTemplate().getId());
rda.setAdditionalProperty("dmpSectionIndex", descriptionEntity.getSectionId());
//Map<String, Object> templateIdsToValues = this.createFieldIdValueMap(descriptionEntity.getDescriptionTemplate());
//rda.setAdditionalProperty("template", descriptionEntity.getDescriptionTemplate()); //TODO
try {
List<FieldModel> idNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.dataset_id");
if (!idNodes.isEmpty()) {
rda.setDatasetId(datasetIdRDAMapper.toRDA(idNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}
if (rda.getDatasetId() == null) {
rda.setDatasetId(new DatasetId(descriptionEntity.getId().toString(), DatasetId.Type.OTHER));
}
List<FieldModel> typeNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.type");
for (FieldModel typeNode : typeNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(typeNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) rda.setType(fieldValues.stream().filter(x-> x.getTextValue() != null && x.getTextValue().isBlank()).map(eu.eudat.commonmodels.models.description.FieldModel::getTextValue).findFirst().orElse(null));
if (rda.getType() != null && !rda.getType().isBlank()) break;;
}
if (rda.getType() == null || rda.getType().isBlank()) rda.setType("DMP Dataset");
List<FieldModel> languageNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.language");
for (FieldModel languageNode : languageNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(languageNode, descriptionEntity.getProperties());
try {
if (!fieldValues.isEmpty()) rda.setLanguage(fieldValues.stream().filter(x -> x.getTextValue() != null && x.getTextValue().isBlank()).map(x -> Language.fromValue(x.getTextValue())).findFirst().orElse(null));
}
catch (IllegalArgumentException e){
logger.warn("Language from semantic rda.dataset.language was not found.");
}
if (rda.getLanguage() != null) break;;
}
if (rda.getLanguage() == null) rda.setLanguage(languageRDAMapper.toRDA(descriptionEntity.getDescriptionTemplate().getLanguage()));
List<FieldModel> metadataNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.metadata");
if (!metadataNodes.isEmpty()) {
Map<String, Object> valueFieldsMap = new HashMap<>();
valueFieldsMap.put("valueFields", this.getAllValueFields(descriptionEntity.getProperties()));
rda.setMetadata(metadataRDAMapper.toRDA(metadataNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}else{
rda.setMetadata(new ArrayList<>());
}
//TODO
// List<FieldModel> qaNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.data_quality_assurance");
// if (!qaNodes.isEmpty()) {
// rda.setDataQualityAssurance(qaNodes.stream().filter(qaNode -> qaNode.getData() != null).map(qaNode -> qaNode.getData().getValue()).collect(Collectors.toList()));
// for (int i = 0; i < qaNodes.size(); i++) {
// rda.setAdditionalProperty("qaId" + (i + 1), qaNodes.get(i).getId());
// }
// List<String> qaList = new ArrayList<>();
// String qa;
// for(FieldModel node: qaNodes){
// if (node.getData() == null) {
// continue;
// }
// JsonNode valueNode = mapper.readTree(node.getData().getValue());
// if(valueNode.isArray()){
// Iterator<JsonNode> iter = valueNode.elements();
// while(iter.hasNext()) {
// qa = iter.next().asText();
// qaList.add(qa);
// }
// }
// }
// String data_quality;
// for(FieldModel dqa: qaNodes){
// if (dqa.getData() == null) {
// continue;
// }
// data_quality = dqa.getData().getValue();
// if(!data_quality.isEmpty()){
// qaList.add(data_quality);
// rda.setAdditionalProperty("otherDQAID", dqa.getId());
// rda.setAdditionalProperty("otherDQA", data_quality);
// break;
// }
// }
// rda.setDataQualityAssurance(qaList);
// }else{
// rda.setDataQualityAssurance(new ArrayList<>());
// }
List<FieldModel> preservationNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.preservation_statement");
for (FieldModel preservationNode : preservationNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(preservationNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) rda.setPreservationStatement(fieldValues.stream().filter(x-> x.getTextValue() != null && x.getTextValue().isBlank()).map(eu.eudat.commonmodels.models.description.FieldModel::getTextValue).findFirst().orElse(null));
if (rda.getPreservationStatement() != null && !rda.getPreservationStatement().isBlank()) break;;
}
List<FieldModel> distributionNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.distribution");
if (!distributionNodes.isEmpty()) {
rda.setDistribution(distributionRDAMapper.toRDA(distributionNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}else{
rda.setDistribution(new ArrayList<>());
}
List<FieldModel> keywordNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.keyword");
for (FieldModel keywordNode : keywordNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(keywordNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) {
rda.setKeyword(fieldValues.stream().filter(x -> (x.getTextValue() != null && x.getTextValue().isBlank()) || (x.getTextListValue() != null && !x.getTextListValue().isEmpty())).map(x -> {
if (x.getTextListValue() != null && !x.getTextListValue().isEmpty()) {
return x.getTextListValue().stream().map(node -> keywordRDAMapper.toRDA(node)).collect(Collectors.toList());
} else {
return List.of(keywordRDAMapper.toRDA(x.getTextValue()));
}
}).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList()));
}
}
if (rda.getKeyword() != null){
int i = 0 ;
for (String keyword : rda.getKeyword()) {
rda.setAdditionalProperty("keyword" + (i + 1), keyword);
i++;
}
}
// else if (apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().exists()) { //TODO
// List<String> tags = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(descriptionEntity.getId().toString()).getTags().stream().map(Tag::getName).collect(Collectors.toList());
// rda.setKeyword(tags);
// }
List<FieldModel> personalDataNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.sensitive_data");
for (FieldModel personalDataNode : personalDataNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(personalDataNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) rda.setPersonalData(fieldValues.stream().filter(x-> x.getTextValue() != null && x.getTextValue().isBlank()).map(x-> Dataset.PersonalData.fromValue(x.getTextValue())).findFirst().orElse(null));
if (rda.getPersonalData() != null) break;
}
if (rda.getPersonalData() != null) rda.setPersonalData(Dataset.PersonalData.UNKNOWN);
List<FieldModel> securityAndPrivacyNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.security_and_privacy");
if (!securityAndPrivacyNodes.isEmpty()) {
rda.setSecurityAndPrivacy(securityAndPrivacyRDAMapper.toRDA(securityAndPrivacyNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}else{
rda.setSecurityAndPrivacy(new ArrayList<>());
}
List<FieldModel> sensitiveDataNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.sensitive_data");
for (FieldModel sensitiveDataNode : sensitiveDataNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(sensitiveDataNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) rda.setSensitiveData(fieldValues.stream().filter(x-> x.getTextValue() != null && x.getTextValue().isBlank()).map(x-> Dataset.SensitiveData.fromValue(x.getTextValue())).findFirst().orElse(null));
if (rda.getSensitiveData() != null) break;
}
if (rda.getSensitiveData() != null) rda.setSensitiveData(Dataset.SensitiveData.UNKNOWN);
List<FieldModel> technicalResourceNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.technical_resource");
if (!technicalResourceNodes.isEmpty()) {
rda.setTechnicalResource(technicalResourceRDAMapper.toRDA(technicalResourceNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}else{
rda.setTechnicalResource(new ArrayList<>());
}
List<FieldModel> issuedNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dataset.issued");
for (FieldModel issuedNode : issuedNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(issuedNode, descriptionEntity.getProperties());
if (!fieldValues.isEmpty()) rda.setIssued(fieldValues.stream().filter(x-> x.getTextValue() != null && x.getTextValue().isBlank()).map(eu.eudat.commonmodels.models.description.FieldModel::getTextValue).findFirst().orElse(null));
if (rda.getIssued() != null && !rda.getIssued().isBlank()) break;;
}
//TODO
// List<FieldModel> contributorNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dmp.contributor");
// if (!contributorNodes.isEmpty()) {
// dmp.getContributor().addAll(contributorNodes.stream().filter(contributorNode -> contributorNode.getData() != null).map(contributorNode -> {
// try {
// JsonNode value = mapper.readTree(contributorNode.getData().getValue());
// if (value.isArray()) {
// return StreamSupport.stream(value.spliterator(), false).map(node -> DmpUserContributorRDAMapper.toRDA(node.asText())).collect(Collectors.toList());
// } else {
// return Collections.singletonList(new Contributor());
// }
// }catch (JsonProcessingException e) {
// return null;
// }
// }).filter(Objects::nonNull).flatMap(Collection::stream).toList());
// dmp.setContributor(dmp.getContributor().stream().filter(contributor -> contributor.getContributorId() != null && contributor.getName() != null).collect(Collectors.toList()));
// }
List<FieldModel> costNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dmp.cost");
if (!costNodes.isEmpty()) {
dmp.getCost().addAll(costRDAMapper.toRDA(costNodes, this.getAllValueFields(descriptionEntity.getProperties())));
}
List<FieldModel> ethicsNodes = this.templateFieldSearcherService.searchFieldsBySemantics(descriptionEntity.getDescriptionTemplate(), "rda.dmp.ethical_issues");
if (!ethicsNodes.isEmpty()) {
for(FieldModel node: ethicsNodes){
List<eu.eudat.commonmodels.models.description.FieldModel> fieldValues = this.findValueField(node, descriptionEntity.getProperties());
eu.eudat.commonmodels.models.description.FieldModel fieldValue = fieldValues.getFirst();
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.ethical_issues")).findFirst().orElse("");
if (fieldValue == null) {
continue;
}
String rdaValue = fieldValue.getTextValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
if(rdaProperty.contains("exist")){
try {
Dmp.EthicalIssuesExist exists = dmp.getEthicalIssuesExist();
if(exists == null
|| ((exists == Dmp.EthicalIssuesExist.NO || exists == Dmp.EthicalIssuesExist.UNKNOWN) && rdaValue.equals("yes"))
|| (exists == Dmp.EthicalIssuesExist.YES && !(rdaValue.equals("no") || rdaValue.equals("unknown")))
|| (exists == Dmp.EthicalIssuesExist.UNKNOWN && rdaValue.equals("no"))){
dmp.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(rdaValue));
}
}catch(IllegalArgumentException e){
logger.warn(e.getLocalizedMessage() + ". Setting ethical_issues_exist to unknown");
dmp.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
}
}
else if(rdaProperty.contains("description")){
if(dmp.getEthicalIssuesDescription() == null){
dmp.setEthicalIssuesDescription(rdaValue);
}
else{
dmp.setEthicalIssuesDescription(dmp.getEthicalIssuesDescription() + ", " + rdaValue);
}
}
else if(rdaProperty.contains("report")){
try {
dmp.setEthicalIssuesReport(URI.create(rdaValue));
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
}
}
}
//TODO
// List<FieldModel> foundNodes = Stream.of(typeNodes, languageNodes, metadataNodes, qaNodes , preservationNodes, distributionNodes,
// keywordNodes, personalDataNodes, securityAndPrivacyNodes, sensitiveDataNodes, technicalResourceNodes).flatMap(Collection::stream).toList();
// templateIdsToValues.entrySet().forEach(entry -> {
// boolean isFound = foundNodes.stream().anyMatch(node -> node.getId().equals(entry.getKey()));
// if (!isFound && entry.getValue() != null && !entry.getValue().toString().isEmpty()) {
// try {
// Instant time = Instant.parse(entry.getValue().toString());
// rda.setAdditionalProperty(entry.getKey(), DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault()).format(time));
// } catch (DateTimeParseException e) {
// rda.setAdditionalProperty(entry.getKey(), entry.getValue());
// }
// }
// });
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return rda;
}
public DescriptionModel toEntity(Dataset rda, DescriptionTemplateModel defaultProfile) {
throw new UnsupportedOperationException();
}
}

View File

@ -0,0 +1,210 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.Distribution;
import eu.eudat.file.transformer.model.rda.License;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class DistributionRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DistributionRDAMapper.class);
private final LicenseRDAMapper licenseRDAMapper;
private final HostRDAMapper hostRDAMapper;
public DistributionRDAMapper(LicenseRDAMapper licenseRDAMapper, HostRDAMapper hostRDAMapper) {
this.licenseRDAMapper = licenseRDAMapper;
this.hostRDAMapper = hostRDAMapper;
}
public List<Distribution> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, Distribution> rdaMap = new HashMap<>();
for (FieldModel node: nodes) {
String rdaProperty = getRdaDistributionProperty(node);
if(rdaProperty.isEmpty() || node.getData() == null){
continue;
}
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || (rdaValue.getTextValue() == null && rdaValue.getReferences() == null)){
continue;
}
String key = node.getNumbering();
if(!key.contains("mult")){
key = "0";
}
else{
key = "" + key.charAt(4);
}
Distribution rda;
if(rdaMap.containsKey(key)){
rda = rdaMap.get(key);
}
else {
rda = new Distribution();
rdaMap.put(key, rda);
}
for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
if (rdaProperty.contains(exportPropertyName.getName())) {
switch (exportPropertyName) {
case ACCESS_URL:
rda.setAccessUrl(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.ACCESS_URL.getName(), node.getId());
break;
case AVAILABLE_UNTIL:
rda.setAvailableUntil(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.AVAILABLE_UNTIL.getName(), node.getId());
break;
case DOWNLOAD_URL:
rda.setDownloadUrl(URI.create(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.DOWNLOAD_URL.getName(), node.getId());
break;
case DESCRIPTION:
if(!rdaProperty.contains("host")) {
rda.setDescription(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
}
break;
case DATA_ACCESS:
try {
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.DATA_ACCESS.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution data access " + rdaValue + " from semantic distribution.data_access is not valid. Data access will not be set set.");
}
break;
case BYTE_SIZE:
rda.setByteSize(Integer.parseInt(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.BYTE_SIZE.getName(), node.getId());
break;
case LICENSE:
List<FieldModel> licenseNodes = nodes.stream().filter(lnode -> {
for(String schematic: lnode.getSchematics()){
if(schematic.startsWith("rda.dataset.distribution.license")){
return true;
}
}
return false;
}).collect(Collectors.toList());
License license = licenseRDAMapper.toRDA(licenseNodes, valueFields);
rda.setLicense(license != null? Collections.singletonList(license): new ArrayList<>());
break;
case FORMAT:
//TODO
// try {
// JsonNode valueNode = mapper.readTree(node.getData().getValue());
// if(valueNode.isArray()){
// Iterator<JsonNode> iter = valueNode.elements();
// List<String> formats = new ArrayList<>();
// int i = 1;
// while(iter.hasNext()) {
// JsonNode current = iter.next();
// String format = current.toString();
//
// Map<String, String> result = mapper.readValue(format, HashMap.class);
// format = result.get("label");
// formats.add(format);
// rda.setAdditionalProperty("format" + i++, mapper.readTree(current.toString()));
//
// }
// rda.setFormat(formats);
// }
// else{
// if(rda.getFormat() == null || rda.getFormat().isEmpty()){
// rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(","))));
// }
// else{
// rda.getFormat().addAll(Arrays.asList(rdaValue.replace(" ", "").split(",")));
// }
// }
// rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.getId());
// }
// catch(JsonProcessingException e){
// logger.warn(e.getMessage());
// }
break;
case TITLE:
if(!rdaProperty.contains("host")) {
rda.setTitle(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
}
break;
case HOST:
rda.setHost(hostRDAMapper.toRDA(nodes, valueFields, node.getNumbering()));
break;
}
}
}
}
return rdaMap.values().stream()
.filter(distro -> distro.getTitle() != null).collect(Collectors.toList());
}
private static String getRdaDistributionProperty(FieldModel node) {
return node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution")).findFirst().orElse("");
}
private enum ExportPropertyName {
ACCESS_URL("access_url"),
AVAILABLE_UNTIL("available_until"),
BYTE_SIZE("byte_size"),
DATA_ACCESS("data_access"),
DESCRIPTION("description"),
DOWNLOAD_URL("download_url"),
FORMAT("format"),
HOST("host"),
LICENSE("license"),
TITLE("title");
private final String name;
ExportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private enum ImportPropertyName {
ACCESS_URL("accessurlId"),
AVAILABLE_UNTIL("availableUtilId"),
BYTE_SIZE("byteSizeId"),
DATA_ACCESS("dataAccessId"),
DESCRIPTION("descriptionId"),
DOWNLOAD_URL("downloadUrlId"),
FORMAT("formatId"),
/*HOST("host"),
LICENSE("license"),*/
TITLE("titleId");
private final String name;
ImportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ImportPropertyName fromString(String name) throws Exception {
for (ImportPropertyName importPropertyName: ImportPropertyName.values()) {
if (importPropertyName.getName().equals(name)) {
return importPropertyName;
}
}
throw new Exception("No name available");
}
}
}

View File

@ -0,0 +1,40 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.dmp.DmpContactModel;
import eu.eudat.file.transformer.model.rda.Contact;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class DmpContactModelContactRDAMapper {
private final ContactRDAMapper contactRDAMapper;
public DmpContactModelContactRDAMapper(ContactRDAMapper contactRDAMapper) {
this.contactRDAMapper = contactRDAMapper;
}
public Contact toRDA(DmpContactModel model) {
if (model == null) return null;
Contact rda = new Contact();
if (model.getUser() != null){
rda = this.contactRDAMapper.toRDA(model.getUser());
} else {
if (model.getLastName() == null) throw new IllegalArgumentException("Last Name is missing");
if (model.getEmail() == null) throw new IllegalArgumentException("Email is missing");
rda.setName(model.getLastName() + " " + model.getFirstName());
rda.setMbox(model.getEmail());
}
return rda;
}
public DmpContactModel toEntity(Contact rda) {
if (rda == null) return null;
DmpContactModel entity = new DmpContactModel();
entity.setUser(this.contactRDAMapper.toEntity(rda));
return entity;
}
}

View File

@ -0,0 +1,25 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.DmpId;
import org.springframework.stereotype.Component;
import java.util.UUID;
@Component
public class DmpIdRDAMapper{
public DmpId toRDA(Object id) {
if (id == null) return null;
DmpId rda = new DmpId();
rda.setIdentifier(id.toString());
if (id instanceof UUID) {
rda.setType(DmpId.Type.OTHER);
} else {
rda.setType(DmpId.Type.DOI);
}
return rda;
}
public Object toEntity(DmpId rda) {
return rda == null ? null : rda.getIdentifier();
}
}

View File

@ -0,0 +1,231 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.enums.DmpAccessType;
import eu.eudat.commonmodels.enums.DmpUserRole;
import eu.eudat.commonmodels.models.DmpUserModel;
import eu.eudat.commonmodels.models.EntityDoiModel;
import eu.eudat.commonmodels.models.UserModel;
import eu.eudat.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.commonmodels.models.dmp.DmpContactModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.commonmodels.models.dmp.DmpPropertiesModel;
import eu.eudat.commonmodels.models.dmpblueprint.DmpBlueprintModel;
import eu.eudat.commonmodels.models.dmpreference.DmpReferenceModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.file.transformer.model.ProjectModel;
import eu.eudat.file.transformer.model.rda.DmpId;
import eu.eudat.file.transformer.model.rda.Dmp;
import eu.eudat.file.transformer.service.descriptiontemplatesearcher.TemplateFieldSearcherService;
import eu.eudat.file.transformer.service.json.JsonHandlingService;
import eu.eudat.file.transformer.service.rdafiletransformer.RdaFileTransformerServiceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.*;
@Component
public class DmpRDAMapper{
private final DatasetRDAMapper datasetRDAMapper;
private final DmpIdRDAMapper dmpIdRDAMapper;
private final ContactRDAMapper contactRDAMapper;
private final RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties;
private final DmpContactModelContactRDAMapper dmpContactModelContactRDAMapper;
private final ReferenceContributorRDAMapper referenceContributorRDAMapper;
private final DmpUserContributorRDAMapper dmpUserContributorRDAMapper;
private final ProjectRDAMapper projectRDAMapper;
private final JsonHandlingService jsonHandlingService;
private final LanguageRDAMapper languageRDAMapper;
@Autowired
public DmpRDAMapper(DatasetRDAMapper datasetRDAMapper, DmpIdRDAMapper dmpIdRDAMapper, ContactRDAMapper contactRDAMapper, RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties, DmpContactModelContactRDAMapper dmpContactModelContactRDAMapper, ReferenceContributorRDAMapper referenceContributorRDAMapper, DmpUserContributorRDAMapper dmpUserContributorRDAMapper, ProjectRDAMapper projectRDAMapper, JsonHandlingService jsonHandlingService, LanguageRDAMapper languageRDAMapper) {
this.datasetRDAMapper = datasetRDAMapper;
this.dmpIdRDAMapper = dmpIdRDAMapper;
this.contactRDAMapper = contactRDAMapper;
this.rdaFileTransformerServiceProperties = rdaFileTransformerServiceProperties;
this.dmpContactModelContactRDAMapper = dmpContactModelContactRDAMapper;
this.referenceContributorRDAMapper = referenceContributorRDAMapper;
this.dmpUserContributorRDAMapper = dmpUserContributorRDAMapper;
this.projectRDAMapper = projectRDAMapper;
this.jsonHandlingService = jsonHandlingService;
this.languageRDAMapper = languageRDAMapper;
}
public Dmp toRDA(DmpModel dmp) {
if (dmp == null) throw new IllegalArgumentException("DMP is missing");
if (dmp.getCreatedAt() == null) throw new IllegalArgumentException("DMP Created is missing");
if (dmp.getUpdatedAt() == null) throw new IllegalArgumentException("DMP Modified is missing");
if (dmp.getLabel() == null) throw new IllegalArgumentException("DMP Label is missing");
if (dmp.getProperties() == null) throw new IllegalArgumentException("DMP is missing language and contact properties");
if (dmp.getDescriptions() == null || dmp.getDescriptions().isEmpty()) throw new IllegalArgumentException("DMP has no Datasets");
List<ReferenceModel> grants = this.getReferenceModelOfTypeCode(dmp, this.rdaFileTransformerServiceProperties.getGrantReferenceCode());
List<ReferenceModel> researchers = this.getReferenceModelOfTypeCode(dmp, this.rdaFileTransformerServiceProperties.getResearcherReferenceCode());
List<ReferenceModel> funders = this.getReferenceModelOfTypeCode(dmp, this.rdaFileTransformerServiceProperties.getFunderReferenceCode());
List<ReferenceModel> projects = this.getReferenceModelOfTypeCode(dmp, this.rdaFileTransformerServiceProperties.getProjectReferenceCode());
Dmp rda = new Dmp();
if (dmp.getEntityDois() != null && !dmp.getEntityDois().isEmpty()) {
for(EntityDoiModel doi: dmp.getEntityDois()){
if(doi.getRepositoryId() != null && doi.getRepositoryId().equals("Zenodo")){
rda.setDmpId(dmpIdRDAMapper.toRDA(doi.getDoi()));
}
}
} else {
rda.setDmpId(dmpIdRDAMapper.toRDA(dmp.getId()));
}
rda.setCreated(dmp.getCreatedAt());
rda.setDescription(dmp.getDescription());
rda.setModified(dmp.getUpdatedAt());
rda.setTitle(dmp.getLabel());
rda.setLanguage(languageRDAMapper.toRDA(dmp.getLanguage() != null ? dmp.getLanguage() : "en"));
if (dmp.getProperties() != null) {
//TODO
// if (extraProperties.get("ethicalIssues") != null) {
// rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(extraProperties.get("ethicalIssues").toString()));
// } else {
// rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
// }
// if (extraProperties.get("costs") != null) {
// rda.setCost(new ArrayList<>());
// ((List) extraProperties.get("costs")).forEach(costl -> {
// try {
// rda.getCost().add(CostRDAMapper.toRDA((Map)costl));
// } catch (JsonProcessingException e) {
// logger.error(e.getMessage(), e);
// }
// });
// }
if (dmp.getProperties().getContacts() != null && !dmp.getProperties().getContacts().isEmpty()){
UserModel userContact = dmp.getProperties().getContacts().stream().filter(x-> x.getUser() != null).map(DmpContactModel::getUser).findFirst().orElse(null);
if (userContact != null) {
rda.setContact(contactRDAMapper.toRDA(userContact));
} else {
rda.setContact(dmpContactModelContactRDAMapper.toRDA(dmp.getProperties().getContacts().getFirst()));
}
}
}
UserModel creator = null;
if (dmp.getCreator() != null) {
creator = dmp.getCreator();
} else if (dmp.getUsers() != null){
creator = dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(DmpUserRole.Owner)).map(DmpUserModel::getUser).findFirst().orElse(null);
}
if (creator != null) rda.setContact(contactRDAMapper.toRDA(creator));
if (!researchers.isEmpty()) rda.setContributor(referenceContributorRDAMapper.toRDAs(researchers));
if (dmp.getUsers() != null && !dmp.getUsers().isEmpty()){
if (rda.getContributor() == null) rda.setContributor(new ArrayList<>());
rda.getContributor().addAll(dmpUserContributorRDAMapper.toRDAs(dmp.getUsers()));
}
if (!projects.isEmpty() && !grants.isEmpty() && !funders.isEmpty()) {
ProjectModel projectModel = new ProjectModel();
projectModel.setFunder(funders.getFirst());
projectModel.setGrant(grants.getFirst());
projectModel.setProject(projects.getFirst());
rda.setProject(List.of(projectRDAMapper.toRDA(projectModel, null))); //TODO
}
if (dmp.getDescriptions() != null) rda.setAdditionalProperty("templates", jsonHandlingService.toJsonSafe(dmp.getDescriptions().stream().filter(x-> x.getDescriptionTemplate() != null && x.getDescriptionTemplate().getId() != null).map(descriptionModel -> descriptionModel.getDescriptionTemplate().getId()).toList()));
if (dmp.getDmpBlueprint() != null) rda.setAdditionalProperty("blueprintId", dmp.getDmpBlueprint().getId().toString());
if (dmp.getPublicAfter() != null) rda.setAdditionalProperty("publicDate", jsonHandlingService.toJsonSafe(dmp.getPublicAfter()));
if (dmp.getAccessType() != null) rda.setAdditionalProperty("visible", dmp.getAccessType());
if (dmp.getProperties() != null) rda.setAdditionalProperty("dmpProperties", jsonHandlingService.toJsonSafe(dmp.getProperties()));
Map<String, Object> datasetExtraData = new HashMap<>();
datasetExtraData.put("dmp", rda);
rda.setDataset(dmp.getDescriptions().stream().map(dataset -> datasetRDAMapper.toRDA(dataset, datasetExtraData)).toList());
return rda;
}
private List<ReferenceModel> getReferenceModelOfTypeCode(DmpModel dmp, String code){
List<ReferenceModel> response = new ArrayList<>();
if (dmp.getReferences() == null) return response;
for (DmpReferenceModel dmpReferenceModel : dmp.getReferences()){
if (dmpReferenceModel.getReference() != null && dmpReferenceModel.getReference().getType() != null && dmpReferenceModel.getReference().getType().getCode() != null && dmpReferenceModel.getReference().getType().getCode().equals(code)){
response.add(dmpReferenceModel.getReference());
}
}
return response;
}
public DmpModel toEntity(Dmp rda, Map<String, Object> extraData) {
if (extraData == null) throw new IllegalArgumentException("profiles is missing");
Object dmpObject = extraData.getOrDefault("profiles", null);
if (dmpObject == null) throw new IllegalArgumentException("profiles is missing");
List<DescriptionTemplateModel> profiles = (List<DescriptionTemplateModel>)dmpObject;
DmpModel entity = new DmpModel();
entity.setLabel(rda.getTitle());
entity.setCreatedAt(rda.getCreated());
entity.setUpdatedAt(rda.getModified());
entity.setDescription(rda.getDescription());
entity.setLanguage(languageRDAMapper.toEntity(rda.getLanguage()));
String dmpProperties = (String) rda.getAdditionalProperties().getOrDefault("dmpProperties", null);
if (dmpProperties != null && !dmpProperties.isBlank()) entity.setProperties(jsonHandlingService.fromJsonSafe(DmpPropertiesModel.class, dmpProperties));
String accessType = (String) rda.getAdditionalProperties().getOrDefault("visible", null);
if (accessType != null && !accessType.isBlank()) entity.setAccessType(DmpAccessType.of((Short.parseShort(accessType))));
String publicDate = (String) rda.getAdditionalProperties().getOrDefault("publicDate", null);
if (publicDate != null && !publicDate.isBlank()) entity.setPublicAfter(jsonHandlingService.fromJsonSafe(Instant.class, publicDate));
String blueprintId = (String) rda.getAdditionalProperties().getOrDefault("blueprintId", null);
if (blueprintId != null && !blueprintId.isBlank()){
DmpBlueprintModel blueprintModel = new DmpBlueprintModel();
blueprintModel.setId(UUID.fromString(blueprintId));
entity.setDmpBlueprint(blueprintModel);
}
if (rda.getDmpId().getType() == DmpId.Type.DOI) {
EntityDoiModel entityDoiModel = new EntityDoiModel();
entityDoiModel.setDoi(rda.getDmpId().getIdentifier());
entity.setEntityDois(List.of(entityDoiModel));
}
//TODO
/*if (((List<String>) rda.getAdditionalProperties().get("templates")) != null && !((List<String>) rda.getAdditionalProperties().get("templates")).isEmpty() && entity.getId() != null) {
entity.setAssociatedDmps(((List<String>) rda.getAdditionalProperties().get("templates")).stream().map(x -> {
try {
return this.getProfile(x, entity.getId());
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
}).filter(Objects::nonNull).collect(Collectors.toSet()));
}*/
entity.setReferences(new ArrayList<>());
if (rda.getContributor() != null && !rda.getContributor().isEmpty()) {
entity.getReferences().addAll(referenceContributorRDAMapper.toEntities(rda.getContributor().stream().filter(r -> r.getContributorId() != null).toList()).stream().map(reference -> {
DmpReferenceModel dmpReference = new DmpReferenceModel();
dmpReference.setReference(reference);
return dmpReference;
}).toList());
}
if (rda.getProject() != null && !rda.getProject().isEmpty()) {
entity.getReferences().addAll(projectRDAMapper.toEntities(rda.getProject()).stream().map(x-> {
List<ReferenceModel> referenceModels = new ArrayList<>();
if (x.getProject() != null) referenceModels.add(x.getProject());
if (x.getFunder() != null) referenceModels.add(x.getFunder());
if (x.getGrant() != null) referenceModels.add(x.getGrant());
return referenceModels;
}).flatMap(Collection::stream)
.map(reference -> {
DmpReferenceModel dmpReference = new DmpReferenceModel();
dmpReference.setReference(reference);
return dmpReference;
}).toList());
}
entity.setDescriptions(rda.getDataset().stream().map(rda1 -> datasetRDAMapper.toEntity(rda1, profiles.getFirst())).toList()); //TODO
return entity;
}
}

View File

@ -0,0 +1,50 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.enums.ContactInfoType;
import eu.eudat.commonmodels.models.DmpUserModel;
import eu.eudat.commonmodels.models.UserContactInfoModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.file.transformer.model.rda.Contributor;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@Component
public class DmpUserContributorRDAMapper {
private final ContributorIdRDAMapper contributorIdRDAMapper;
public DmpUserContributorRDAMapper(ContributorIdRDAMapper contributorIdRDAMapper) {
this.contributorIdRDAMapper = contributorIdRDAMapper;
}
public Contributor toRDA(DmpUserModel userDMP) {
if (userDMP == null) return null;
if (userDMP.getUser() == null) throw new IllegalArgumentException("User is missing");
if (userDMP.getUser().getName() == null) throw new IllegalArgumentException("Contributor Name is missing");
Contributor rda = new Contributor();
rda.setContributorId(contributorIdRDAMapper.toRDA(userDMP.getUser().getId().toString()));
rda.setName(userDMP.getUser().getName());
UserContactInfoModel emailContact = userDMP.getUser().getContacts() == null ? null : userDMP.getUser().getContacts().stream().filter(userContactInfo -> userContactInfo.getType().equals(ContactInfoType.Email)).findFirst().orElse(null);
if (emailContact != null) {
rda.setMbox(emailContact.getValue());
}
rda.setRole(new HashSet<>(List.of(userDMP.getRole().name())));
return rda;
}
public List<Contributor> toRDAs(List<DmpUserModel> userDMPs) {
if (userDMPs == null) return null;
List<Contributor> items = new ArrayList<>();
for (DmpUserModel userDMP : userDMPs){
Contributor item = this.toRDA(userDMP);
if (item != null) items.add(item);
}
return items;
}
}

View File

@ -0,0 +1,27 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.FunderId;
import org.springframework.stereotype.Component;
import java.util.Map;
import java.util.UUID;
@Component
public class FunderIdRDAMapper {
public FunderId toRDA(Object id) {
if (id == null) return null;
FunderId rda = new FunderId();
rda.setIdentifier(id.toString());
if (id instanceof UUID) {
rda.setType(FunderId.Type.OTHER);
} else {
rda.setType(FunderId.Type.FUNDREF);
}
return rda;
}
public String toEntity(FunderId rda) {
return rda == null ? null : rda.getIdentifier();
}
}

View File

@ -0,0 +1,82 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.commonmodels.models.reference.ReferenceTypeModel;
import eu.eudat.file.transformer.model.FundingModel;
import eu.eudat.file.transformer.model.rda.Funding;
import eu.eudat.file.transformer.service.rdafiletransformer.RdaFileTransformerServiceProperties;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class FundingRDAMapper {
private final GrantIdRDAMapper grantIdRDAMapper;
private final FunderIdRDAMapper funderIdRDAMapper;
private final RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties;
public FundingRDAMapper(GrantIdRDAMapper grantIdRDAMapper, FunderIdRDAMapper funderIdRDAMapper, RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties) {
this.grantIdRDAMapper = grantIdRDAMapper;
this.funderIdRDAMapper = funderIdRDAMapper;
this.rdaFileTransformerServiceProperties = rdaFileTransformerServiceProperties;
}
public Funding toRDA(FundingModel model) {
if (model == null || (model.getGrant() == null && model.getFunder() == null)) return null;
Funding rda = new Funding();
String referencePrefix;
String shortReference;
int prefixLength = 0;
if (model.getFunder() != null && model.getFunder().getReference() != null) {
referencePrefix = model.getFunder().getReference().split(":")[0];
prefixLength = referencePrefix.length() == model.getFunder().getReference().length() ? referencePrefix.length() - 1 : referencePrefix.length();
shortReference = model.getFunder().getReference().substring(prefixLength + 1);
rda.setFunderId(funderIdRDAMapper.toRDA(shortReference));
} else if (model.getFunder() != null){
rda.setFunderId(funderIdRDAMapper.toRDA(model.getFunder().getId()));
}
if (model.getGrant() != null && model.getGrant() != null) {
referencePrefix = model.getGrant().getReference().split(":")[0];
prefixLength = referencePrefix.length() == model.getGrant().getReference().length() ? referencePrefix.length() - 1 : referencePrefix.length();
shortReference = model.getGrant().getReference().substring(prefixLength + 1);
rda.setGrantId(grantIdRDAMapper.toRDA(shortReference));
} else if (model.getGrant() != null) {
rda.setGrantId(grantIdRDAMapper.toRDA(model.getGrant().getId().toString()));
}
return rda;
}
public FundingModel toEntity(Funding rda) {
if (rda == null) return null;
FundingModel references = new FundingModel();
ReferenceModel funder = new ReferenceModel();
ReferenceTypeModel funderReferenceTypeModel = new ReferenceTypeModel();
funderReferenceTypeModel.setCode(rdaFileTransformerServiceProperties.getFunderReferenceCode());
funder.setType(funderReferenceTypeModel);
funder.setReference(rda.getFunderId().getIdentifier());
references.setFunder(funder);
ReferenceModel grant = new ReferenceModel();
ReferenceTypeModel grantReferenceTypeModel = new ReferenceTypeModel();
grantReferenceTypeModel.setCode(rdaFileTransformerServiceProperties.getGrantReferenceCode());
grant.setType(grantReferenceTypeModel);
grant.setReference(rda.getGrantId().getIdentifier());
references.setGrant(grant);
return references;
}
public List<FundingModel> toEntities(List<Funding> rdas) {
if (rdas == null) return null;
List<FundingModel> items = new ArrayList<>();
for (Funding rda : rdas){
FundingModel item = this.toEntity(rda);
if (item != null) items.add(item);
}
return items;
}
}

View File

@ -0,0 +1,23 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.GrantId;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class GrantIdRDAMapper {
public GrantId toRDA(String id) {
if (id == null || id.isBlank()) return null;
GrantId rda = new GrantId();
rda.setIdentifier(id);
rda.setType(GrantId.Type.OTHER);
return rda;
}
public String toEntity(GrantId rda) {
return rda == null ? null : rda.getIdentifier();
}
}

View File

@ -0,0 +1,200 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.Host;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.*;
@Component
public class HostRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(HostRDAMapper.class);
public Host toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields, String numbering){
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Host rda = new Host();
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution.host")).findFirst().orElse("");
if (rdaProperty.contains("host")) {
int firstDiff = MyStringUtils.getFirstDifference(numbering, node.getNumbering());
if (firstDiff == -1 || firstDiff >= 2) {
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
for (ExportPropertyName propertyName: ExportPropertyName.values()) {
if (rdaProperty.contains(propertyName.getName())) {
switch (propertyName) {
case AVAILABILITY:
rda.setAvailability(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.AVAILABILITY.getName(), node.getId());
break;
case BACKUP_FREQUENCY:
rda.setBackupFrequency(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.BACKUP_FREQUENCY.getName(), node.getId());
break;
case BACKUP_TYPE:
rda.setBackupType(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.BACKUP_TYPE.getName(), node.getId());
break;
case CERTIFIED_WITH:
try {
rda.setCertifiedWith(Host.CertifiedWith.fromValue(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.CERTIFIED_WITH.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution host certified with " + rdaValue + "from semantic distribution.host.certified_with is not valid. Certified_with will not be set set.");
}
break;
case DESCRIPTION:
rda.setDescription(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
case GEO_LOCATION:
// if (rdaValue.getTextValue().startsWith("{")) {
// try {
// rdaValue = mapper.readValue(rdaValue.getTextValue(), Map.class).get("id").toString();
// } catch (JsonProcessingException e) {
// logger.warn(e.getLocalizedMessage() + ". Try to pass value as is");
// }
// }
// try {
// rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue.getTextValue()));
// rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId());
// }
// catch (IllegalArgumentException e) {
// logger.warn("Distribution host geo location " + rdaValue.getTextValue() + "from semantic distribution.host.geo_location is not valid. Geo location will not be set set.");
// }
//TODO
rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId());
break;
case PID_SYSTEM:
// try{
// JsonNode valueNode = mapper.readTree(rdaValue.getTextValue());
// Iterator<JsonNode> iter = valueNode.elements();
// List<String> pList = new ArrayList<>();
// while(iter.hasNext()) {
// pList.add(iter.next().asText());
// }
// List<PidSystem> pidList;
// if(pList.size() == 0){
// pidList = Arrays.stream(rdaValue.replaceAll("[\\[\"\\]]","").split(","))
// .map(PidSystem::fromValue).collect(Collectors.toList());
// }
// else{
// pidList = pList.stream().map(PidSystem::fromValue).collect(Collectors.toList());
// }
// rda.setPidSystem(pidList);
// rda.setAdditionalProperty(ImportPropertyName.PID_SYSTEM.getName(), node.getId());
// }
// catch (IllegalArgumentException e){
// rda.setPidSystem(new ArrayList<>());
// break;
// } catch (JsonProcessingException e) {
// throw new RuntimeException(e);
// }
//TODO
break;
case STORAGE_TYPE:
rda.setStorageType(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.STORAGE_TYPE.getName(), node.getId());
break;
case SUPPORT_VERSIONING:
try {
rda.setSupportVersioning(Host.SupportVersioning.fromValue(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.SUPPORT_VERSIONING.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution host support versioning " + rdaValue + "from semantic distribution.host.support_versioning is not valid. Support versioning will not be set set.");
}
break;
case TITLE:
rda.setTitle(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
break;
case URL:
try {
rda.setUrl(URI.create(rdaValue.getTextValue()));
rda.setAdditionalProperty(ImportPropertyName.URL.getName(), node.getId());
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
break;
}
}
}
}
}
}
if(rda.getTitle() == null || rda.getUrl() == null){
return null;
}
return rda;
}
private enum ExportPropertyName {
AVAILABILITY("availability"),
BACKUP_FREQUENCY("backup_frequency"),
BACKUP_TYPE("backup_type"),
CERTIFIED_WITH("certified_with"),
DESCRIPTION("description"),
GEO_LOCATION("geo_location"),
PID_SYSTEM("pid_system"),
STORAGE_TYPE("storage_type"),
SUPPORT_VERSIONING("support_versioning"),
TITLE("title"),
URL("url");
private final String name;
ExportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private enum ImportPropertyName {
AVAILABILITY("availabilityId"),
BACKUP_FREQUENCY("backup_frequencyId"),
BACKUP_TYPE("backup_typeId"),
CERTIFIED_WITH("certified_withId"),
DESCRIPTION("descriptionId"),
GEO_LOCATION("geo_locationId"),
PID_SYSTEM("pid_systemId"),
STORAGE_TYPE("storage_typeId"),
SUPPORT_VERSIONING("support_versioningId"),
TITLE("titleId"),
URL("urlId");
private final String name;
ImportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ImportPropertyName fromString(String name) throws Exception {
for (ImportPropertyName importPropertyName: ImportPropertyName.values()) {
if (importPropertyName.getName().equals(name)) {
return importPropertyName;
}
}
throw new Exception("No name available");
}
}
}

View File

@ -0,0 +1,16 @@
package eu.eudat.file.transformer.model.rda.mapper;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class KeywordRDAMapper {
public String toRDA(String value) {
return value;
}
public String toEntity(String rda) {
return rda;
}
}

View File

@ -1,25 +1,26 @@
package eu.eudat.file.transformer.rda.mapper;
package eu.eudat.file.transformer.model.rda.mapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.rda.Language;
import eu.eudat.file.transformer.model.FundingModel;
import eu.eudat.file.transformer.model.rda.Funding;
import eu.eudat.file.transformer.model.rda.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.*;
@Component
public class LanguageRDAMapper {
private final static Map<String, Object> langMap = new HashMap<>();
private final static Map<String, String> langMap = new HashMap<>();
private static final Logger logger = LoggerFactory.getLogger(LanguageRDAMapper.class);
static {
try {
ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper = new ObjectMapper(); //TODO
InputStreamReader isr = new InputStreamReader(LanguageRDAMapper.class.getClassLoader().getResource("internal/rda-lang-map.json").openStream(), StandardCharsets.UTF_8);
langMap.putAll(mapper.readValue(isr, LinkedHashMap.class));
isr.close();
@ -29,17 +30,20 @@ public class LanguageRDAMapper {
}
}
public static Language mapLanguageIsoToRDAIso(String code) {
public Language toRDA(String code) {
if (code == null || code.isBlank()) return null;
return langMap.entrySet().stream().map(entry -> {
if (entry.getValue().toString().equals(code)) {
if (entry.getValue().equals(code)) {
return Language.fromValue(entry.getKey());
} else {
return null;
}
}).filter(Objects::nonNull).findFirst().get();
}).filter(Objects::nonNull).findFirst().orElse(null);
}
public static String mapRDAIsoToLanguageIso(Language lang) {
return langMap.get(lang.value()).toString();
public String toEntity(Language lang) {
if (lang == null) return null;
return langMap.get(lang.value());
}
}

View File

@ -0,0 +1,67 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.License;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.util.List;
import java.util.Map;
@Component
public class LicenseRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(LicenseRDAMapper.class);
public License toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("extraData is missing");
License rda = new License();
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution.license")).findFirst().orElse("");
eu.eudat.commonmodels.models.description.FieldModel valueField = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(valueField == null || valueField.getTextValue() == null || valueField.getTextValue().isBlank()){
continue;
}
for (LicenceProperties licenceProperties: LicenceProperties.values()) {
if (rdaProperty.contains(licenceProperties.getName())) {
switch (licenceProperties) {
case LICENSE_REF:
try {
rda.setLicenseRef(URI.create(valueField.getTextValue()));
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
break;
case START_DATE:
rda.setStartDate(valueField.getTextValue());
break;
}
}
}
}
if(rda.getLicenseRef() == null || rda.getStartDate() == null){
return null;
}
return rda;
}
public enum LicenceProperties {
LICENSE_REF("license_ref"),
START_DATE("start_date");
private String name;
LicenceProperties(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -0,0 +1,113 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.commonmodels.models.reference.ReferenceFieldModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.file.transformer.model.rda.Metadatum;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class MetadataRDAMapper{
private static final Logger logger = LoggerFactory.getLogger(MetadataRDAMapper.class);
private final MetadataStandardIdRDAMapper metadataStandardIdRDAMapper;
public MetadataRDAMapper(MetadataStandardIdRDAMapper metadataStandardIdRDAMapper) {
this.metadataStandardIdRDAMapper = metadataStandardIdRDAMapper;
}
public List<Metadatum> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, String> rdaMap = new HashMap<>();
List<Metadatum> rdas = new ArrayList<>();
for (FieldModel node : nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.metadata")).findFirst().orElse("");
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x -> x.getId().equals(node.getId())).findFirst().orElse(null);
for (PropertyName propertyName : PropertyName.values()) {
if (rdaProperty.contains(propertyName.getName())) {
switch (propertyName) {
case METADATA_STANDARD_ID:
if (rdaValue != null && rdaValue.getReferences() != null) {
for (ReferenceModel referenceModel : rdaValue.getReferences()) { //TODO reference ??
String uri = referenceModel != null && referenceModel.getDefinition() != null && referenceModel.getDefinition().getFields() != null ? referenceModel.getDefinition().getFields().stream().filter(x-> x.getCode().equals("uri")).map(ReferenceFieldModel::getValue).findFirst().orElse(null) : null;
if (uri!= null && !uri.isBlank()) { //TODO
rdas.add(new Metadatum());
rdas.getLast().setMetadataStandardId(metadataStandardIdRDAMapper.toRDA(uri));
rdas.getLast().setDescription(referenceModel.getLabel());
rdas.getLast().setAdditionalProperty("fieldId", node.getId());
rdas.getLast().setAdditionalProperty("valueId", referenceModel.getReference());
rdaMap.put(uri, node.getNumbering());
}
}
} else if (rdaValue != null && rdaValue.getTextValue() != null && !rdaValue.getTextValue().isEmpty()) {
rdas.add(new Metadatum());
rdas.getLast().setMetadataStandardId(metadataStandardIdRDAMapper.toRDA(rdaValue.getTextValue()));
rdas.getLast().setAdditionalProperty("identifierId", node.getId());
rdaMap.put(rdaValue.getTextValue(), node.getNumbering());
}
break;
case DESCRIPTION:
if (rdaValue != null && rdaValue.getTextValue() != null && !rdaValue.getTextValue().isEmpty()) {
Metadatum rda = getRelative(rdas, rdaMap, node.getNumbering());
if (rda != null) {
rda.setDescription(rdaValue.getTextValue());
rda.setAdditionalProperty("descriptionId", node.getId());
} else {
rdas.stream().filter(rda1 -> rda1.getDescription() == null || rda1.getDescription().isEmpty()).forEach(rda1 -> rda1.setDescription(rdaValue.getTextValue()));
}
}
break;
case LANGUAGE:
if (rdaValue != null && rdaValue.getTextValue() != null && !rdaValue.getTextValue().isEmpty()) {
String language = rdaValue.getTextValue();
Metadatum.Language lang = Metadatum.Language.fromValue(language);
Metadatum rda = getRelative(rdas, rdaMap, node.getNumbering());
if (rda != null) {
rda.setLanguage(lang);
rda.setAdditionalProperty("languageId", node.getId());
} else {
rdas.forEach(rda1 -> rda1.setLanguage(lang));
}
}
break;
}
}
}
}
return rdas;
}
private static Metadatum getRelative(List<Metadatum> rdas, Map<String, String> rdaMap, String numbering) {
String target = rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering) > 0)
.max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering))).map(Map.Entry::getKey).orElse("");
return rdas.stream().filter(rda -> rda.getMetadataStandardId().getIdentifier().equals(target)).distinct().findFirst().orElse(null);
}
private enum PropertyName {
METADATA_STANDARD_ID("metadata_standard_id"),
DESCRIPTION("description"),
LANGUAGE("language");
private final String name;
PropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -0,0 +1,21 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.file.transformer.model.rda.MetadataStandardId;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class MetadataStandardIdRDAMapper {
public MetadataStandardId toRDA(String uri) {
if (uri == null || uri.isBlank()) return null;
MetadataStandardId rda = new MetadataStandardId();
rda.setIdentifier(uri);
rda.setType(MetadataStandardId.Type.URL);
return rda;
}
public String toEntity(MetadataStandardId rda) {
return rda == null ? null : rda.getIdentifier();
}
}

View File

@ -0,0 +1,102 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.reference.ReferenceDefinitionModel;
import eu.eudat.commonmodels.models.reference.ReferenceFieldModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.commonmodels.models.reference.ReferenceTypeModel;
import eu.eudat.file.transformer.model.FundingModel;
import eu.eudat.file.transformer.model.ProjectModel;
import eu.eudat.file.transformer.model.rda.Funding;
import eu.eudat.file.transformer.model.rda.Project;
import eu.eudat.file.transformer.service.rdafiletransformer.RdaFileTransformerServiceProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
@Component
public class ProjectRDAMapper {
private final static Logger logger = LoggerFactory.getLogger(ProjectRDAMapper.class);
private final FundingRDAMapper fundingRDAMapper;
private final RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties;
public ProjectRDAMapper(FundingRDAMapper fundingRDAMapper, RdaFileTransformerServiceProperties rdaFileTransformerServiceProperties) {
this.fundingRDAMapper = fundingRDAMapper;
this.rdaFileTransformerServiceProperties = rdaFileTransformerServiceProperties;
}
public Project toRDA(ProjectModel model, Map<String, Object> extraData) {
if (model == null) return null;
if (model.getProject() == null) throw new IllegalArgumentException("Project is missing");
if (model.getProject().getLabel() == null || model.getProject().getLabel().isBlank()) throw new IllegalArgumentException("Project Title is missing");
Project rda = new Project();
try {
rda.setTitle(model.getProject().getLabel());
rda.setDescription(model.getProject().getDescription());
if (model.getProject().getDefinition() != null && model.getProject().getDefinition().getFields() != null) {
String startDateString = model.getProject().getDefinition().getFields().stream().filter(field -> field.getCode().equals("startDate")).map(ReferenceFieldModel::getValue).findFirst().orElse(null);
if (startDateString != null) {
rda.setStart(startDateString);
}
String endDateString = model.getProject().getDefinition().getFields().stream().filter(field -> field.getCode().equals("endDate")).map(ReferenceFieldModel::getValue).findFirst().orElse(null);
if (endDateString != null) {
rda.setEnd(endDateString);
}
}
FundingModel fundingModel = new FundingModel();
fundingModel.setGrant(model.getGrant());
fundingModel.setFunder(model.getFunder());
rda.setFunding(List.of(fundingRDAMapper.toRDA(fundingModel)));
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
return rda;
}
public ProjectModel toEntity(Project rda) {
ProjectModel projectModel = new ProjectModel();
ReferenceModel project = new ReferenceModel();
project.setLabel(rda.getTitle());
project.setDescription(rda.getDescription());
ReferenceTypeModel referenceTypeModel = new ReferenceTypeModel();
referenceTypeModel.setCode(rdaFileTransformerServiceProperties.getProjectReferenceCode());
project.setType(referenceTypeModel);
ReferenceDefinitionModel projectDefinition = new ReferenceDefinitionModel();
projectDefinition.setFields(new ArrayList<>());
if (rda.getStart() != null && !rda.getStart().isEmpty()) {
ReferenceFieldModel startDateField = new ReferenceFieldModel();
startDateField.setCode("startDate");
startDateField.setValue(rda.getStart());
projectDefinition.getFields().add(startDateField);
}
if (rda.getEnd() != null && !rda.getEnd().isEmpty()) {
ReferenceFieldModel startDateField = new ReferenceFieldModel();
startDateField.setCode("endDate");
startDateField.setValue(rda.getEnd());
projectDefinition.getFields().add(startDateField);
}
project.setDefinition(projectDefinition);
projectModel.setProject(project);
List<FundingModel> fundingModels = fundingRDAMapper.toEntities(rda.getFunding());
if (fundingModels != null && !fundingModels.isEmpty()){
projectModel.setFunder(fundingModels.getFirst().getFunder()); //TODO
projectModel.setGrant(fundingModels.getFirst().getGrant());
}
return projectModel;
}
public List<ProjectModel> toEntities(List<Project> rdas) {
if (rdas == null) return null;
List<ProjectModel> items = new ArrayList<>();
for (Project rda : rdas){
ProjectModel item = this.toEntity(rda);
if (item != null) items.add(item);
}
return items;
}
}

View File

@ -0,0 +1,73 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.reference.ReferenceDefinitionModel;
import eu.eudat.commonmodels.models.reference.ReferenceFieldModel;
import eu.eudat.commonmodels.models.reference.ReferenceModel;
import eu.eudat.file.transformer.model.FundingModel;
import eu.eudat.file.transformer.model.rda.Contributor;
import eu.eudat.file.transformer.model.rda.Funding;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@Component
public class ReferenceContributorRDAMapper{
private final ContributorIdRDAMapper contributorIdRDAMapper;
public ReferenceContributorRDAMapper(ContributorIdRDAMapper contributorIdRDAMapper) {
this.contributorIdRDAMapper = contributorIdRDAMapper;
}
public Contributor toRDA(ReferenceModel researcher) {
if (researcher == null) return null;
Contributor rda = new Contributor();
rda.setContributorId(contributorIdRDAMapper.toRDA(researcher.getReference()));
rda.setName(researcher.getLabel());
if (researcher.getDefinition() != null && researcher.getDefinition().getFields() != null) {
ReferenceFieldModel emailField = researcher.getDefinition().getFields().stream().filter(field -> field.getCode().equals("primaryEmail")).findFirst().orElse(null);
if (emailField != null) {
rda.setMbox(emailField.getValue());
}
}
return rda;
}
public List<Contributor> toRDAs(List<ReferenceModel> researchers) {
if (researchers == null) return null;
List<Contributor> items = new ArrayList<>();
for (ReferenceModel researcher : researchers){
Contributor item = this.toRDA(researcher);
if (item != null) items.add(item);
}
return items;
}
public ReferenceModel toEntity(Contributor rda) {
if (rda == null || rda.getContributorId() == null) return null;
ReferenceModel reference = new ReferenceModel();
reference.setReference(contributorIdRDAMapper.toEntity(rda.getContributorId()));
reference.setLabel(rda.getName());
ReferenceFieldModel field = new ReferenceFieldModel();
field.setCode("primaryEmail");
field.setValue(rda.getMbox());
reference.setDefinition(new ReferenceDefinitionModel());
reference.getDefinition().setFields(List.of(field));
return reference;
}
public List<ReferenceModel> toEntities(List<Contributor> rdas) {
if (rdas == null) return null;
List<ReferenceModel> items = new ArrayList<>();
for (Contributor rda : rdas){
ReferenceModel item = this.toEntity(rda);
if (item != null) items.add(item);
}
return items;
}
}

View File

@ -1,29 +1,29 @@
package eu.eudat.file.transformer.rda.mapper;
package eu.eudat.file.transformer.model.rda.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.SecurityAndPrivacy;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.SecurityAndPrivacy;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class SecurityAndPrivacyRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(SecurityAndPrivacyRDAMapper.class);
public static List<SecurityAndPrivacy> toRDAList(List<FieldFileTransformerModel> nodes) {
public List<SecurityAndPrivacy> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, SecurityAndPrivacy> rdaMap = new HashMap<>();
for (FieldFileTransformerModel node: nodes) {
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.security_and_privacy")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
SecurityAndPrivacy rda = getRelative(rdaMap, node.getNumbering());
@ -34,11 +34,11 @@ public class SecurityAndPrivacyRDAMapper {
if (rdaProperty.contains(exportPropertyName.getName())) {
switch (exportPropertyName) {
case TITLE:
rda.setTitle(rdaValue);
rda.setTitle(rdaValue.getTextValue());
rda.getAdditionalProperties().put(ImportPropertyName.TITLE.getName(), node.getId());
break;
case DESCRIPTION:
rda.setDescription(rdaValue);
rda.setDescription(rdaValue.getTextValue());
rda.getAdditionalProperties().put(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
}
@ -51,61 +51,6 @@ public class SecurityAndPrivacyRDAMapper {
.collect(Collectors.toList());
}
//TODO
/*
public static List<Field> toProperties(List<SecurityAndPrivacy> rdas) {
List<Field> properties = new ArrayList<>();
rdas.forEach(rda -> rda.getAdditionalProperties().entrySet().forEach(entry -> {
try {
Field field = new Field();
field.setKey(entry.getValue().toString());
ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
switch(importPropertyName) {
case TITLE:
field.setValue(rda.getTitle());
break;
case DESCRIPTION:
field.setValue(rda.getDescription());
break;
}
properties.add(field);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}));
return properties;
}
*/
public static SecurityAndPrivacy toRDA(JsonNode node) {
SecurityAndPrivacy rda = new SecurityAndPrivacy();
String rdaProperty = "";
JsonNode schematics = node.get("schematics");
if(schematics.isArray()){
for(JsonNode schematic: schematics){
if(schematic.asText().startsWith("rda.dataset.security_and_privacy")){
rdaProperty = schematic.asText();
break;
}
}
}
String value = node.get("value").asText();
if (rdaProperty.contains("description")) {
rda.setDescription(value);
}
if (rdaProperty.contains("title")) {
rda.setTitle(value);
}
if (rda.getTitle() == null) {
throw new IllegalArgumentException("Security And Privacy Title is missing");
}
return rda;
}
private static SecurityAndPrivacy getRelative(Map<String, SecurityAndPrivacy> rdaMap, String numbering) {
return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0)
.max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new SecurityAndPrivacy());

View File

@ -0,0 +1,21 @@
package eu.eudat.file.transformer.model.rda.mapper;
import eu.eudat.commonmodels.models.TagModel;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class TagRDAMapper {
public String toRDA(TagModel value) {
return value == null ? null : value.getLabel();
}
public TagModel toEntity(String rda) {
if (rda == null || rda.isBlank()) return null;
TagModel tagModel = new TagModel();
tagModel.setLabel(rda);
return tagModel;
}
}

View File

@ -1,29 +1,28 @@
package eu.eudat.file.transformer.rda.mapper;
package eu.eudat.file.transformer.model.rda.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.TechnicalResource;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import eu.eudat.file.transformer.model.rda.TechnicalResource;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.util.*;
import java.util.stream.Collectors;
@Component
public class TechnicalResourceRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(TechnicalResourceRDAMapper.class);
public static List<TechnicalResource> toRDAList(List<FieldFileTransformerModel> nodes) {
public List<TechnicalResource> toRDA(List<FieldModel> nodes, List<eu.eudat.commonmodels.models.description.FieldModel> valueFields ) {
if (nodes == null) return null;
if (valueFields == null) throw new IllegalArgumentException("valueFields is missing");
Map<String, TechnicalResource> rdaMap = new HashMap<>();
for (FieldFileTransformerModel node: nodes) {
for (FieldModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.technical_resource")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
eu.eudat.commonmodels.models.description.FieldModel rdaValue = valueFields.stream().filter(x-> x.getId().equals(node.getId())).findFirst().orElse(null);
if(rdaValue == null || rdaValue.getTextValue() == null || rdaValue.getTextValue().isBlank()){
continue;
}
TechnicalResource rda = getRelative(rdaMap, node.getNumbering());
@ -34,11 +33,11 @@ public class TechnicalResourceRDAMapper {
if (rdaProperty.contains(exportPropertyName.getName())) {
switch (exportPropertyName) {
case NAME:
rda.setName(rdaValue);
rda.setName(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.NAME.getName(), node.getId());
break;
case DESCRIPTION:
rda.setDescription(rdaValue);
rda.setDescription(rdaValue.getTextValue());
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
}
@ -51,62 +50,6 @@ public class TechnicalResourceRDAMapper {
.collect(Collectors.toList());
}
//TODO
/*
public static List<Field> toProperties(List<TechnicalResource> rdas) {
List<Field> properties = new ArrayList<>();
rdas.forEach(rda -> rda.getAdditionalProperties().entrySet().forEach(entry -> {
try {
Field field = new Field();
field.setKey(entry.getValue().toString());
ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
switch(importPropertyName) {
case DESCRIPTION:
field.setValue(rda.getDescription());
break;
case NAME:
field.setValue(rda.getName());
break;
}
properties.add(field);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
}));
return properties;
}
*/
public static TechnicalResource toRDA(JsonNode node) {
TechnicalResource rda = new TechnicalResource();
String rdaProperty = "";
JsonNode schematics = node.get("schematics");
if(schematics.isArray()){
for(JsonNode schematic: schematics){
if(schematic.asText().startsWith("rda.dataset.technical_resource")){
rdaProperty = schematic.asText();
break;
}
}
}
String value = node.get("value").asText();
if (rdaProperty.contains("description")) {
rda.setDescription(value);
}
if (rdaProperty.contains("name")) {
rda.setName(value);
}
if (rda.getName() == null) {
throw new IllegalArgumentException("Technical Resources Name is missing");
}
return rda;
}
private static TechnicalResource getRelative(Map<String, TechnicalResource> rdaMap, String numbering) {
return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0)
.max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new TechnicalResource());

View File

@ -1,20 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.ContactId;
import java.util.UUID;
public class ContactIdRDAMapper {
public static ContactId toRDA(UUID id) {
ContactId rda = new ContactId();
rda.setIdentifier(id.toString());
rda.setType(ContactId.Type.OTHER);
return rda;
}
public static UUID toEntity(ContactId rda) {
return UUID.fromString(rda.getIdentifier());
}
}

View File

@ -1,39 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.enums.ContactInfoType;
import eu.eudat.file.transformer.models.user.UserContactInfoFileTransformerModel;
import eu.eudat.file.transformer.models.user.UserFileTransformerModel;
import eu.eudat.file.transformer.rda.Contact;
import java.util.List;
public class ContactRDAMapper {
public static Contact toRDA(UserFileTransformerModel creator) {
Contact rda = new Contact();
if (creator.getName() == null) {
throw new IllegalArgumentException("Contact Name is missing");
}
rda.setName(creator.getName());
//TODO: GetEmail
UserContactInfoFileTransformerModel emailContact = creator.getContacts().stream().filter(userContactInfo -> userContactInfo.getType().equals(ContactInfoType.Email)).findFirst().orElse(null);
if (emailContact == null) {
throw new IllegalArgumentException("Contact Email is missing");
}
rda.setMbox(emailContact.getValue());
rda.setContactId(ContactIdRDAMapper.toRDA(creator.getId()));
return rda;
}
public static UserFileTransformerModel toEntity(Contact rda) {
UserFileTransformerModel entity = new UserFileTransformerModel();
entity.setId(ContactIdRDAMapper.toEntity(rda.getContactId()));
entity.setName(rda.getName());
UserContactInfoFileTransformerModel emailContactInfo = new UserContactInfoFileTransformerModel();
emailContactInfo.setType(ContactInfoType.Email);
emailContactInfo.setValue(rda.getMbox());
entity.setContacts(List.of(emailContactInfo));
// entity.setEmail(rda.getMbox());//TODO: GetEmail
return entity;
}
}

View File

@ -1,22 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.ContributorId;
public class ContributorIdRDAMapper {
public static ContributorId toRDA(Object id) {
ContributorId rda = new ContributorId();
String idParts[] = id.toString().split(":");
String prefix = idParts.length > 1 ? idParts[0] : id.toString();
if (prefix.equals("orcid")) {
String finalId = id.toString().replace(prefix + ":", "");
rda.setIdentifier("http://orcid.org/" + finalId);
rda.setType(ContributorId.Type.ORCID);
} else {
rda.setIdentifier(id.toString());
rda.setType(ContributorId.Type.OTHER);
}
return rda;
}
}

View File

@ -1,90 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.enums.ContactInfoType;
import eu.eudat.file.transformer.models.dmp.DmpUserFileTransformerModel;
import eu.eudat.file.transformer.models.reference.DefinitionFileTransformerModel;
import eu.eudat.file.transformer.models.reference.FieldFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.user.UserContactInfoFileTransformerModel;
import eu.eudat.file.transformer.rda.Contributor;
import eu.eudat.file.transformer.rda.ContributorId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
public class ContributorRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(ContributorRDAMapper.class);
public static Contributor toRDA(DmpUserFileTransformerModel userDMP) {
Contributor rda = new Contributor();
rda.setContributorId(ContributorIdRDAMapper.toRDA(userDMP.getUser().getId()));
if (userDMP.getUser().getName() == null) {
throw new IllegalArgumentException("Contributor Name is missing");
}
rda.setName(userDMP.getUser().getName());
UserContactInfoFileTransformerModel emailContact = userDMP.getUser().getContacts().stream().filter(userContactInfo -> userContactInfo.getType().equals(ContactInfoType.Email)).findFirst().orElse(null);
if (emailContact != null) {
rda.setMbox(emailContact.getValue());
}
rda.setRole(new HashSet<>(List.of(userDMP.getRole().name())));
return rda;
}
public static Contributor toRDA(ReferenceFileTransformerModel researcher) {
Contributor rda = new Contributor();
rda.setContributorId(ContributorIdRDAMapper.toRDA(researcher.getReference()));
rda.setName(researcher.getLabel());
if (researcher.getDefinition() != null) {
FieldFileTransformerModel emailField = researcher.getDefinition().getFields().stream().filter(field -> field.getCode().equals("primaryEmail")).findFirst().orElse(null);
if (emailField != null) {
rda.setMbox(emailField.getValue());
}
}
// rda.setRole(new HashSet<>(Arrays.asList(UserDMP.UserDMPRoles.fromInteger(userDMP.getRole()).name())));
return rda;
}
public static Contributor toRDA(String value) {
ObjectMapper mapper = new ObjectMapper();
try {
ReferenceFileTransformerModel researcher = mapper.readValue(value, ReferenceFileTransformerModel.class);
return toRDA(researcher);
} catch (IOException e) {
logger.error(e.getMessage(), e);
}
return null;
}
public static ReferenceFileTransformerModel toEntity(Contributor rda) {
ReferenceFileTransformerModel reference = new ReferenceFileTransformerModel();
String referenceString;
if (rda.getContributorId() != null) {
if (rda.getContributorId().getType() == ContributorId.Type.ORCID) {
String id = rda.getContributorId().getIdentifier().replace("http://orcid.org/", "");
referenceString = "orcid:" + id;
} else {
String idParts[] = rda.getContributorId().getIdentifier().split(":");
if (idParts.length == 1) {
referenceString = "dmp:" + rda.getContributorId().getIdentifier();
} else {
referenceString = rda.getContributorId().getIdentifier();
}
}
reference.setReference(referenceString);
reference.setLabel(rda.getName());
FieldFileTransformerModel field = new FieldFileTransformerModel();
field.setCode("primaryEmail");
field.setValue(rda.getMbox());
reference.setDefinition(new DefinitionFileTransformerModel());
reference.getDefinition().setFields(List.of(field));
} else {
return null;
}
return reference;
}
}

View File

@ -1,114 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.Cost;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
public class CostRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static Cost toRDA(Map<String, Object> cost) throws JsonProcessingException {
Cost rda = new Cost();
Map<String, Object> code = mapper.readValue((String) cost.get("code"), HashMap.class);
rda.setCurrencyCode(Cost.CurrencyCode.fromValue((String) code.get("value")));
rda.setDescription((String) cost.get("description"));
if (cost.get("title") == null) {
throw new IllegalArgumentException("Cost Title is missing");
}
rda.setTitle((String) cost.get("title"));
rda.setValue(((Integer) cost.get("value")).doubleValue());
return rda;
}
public static List<Cost> toRDAList(List<FieldFileTransformerModel> nodes) throws JsonProcessingException {
ObjectMapper mapper = new ObjectMapper();
Map<String, Cost> rdaMap = new HashMap<>();
for(FieldFileTransformerModel node: nodes){
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.cost")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
String key = node.getNumbering();
if(!key.contains("mult")){
key = "0";
}
else{
key = "" + key.charAt(4);
}
Cost rda;
if(rdaMap.containsKey(key)){
rda = rdaMap.get(key);
}
else{
rda = new Cost();
rdaMap.put(key, rda);
}
if(rdaProperty.contains("value")){
try {
rda.setValue(Double.valueOf(rdaValue));
}
catch (NumberFormatException e) {
logger.warn("Dmp cost value " + rdaValue + " is not valid. Cost value will not be set.");
}
}
else if(rdaProperty.contains("currency_code")){
try {
HashMap<String, String> result =
new ObjectMapper().readValue(rdaValue, HashMap.class);
rda.setCurrencyCode(Cost.CurrencyCode.fromValue(result.get("value")));
}
catch (Exception e) {
logger.warn("Dmp cost currency code is not valid and will not be set.");
}
}
else if(rdaProperty.contains("title")){
Iterator<JsonNode> iter = mapper.readTree(rdaValue).elements();
StringBuilder title = new StringBuilder();
while(iter.hasNext()){
String next = iter.next().asText();
if(!next.equals("Other")) {
title.append(next).append(", ");
}
}
if(title.length() > 2){
rda.setTitle(title.substring(0, title.length() - 2));
}
else{
String t = rda.getTitle();
if(t == null){ // only other as title
rda.setTitle(rdaValue);
}
else{ // option + other
rda.setTitle(t + ", " + rdaValue);
}
}
}
else if(rdaProperty.contains("description")){
rda.setDescription(rdaValue);
}
}
List<Cost> rdaList = rdaMap.values().stream()
.filter(cost -> cost.getTitle() != null)
.collect(Collectors.toList());
return rdaList;
}
}

View File

@ -1,139 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.DatasetId;
import eu.eudat.file.transformer.utils.json.JsonSearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class DatasetIdRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DatasetIdRDAMapper.class);
/*public static DatasetId toRDA(UUID id) {
DatasetId rda = new DatasetId();
rda.setIdentifier(id.toString());
rda.setType(DatasetId.Type.OTHER);
return rda;
}*/
public static DatasetId toRDA(List<FieldFileTransformerModel> nodes) {
DatasetId data = new DatasetId();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.dataset_id")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
ObjectMapper mapper = new ObjectMapper();
try {
Map<String, Object> values = mapper.readValue(rdaValue, HashMap.class);
if (!values.isEmpty()) {
values.entrySet().forEach(entry -> finalRDAMap(data, entry.getKey(), (String) entry.getValue()));
} else {
finalRDAMap(data, rdaProperty, rdaValue);
}
} catch (IOException e) {
logger.warn(e.getMessage() + ".Passing value as is");
finalRDAMap(data, rdaProperty, rdaValue);
}
}
if (data.getIdentifier() != null && data.getType() != null) {
return data;
}
return null;
}
private static void finalRDAMap(DatasetId rda, String property, String value) {
if (value != null) {
for (DatasetIdProperties datasetIdProperties : DatasetIdProperties.values()) {
if (property.contains(datasetIdProperties.getName())) {
switch (datasetIdProperties) {
case IDENTIFIER:
rda.setIdentifier(value);
break;
case TYPE:
try {
rda.setType(DatasetId.Type.fromValue(value));
}
catch (IllegalArgumentException e){
logger.warn("Type " + value + " from semantic rda.dataset.dataset_id.type was not found. Setting type to OTHER.");
rda.setType(DatasetId.Type.OTHER);
}
break;
}
}
}
}
}
//TODO
/*
public static List<Field> toProperties(DatasetId rda, JsonNode node) {
List<Field> properties = new ArrayList<>();
List<JsonNode> idNodes = JsonSearcher.findNodes(node, "schematics", "rda.dataset.dataset_id");
for (JsonNode idNode: idNodes) {
for (DatasetIdProperties datasetIdProperties : DatasetIdProperties.values()) {
JsonNode schematics = idNode.get("schematics");
if(schematics.isArray()){
for(JsonNode schematic: schematics){
if(schematic.asText().endsWith(datasetIdProperties.getName())){
switch (datasetIdProperties) {
case IDENTIFIER:
Field field1 = new Field();
field1.setKey(idNode.get("id").asText());
field1.setValue(rda.getIdentifier());
properties.add(field1);
break;
case TYPE:
Field field2 = new Field();
field2.setKey(idNode.get("id").asText());
field2.setValue(rda.getType().value());
properties.add(field2);
break;
}
break;
}
}
}
}
}
return properties;
}
*/
private enum DatasetIdProperties {
IDENTIFIER("identifier"),
TYPE("type");
private final String name;
DatasetIdProperties(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -1,436 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.SectionFileTransformerModel;
import eu.eudat.file.transformer.models.tag.TagFileTransformerModel;
import eu.eudat.file.transformer.rda.*;
import eu.eudat.file.transformer.utils.descriptionTemplate.TemplateFieldSearcher;
import eu.eudat.file.transformer.utils.json.JsonSearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.net.URI;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
@Component
public class DatasetRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DatasetRDAMapper.class);
private final ObjectMapper mapper;
@Autowired
public DatasetRDAMapper() {
this.mapper = new ObjectMapper();
}
public Dataset toRDA(DescriptionFileTransformerModel descriptionEntity, Dmp dmp) {
Dataset rda = new Dataset();
// rda.setDatasetId(DatasetIdRDAMapper.toRDA(dataset.getId()));
if (descriptionEntity.getLabel() == null) {
throw new IllegalArgumentException("Dataset Label is missing");
}
Map<String, Object> templateIdsToValues = this.createFieldIdValueMap(descriptionEntity.getDescriptionTemplate());
rda.setTitle(descriptionEntity.getLabel());
rda.setDescription(descriptionEntity.getDescription());
//rda.setAdditionalProperty("template", descriptionEntity.getDescriptionTemplate()); //TODO
try {
List<FieldFileTransformerModel> idNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.dataset_id");
if (!idNodes.isEmpty()) {
rda.setDatasetId(DatasetIdRDAMapper.toRDA(idNodes));
}
if (rda.getDatasetId() == null) {
rda.setDatasetId(new DatasetId(descriptionEntity.getId().toString(), DatasetId.Type.OTHER));
}
List<FieldFileTransformerModel> typeNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.type");
if (!typeNodes.isEmpty() && typeNodes.get(0).getData() != null && !typeNodes.get(0).getData().getValue().isEmpty()) {
rda.setType(typeNodes.get(0).getData().getValue());
} else {
rda.setType("DMP Dataset");
}
List<FieldFileTransformerModel> languageNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.language");
if (!languageNodes.isEmpty() && languageNodes.get(0).getData() != null && !languageNodes.get(0).getData().getValue().isEmpty()) {
String lang = languageNodes.get(0).getData().getValue();
try {
rda.setLanguage(Language.fromValue(lang));
}
catch (IllegalArgumentException e){
//TODO
logger.warn("Language " + lang + " from semantic rda.dataset.language was not found. Setting '" + descriptionEntity.getDescriptionTemplate().getLanguage() +"' as language from the dataset profile.");
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(descriptionEntity.getDescriptionTemplate().getLanguage()));
}
} else {
//TODO
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(descriptionEntity.getDescriptionTemplate().getLanguage()));
}
List<FieldFileTransformerModel> metadataNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.metadata");
if (!metadataNodes.isEmpty()) {
rda.setMetadata(MetadataRDAMapper.toRDAList(metadataNodes));
}else{
rda.setMetadata(new ArrayList<>());
}
List<FieldFileTransformerModel> qaNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.data_quality_assurance");
if (!qaNodes.isEmpty()) {
rda.setDataQualityAssurance(qaNodes.stream().filter(qaNode -> qaNode.getData() != null).map(qaNode -> qaNode.getData().getValue()).collect(Collectors.toList()));
for (int i = 0; i < qaNodes.size(); i++) {
rda.setAdditionalProperty("qaId" + (i + 1), qaNodes.get(i).getId());
}
List<String> qaList = new ArrayList<>();
String qa;
for(FieldFileTransformerModel node: qaNodes){
if (node.getData() == null) {
continue;
}
JsonNode valueNode = mapper.readTree(node.getData().getValue());
if(valueNode.isArray()){
Iterator<JsonNode> iter = valueNode.elements();
while(iter.hasNext()) {
qa = iter.next().asText();
qaList.add(qa);
}
}
}
String data_quality;
for(FieldFileTransformerModel dqa: qaNodes){
if (dqa.getData() == null) {
continue;
}
data_quality = dqa.getData().getValue();
if(!data_quality.isEmpty()){
qaList.add(data_quality);
rda.setAdditionalProperty("otherDQAID", dqa.getId());
rda.setAdditionalProperty("otherDQA", data_quality);
break;
}
}
rda.setDataQualityAssurance(qaList);
}else{
rda.setDataQualityAssurance(new ArrayList<>());
}
List<FieldFileTransformerModel> preservationNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.preservation_statement");
if (!preservationNodes.isEmpty() && preservationNodes.get(0).getData() != null && !preservationNodes.get(0).getData().getValue().isEmpty()) {
rda.setPreservationStatement(preservationNodes.get(0).getData().getValue());
}
List<FieldFileTransformerModel> distributionNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.distribution");
if (!distributionNodes.isEmpty()) {
rda.setDistribution(DistributionRDAMapper.toRDAList(distributionNodes));
}else{
rda.setDistribution(new ArrayList<>());
}
List<FieldFileTransformerModel> keywordNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.keyword");
if (!keywordNodes.isEmpty()) {
rda.setKeyword(keywordNodes.stream().filter(keywordNode -> keywordNode.getData() != null).map(keywordNode -> {
try {
JsonNode value = mapper.readTree(keywordNode.getData().getValue());
if (value.isArray()) {
return StreamSupport.stream(value.spliterator(), false).map(node -> KeywordRDAMapper.toRDA(node.toString())).flatMap(Collection::stream).collect(Collectors.toList());
} else {
return KeywordRDAMapper.toRDA(keywordNode.getData().getValue());
}
}catch (JsonProcessingException e) {
logger.error(e.getMessage(), e);
return null;
}
}).filter(Objects::nonNull).flatMap(Collection::stream).collect(Collectors.toList()));
for (int i = 0; i < keywordNodes.size(); i++) {
rda.setAdditionalProperty("keyword" + (i + 1), keywordNodes.get(i).getId());
}
}
// else if (apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().exists()) { //TODO
// List<String> tags = apiContext.getOperationsContext().getElasticRepository().getDatasetRepository().findDocument(descriptionEntity.getId().toString()).getTags().stream().map(Tag::getName).collect(Collectors.toList());
// rda.setKeyword(tags);
// }
List<FieldFileTransformerModel> personalDataNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.personal_data");
if (!personalDataNodes.isEmpty()) {
try{
rda.setPersonalData(personalDataNodes.stream().filter(personalDataNode -> personalDataNode.getData() != null).map(personalDataNode -> Dataset.PersonalData.fromValue(personalDataNode.getData().getValue())).findFirst().get());
}catch(IllegalArgumentException e){
rda.setPersonalData(Dataset.PersonalData.UNKNOWN);
}
} else {
rda.setPersonalData(Dataset.PersonalData.UNKNOWN);
}
List<FieldFileTransformerModel> securityAndPrivacyNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.security_and_privacy");
if (!securityAndPrivacyNodes.isEmpty()) {
rda.setSecurityAndPrivacy(SecurityAndPrivacyRDAMapper.toRDAList(securityAndPrivacyNodes));
}else{
rda.setSecurityAndPrivacy(new ArrayList<>());
}
List<FieldFileTransformerModel> sensitiveDataNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.sensitive_data");
if (!sensitiveDataNodes.isEmpty()) {
try{
rda.setSensitiveData(sensitiveDataNodes.stream().filter(sensitiveDataNode -> sensitiveDataNode.getData() != null).map(sensitiveDataNode -> Dataset.SensitiveData.fromValue(sensitiveDataNode.getData().getValue())).findFirst().get());
}catch(IllegalArgumentException e){
rda.setSensitiveData(Dataset.SensitiveData.UNKNOWN);
}
} else {
rda.setSensitiveData(Dataset.SensitiveData.UNKNOWN);
}
List<FieldFileTransformerModel> technicalResourceNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.technical_resource");
if (!technicalResourceNodes.isEmpty()) {
rda.setTechnicalResource(TechnicalResourceRDAMapper.toRDAList(technicalResourceNodes));
}else{
rda.setTechnicalResource(new ArrayList<>());
}
List<FieldFileTransformerModel> issuedNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dataset.issued");
if (!issuedNodes.isEmpty() && issuedNodes.get(0).getData() != null && !issuedNodes.get(0).getData().getValue().isEmpty()) {
rda.setIssued(issuedNodes.get(0).getData().getValue());
}
List<FieldFileTransformerModel> contributorNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dmp.contributor");
if (!contributorNodes.isEmpty()) {
dmp.getContributor().addAll(contributorNodes.stream().filter(contributorNode -> contributorNode.getData() != null).map(contributorNode -> {
try {
JsonNode value = mapper.readTree(contributorNode.getData().getValue());
if (value.isArray()) {
return StreamSupport.stream(value.spliterator(), false).map(node -> ContributorRDAMapper.toRDA(node.asText())).collect(Collectors.toList());
} else {
return Collections.singletonList(new Contributor());
}
}catch (JsonProcessingException e) {
return null;
}
}).filter(Objects::nonNull).flatMap(Collection::stream).toList());
dmp.setContributor(dmp.getContributor().stream().filter(contributor -> contributor.getContributorId() != null && contributor.getName() != null).collect(Collectors.toList()));
}
List<FieldFileTransformerModel> costNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dmp.cost");
if (!costNodes.isEmpty()) {
dmp.getCost().addAll(CostRDAMapper.toRDAList(costNodes));
}
List<FieldFileTransformerModel> ethicsNodes = TemplateFieldSearcher.searchFields(descriptionEntity.getDescriptionTemplate(), "schematics", "rda.dmp.ethical_issues");
if (!ethicsNodes.isEmpty()) {
for(FieldFileTransformerModel node: ethicsNodes){
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dmp.ethical_issues")).findFirst().orElse("");
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
if(rdaProperty.contains("exist")){
try {
Dmp.EthicalIssuesExist exists = dmp.getEthicalIssuesExist();
if(exists == null
|| ((exists == Dmp.EthicalIssuesExist.NO || exists == Dmp.EthicalIssuesExist.UNKNOWN) && rdaValue.equals("yes"))
|| (exists == Dmp.EthicalIssuesExist.YES && !(rdaValue.equals("no") || rdaValue.equals("unknown")))
|| (exists == Dmp.EthicalIssuesExist.UNKNOWN && rdaValue.equals("no"))){
dmp.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(rdaValue));
}
}catch(IllegalArgumentException e){
logger.warn(e.getLocalizedMessage() + ". Setting ethical_issues_exist to unknown");
dmp.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
}
}
else if(rdaProperty.contains("description")){
if(dmp.getEthicalIssuesDescription() == null){
dmp.setEthicalIssuesDescription(rdaValue);
}
else{
dmp.setEthicalIssuesDescription(dmp.getEthicalIssuesDescription() + ", " + rdaValue);
}
}
else if(rdaProperty.contains("report")){
try {
dmp.setEthicalIssuesReport(URI.create(rdaValue));
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
}
}
}
List<FieldFileTransformerModel> foundNodes = Stream.of(typeNodes, languageNodes, metadataNodes, qaNodes, preservationNodes, distributionNodes,
keywordNodes, personalDataNodes, securityAndPrivacyNodes, sensitiveDataNodes, technicalResourceNodes).flatMap(Collection::stream).toList();
templateIdsToValues.entrySet().forEach(entry -> {
boolean isFound = foundNodes.stream().anyMatch(node -> node.getId().equals(entry.getKey()));
if (!isFound && entry.getValue() != null && !entry.getValue().toString().isEmpty()) {
try {
Instant time = Instant.parse(entry.getValue().toString());
rda.setAdditionalProperty(entry.getKey(), DateTimeFormatter.ofPattern("yyyy-MM-dd").withZone(ZoneId.systemDefault()).format(time));
} catch (DateTimeParseException e) {
rda.setAdditionalProperty(entry.getKey(), entry.getValue());
}
}
});
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return rda;
}
public DescriptionFileTransformerModel toEntity(Dataset rda, DescriptionTemplateFileTransformerModel defaultProfile) {
DescriptionFileTransformerModel entity = new DescriptionFileTransformerModel();
entity.setLabel(rda.getTitle());
entity.setDescription(rda.getDescription());
/*try {
DescriptionTemplateEntity profile = apiContext.getOperationsContext().getDatabaseRepository().getDatasetProfileDao().find(UUID.fromString(rda.getAdditionalProperties().get("template").toString()));
//entity.setDescriptionTemplateId(profile.getId()); //TODO
}catch(Exception e) {
logger.warn(e.getMessage(), e);*/
entity.setDescriptionTemplate(defaultProfile); //TODO
// }
try {
// PropertyDefini properties = new PropertyDefinition();
// properties.setFields(new ArrayList<>());
String datasetDescriptionJson = mapper.writeValueAsString(entity.getDescriptionTemplate());
JsonNode datasetDescriptionObj = mapper.readTree(datasetDescriptionJson);
List<FieldFileTransformerModel> typeNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.type");
if (!typeNodes.isEmpty()) {
typeNodes.get(0).getData().setValue(rda.getType());
}
List<FieldFileTransformerModel> languageNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.language");
if (!languageNodes.isEmpty() && rda.getLanguage() != null) {
languageNodes.get(0).getData().setValue(rda.getLanguage().value());
}
//TODO
/*if (rda.getMetadata() != null) {
properties.getFields().addAll(MetadataRDAMapper.toProperties(rda.getMetadata()));
}*/
//TODO
/*if (rda.getDatasetId() != null) {
properties.getFields().addAll(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());
for (int i = 0; i < qaIds.size(); i++) {
properties.put(qaIds.get(i), rda.getDataQualityAssurance().get(i));
}*/
List<FieldFileTransformerModel> qaNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.data_quality_assurance");
if (!qaNodes.isEmpty() && rda.getDataQualityAssurance() != null && !rda.getDataQualityAssurance().isEmpty()) {
ObjectMapper m = new ObjectMapper();
List<String> qas = new ArrayList<>(rda.getDataQualityAssurance());
if(!qas.isEmpty()){
qaNodes.get(0).getData().setValue(mapper.writeValueAsString(qas));
if(rda.getAdditionalProperties().containsKey("otherDQAID")){
List<FieldFileTransformerModel> subFields = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "id", (String) rda.getAdditionalProperties().get("otherDQAID"));
if (subFields != null && !subFields.isEmpty()) {
subFields.get(0).getData().setValue((String) rda.getAdditionalProperties().get("otherDQA"));
}
}
}
}
List<FieldFileTransformerModel> preservationNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.preservation_statement");
if (!preservationNodes.isEmpty()) {
preservationNodes.get(0).getData().setValue(rda.getPreservationStatement());
}
List<FieldFileTransformerModel> issuedNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.issued");
if (!issuedNodes.isEmpty()) {
issuedNodes.get(0).getData().setValue(rda.getIssued());
}
//TODO
/*if (rda.getDistribution() != null && !rda.getDistribution().isEmpty()) {
properties.getFields().addAll(DistributionRDAMapper.toProperties(rda.getDistribution().get(0), datasetDescriptionObj));
}*/
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++) {
List<FieldFileTransformerModel> tagField = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "id", keywordIds.get(i));
if (takeAll) {
List<String> tags = new ArrayList<>();
for (String keyword : rda.getKeyword()) {
tags.add(mapper.writeValueAsString(toTagEntity(keyword)));
}
tagField.get(0).getData().setValue(String.valueOf(tags));
} else {
tagField.get(0).getData().setValue(mapper.writeValueAsString(toTagEntity(rda.getKeyword().get(i))));
}
/*properties.getFields().add(field);
Field field1 = new Field();
field1.setKey(keywordIds.get(i));
field1.setValue(rda.getKeyword().get(i));
properties.getFields().add(field1);*/
}
}
List<FieldFileTransformerModel> personalDataNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.personal_data");
if (!personalDataNodes.isEmpty()) {
personalDataNodes.get(0).getData().setValue(rda.getPersonalData().value());
}
//TODO
/*if (rda.getSecurityAndPrivacy() != null) {
properties.getFields().addAll(SecurityAndPrivacyRDAMapper.toProperties(rda.getSecurityAndPrivacy()));
}*/
List<FieldFileTransformerModel> sensitiveDataNodes = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "schematics", "rda.dataset.sensitive_data");
if (!sensitiveDataNodes.isEmpty()) {
sensitiveDataNodes.get(0).getData().setValue(rda.getSensitiveData().value());
}
//TODO
/*if (rda.getTechnicalResource() != null) {
properties.getFields().addAll(TechnicalResourceRDAMapper.toProperties(rda.getTechnicalResource()));
}*/
rda.getAdditionalProperties().entrySet().stream()
.filter(entry -> !entry.getKey().equals("template") && !entry.getKey().startsWith("qaId") && !entry.getKey().startsWith("keyword"))
.forEach(entry -> {
List<FieldFileTransformerModel> field = TemplateFieldSearcher.searchFields(entity.getDescriptionTemplate(), "id", entry.getKey());
field.get(0).getData().setValue((String) entry.getValue());
});
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return entity;
}
private static TagFileTransformerModel toTagEntity(String name) {
TagFileTransformerModel tag = new TagFileTransformerModel();
tag.setId(UUID.randomUUID());
tag.setLabel(name);
return tag;
}
private Map<String, Object> createFieldIdValueMap(DescriptionTemplateFileTransformerModel template) {
Map<String, Object> result = new HashMap<>();
template.getDefinition().getPages().forEach(page -> page.getSections().forEach(section -> result.putAll(createFieldIdValueMapFromSection(section))));
return result;
}
private Map<String, Object> createFieldIdValueMapFromSection(SectionFileTransformerModel section) {
Map<String, Object> result = new HashMap<>();
if (section.getSections() != null && !section.getSections().isEmpty()) {
section.getSections().forEach(subSection -> result.putAll(createFieldIdValueMapFromSection(subSection)));
}
if (section.getFieldSets() != null && !section.getFieldSets().isEmpty()) {
section.getFieldSets().stream().filter(fieldSet -> fieldSet.getFields() != null && !fieldSet.getFields().isEmpty())
.forEach(fieldSet -> fieldSet.getFields().stream().filter(field -> field.getData() != null).forEach(field -> result.put(field.getId(), field.getData().getValue())));
}
return result;
}
}

View File

@ -1,439 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.Distribution;
import eu.eudat.file.transformer.rda.License;
import eu.eudat.file.transformer.utils.json.JsonSearcher;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
public class DistributionRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DistributionRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static List<Distribution> toRDAList(List<FieldFileTransformerModel> nodes) {
Map<String, Distribution> rdaMap = new HashMap<>();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = getRdaDistributionProperty(node);
if(rdaProperty.isEmpty() || node.getData() == null){
continue;
}
String rdaValue = node.getData().getValue();
//if(rdaValue == null || rdaValue.isEmpty()){
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
String key = node.getNumbering();
if(!key.contains("mult")){
key = "0";
}
else{
key = "" + key.charAt(4);
}
Distribution rda;
if(rdaMap.containsKey(key)){
rda = rdaMap.get(key);
}
else {
rda = new Distribution();
rdaMap.put(key, rda);
}
/* Distribution rda = getRelative(rdaMap, node.get("numbering").asText());
if (!rdaMap.containsValue(rda)) {
rdaMap.put(node.get("numbering").asText(), rda);
} */
for (ExportPropertyName exportPropertyName : ExportPropertyName.values()) {
if (rdaProperty.contains(exportPropertyName.getName())) {
switch (exportPropertyName) {
case ACCESS_URL:
rda.setAccessUrl(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.ACCESS_URL.getName(), node.getId());
break;
case AVAILABLE_UNTIL:
rda.setAvailableUntil(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.AVAILABLE_UNTIL.getName(), node.getId());
break;
case DOWNLOAD_URL:
rda.setDownloadUrl(URI.create(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.DOWNLOAD_URL.getName(), node.getId());
break;
case DESCRIPTION:
if(!rdaProperty.contains("host")) {
rda.setDescription(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
}
break;
case DATA_ACCESS:
try {
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.DATA_ACCESS.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution data access " + rdaValue + " from semantic distribution.data_access is not valid. Data access will not be set set.");
}
break;
case BYTE_SIZE:
rda.setByteSize(Integer.parseInt(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.BYTE_SIZE.getName(), node.getId());
break;
case LICENSE:
List<FieldFileTransformerModel> licenseNodes = nodes.stream().filter(lnode -> {
//if(lnode.get("schematics").isArray()){
for(String schematic: lnode.getSchematics()){
if(schematic.startsWith("rda.dataset.distribution.license")){
return true;
}
}
//}
return false;
}).collect(Collectors.toList());
License license = LicenseRDAMapper.toRDA(licenseNodes);
rda.setLicense(license != null? Collections.singletonList(license): new ArrayList<>());
break;
case FORMAT:
try {
JsonNode valueNode = mapper.readTree(node.getData().getValue());
if(valueNode.isArray()){
Iterator<JsonNode> iter = valueNode.elements();
List<String> formats = new ArrayList<>();
int i = 1;
while(iter.hasNext()) {
JsonNode current = iter.next();
String format = current.toString();
Map<String, String> result = mapper.readValue(format, HashMap.class);
format = result.get("label");
formats.add(format);
rda.setAdditionalProperty("format" + i++, mapper.readTree(current.toString()));
}
rda.setFormat(formats);
}
else{
if(rda.getFormat() == null || rda.getFormat().isEmpty()){
rda.setFormat(new ArrayList<>(Arrays.asList(rdaValue.replace(" ", "").split(","))));
}
else{
rda.getFormat().addAll(Arrays.asList(rdaValue.replace(" ", "").split(",")));
}
}
rda.setAdditionalProperty(ImportPropertyName.FORMAT.getName(), node.getId());
}
catch(JsonProcessingException e){
logger.warn(e.getMessage());
}
break;
case TITLE:
if(!rdaProperty.contains("host")) {
rda.setTitle(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
}
break;
case HOST:
rda.setHost(HostRDAMapper.toRDA(nodes, node.getNumbering()));
break;
}
}
}
}
return rdaMap.values().stream()
.filter(distro -> distro.getTitle() != null).collect(Collectors.toList());
}
//TODO
/*public static List<Field> toProperties(List<Distribution> rdas) {
List<Field> properties = new ArrayList<>();
rdas.forEach(rda -> {
rda.getAdditionalProperties().entrySet().forEach(entry -> {
try {
Field field = new Field();
field.setKey(entry.getValue().toString());
ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
switch (importPropertyName) {
case ACCESS_URL:
field.setValue(rda.getAccessUrl());
break;
case TITLE:
field.setValue(rda.getTitle());
break;
case DESCRIPTION:
field.setValue(rda.getDescription());
break;
case FORMAT:
field.setValue(rda.getFormat().get(0));
break;
case BYTE_SIZE:
field.setValue(rda.getByteSize().toString());
break;
case DATA_ACCESS:
field.setValue(rda.getDataAccess().value());
break;
case DOWNLOAD_URL:
field.setValue(rda.getDownloadUrl().toString());
break;
case AVAILABLE_UNTIL:
field.setValue(rda.getAvailableUntil());
break;
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
});
if (rda.getHost() != null) {
properties.addAll(HostRDAMapper.toProperties(rda.getHost()));
}
if (rda.getLicense() != null && !rda.getLicense().isEmpty()) {
properties.addAll(LicenseRDAMapper.toProperties(rda.getLicense()));
}
});
return properties;
}
public static List<Field> toProperties(Distribution rda, JsonNode root) {
List<Field> properties = new ArrayList<>();
List<JsonNode> distributionNodes = JsonSearcher.findNodes(root, "schematics", "rda.dataset.distribution");
for (JsonNode distributionNode: distributionNodes) {
for (ExportPropertyName exportPropertyName: ExportPropertyName.values()) {
JsonNode schematics = distributionNode.get("schematics");
if(schematics.isArray()){
for(JsonNode schematic: schematics){
Field field = new Field();
field.setKey(distributionNode.get("id").asText());
if(schematic.asText().contains(exportPropertyName.getName())){
switch (exportPropertyName) {
case ACCESS_URL:
field.setValue(rda.getAccessUrl());
break;
case DESCRIPTION:
field.setValue(rda.getDescription());
break;
case TITLE:
field.setValue(rda.getTitle());
break;
case AVAILABLE_UNTIL:
field.setValue(rda.getAvailableUntil());
break;
case DOWNLOAD_URL:
if (rda.getDownloadUrl() != null) {
field.setValue(rda.getDownloadUrl().toString());
}
break;
case DATA_ACCESS:
field.setValue(rda.getDataAccess().value());
break;
case BYTE_SIZE:
if (rda.getByteSize() != null) {
field.setValue(rda.getByteSize().toString());
}
break;
case FORMAT:
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();
List<Object> standardFormats = new ArrayList<>();
rda.getAdditionalProperties().forEach((key, value) -> {
try {
if (key.matches("format\\d+")) {
standardFormats.add(additionalProperties.get(key));
Field field1 = new Field();
field1.setKey(distributionNode.get("id").asText());
field1.setValue(mapper.writeValueAsString(standardFormats));
properties.add(field1);
}
} catch (JsonProcessingException e) {
logger.error(e.getMessage(), e);
}
});
}
}
else if(style.equals("freetext")){
field.setValue(String.join(", ", rda.getFormat()));
}
}
break;
case LICENSE:
if (rda.getLicense() != null && !rda.getLicense().isEmpty()) {
properties.addAll(LicenseRDAMapper.toProperties(rda.getLicense().get(0), root));
}
break;
case HOST:
if (rda.getHost() != null) {
properties.addAll(HostRDAMapper.toProperties(rda.getHost()));
}
break;
}
if (field.getValue() != null) {
properties.add(field);
}
break;
}
}
}
}
}
return properties;
}*/
public static Distribution toRDA(List<FieldFileTransformerModel> nodes) {
Distribution rda = new Distribution();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = getRdaDistributionProperty(node);
if(rdaProperty.isEmpty()){
continue;
}
String rdaValue = node.getData().getValue();
for (ExportPropertyName exportPropertyName: ExportPropertyName.values()) {
if (rdaProperty.contains(exportPropertyName.getName())) {
switch (exportPropertyName) {
case ACCESS_URL:
rda.setAccessUrl(rdaValue);
break;
case DESCRIPTION:
rda.setDescription(rdaValue);
break;
case TITLE:
rda.setTitle(rdaValue);
break;
case AVAILABLE_UNTIL:
rda.setAvailableUntil(rdaValue);
break;
case DOWNLOAD_URL:
rda.setDownloadUrl(URI.create(rdaValue));
break;
case DATA_ACCESS:
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
break;
case BYTE_SIZE:
rda.setByteSize(Integer.parseInt(rdaValue));
break;
case FORMAT:
rda.setFormat(Collections.singletonList(rdaValue));
break;
case LICENSE:
List<FieldFileTransformerModel> licenseNodes = nodes.stream().filter(lnode -> lnode.getSchematics().stream().anyMatch(schematic -> schematic.startsWith("rda.dataset.distribution.license"))).collect(Collectors.toList());
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(licenseNodes)));
break;
case HOST:
List<FieldFileTransformerModel> hostNodes = nodes.stream().filter(lnode -> lnode.getSchematics().stream().anyMatch(schematic -> schematic.startsWith("rda.dataset.distribution.host"))).collect(Collectors.toList());
rda.setHost(HostRDAMapper.toRDA(hostNodes, "0"));
break;
}
}
}
/*if (rdaProperty.contains("access_url")) {
rda.setAccessUrl(rdaValue);
} else if (rdaProperty.contains("available_util")) {
rda.setAvailableUntil(rdaValue);
} else if (rdaProperty.contains("byte_size")) {
rda.setByteSize(Integer.parseInt(rdaValue));
} else if (rdaProperty.contains("data_access")) {
rda.setDataAccess(Distribution.DataAccess.fromValue(rdaValue));
} else if (rdaProperty.contains("description")) {
rda.setDescription(rdaValue);
} else if (rdaProperty.contains("download_url")) {
rda.setDownloadUrl(URI.create(rdaValue));
} else if (rdaProperty.contains("format")) {
rda.setFormat(Collections.singletonList(rdaValue));
} else if (rdaProperty.contains("host")) {
// rda.setHost(HostRDAMapper.toRDA(node));
} else if (rdaProperty.contains("license")) {
rda.setLicense(Collections.singletonList(LicenseRDAMapper.toRDA(node)));
} else if (rdaProperty.contains("title")) {
rda.setTitle(rdaValue);
}*/
}
if (rda.getTitle() == null) {
throw new IllegalArgumentException("Distribution title is missing");
}
if (rda.getDataAccess() == null) {
throw new IllegalArgumentException("Distribution Data Access is missing");
}
return rda;
}
private static String getRdaDistributionProperty(FieldFileTransformerModel node) {
return node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution")).findFirst().orElse("");
}
private static Distribution getRelative( Map<String, Distribution> rdaMap, String numbering) {
return rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering) > 0)
.max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getKey(), numbering))).map(Map.Entry::getValue).orElse(new Distribution());
}
private enum ExportPropertyName {
ACCESS_URL("access_url"),
AVAILABLE_UNTIL("available_until"),
BYTE_SIZE("byte_size"),
DATA_ACCESS("data_access"),
DESCRIPTION("description"),
DOWNLOAD_URL("download_url"),
FORMAT("format"),
HOST("host"),
LICENSE("license"),
TITLE("title");
private final String name;
ExportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private enum ImportPropertyName {
ACCESS_URL("accessurlId"),
AVAILABLE_UNTIL("availableUtilId"),
BYTE_SIZE("byteSizeId"),
DATA_ACCESS("dataAccessId"),
DESCRIPTION("descriptionId"),
DOWNLOAD_URL("downloadUrlId"),
FORMAT("formatId"),
/*HOST("host"),
LICENSE("license"),*/
TITLE("titleId");
private final String name;
ImportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ImportPropertyName fromString(String name) throws Exception {
for (ImportPropertyName importPropertyName: ImportPropertyName.values()) {
if (importPropertyName.getName().equals(name)) {
return importPropertyName;
}
}
throw new Exception("No name available");
}
}
}

View File

@ -1,19 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.DmpId;
import java.util.UUID;
public class DmpIdRDAMapper {
public static DmpId toRDA(Object id) {
DmpId rda = new DmpId();
rda.setIdentifier(id.toString());
if (id instanceof UUID) {
rda.setType(DmpId.Type.OTHER);
} else {
rda.setType(DmpId.Type.DOI);
}
return rda;
}
}

View File

@ -1,209 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpUserFileTransformerModel;
import eu.eudat.file.transformer.models.entitydoi.EntityDoiFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.models.user.UserFileTransformerModel;
import eu.eudat.file.transformer.rda.Dmp;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.management.InvalidApplicationException;
import java.io.IOException;
import java.util.*;
@Component
public class DmpRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(DmpRDAMapper.class);
private DatasetRDAMapper datasetRDAMapper;
private final ObjectMapper mapper;
@Autowired
public DmpRDAMapper(DatasetRDAMapper datasetRDAMapper) throws IOException {
this.datasetRDAMapper = datasetRDAMapper;
this.mapper = new ObjectMapper();
}
public Dmp toRDA(DmpFileTransformerModel dmp) throws InvalidApplicationException, JsonProcessingException {
List<ReferenceFileTransformerModel> grants = new ArrayList<>();
List<ReferenceFileTransformerModel> researchers = new ArrayList<>();
List<ReferenceFileTransformerModel> organizations = new ArrayList<>();
List<ReferenceFileTransformerModel> funders = new ArrayList<>();
List<ReferenceFileTransformerModel> projects = new ArrayList<>();
if (dmp.getDmpReferences() != null) {
grants = dmp.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Grants)).toList();
researchers = dmp.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(reference -> reference.getType().equals(ReferenceType.Researcher)).toList();
organizations = dmp.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Organizations)).toList();
funders = dmp.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(referenceFileModel -> referenceFileModel.getType().equals(ReferenceType.Funder)).toList();
projects = dmp.getDmpReferences().stream().map(DmpReferenceFileTransformerModel::getReference).filter(reference -> reference.getType().equals(ReferenceType.Project)).toList();
}
if (dmp.getDescriptions() == null || dmp.getDescriptions().isEmpty()) { //TODO
throw new IllegalArgumentException("DMP has no Datasets");
}
Map<String, Object> extraProperties;
if (dmp.getProperties() == null) {
throw new IllegalArgumentException("DMP is missing language and contact properties");
} else {
extraProperties = mapper.readValue(dmp.getProperties(), HashMap.class);
/*if (extraProperties.get("language") == null) {
throw new IllegalArgumentException("DMP must have it's language property defined");
}*/
if (extraProperties.get("contacts") == null) {
throw new IllegalArgumentException("DMP must have it's contact property defined");
}
}
Dmp rda = new Dmp();
if (dmp.getEntityDois() != null && !dmp.getEntityDois().isEmpty()) {
for(EntityDoiFileTransformerModel doi: dmp.getEntityDois()){
if(doi.getRepositoryId().equals("Zenodo")){
rda.setDmpId(DmpIdRDAMapper.toRDA(doi.getDoi()));
}
}
} else {
rda.setDmpId(DmpIdRDAMapper.toRDA(dmp.getId()));
}
if (dmp.getCreatedAt() == null) {
throw new IllegalArgumentException("DMP Created is missing");
}
if (dmp.getUpdatedAt() == null) {
throw new IllegalArgumentException("DMP Modified is missing");
}
if (dmp.getLabel() == null) {
throw new IllegalArgumentException("DMP Label is missing");
}
rda.setCreated(dmp.getCreatedAt()); //TODO
rda.setDescription(dmp.getDescription());
rda.setModified(dmp.getUpdatedAt());
rda.setTitle(dmp.getLabel());
rda.setLanguage(LanguageRDAMapper.mapLanguageIsoToRDAIso(dmp.getLanguage() != null ? dmp.getLanguage() : "en"));
if (!extraProperties.isEmpty()) {
if (extraProperties.get("ethicalIssues") != null) {
rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.fromValue(extraProperties.get("ethicalIssues").toString()));
} else {
rda.setEthicalIssuesExist(Dmp.EthicalIssuesExist.UNKNOWN);
}
if (extraProperties.get("costs") != null) {
rda.setCost(new ArrayList<>());
((List) extraProperties.get("costs")).forEach(costl -> {
try {
rda.getCost().add(CostRDAMapper.toRDA((Map)costl));
} catch (JsonProcessingException e) {
logger.error(e.getMessage(), e);
}
});
}
UUID contactId = UUID.fromString((String) ((List<Map<String, Object>>) extraProperties.get("contacts")).get(0).get("userId"));
if (contactId != null) {
UserFileTransformerModel userContact = dmp.getDmpUsers().stream().map(DmpUserFileTransformerModel::getUser)
.filter(userFileModel -> userFileModel.getId().equals(contactId))
.findFirst().orElse(null);
if (userContact != null) {
rda.setContact(ContactRDAMapper.toRDA(userContact));
}
}
}
/*UserInfo creator;
if (dmp.getCreator() != null) {
creator = dmp.getCreator();
} else {
creator = dmp.getUsers().stream().filter(userDMP -> userDMP.getRole().equals(UserDMP.UserDMPRoles.OWNER.getValue())).map(UserDMP::getUser).findFirst().orElse(new UserInfo());
}
rda.setContact(ContactRDAMapper.toRDA(creator));*/
rda.setContributor(new ArrayList<>());
if (!researchers.isEmpty()) {
rda.getContributor().addAll(researchers.stream().map(ContributorRDAMapper::toRDA).toList());
}
rda.getContributor().addAll(dmp.getDmpUsers().stream().map(ContributorRDAMapper::toRDA).toList());
rda.setDataset(dmp.getDescriptions().stream().map(dataset -> datasetRDAMapper.toRDA(dataset, rda)).toList());
if (!projects.isEmpty() && !grants.isEmpty() && !funders.isEmpty()) {
rda.setProject(List.of(ProjectRDAMapper.toRDA(projects.get(0), grants.get(0), funders.get(0))));
}
rda.setAdditionalProperty("templates", dmp.getDescriptions().stream().map(descriptionFileTransformerModel -> descriptionFileTransformerModel.getDescriptionTemplate().getId().toString()).toList());
return rda;
}
public DmpFileTransformerModel toEntity(Dmp rda, List<DescriptionTemplateFileTransformerModel> profiles) throws InvalidApplicationException, JsonProcessingException {
DmpFileTransformerModel entity = new DmpFileTransformerModel();
entity.setLabel(rda.getTitle());
/*if (rda.getDmpId().getType() == DmpId.Type.DOI) { //TODO
try {
//TODO: Find from doi = rda.getDmpId().getIdentifier()
EntityDoi doi = new EntityDoi();
List<EntityDoi> dois = new ArrayList<>();
dois.add(doi);
entity.setEntityDois(dois);
}
catch (NoResultException e) {
logger.warn("No entity doi: " + rda.getDmpId().getIdentifier() + " found in database. No dois are added to dmp.");
entity.setDois(new HashSet<>());
}
}*/
/*if (((List<String>) rda.getAdditionalProperties().get("templates")) != null && !((List<String>) rda.getAdditionalProperties().get("templates")).isEmpty() && entity.getId() != null) {
entity.setAssociatedDmps(((List<String>) rda.getAdditionalProperties().get("templates")).stream().map(x -> {
try {
return this.getProfile(x, entity.getId());
} catch (InvalidApplicationException e) {
throw new RuntimeException(e);
}
}).filter(Objects::nonNull).collect(Collectors.toSet()));
}*/
/*if (entity.getAssociatedDmps() == null) {
entity.setAssociatedDmps(new HashSet<>());
}*/
/*if (profiles != null && entity.getId() != null) {
for (String profile : profiles) {
entity.getAssociatedDmps().add(this.getProfile(profile, entity.getId()));
}
}*/
entity.setDmpReferences(new ArrayList<>());
if (rda.getContributor() != null && !rda.getContributor().isEmpty() && rda.getContributor().get(0).getContributorId() != null) {
entity.getDmpReferences().addAll(rda.getContributor().stream().filter(r -> r.getContributorId() != null).map(ContributorRDAMapper::toEntity)
.map(reference -> {
DmpReferenceFileTransformerModel dmpReference = new DmpReferenceFileTransformerModel();
dmpReference.setReference(reference);
return dmpReference;
}).toList());
}
entity.setCreatedAt(rda.getCreated());
entity.setUpdatedAt(rda.getModified());
entity.setDescription(rda.getDescription());
entity.setDescriptions(rda.getDataset().stream().map(rda1 -> datasetRDAMapper.toEntity(rda1, profiles.get(0))).toList());
if (!rda.getProject().isEmpty()) {
entity.getDmpReferences().addAll(ProjectRDAMapper.toEntity(rda.getProject().get(0)).stream()
.map(reference -> {
DmpReferenceFileTransformerModel dmpReference = new DmpReferenceFileTransformerModel();
dmpReference.setReference(reference);
return dmpReference;
}).toList());
}
Map<String, Object> extraProperties = new HashMap<>();
extraProperties.put("language", LanguageRDAMapper.mapRDAIsoToLanguageIso(rda.getLanguage()));
entity.setProperties(mapper.writeValueAsString(extraProperties));
return entity;
}
// private DmpDescriptionTemplateEntity getProfile(String descriptionTemplateId, UUID dmpId) throws InvalidApplicationException {
// return this.queryFactory.query(DmpDescriptionTemplateQuery.class).dmpIds(dmpId).descriptionTemplateIds(UUID.fromString(descriptionTemplateId)).first();
// }
}

View File

@ -1,19 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.FunderId;
import java.util.UUID;
public class FunderIdRDAMapper {
public static FunderId toRDA(Object id) {
FunderId rda = new FunderId();
rda.setIdentifier(id.toString());
if (id instanceof UUID) {
rda.setType(FunderId.Type.OTHER);
} else {
rda.setType(FunderId.Type.FUNDREF);
}
return rda;
}
}

View File

@ -1,48 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.rda.Funding;
import java.util.ArrayList;
import java.util.List;
public class FundingRDAMapper {
public static Funding toRDA(ReferenceFileTransformerModel grant, ReferenceFileTransformerModel funder) {
Funding rda = new Funding();
String referencePrefix;
String shortReference;
Integer prefixLength = 0;
if (funder.getReference() != null) {
referencePrefix = funder.getReference().split(":")[0];
prefixLength = referencePrefix.length() == funder.getReference().length() ? referencePrefix.length() - 1 : referencePrefix.length();
shortReference = funder.getReference().substring(prefixLength + 1);
rda.setFunderId(FunderIdRDAMapper.toRDA(shortReference));
} else {
rda.setFunderId(FunderIdRDAMapper.toRDA(funder.getId()));
}
if (grant.getReference() != null) {
referencePrefix = grant.getReference().split(":")[0];
prefixLength = referencePrefix.length() == grant.getReference().length() ? referencePrefix.length() - 1 : referencePrefix.length();
shortReference = grant.getReference().substring(prefixLength + 1);
rda.setGrantId(GrantIdRDAMapper.toRDA(shortReference));
} else {
rda.setGrantId(GrantIdRDAMapper.toRDA(grant.getId().toString()));
}
return rda;
}
public static List<ReferenceFileTransformerModel> toEntity(Funding rda) {
List<ReferenceFileTransformerModel> references = new ArrayList<>();
ReferenceFileTransformerModel funder = new ReferenceFileTransformerModel();
funder.setType(ReferenceType.Funder);
funder.setReference(rda.getFunderId().getIdentifier());
references.add(funder);
ReferenceFileTransformerModel grant = new ReferenceFileTransformerModel();
grant.setType(ReferenceType.Grants);
grant.setReference(rda.getGrantId().getIdentifier());
references.add(grant);
return references;
}
}

View File

@ -1,13 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.GrantId;
public class GrantIdRDAMapper {
public static GrantId toRDA(String id) {
GrantId rda = new GrantId();
rda.setIdentifier(id);
rda.setType(GrantId.Type.OTHER);
return rda;
}
}

View File

@ -1,262 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.Host;
import eu.eudat.file.transformer.rda.PidSystem;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.*;
import java.util.stream.Collectors;
public class HostRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(HostRDAMapper.class);
public static Host toRDA(List<FieldFileTransformerModel> nodes, String numbering) {
ObjectMapper mapper = new ObjectMapper();
Host rda = new Host();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution.host")).findFirst().orElse("");
if (rdaProperty.contains("host")) {
int firstDiff = MyStringUtils.getFirstDifference(numbering, node.getNumbering());
if (firstDiff == -1 || firstDiff >= 2) {
if (node.getData() == null) {
continue;
}
String rdaValue = node.getData().getValue();
if(rdaValue == null || rdaValue.isEmpty()){
continue;
}
for (ExportPropertyName propertyName: ExportPropertyName.values()) {
if (rdaProperty.contains(propertyName.getName())) {
switch (propertyName) {
case AVAILABILITY:
rda.setAvailability(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.AVAILABILITY.getName(), node.getId());
break;
case BACKUP_FREQUENCY:
rda.setBackupFrequency(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.BACKUP_FREQUENCY.getName(), node.getId());
break;
case BACKUP_TYPE:
rda.setBackupType(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.BACKUP_TYPE.getName(), node.getId());
break;
case CERTIFIED_WITH:
try {
rda.setCertifiedWith(Host.CertifiedWith.fromValue(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.CERTIFIED_WITH.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution host certified with " + rdaValue + "from semantic distribution.host.certified_with is not valid. Certified_with will not be set set.");
}
break;
case DESCRIPTION:
rda.setDescription(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.DESCRIPTION.getName(), node.getId());
break;
case GEO_LOCATION:
if (rdaValue.startsWith("{")) {
try {
rdaValue = mapper.readValue(rdaValue, Map.class).get("id").toString();
} catch (JsonProcessingException e) {
logger.warn(e.getLocalizedMessage() + ". Try to pass value as is");
}
}
try {
rda.setGeoLocation(Host.GeoLocation.fromValue(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.GEO_LOCATION.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution host geo location " + rdaValue + "from semantic distribution.host.geo_location is not valid. Geo location will not be set set.");
}
break;
case PID_SYSTEM:
try{
JsonNode valueNode = mapper.readTree(rdaValue);
Iterator<JsonNode> iter = valueNode.elements();
List<String> pList = new ArrayList<>();
while(iter.hasNext()) {
pList.add(iter.next().asText());
}
List<PidSystem> pidList;
if(pList.size() == 0){
pidList = Arrays.stream(rdaValue.replaceAll("[\\[\"\\]]","").split(","))
.map(PidSystem::fromValue).collect(Collectors.toList());
}
else{
pidList = pList.stream().map(PidSystem::fromValue).collect(Collectors.toList());
}
rda.setPidSystem(pidList);
rda.setAdditionalProperty(ImportPropertyName.PID_SYSTEM.getName(), node.getId());
}
catch (IllegalArgumentException e){
rda.setPidSystem(new ArrayList<>());
break;
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
break;
case STORAGE_TYPE:
rda.setStorageType(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.STORAGE_TYPE.getName(), node.getId());
break;
case SUPPORT_VERSIONING:
try {
rda.setSupportVersioning(Host.SupportVersioning.fromValue(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.SUPPORT_VERSIONING.getName(), node.getId());
}
catch (IllegalArgumentException e) {
logger.warn("Distribution host support versioning " + rdaValue + "from semantic distribution.host.support_versioning is not valid. Support versioning will not be set set.");
}
break;
case TITLE:
rda.setTitle(rdaValue);
rda.setAdditionalProperty(ImportPropertyName.TITLE.getName(), node.getId());
break;
case URL:
try {
rda.setUrl(URI.create(rdaValue));
rda.setAdditionalProperty(ImportPropertyName.URL.getName(), node.getId());
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
break;
}
}
}
}
}
}
if(rda.getTitle() == null || rda.getUrl() == null){
return null;
}
return rda;
}
//TODO
/*
public static List<Field> toProperties(Host rda) {
List<Field> properties = new ArrayList<>();
rda.getAdditionalProperties().entrySet().forEach(entry -> {
try {
ImportPropertyName importPropertyName = ImportPropertyName.fromString(entry.getKey());
Field field = new Field();
field.setKey(entry.getValue().toString());
switch (importPropertyName) {
case AVAILABILITY:
field.setValue(rda.getAvailability());
break;
case TITLE:
field.setValue(rda.getTitle());
break;
case DESCRIPTION:
field.setValue(rda.getDescription());
break;
case BACKUP_FREQUENCY:
field.setValue(rda.getBackupFrequency());
break;
case BACKUP_TYPE:
field.setValue(rda.getBackupType());
break;
case CERTIFIED_WITH:
field.setValue(rda.getCertifiedWith().value());
break;
case GEO_LOCATION:
field.setValue(rda.getGeoLocation().value());
break;
case PID_SYSTEM:
List<Object> pids = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
for(PidSystem pid: rda.getPidSystem()){
pids.add(pid.value());
}
if(!pids.isEmpty()){
field.setValue(mapper.writeValueAsString(pids));
}
break;
case STORAGE_TYPE:
field.setValue(rda.getStorageType());
break;
case SUPPORT_VERSIONING:
field.setValue(rda.getSupportVersioning().value());
break;
case URL:
field.setValue(rda.getUrl().toString());
break;
}
properties.add(field);
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
});
return properties;
}
*/
private enum ExportPropertyName {
AVAILABILITY("availability"),
BACKUP_FREQUENCY("backup_frequency"),
BACKUP_TYPE("backup_type"),
CERTIFIED_WITH("certified_with"),
DESCRIPTION("description"),
GEO_LOCATION("geo_location"),
PID_SYSTEM("pid_system"),
STORAGE_TYPE("storage_type"),
SUPPORT_VERSIONING("support_versioning"),
TITLE("title"),
URL("url");
private final String name;
ExportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private enum ImportPropertyName {
AVAILABILITY("availabilityId"),
BACKUP_FREQUENCY("backup_frequencyId"),
BACKUP_TYPE("backup_typeId"),
CERTIFIED_WITH("certified_withId"),
DESCRIPTION("descriptionId"),
GEO_LOCATION("geo_locationId"),
PID_SYSTEM("pid_systemId"),
STORAGE_TYPE("storage_typeId"),
SUPPORT_VERSIONING("support_versioningId"),
TITLE("titleId"),
URL("urlId");
private final String name;
ImportPropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public static ImportPropertyName fromString(String name) throws Exception {
for (ImportPropertyName importPropertyName: ImportPropertyName.values()) {
if (importPropertyName.getName().equals(name)) {
return importPropertyName;
}
}
throw new Exception("No name available");
}
}
}

View File

@ -1,30 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.file.transformer.models.tag.TagFileTransformerModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class KeywordRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(KeywordRDAMapper.class);
private static final ObjectMapper mapper = new ObjectMapper();
public static List<String> toRDA(String value) {
if (!value.isEmpty() && !value.equals("null")) {
try {
TagFileTransformerModel tag = mapper.readValue(value, TagFileTransformerModel.class);
return new ArrayList<>(Collections.singletonList(tag.getLabel()));
} catch (JsonProcessingException e) {
logger.warn(e.getMessage() + ". Attempting to parse it as a String since its a new tag.");
return new ArrayList<>(Collections.singletonList(value));
}
}
return new ArrayList<>();
}
}

View File

@ -1,135 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.License;
import eu.eudat.file.transformer.utils.json.JsonSearcher;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
public class LicenseRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(LicenseRDAMapper.class);
public static License toRDA(List<FieldFileTransformerModel> nodes) {
License rda = new License();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.distribution.license")).findFirst().orElse("");
String value = node.getData().getValue();
if(value == null || value.isEmpty()){
continue;
}
for (LicenceProperties licenceProperties: LicenceProperties.values()) {
if (rdaProperty.contains(licenceProperties.getName())) {
switch (licenceProperties) {
case LICENSE_REF:
try {
rda.setLicenseRef(URI.create(value));
} catch (IllegalArgumentException e) {
logger.warn(e.getLocalizedMessage() + ". Skipping url parsing");
}
break;
case START_DATE:
rda.setStartDate(value);
break;
}
}
}
/*if (rdaProperty.contains("license_ref")) {
rda.setLicenseRef(URI.create(value));
rda.setAdditionalProperty("license_refId", node.get("id").asText());
} else if (rdaProperty.contains("start_date")) {
rda.setStartDate(value);
rda.setAdditionalProperty("start_dateId", node.get("id").asText());
}*/
}
if(rda.getLicenseRef() == null || rda.getStartDate() == null){
return null;
}
return rda;
}
//TODO
/*public static List<Field> toProperties(List<License> rdas) {
List<Field> properties = new ArrayList<>();
rdas.forEach(rda -> {
rda.getAdditionalProperties().entrySet().forEach(entry -> {
Field field = new Field();
field.setKey(entry.getValue().toString());
switch (entry.getKey()) {
case "license_refId":
field.setValue(rda.getLicenseRef().toString());
break;
case "start_dateId":
field.setValue(rda.getStartDate());
break;
}
properties.add(field);
});
});
return properties;
}
public static List<Field> toProperties(License rda, JsonNode root) {
List<Field> properties = new ArrayList<>();
List<JsonNode> licenseNodes = JsonSearcher.findNodes(root, "schematics", "rda.dataset.distribution.license");
for (JsonNode licenseNode: licenseNodes) {
for (LicenceProperties licenceProperty: LicenceProperties.values()) {
JsonNode schematics = licenseNode.get("schematics");
if(schematics.isArray()) {
for (JsonNode schematic : schematics) {
if (schematic.asText().endsWith(licenceProperty.getName())) {
switch (licenceProperty) {
case LICENSE_REF:
if (rda.getLicenseRef() != null) {
Field field = new Field();
field.setKey(licenseNode.get("id").asText());
field.setValue(rda.getLicenseRef().toString());
properties.add(field);
}
break;
case START_DATE:
Field field = new Field();
field.setKey(licenseNode.get("id").asText());
field.setValue(rda.getStartDate());
properties.add(field);
break;
}
}
break;
}
}
}
}
return properties;
}*/
public enum LicenceProperties {
LICENSE_REF("license_ref"),
START_DATE("start_date");
private String name;
LicenceProperties(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
}

View File

@ -1,230 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.TextNode;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.rda.Metadatum;
import eu.eudat.file.transformer.utils.string.MyStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;
public class MetadataRDAMapper {
private static final Logger logger = LoggerFactory.getLogger(MetadataRDAMapper.class);
public static List<Metadatum> toRDAList(List<FieldFileTransformerModel> nodes) {
ObjectMapper mapper = new ObjectMapper();
Map<String, String> rdaMap = new HashMap<>();
List<Metadatum> rdas = new ArrayList<>();
for (FieldFileTransformerModel node: nodes) {
String rdaProperty = node.getSchematics().stream().filter(schematic -> schematic.startsWith("rda.dataset.metadata")).findFirst().orElse("");
try {
if (node.getData() == null) {
continue;
}
String stringValue = node.getData().getValue().startsWith("[") ? node.getData().getValue() : "\"" + node.getData().getValue() + "\"";
JsonNode rdaValue = mapper.readTree(stringValue);
for (PropertyName propertyName : PropertyName.values()) {
if (rdaProperty.contains(propertyName.getName())) {
switch (propertyName) {
case METADATA_STANDARD_ID:
if (rdaValue instanceof ArrayNode) {
for (Iterator<JsonNode> it = rdaValue.elements(); it.hasNext(); ) {
JsonNode data = null;
data = mapper.readTree(it.next().asText());
if (data.get("uri") != null) {
rdas.add(new Metadatum());
rdas.get(rdas.size() - 1).setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(data.get("uri").asText()));
rdas.get(rdas.size() - 1).setDescription(data.get("label").asText());
rdas.get(rdas.size() - 1).setAdditionalProperty("fieldId", node.getId());
rdas.get(rdas.size() - 1).setAdditionalProperty("valueId", data.get("id").asText());
rdaMap.put(data.get("uri").asText(), node.getNumbering());
}
}
} else if (rdaValue instanceof TextNode && rdaProperty.contains("identifier") && !rdaValue.asText().isEmpty()) {
rdas.add(new Metadatum());
rdas.get(rdas.size() - 1).setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(rdaValue.asText()));
rdas.get(rdas.size() - 1).setAdditionalProperty("identifierId", node.getId());
rdaMap.put(rdaValue.asText(), node.getNumbering());
}
break;
case DESCRIPTION:
if (!rdaValue.asText().isEmpty()) {
Metadatum rda = getRelative(rdas, rdaMap, node.getNumbering());
if (rda != null) {
rda.setDescription(rdaValue.asText());
rda.setAdditionalProperty("descriptionId", node.getId());
} else {
rdas.stream().filter(rda1 -> rda1.getDescription() == null || rda1.getDescription().isEmpty()).forEach(rda1 -> rda1.setDescription(rdaValue.asText()));
}
}
break;
case LANGUAGE:
String language = rdaValue.asText();
Metadatum.Language lang = Metadatum.Language.fromValue(language);
Metadatum rda = getRelative(rdas, rdaMap, node.getNumbering());
if (rda != null) {
rda.setLanguage(lang);
rda.setAdditionalProperty("languageId", node.getId());
} else {
rdas.forEach(rda1 -> rda1.setLanguage(lang));
}
break;
}
}
}
} catch (JsonProcessingException e) {
logger.error(e.getMessage(), e);
}
}
return rdas;
}
//TODO
/*
public static void toProperties(List<Metadatum> rdas, List<FieldFileTransformerModel> fields) {
List<Object> standardIds = new ArrayList<>();
ObjectMapper mapper = new ObjectMapper();
rdas.forEach(rda -> {
rda.getAdditionalProperties().entrySet().forEach(entry -> {
try {
switch (entry.getKey()) {
case "fieldId":
Map<String, String> metadata = toMap(rda);
standardIds.add(metadata);
Field field1 = new Field();
field1.setKey(entry.getValue().toString());
field1.setValue(mapper.writeValueAsString(standardIds));
properties.add(field1);
break;
case "identifierId":
Field field2 = new Field();
field2.setKey(entry.getValue().toString());
field2.setValue(rda.getMetadataStandardId().getIdentifier());
properties.add(field2);
break;
case "descriptionId":
Field field3 = new Field();
field3.setKey(entry.getValue().toString());
field3.setValue(rda.getDescription());
properties.add(field3);
break;
case "languageId":
if (rda.getLanguage() != null) {
Field field4 = new Field();
field4.setKey(entry.getValue().toString());
field4.setValue(rda.getLanguage().value());
properties.add(field4);
}
break;
}
}catch (Exception e) {
logger.error(e.getMessage(), e);
}
});
});
return properties;
}
*/
public static Metadatum toRDA(JsonNode node) {
Metadatum rda = new Metadatum();
String rdaProperty = "";
JsonNode schematics = node.get("schematics");
if(schematics.isArray()){
for(JsonNode schematic: schematics){
if(schematic.asText().startsWith("rda.dataset.metadata")){
rdaProperty = schematic.asText();
break;
}
}
}
JsonNode rdaValue = node.get("value");
if (rdaProperty.contains("metadata_standard_id")) {
if (rdaValue instanceof ArrayNode) {
for (Iterator<JsonNode> it = rdaValue.elements(); it.hasNext(); ) {
JsonNode data = it.next();
if (data.get("uri") != null) {
rda.setMetadataStandardId(MetadataStandardIdRDAMapper.toRDA(data.get("uri").asText()));
}
}
}
} else if (rdaProperty.contains("description")) {
rda.setDescription(rdaValue.asText());
} else if (rdaProperty.contains("language")) {
String language = rdaValue.asText();
Metadatum.Language lang = Metadatum.Language.fromValue(language);
rda.setLanguage(lang);
}
return rda;
}
private static Metadatum getRelative(List<Metadatum> rdas, Map<String, String> rdaMap, String numbering) {
String target = rdaMap.entrySet().stream().filter(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering) > 0)
.max(Comparator.comparingInt(entry -> MyStringUtils.getFirstDifference(entry.getValue(), numbering))).map(Map.Entry::getKey).orElse("");
return rdas.stream().filter(rda -> rda.getMetadataStandardId().getIdentifier().equals(target)).distinct().findFirst().orElse(null);
}
private enum PropertyName {
METADATA_STANDARD_ID("metadata_standard_id"),
DESCRIPTION("description"),
LANGUAGE("language");
private final String name;
PropertyName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
private static Map<String, String> toMap(Metadatum rda) {
Map<String, String> result = new HashMap<>();
ObjectMapper mapper = new ObjectMapper();
Map<String, Object> metadata = mapper.convertValue(rda, Map.class);
Map<String, String> additionalProperties = mapper.convertValue(metadata.get("additional_properties"), Map.class);
String id = additionalProperties.remove("valueId");
additionalProperties.clear();
additionalProperties.put("id", id);
Map<String, String> metadataStandardId = mapper.convertValue(metadata.get("metadata_standard_id"), Map.class);
String url = metadataStandardId.remove("identifier");
metadataStandardId.remove("type");
metadataStandardId.put("uri", url);
metadata.remove("additional_properties");
metadata.remove("metadata_standard_id");
Map<String, String> newMetadata = metadata.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue().toString()));
String label = newMetadata.remove("description");
newMetadata.put("label", label);
result.putAll(newMetadata);
result.putAll(metadataStandardId);
result.putAll(additionalProperties);
return result;
}
}

View File

@ -1,13 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.rda.MetadataStandardId;
public class MetadataStandardIdRDAMapper {
public static MetadataStandardId toRDA(String uri) {
MetadataStandardId rda = new MetadataStandardId();
rda.setIdentifier(uri);
rda.setType(MetadataStandardId.Type.URL);
return rda;
}
}

View File

@ -1,69 +0,0 @@
package eu.eudat.file.transformer.rda.mapper;
import eu.eudat.file.transformer.enums.ReferenceType;
import eu.eudat.file.transformer.models.reference.DefinitionFileTransformerModel;
import eu.eudat.file.transformer.models.reference.FieldFileTransformerModel;
import eu.eudat.file.transformer.models.reference.ReferenceFileTransformerModel;
import eu.eudat.file.transformer.rda.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.*;
public class ProjectRDAMapper {
private final static Logger logger = LoggerFactory.getLogger(ProjectRDAMapper.class);
public static Project toRDA(ReferenceFileTransformerModel project, ReferenceFileTransformerModel grant, ReferenceFileTransformerModel funder) {
Project rda = new Project();
try {
rda.setTitle(project.getLabel());
rda.setDescription(project.getDescription());
String startDateString = project.getDefinition().getFields().stream().filter(field -> field.getCode().equals("startDate")).map(FieldFileTransformerModel::getValue).findFirst().orElse(null);
if (startDateString != null) {
rda.setStart(startDateString);
}
String endDateString = project.getDefinition().getFields().stream().filter(field -> field.getCode().equals("endDate")).map(FieldFileTransformerModel::getValue).findFirst().orElse(null);
if (endDateString != null) {
rda.setEnd(endDateString);
}
rda.setFunding(List.of(FundingRDAMapper.toRDA(grant, funder)));
if (rda.getTitle() == null) {
throw new IllegalArgumentException("Project Title is missing");
}
} catch (Exception e) {
logger.error(e.getLocalizedMessage(), e);
}
return rda;
}
public static List<ReferenceFileTransformerModel> toEntity(Project rda) {
List<ReferenceFileTransformerModel> entities = new ArrayList<>();
ReferenceFileTransformerModel project = new ReferenceFileTransformerModel();
project.setLabel(rda.getTitle());
project.setDescription(rda.getDescription());
project.setType(ReferenceType.Project);
DefinitionFileTransformerModel projectDefinition = new DefinitionFileTransformerModel();
projectDefinition.setFields(new ArrayList<>());
if (rda.getStart() != null && !rda.getStart().isEmpty()) {
FieldFileTransformerModel startDateField = new FieldFileTransformerModel();
startDateField.setCode("startDate");
startDateField.setValue(rda.getStart());
projectDefinition.getFields().add(startDateField);
}
if (rda.getEnd() != null && !rda.getEnd().isEmpty()) {
FieldFileTransformerModel startDateField = new FieldFileTransformerModel();
startDateField.setCode("endDate");
startDateField.setValue(rda.getEnd());
projectDefinition.getFields().add(startDateField);
}
project.setDefinition(projectDefinition);
entities.add(project);
for (int i = 0; i < rda.getFunding().size(); i++) {
entities.addAll(FundingRDAMapper.toEntity(rda.getFunding().get(i)));
}
return entities;
}
}

View File

@ -0,0 +1,12 @@
package eu.eudat.file.transformer.service.descriptiontemplatesearcher;
import eu.eudat.commonmodels.models.descriptiotemplate.DescriptionTemplateModel;
import eu.eudat.commonmodels.models.descriptiotemplate.FieldModel;
import java.util.List;
public interface TemplateFieldSearcherService {
List<FieldModel> searchFieldsById(DescriptionTemplateModel template, String value);
List<FieldModel> searchFieldsBySemantics(DescriptionTemplateModel template, String value);
}

View File

@ -0,0 +1,25 @@
package eu.eudat.file.transformer.service.descriptiontemplatesearcher;
import eu.eudat.commonmodels.models.descriptiotemplate.*;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
@Component
public class TemplateFieldSearcherServiceImpl implements TemplateFieldSearcherService {
@Override
public List<FieldModel> searchFieldsById(DescriptionTemplateModel template, String value) {
if (template == null || template.getDefinition() == null) return new ArrayList<>();
return template.getDefinition().getFieldById(value);
}
@Override
public List<FieldModel> searchFieldsBySemantics(DescriptionTemplateModel template, String value) {
if (template == null || template.getDefinition() == null) return new ArrayList<>();
List<FieldModel> fieldModels = template.getDefinition().getAllField();
if (fieldModels == null) return new ArrayList<>();
return fieldModels.stream().filter(x-> x.getSchematics() != null && x.getSchematics().contains(value)).toList();
}
}

View File

@ -0,0 +1,56 @@
package eu.eudat.file.transformer.service.json;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@Scope(value = ConfigurableBeanFactory.SCOPE_SINGLETON)
public class JsonHandlingService {
private final ObjectMapper objectMapper;
public JsonHandlingService() {
this.objectMapper = new ObjectMapper();
objectMapper.registerModule(new JavaTimeModule());
}
public String toJson(Object item) throws JsonProcessingException {
if (item == null) return null;
return objectMapper.writeValueAsString(item);
}
public String toJsonSafe(Object item) {
if (item == null) return null;
try {
return objectMapper.writeValueAsString(item);
} catch (Exception ex) {
return null;
}
}
public <T> T fromJson(Class<T> type, String json) throws JsonProcessingException {
if (json == null) return null;
return objectMapper.readValue(json, type);
}
public HashMap<String, String> mapFromJson(String json) throws JsonProcessingException {
ObjectReader reader = objectMapper.readerFor(Map.class);
return reader.readValue(json);
}
public <T> T fromJsonSafe(Class<T> type, String json) {
if (json == null) return null;
try {
return objectMapper.readValue(json, type);
} catch (Exception ex) {
return null;
}
}
}

View File

@ -0,0 +1,104 @@
package eu.eudat.file.transformer.service.rdafiletransformer;
import eu.eudat.commonmodels.models.FileEnvelopeModel;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.models.misc.FileFormat;
import eu.eudat.file.transformer.model.rda.Dataset;
import eu.eudat.file.transformer.model.rda.Dmp;
import eu.eudat.file.transformer.model.rda.RDAModel;
import eu.eudat.file.transformer.model.rda.mapper.DatasetRDAMapper;
import eu.eudat.file.transformer.model.rda.mapper.DmpRDAMapper;
import eu.eudat.file.transformer.service.json.JsonHandlingService;
import eu.eudat.file.transformer.service.storage.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import javax.management.InvalidApplicationException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class RdaFileTransformerService implements FileTransformerClient {
private final DmpRDAMapper dmpRDAMapper;
private final DatasetRDAMapper descriptionRDAMapper;
private final FileStorageService storageService;
private final RdaFileTransformerServiceProperties properties;
private final JsonHandlingService jsonHandlingService;
@Autowired
public RdaFileTransformerService(DmpRDAMapper dmpRDAMapper, DatasetRDAMapper descriptionRDAMapper, FileStorageService storageService, RdaFileTransformerServiceProperties properties, JsonHandlingService jsonHandlingService) {
this.dmpRDAMapper = dmpRDAMapper;
this.descriptionRDAMapper = descriptionRDAMapper;
this.storageService = storageService;
this.properties = properties;
this.jsonHandlingService = jsonHandlingService;
}
@Override
public FileEnvelopeModel exportDmp(DmpModel dmpFileTransformerModel, String variant) throws IOException {
Dmp dmp = this.dmpRDAMapper.toRDA(dmpFileTransformerModel);
RDAModel rdaModel = new RDAModel();
rdaModel.setDmp(dmp);
String dmpJson = jsonHandlingService.toJsonSafe(rdaModel);
byte[] bytes = dmpJson.getBytes(StandardCharsets.UTF_8);
FileEnvelopeModel wordFile = new FileEnvelopeModel();
if (this.getConfiguration().isUseSharedStorage()) {
String fileRef = this.storageService.storeFile(bytes);
wordFile.setFileRef(fileRef);
} else {
wordFile.setFile(bytes);
}
wordFile.setFilename(dmpFileTransformerModel.getLabel() + ".json");
return wordFile;
}
@Override
public FileEnvelopeModel exportDescription(DescriptionModel descriptionFileTransformerModel, String format) throws InvalidApplicationException, IOException {
Dmp dmp = this.dmpRDAMapper.toRDA(descriptionFileTransformerModel.getDmp());
Map<String, Object> datasetExtraData = new HashMap<>();
datasetExtraData.put("dmp", dmp);
Dataset dataset = this.descriptionRDAMapper.toRDA(descriptionFileTransformerModel, datasetExtraData);
String dmpJson = jsonHandlingService.toJsonSafe(dataset);
byte[] bytes = dmpJson.getBytes(StandardCharsets.UTF_8);
FileEnvelopeModel wordFile = new FileEnvelopeModel();
if (this.getConfiguration().isUseSharedStorage()) {
String fileRef = this.storageService.storeFile(bytes);
wordFile.setFileRef(fileRef);
} else {
wordFile.setFile(bytes);
}
wordFile.setFilename(descriptionFileTransformerModel.getLabel() + ".json");
return wordFile;
}
@Override
public DmpModel importDmp(FileEnvelopeModel envelope) {
throw new UnsupportedOperationException("import not supported");
}
@Override
public DescriptionModel importDescription(FileEnvelopeModel envelope) {
throw new UnsupportedOperationException("import not supported");
}
@Override
public FileTransformerConfiguration getConfiguration() {
List<FileFormat> supportedFormats = List.of(new FileFormat("json", false, null));
FileTransformerConfiguration configuration = new FileTransformerConfiguration();
configuration.setFileTransformerId(this.properties.getTransformerId());
configuration.setExportVariants(supportedFormats);
configuration.setImportVariants(null);
configuration.setUseSharedStorage(this.properties.isUseSharedStorage());
return configuration;
}
}

View File

@ -0,0 +1,9 @@
package eu.eudat.file.transformer.service.rdafiletransformer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties({RdaFileTransformerServiceProperties.class})
public class RdaFileTransformerServiceConfiguration {
}

View File

@ -0,0 +1,98 @@
package eu.eudat.file.transformer.service.rdafiletransformer;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "rda-file-transformer")
public class RdaFileTransformerServiceProperties {
private String transformerId;
private boolean useSharedStorage;
private String organizationReferenceCode;
private String grantReferenceCode;
private String funderReferenceCode;
private String researcherReferenceCode;
private String licenceReferenceCode;
private String projectReferenceCode;
private String datasetReferenceCode;
private String publicationReferenceCode;
public String getTransformerId() {
return transformerId;
}
public void setTransformerId(String transformerId) {
this.transformerId = transformerId;
}
public boolean isUseSharedStorage() {
return useSharedStorage;
}
public void setUseSharedStorage(boolean useSharedStorage) {
this.useSharedStorage = useSharedStorage;
}
public String getOrganizationReferenceCode() {
return organizationReferenceCode;
}
public void setOrganizationReferenceCode(String organizationReferenceCode) {
this.organizationReferenceCode = organizationReferenceCode;
}
public String getGrantReferenceCode() {
return grantReferenceCode;
}
public void setGrantReferenceCode(String grantReferenceCode) {
this.grantReferenceCode = grantReferenceCode;
}
public String getFunderReferenceCode() {
return funderReferenceCode;
}
public void setFunderReferenceCode(String funderReferenceCode) {
this.funderReferenceCode = funderReferenceCode;
}
public String getResearcherReferenceCode() {
return researcherReferenceCode;
}
public void setResearcherReferenceCode(String researcherReferenceCode) {
this.researcherReferenceCode = researcherReferenceCode;
}
public String getProjectReferenceCode() {
return projectReferenceCode;
}
public void setProjectReferenceCode(String projectReferenceCode) {
this.projectReferenceCode = projectReferenceCode;
}
public String getLicenceReferenceCode() {
return licenceReferenceCode;
}
public void setLicenceReferenceCode(String licenceReferenceCode) {
this.licenceReferenceCode = licenceReferenceCode;
}
public String getDatasetReferenceCode() {
return datasetReferenceCode;
}
public void setDatasetReferenceCode(String datasetReferenceCode) {
this.datasetReferenceCode = datasetReferenceCode;
}
public String getPublicationReferenceCode() {
return publicationReferenceCode;
}
public void setPublicationReferenceCode(String publicationReferenceCode) {
this.publicationReferenceCode = publicationReferenceCode;
}
}

View File

@ -0,0 +1,7 @@
package eu.eudat.file.transformer.service.storage;
public interface FileStorageService {
String storeFile(byte[] data);
byte[] readFile(String fileRef);
}

View File

@ -0,0 +1,9 @@
package eu.eudat.file.transformer.service.storage;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@Configuration
@EnableConfigurationProperties(FileStorageServiceProperties.class)
public class FileStorageServiceConfiguration {
}

View File

@ -1,6 +1,5 @@
package eu.eudat.file.transformer.utils.service.storage;
package eu.eudat.file.transformer.service.storage;
import eu.eudat.file.transformer.configuration.FileStorageProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
@ -11,16 +10,17 @@ import java.nio.file.*;
import java.util.UUID;
@Service
public class FileStorageService {
private final static Logger logger = LoggerFactory.getLogger(FileStorageService.class);
public class FileStorageServiceImpl implements FileStorageService {
private final static Logger logger = LoggerFactory.getLogger(FileStorageServiceImpl.class);
private final FileStorageProperties properties;
private final FileStorageServiceProperties properties;
@Autowired
public FileStorageService(FileStorageProperties properties) {
public FileStorageServiceImpl(FileStorageServiceProperties properties) {
this.properties = properties;
}
@Override
public String storeFile(byte[] data) {
try {
String fileName = UUID.randomUUID().toString();
@ -33,6 +33,7 @@ public class FileStorageService {
return null;
}
@Override
public byte[] readFile(String fileRef) {
try (FileInputStream inputStream = new FileInputStream(properties.getTransientPath() + "/" + fileRef)) {
return inputStream.readAllBytes();

View File

@ -1,15 +1,15 @@
package eu.eudat.file.transformer.configuration;
package eu.eudat.file.transformer.service.storage;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.bind.ConstructorBinding;
@ConfigurationProperties(prefix = "file.storage")
public class FileStorageProperties {
public class FileStorageServiceProperties {
private final String temp;
private final String transientPath;
@ConstructorBinding
public FileStorageProperties(String temp, String transientPath) {
public FileStorageServiceProperties(String temp, String transientPath) {
this.temp = temp;
this.transientPath = transientPath;
}

View File

@ -1,63 +0,0 @@
package eu.eudat.file.transformer.utils.descriptionTemplate;
import eu.eudat.file.transformer.models.descriptiontemplate.DescriptionTemplateFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.FieldSetFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.PageFileTransformerModel;
import eu.eudat.file.transformer.models.descriptiontemplate.definition.SectionFileTransformerModel;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class TemplateFieldSearcher {
public static List<FieldFileTransformerModel> searchFields(DescriptionTemplateFileTransformerModel template, String key, String value) {
List<FieldFileTransformerModel> result;
List<PageFileTransformerModel> pages = template.getDefinition().getPages();
result = pages.stream().flatMap(pageFileTransformerModel -> searchFieldsFromSections(pageFileTransformerModel.getSections(), key, value).stream()).toList();
return result;
}
private static List<FieldFileTransformerModel> searchFieldsFromSections(List<SectionFileTransformerModel> sections, String key, String value) {
List<FieldFileTransformerModel> result = new ArrayList<>();
for (SectionFileTransformerModel section : sections) {
if (section.getSections() != null && !section.getSections().isEmpty()) {
result.addAll(searchFieldsFromSections(section.getSections(), key, value));
}
if (section.getFieldSets() != null && !section.getFieldSets().isEmpty()) {
List<FieldSetFileTransformerModel> fieldSets = section.getFieldSets();
for (FieldSetFileTransformerModel fieldSet : fieldSets) {
List<FieldFileTransformerModel> fields = fieldSet.getFields();
for (FieldFileTransformerModel field : fields) {
Method keyGetter = Arrays.stream(FieldFileTransformerModel.class.getDeclaredMethods()).filter(method -> method.getName().equals(makeGetter(key))).findFirst().orElse(null);
if (keyGetter != null && keyGetter.canAccess(field)) {
try {
if (keyGetter.invoke(field).equals(value) || keyGetter.invoke(field).toString().startsWith(value)) {
result.add(field);
} else if(keyGetter.getReturnType().isAssignableFrom(List.class)) {
List nodes = (List) keyGetter.invoke(field);
for (Object item : nodes) {
if (item.toString().equals(value) || item.toString().startsWith(value)) {
result.add(field);
}
}
}
} catch (IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
}
}
}
}
return result;
}
private static String makeGetter(String fieldName) {
return "get" + fieldName.substring(0, 1).toUpperCase(Locale.ROOT) + fieldName.substring(1);
}
}

View File

@ -1,81 +0,0 @@
package eu.eudat.file.transformer.utils.json;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
public class JsonSearcher {
public static List<JsonNode> findNodes(JsonNode root, String key, String value) {
return findNodes(root, key, value, false);
}
public static List<JsonNode> findNodes(JsonNode root, String key, String value, boolean parent) {
List<JsonNode> nodes = new ArrayList<>();
for (Iterator<JsonNode> it = root.elements(); it.hasNext(); ) {
JsonNode node = it.next();
int found = 0;
for (Iterator<String> iter = node.fieldNames(); iter.hasNext(); ) {
String fieldName = iter.next();
if (fieldName.equals(key)) {
if (node.get(fieldName).asText().equals(value) || node.get(fieldName).asText().startsWith(value)) {
if (parent) {
nodes.add(root);
} else {
nodes.add(node);
}
found++;
}
else if(node.get(fieldName).isArray()){
for(JsonNode item: node.get(fieldName)){
if(item.asText().equals(value) || item.asText().startsWith(value)){
if (parent) {
nodes.add(root);
} else {
nodes.add(node);
}
found++;
}
}
}
}
}
if (found == 0) {
nodes.addAll(findNodes(node, key, value, parent));
}
}
return nodes;
}
public static List<String> getParentValues(JsonNode root, String childValue, String key) {
List<String> values = new LinkedList<>();
for (Iterator<JsonNode> it = root.elements(); it.hasNext(); ) {
JsonNode node = it.next();
int found = 0;
for (Iterator<String> iter = node.fieldNames(); iter.hasNext(); ) {
String fieldName = iter.next();
if (fieldName.equals(key)) {
if (node.get(fieldName).asText().equals(childValue) || node.get(fieldName).asText().startsWith(childValue)) {
values.add(childValue);
found++;
}
}
}
if (found == 0) {
values.addAll(getParentValues(node, childValue, key));
if (!values.isEmpty() && node.has(key)) {
values.add(node.get(key).asText());
values.remove(childValue);
}
}
}
return values;
}
}

19
pom.xml
View File

@ -33,5 +33,24 @@
<module>core</module>
<module>web</module>
</modules>
<profiles>
<profile>
<id>dev</id>
<repositories>
<repository>
<id>dev</id>
<name>Dev Profile</name>
<url>${devProfileUrl}</url>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>dev</id>
<name>Dev Profile</name>
<url>${devProfileUrlDeposit}</url>
</repository>
</distributionManagement>
</profile>
</profiles>
</project>

View File

@ -1,47 +1,42 @@
package eu.eudat.file.transformer.controller;
import eu.eudat.commonmodels.models.FileEnvelopeModel;
import eu.eudat.commonmodels.models.description.DescriptionModel;
import eu.eudat.commonmodels.models.dmp.DmpModel;
import eu.eudat.file.transformer.interfaces.FileTransformerClient;
import eu.eudat.file.transformer.interfaces.FileTransformerConfiguration;
import eu.eudat.file.transformer.models.description.DescriptionFileTransformerModel;
import eu.eudat.file.transformer.models.dmp.DmpFileTransformerModel;
import eu.eudat.file.transformer.models.misc.FileEnvelope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/file")
public class FileTransformerController {
@RequestMapping("/api/file-transformer")
public class FileTransformerController implements eu.eudat.file.transformer.interfaces.FileTransformerController {
private final FileTransformerClient fileTransformerExecutor;
@Autowired
public FileTransformerController(FileTransformerClient fileTransformerExecutor) {
this.fileTransformerExecutor = fileTransformerExecutor;
this.fileTransformerExecutor = fileTransformerExecutor;
}
@PostMapping("/export/dmp")
public FileEnvelope exportDmp(@RequestBody DmpFileTransformerModel dmpDepositModel) throws Exception {
return fileTransformerExecutor.exportDmp(dmpDepositModel);
public FileEnvelopeModel exportDmp(@RequestBody DmpModel dmpDepositModel, @RequestParam(value = "format",required = false)String format) throws Exception {
return fileTransformerExecutor.exportDmp(dmpDepositModel, format);
}
@PostMapping("/export/description")
public FileEnvelope exportDescription(@RequestBody DescriptionFileTransformerModel descriptionFileTransformerModel, @RequestParam(value = "format",required = false)String format, @RequestParam(value = "descriptionId",required = false) String descriptionId) throws Exception {
return fileTransformerExecutor.exportDescription(descriptionFileTransformerModel, format);
public FileEnvelopeModel exportDescription(@RequestBody DescriptionModel descriptionModel, @RequestParam(value = "format",required = false)String format) throws Exception {
return fileTransformerExecutor.exportDescription(descriptionModel, format);
}
@PostMapping("/import/dmp")
public DmpFileTransformerModel importFileToDmp(@RequestBody FileEnvelope fileEnvelope) {
public DmpModel importFileToDmp(@RequestBody FileEnvelopeModel fileEnvelope) {
return fileTransformerExecutor.importDmp(fileEnvelope);
}
@PostMapping("/import/description")
public DescriptionFileTransformerModel importFileToDescription(@RequestBody FileEnvelope fileEnvelope) {
public DescriptionModel importFileToDescription(@RequestBody FileEnvelopeModel fileEnvelope) {
return fileTransformerExecutor.importDescription(fileEnvelope);
}
@GetMapping("/formats")
public FileTransformerConfiguration getSupportedFormats() {
return fileTransformerExecutor.getConfiguration();
}

View File

@ -7,5 +7,5 @@ spring:
optional:classpath:config/storage.yml[.yml], optional:classpath:config/storage-${spring.profiles.active}.yml[.yml], optional:file:../config/storage-${spring.profiles.active}.yml[.yml],
optional:classpath:config/security.yml[.yml], optional:classpath:config/security-${spring.profiles.active}.yml[.yml], optional:file:../config/security-${spring.profiles.active}.yml[.yml],
optional:classpath:config/cache.yml[.yml], optional:classpath:config/cache-${spring.profiles.active}.yml[.yml], optional:file:../config/cache-${spring.profiles.active}.yml[.yml],
optional:classpath:config/pdf.yml[.yml], optional:classpath:config/pdf-${spring.profiles.active}.yml[.yml], optional:file:../config/pdf-${spring.profiles.active}.yml[.yml]
optional:classpath:config/rda-file-transformer.yml[.yml], optional:classpath:config/rda-file-transformer-${spring.profiles.active}.yml[.yml], optional:file:../config/rda-file-transformer-${spring.profiles.active}.yml[.yml]

View File

@ -1,3 +0,0 @@
pdf:
converter:
url: ${PDF_CONVERTER_URL:}

View File

@ -0,0 +1,11 @@
rda-file-transformer:
transformerId: "json"
useSharedStorage: true
organizationReferenceCode: "organisations"
grantReferenceCode: "grants"
funderReferenceCode: "funders"
researcherReferenceCode: "researchers"
licenceReferenceCode: "licenses"
projectReferenceCode: "projects"
datasetReferenceCode: "datasets"
publicationReferenceCode: "publications"

View File

@ -5,16 +5,11 @@ web:
allowed-endpoints: [ health ]
idp:
api-key:
enabled: true
authorization-header: Authorization
client-id: ${IDP_APIKEY_CLIENT_ID:}
client-secret: ${IDP_APIKEY_CLIENT_SECRET:}
scope: ${IDP_APIKEY_SCOPE:}
enabled: false
resource:
token-type: JWT #| opaque
opaque:
client-id: ${IDP_OPAQUE_CLIENT_ID:}
client-secret: ${IDP_OPAQUE_CLIENT_SECRET:}
jwt:
claims: [ role, x-role ]
issuer-uri: ${IDP_ISSUER_URI:}
issuer-uri: ${IDP_ISSUER_URI:}
audiences: [ "dmp_zenodo_bridge" ]
validIssuer: ${IDP_ISSUER_URI:}

View File

@ -1,4 +1,4 @@
file:
storage:
temp: ${TEMP_PATH}
transient-path: ${TRANSIENT_PATH}
temp: ${STORAGE_PATH}/tmp
transient-path: ${STORAGE_PATH}/shared