From bb57e49056c7793d68f6251f6a58406950e95aa5 Mon Sep 17 00:00:00 2001 From: amentis Date: Tue, 18 Jun 2024 12:53:26 +0300 Subject: [PATCH] import rda implementation --- .../RdaFileTransformerService.java | 1009 ++++++++++++++++- pom.xml | 2 +- .../controller/FileTransformerController.java | 43 +- 3 files changed, 1024 insertions(+), 30 deletions(-) diff --git a/core/src/main/java/org/opencdmp/filetransformer/rda/service/rdafiletransformer/RdaFileTransformerService.java b/core/src/main/java/org/opencdmp/filetransformer/rda/service/rdafiletransformer/RdaFileTransformerService.java index 25576c8..869d7c8 100644 --- a/core/src/main/java/org/opencdmp/filetransformer/rda/service/rdafiletransformer/RdaFileTransformerService.java +++ b/core/src/main/java/org/opencdmp/filetransformer/rda/service/rdafiletransformer/RdaFileTransformerService.java @@ -1,22 +1,31 @@ package org.opencdmp.filetransformer.rda.service.rdafiletransformer; +import com.fasterxml.jackson.core.JsonProcessingException; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.logging.LoggerService; -import org.opencdmp.commonmodels.enums.ContactInfoType; -import org.opencdmp.commonmodels.enums.DmpUserRole; +import org.opencdmp.commonmodels.enums.*; import org.opencdmp.commonmodels.models.*; import org.opencdmp.commonmodels.models.description.DescriptionModel; import org.opencdmp.commonmodels.models.description.PropertyDefinitionFieldSetItemModel; import org.opencdmp.commonmodels.models.description.PropertyDefinitionFieldSetModel; import org.opencdmp.commonmodels.models.description.PropertyDefinitionModel; +import org.opencdmp.commonmodels.models.descriptiotemplate.DescriptionTemplateModel; import org.opencdmp.commonmodels.models.descriptiotemplate.FieldModel; import org.opencdmp.commonmodels.models.descriptiotemplate.FieldSetModel; +import org.opencdmp.commonmodels.models.descriptiotemplate.fielddata.ReferenceTypeDataModel; import org.opencdmp.commonmodels.models.dmp.DmpBlueprintValueModel; +import org.opencdmp.commonmodels.models.dmp.DmpContactModel; import org.opencdmp.commonmodels.models.dmp.DmpModel; +import org.opencdmp.commonmodels.models.dmp.DmpPropertiesModel; +import org.opencdmp.commonmodels.models.dmpblueprint.DmpBlueprintModel; +import org.opencdmp.commonmodels.models.dmpblueprint.ReferenceTypeFieldModel; import org.opencdmp.commonmodels.models.dmpblueprint.SectionModel; +import org.opencdmp.commonmodels.models.dmpdescriptiontemplate.DmpDescriptionTemplateModel; +import org.opencdmp.commonmodels.models.dmpreference.DmpReferenceDataModel; import org.opencdmp.commonmodels.models.dmpreference.DmpReferenceModel; import org.opencdmp.commonmodels.models.reference.ReferenceFieldModel; import org.opencdmp.commonmodels.models.reference.ReferenceModel; +import org.opencdmp.commonmodels.models.reference.ReferenceTypeModel; import org.opencdmp.filetransformer.rda.model.rda.*; import org.opencdmp.filetransformer.rda.service.descriptiontemplatesearcher.TemplateFieldSearcherService; import org.opencdmp.filetransformerbase.enums.FileTransformerEntityType; @@ -24,7 +33,7 @@ import org.opencdmp.filetransformerbase.interfaces.FileTransformerClient; import org.opencdmp.filetransformerbase.interfaces.FileTransformerConfiguration; import org.opencdmp.filetransformer.rda.service.json.JsonHandlingService; import org.opencdmp.filetransformer.rda.service.storage.FileStorageService; -import org.opencdmp.filetransformerbase.models.misc.FileFormat; +import org.opencdmp.filetransformerbase.models.misc.*; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -35,6 +44,7 @@ import java.net.URI; import java.nio.charset.StandardCharsets; import java.time.Instant; import java.util.*; +import java.util.stream.Collectors; @Service @Scope(value = ConfigurableBeanFactory.SCOPE_PROTOTYPE) @@ -171,7 +181,6 @@ public class RdaFileTransformerService implements FileTransformerClient { rda.setTitle(dmp.getLabel()); rda.setLanguage(this.buildRdaLanguage(dmp.getLanguage() != null ? dmp.getLanguage() : "en")); rda.setContributor(buildRdaContributors(researchers)); - rda.setContributor(buildRdaContributorsByUsers(dmp.getUsers())); rda.setContact(buildRdaContact(dmp)); rda.setEthicalIssuesExist(buildRdaEthicalIssuesExist(dmp)); rda.setEthicalIssuesDescription(buildRdaEthicalIssuesDescription(dmp)); @@ -199,25 +208,25 @@ public class RdaFileTransformerService implements FileTransformerClient { if (model == null) throw new MyApplicationException("Dmp is missing"); Cost cost = new Cost(); - List titleFields = this.getFieldsOfSemantic(model, SEMANTIC_COST_TITLE); + List titleFields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_COST_TITLE); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : titleFields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) cost.setTitle(valueModel.getValue()); } - List descriptionFields = this.getFieldsOfSemantic(model, SEMANTIC_COST_DESCRIPTION); + List descriptionFields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_COST_DESCRIPTION); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : descriptionFields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) cost.setDescription(valueModel.getValue()); } - List valueFields = this.getFieldsOfSemantic(model, SEMANTIC_COST_VALUE); + List valueFields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_COST_VALUE); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : valueFields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getNumberValue() != null) cost.setValue(valueModel.getNumberValue()); } - List codeFields = this.getFieldsOfSemantic(model, SEMANTIC_COST_CURRENCY_CODE); + List codeFields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_COST_CURRENCY_CODE); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : codeFields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) { @@ -287,7 +296,7 @@ public class RdaFileTransformerService implements FileTransformerClient { public Dmp.EthicalIssuesExist buildRdaEthicalIssuesExist(DmpModel model) { if (model == null) throw new MyApplicationException("Dmp is missing"); - List fields = this.getFieldsOfSemantic(model, SEMANTIC_ETHICAL_ISSUES_EXISTS); + List fields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_ETHICAL_ISSUES_EXISTS); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) return Dmp.EthicalIssuesExist.fromValue(valueModel.getValue()); @@ -297,7 +306,7 @@ public class RdaFileTransformerService implements FileTransformerClient { public URI buildRdaEthicalIssueReport(DmpModel model) { if (model == null) throw new MyApplicationException("Dmp is missing"); - List fields = this.getFieldsOfSemantic(model, SEMANTIC_ETHICAL_ISSUES_REPORT); + List fields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_ETHICAL_ISSUES_REPORT); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) return URI.create(valueModel.getValue()); @@ -307,7 +316,7 @@ public class RdaFileTransformerService implements FileTransformerClient { public String buildRdaEthicalIssuesDescription(DmpModel model) { if (model == null) throw new MyApplicationException("Dmp is missing"); - List fields = this.getFieldsOfSemantic(model, SEMANTIC_ETHICAL_ISSUES_DESCRIPTION); + List fields = this.getFieldsOfSemantic(model.getDmpBlueprint(), SEMANTIC_ETHICAL_ISSUES_DESCRIPTION); for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { DmpBlueprintValueModel valueModel = field != null ? this.getDmpBlueprintValue(model, field.getId()) : null; if (valueModel != null && valueModel.getValue() != null && !valueModel.getValue().isBlank()) return valueModel.getValue(); @@ -438,10 +447,10 @@ public class RdaFileTransformerService implements FileTransformerClient { return response; } - private List getFieldsOfSemantic(DmpModel dmp, String semanticKey){ - if (dmp == null || dmp.getDmpBlueprint() == null || dmp.getDmpBlueprint().getDefinition() == null || dmp.getDmpBlueprint().getDefinition().getSections() == null) return null; + private List getFieldsOfSemantic(DmpBlueprintModel blueprintModel, String semanticKey){ + if (blueprintModel == null || blueprintModel.getDefinition() == null || blueprintModel.getDefinition().getSections() == null) return null; List fieldModels = new ArrayList<>(); - for (SectionModel sectionModel : dmp.getDmpBlueprint().getDefinition().getSections()){ + for (SectionModel sectionModel : blueprintModel.getDefinition().getSections()){ if (sectionModel.getFields() != null){ sectionModel.getFields().stream().filter(x -> x.getSemantics() != null && x.getSemantics().contains(semanticKey)).findFirst().ifPresent(fieldModels::add); } @@ -693,7 +702,7 @@ public class RdaFileTransformerService implements FileTransformerClient { valueFound = true; } - fieldValue = this.findValueFieldBySemantic(fieldSet, propertyDefinitionFieldSetItemModel, SEMANTIC_DATASET_DISTRIBUTION_ACCESS_URL); + fieldValue = this.findValueFieldBySemantic(fieldSet, propertyDefinitionFieldSetItemModel, SEMANTIC_DATASET_DISTRIBUTION_LICENCE); if (fieldValue != null && fieldValue.getReferences() != null && !fieldValue.getReferences().isEmpty()) { item.setLicense(new ArrayList<>()); for (ReferenceModel referenceModel : fieldValue.getReferences()) { @@ -1231,17 +1240,912 @@ public class RdaFileTransformerService implements FileTransformerClient { return field != null ? propertyDefinitionFieldSetItemModel.getFields().getOrDefault(field.getId(), null) : null; } //endregion + + + //region import dmp + public DmpModel importDmp(DmpImportModel dmpImportModel) { + if (dmpImportModel.getFile() == null) throw new MyApplicationException("Not file defined"); + + byte[] bytes = null; + if (this.getConfiguration().isUseSharedStorage()) { + bytes = this.storageService.readFile(dmpImportModel.getFile().getFileRef()); + } else { + bytes = dmpImportModel.getFile().getFile(); + } + DmpModel model = new DmpModel(); + try { + RDAModel rdaModel = this.jsonHandlingService.fromJson(RDAModel.class, new String(bytes, StandardCharsets.UTF_8)); + if (rdaModel == null || rdaModel.getDmp() == null) throw new MyApplicationException("DMP is missing"); + + model.setLabel(rdaModel.getDmp().getTitle()); + model.setDescription(rdaModel.getDmp().getDescription()); + model.setAccessType(DmpAccessType.Restricted); + if (rdaModel.getDmp().getLanguage() != null && rdaModel.getDmp().getLanguage().value() != null) model.setLanguage(this.configuration.getRdaFileTransformerServiceProperties().getLanguageMap().getOrDefault(rdaModel.getDmp().getLanguage().value(), "en")); + model.setCreatedAt(rdaModel.getDmp().getCreated()); + model.setUpdatedAt(rdaModel.getDmp().getModified()); + model.setStatus(DmpStatus.Draft); +// model.setUsers(buildDmpUsersByRdaContributors(rdaModel.getDmp().getContributor())); + model.setProperties(buildDmpPropertiesModel(dmpImportModel.getBlueprintModel(), rdaModel.getDmp())); + model.setReferences(buildDmpReferences(dmpImportModel.getBlueprintModel(), rdaModel.getDmp().getProject(), rdaModel.getDmp().getContributor())); + + if (dmpImportModel.getBlueprintModel() != null) model.setDmpBlueprint(dmpImportModel.getBlueprintModel()); + + if (dmpImportModel.getDescriptions() != null && !dmpImportModel.getDescriptions().isEmpty()){ + List dmpDescriptionTemplateModels = new ArrayList<>(); + for (DescriptionImportModel descriptionImportModel: dmpImportModel.getDescriptions()) { + dmpDescriptionTemplateModels.add(this.buildDmpDescriptionTemplateModel(descriptionImportModel)); + } + model.setDescriptionTemplates(dmpDescriptionTemplateModels); + } + + if (rdaModel.getDmp().getDataset() != null && !rdaModel.getDmp().getDataset().isEmpty() && dmpImportModel.getDescriptions() != null){ + model.setDescriptions(new ArrayList<>()); + for (Dataset dataset: rdaModel.getDmp().getDataset()) { + if (dataset.getDatasetId().getIdentifier() != null) { + DescriptionImportModel descriptionImportModel = dmpImportModel.getDescriptions().stream().filter(x -> x.getId().equals(dataset.getDatasetId().getIdentifier())).findFirst().orElse(null); + if (descriptionImportModel != null) model.getDescriptions().add(this.importDescriptionWithModel(descriptionImportModel, dataset, rdaModel.getDmp())); + } + } + } + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + return model; + } + + private List buildDmpUsersByRdaContributors(List contributors) { + List models = new ArrayList<>(); + if (contributors == null) return models; + for (Contributor contributor : contributors){ + models.add(this.buildDmpUserByRdaContributor(contributor)); + } + return models; + } + + private DmpUserModel buildDmpUserByRdaContributor(Contributor contributor) { + if (contributor == null) throw new MyApplicationException("Contributor is missing"); + + DmpUserModel dmpUserModel = new DmpUserModel(); + if (contributor.getContributorId() != null && contributor.getContributorId().getIdentifier() != null) { + try { + UserModel userModel = new UserModel(); + userModel.setId(UUID.fromString(contributor.getContributorId().getIdentifier())); + dmpUserModel.setUser(userModel); + } + catch (IllegalArgumentException e) {} + } + + if (contributor.getRole() != null){ + if (contributor.getRole().equals(DmpUserRole.Owner.name())) dmpUserModel.setRole(DmpUserRole.Owner); + else if (contributor.getRole().equals(DmpUserRole.DescriptionContributor.name())) dmpUserModel.setRole(DmpUserRole.DescriptionContributor); + else if (contributor.getRole().equals(DmpUserRole.Reviewer.name())) dmpUserModel.setRole(DmpUserRole.Reviewer); + else if (contributor.getRole().equals(DmpUserRole.Viewer.name())) dmpUserModel.setRole(DmpUserRole.Viewer); + } + + return dmpUserModel; + } + + private DmpPropertiesModel buildDmpPropertiesModel(DmpBlueprintModel dmpBlueprintModel, Dmp dmp) { + DmpPropertiesModel dmpPropertiesModel = new DmpPropertiesModel(); + if (dmp == null) return dmpPropertiesModel; + + if (dmp.getContact() != null) { + dmpPropertiesModel.setContacts(new ArrayList<>()); + dmpPropertiesModel.getContacts().add(this.buildDmpContactModelByRdaContact(dmp.getContact())); + } + + dmpPropertiesModel.setDmpBlueprintValues(new ArrayList<>()); + + if (dmp.getCost() != null || !dmp.getCost().isEmpty()){ + for (Cost cost: dmp.getCost()) { + dmpPropertiesModel.getDmpBlueprintValues().addAll(buildDmpBlueprintValuesByRdaCost(dmpBlueprintModel, cost)); + } + } + + if (dmp.getEthicalIssuesExist() != null) dmpPropertiesModel.getDmpBlueprintValues().addAll(buildDmpBlueprintValuesByRdaEthicalIssuesExist(dmpBlueprintModel, dmp.getEthicalIssuesExist())); + if (dmp.getEthicalIssuesDescription() != null || !dmp.getEthicalIssuesDescription().isEmpty()) dmpPropertiesModel.getDmpBlueprintValues().addAll(buildDmpBlueprintValuesByRdaEthicalIssuesDescription(dmpBlueprintModel, dmp.getEthicalIssuesDescription())); + if (dmp.getEthicalIssuesReport() != null) dmpPropertiesModel.getDmpBlueprintValues().addAll(buildDmpBlueprintValuesByRdaEthicalIssueReport(dmpBlueprintModel, dmp.getEthicalIssuesReport().toString())); + + return dmpPropertiesModel; + } + + + public List buildDmpBlueprintValuesByRdaCost(DmpBlueprintModel dmpBlueprintModel, Cost cost) { + List dmpBlueprintValueModels = new ArrayList<>(); + + if (dmpBlueprintModel == null || cost == null) return dmpBlueprintValueModels; + + if (cost.getTitle() != null) { + List titleFields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_COST_TITLE); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : titleFields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(cost.getTitle()); + dmpBlueprintValueModels.add(valueModel); + } + } + } + + if (cost.getDescription() != null) { + List descriptionFields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_COST_DESCRIPTION); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : descriptionFields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(cost.getDescription()); + dmpBlueprintValueModels.add(valueModel); + } + } + } + + if (cost.getValue() != null) { + List valueFields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_COST_VALUE); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : valueFields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setNumberValue(cost.getValue()); + dmpBlueprintValueModels.add(valueModel); + } + } + } + + if (cost.getCurrencyCode() != null) { + List codeFields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_COST_CURRENCY_CODE); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : codeFields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(cost.getCurrencyCode().value()); + dmpBlueprintValueModels.add(valueModel); + } + } + } + + return dmpBlueprintValueModels; + } + + public List buildDmpBlueprintValuesByRdaEthicalIssuesExist(DmpBlueprintModel dmpBlueprintModel, Dmp.EthicalIssuesExist ethicalIssuesExist) { + List dmpBlueprintValueModels = new ArrayList<>(); + + if (dmpBlueprintModel == null || ethicalIssuesExist == null) return dmpBlueprintValueModels; + + List fields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_ETHICAL_ISSUES_EXISTS); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(ethicalIssuesExist.value()); + dmpBlueprintValueModels.add(valueModel); + } + } + + return dmpBlueprintValueModels; + } + + public List buildDmpBlueprintValuesByRdaEthicalIssuesDescription(DmpBlueprintModel dmpBlueprintModel, String ethicalIssuesDescription) { + List dmpBlueprintValueModels = new ArrayList<>(); + + if (dmpBlueprintModel == null || ethicalIssuesDescription == null) return dmpBlueprintValueModels; + + List fields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_ETHICAL_ISSUES_DESCRIPTION); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(ethicalIssuesDescription); + dmpBlueprintValueModels.add(valueModel); + } + } + + return dmpBlueprintValueModels; + } + + public List buildDmpBlueprintValuesByRdaEthicalIssueReport(DmpBlueprintModel dmpBlueprintModel, String ethicalIssuesReport) { + List dmpBlueprintValueModels = new ArrayList<>(); + + if (dmpBlueprintModel == null || ethicalIssuesReport == null) return dmpBlueprintValueModels; + + List fields = this.getFieldsOfSemantic(dmpBlueprintModel, SEMANTIC_ETHICAL_ISSUES_REPORT); + for(org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : fields) { + if (field != null){ + DmpBlueprintValueModel valueModel = new DmpBlueprintValueModel(); + valueModel.setFieldId(field.getId()); + valueModel.setValue(ethicalIssuesReport); + dmpBlueprintValueModels.add(valueModel); + } + } + + return dmpBlueprintValueModels; + } + + private DmpContactModel buildDmpContactModelByRdaContact(Contact contact) { + DmpContactModel dmpContactModel = new DmpContactModel(); + + if (contact == null) return dmpContactModel; + + dmpContactModel.setFirstName(contact.getName()); + dmpContactModel.setEmail(contact.getMbox()); + + return dmpContactModel; + } + + private DmpDescriptionTemplateModel buildDmpDescriptionTemplateModel(DescriptionImportModel model) { + DmpDescriptionTemplateModel persist = new DmpDescriptionTemplateModel(); + + if (model == null) return persist; + + if (model.getDescriptionTemplate() != null) persist.setDescriptionTemplateGroupId(model.getDescriptionTemplate().getGroupId()); + persist.setSectionId(model.getSectionId()); + + return persist; + } + + private List buildDmpReferences(DmpBlueprintModel dmpBlueprintModel, List projects, List contributors) { + List models = new ArrayList<>(); + if (dmpBlueprintModel == null) return models; + + if (projects != null && !projects.isEmpty()){ + for (Project project: projects) { + models.add(buildDmpReferenceModel(project.getTitle(), null, dmpBlueprintModel, this.configuration.getRdaFileTransformerServiceProperties().getProjectReferenceCode())); + + if (project.getFunding() != null && !projects.isEmpty()) { + for (Funding funding : project.getFunding()) { + if (funding.getFunderId() != null && funding.getFunderId().getType()!= null && funding.getFunderId().getType().equals(FunderId.Type.FUNDREF)) { + models.add(buildDmpReferenceModel(null, funding.getFunderId().getIdentifier(), dmpBlueprintModel, this.configuration.getRdaFileTransformerServiceProperties().getFunderReferenceCode())); + } + if (funding.getGrantId() != null) { + models.add(buildDmpReferenceModel(null, funding.getGrantId().getIdentifier(), dmpBlueprintModel, this.configuration.getRdaFileTransformerServiceProperties().getGrantReferenceCode())); + } + } + } + } + } + + if (contributors != null && !contributors.isEmpty()){ + for (Contributor contributor: contributors){ + if (contributor.getContributorId() != null && contributor.getName() != null){ + models.add(buildDmpReferenceModel(contributor.getName(), contributor.getContributorId().toString(), dmpBlueprintModel, this.configuration.getRdaFileTransformerServiceProperties().getResearcherReferenceCode())); + } + } + } + + return models; + } + + private DmpReferenceModel buildDmpReferenceModel(String label, String reference, DmpBlueprintModel dmpBlueprintModel, String code){ + DmpReferenceModel dmpReferenceModel = new DmpReferenceModel(); + ReferenceTypeFieldModel referenceTypeFieldModel = this.findBlueprintReferenceFieldIdByCode(code, dmpBlueprintModel); + if (referenceTypeFieldModel != null){ + dmpReferenceModel.setData(this.buildDmpReferenceDataModel(referenceTypeFieldModel.getId())); + dmpReferenceModel.setReference(this.buildReference(label, reference, referenceTypeFieldModel.getReferenceType())); + } + + return dmpReferenceModel; + } + + private DmpReferenceDataModel buildDmpReferenceDataModel(UUID blueprintFieldId) { + DmpReferenceDataModel dmpReferenceDataModel = new DmpReferenceDataModel(); + + if (blueprintFieldId == null) return dmpReferenceDataModel; + dmpReferenceDataModel.setBlueprintFieldId(blueprintFieldId); + + return dmpReferenceDataModel; + } + + private ReferenceModel buildReference(String label, String reference, ReferenceTypeModel referenceTypeModel) { + ReferenceModel referenceModel = new ReferenceModel(); + + referenceModel.setLabel(label); + referenceModel.setReference(reference); + referenceModel.setType(referenceTypeModel); + + return referenceModel; + } + + private ReferenceTypeFieldModel findBlueprintReferenceFieldIdByCode(String code, DmpBlueprintModel dmpBlueprintModel){ + if (dmpBlueprintModel != null && dmpBlueprintModel.getDefinition() != null && dmpBlueprintModel.getDefinition().getSections() != null) { + List sections = dmpBlueprintModel.getDefinition().getSections(); + if (sections != null){ + for (SectionModel section : dmpBlueprintModel.getDefinition().getSections()) { + if (section.getFields() != null){ + for (org.opencdmp.commonmodels.models.dmpblueprint.FieldModel field : section.getFields()) { + if (field.getCategory().equals(DmpBlueprintFieldCategory.ReferenceType)){ + ReferenceTypeFieldModel referenceField = (ReferenceTypeFieldModel) field; + if (referenceField != null && referenceField.getReferenceType() != null && referenceField.getReferenceType().getCode().equals(code)){ + return referenceField; + } + } + } + } + } + } + } + return null; + } + //endregion - + + //region import description + @Override - public DmpModel importDmp(FileEnvelopeModel envelope) { + public DescriptionModel importDescription(DescriptionImportModel descriptionImportModel) { throw new UnsupportedOperationException("import not supported"); } - @Override - public DescriptionModel importDescription(FileEnvelopeModel envelope) { - throw new UnsupportedOperationException("import not supported"); + private DescriptionModel importDescriptionWithModel(DescriptionImportModel descriptionImportModel, Dataset dataset, Dmp dmp) { + if (descriptionImportModel == null) throw new MyApplicationException("description import is missing"); + if (dataset == null) throw new MyApplicationException("dataset is missing"); + + DescriptionModel model = new DescriptionModel(); + + model.setLabel(dataset.getTitle()); + model.setDescription(dataset.getDescription()); + model.setDescriptionTemplate(descriptionImportModel.getDescriptionTemplate()); + model.setSectionId(descriptionImportModel.getSectionId()); + model.setStatus(DescriptionStatus.Draft); + model.setTags(dataset.getKeyword()); + model.setProperties(this.buildPropertyDefinitionModel(descriptionImportModel.getDescriptionTemplate(), dataset, dmp)); + + return model; + } + + private PropertyDefinitionModel buildPropertyDefinitionModel(DescriptionTemplateModel descriptionTemplateModel, Dataset dataset, Dmp dmp) { + PropertyDefinitionModel model = new PropertyDefinitionModel(); + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + Map fieldSets = new HashMap<>(); + + if (dataset.getPersonalData() != null) { + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dataset.getPersonalData().value(), null, SEMANTIC_DATASET_PERSONAL_DATA)); + } + if (dataset.getSensitiveData() != null) { + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dataset.getSensitiveData().value(), null, SEMANTIC_DATASET_SENSITIVE_DATA)); + } + if (dataset.getLanguage() != null && dataset.getLanguage().value() != null){ + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dataset.getLanguage().value(), null, SEMANTIC_DATASET_LANGUAGE)); + } + if (dataset.getType() != null && !dataset.getType().isEmpty()){ + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dataset.getType(), null, SEMANTIC_DATASET_TYPE)); + } + if (dataset.getIssued() != null && !dataset.getIssued().isEmpty()){ + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, null, Instant.parse(dataset.getIssued()), SEMANTIC_DATASET_ISSUED)); + } + if (dataset.getKeyword() != null && !dataset.getKeyword().isEmpty()){ + fieldSets.putAll(this.buildFieldByRdaListValue(descriptionTemplateModel, dataset.getKeyword(), SEMANTIC_DATASET_KEYWORD)); + } + if (dataset.getPreservationStatement() != null){ + fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dataset.getPreservationStatement(), null, SEMANTIC_DATASET_PRESERVATION_STATEMENT)); + } + if (dataset.getDataQualityAssurance() != null && !dataset.getDataQualityAssurance().isEmpty()){ + fieldSets.putAll(this.buildFieldByRdaListValue(descriptionTemplateModel, dataset.getDataQualityAssurance(), SEMANTIC_DATASET_DATA_QUALITY_ASSURANCE)); + } + if (dataset.getMetadata() != null) fieldSets.putAll(this.buildFieldByRdaMetadata(descriptionTemplateModel, dataset.getMetadata())); + if (dataset.getSecurityAndPrivacy() != null) fieldSets.putAll(this.buildFieldByRdaSecurityAndPrivacy(descriptionTemplateModel, dataset.getSecurityAndPrivacy())); + if (dataset.getTechnicalResource() != null) fieldSets.putAll(this.buildFieldByRdaTechnicalResource(descriptionTemplateModel, dataset.getTechnicalResource())); + if (dmp.getCost() != null) fieldSets.putAll(this.buildFieldByRdaCost(descriptionTemplateModel, dmp.getCost())); + if (dmp.getContributor() != null) fieldSets.putAll(this.buildFieldByRdaContributor(descriptionTemplateModel, dmp.getContributor())); + if (dataset.getDistribution() != null) fieldSets.putAll(this.buildFieldByRdaDistribution(descriptionTemplateModel, dataset.getDistribution())); + if (dmp.getEthicalIssuesExist() != null) fieldSets.putAll(this.buildFieldByRdaSingleValue(descriptionTemplateModel, dmp.getEthicalIssuesExist().value(), null, SEMANTIC_ETHICAL_ISSUES_EXISTS)); + + model.setFieldSets(fieldSets); + return model; + } + + private Map buildFieldByRdaSingleValue(DescriptionTemplateModel descriptionTemplateModel, String value, Instant dateValue, String semantic){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(semantic)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()) { + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (value != null && dateValue == null) { + fieldModel.setTextValue(value); + valueFound = true; + } + if (value == null && dateValue != null) { + fieldModel.setDateValue(dateValue); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaMetadata(DescriptionTemplateModel descriptionTemplateModel, List metadatas){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (metadatas == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_DATASET_METADATA_DESCRIPTION, SEMANTIC_DATASET_METADATA_LANGUAGE, + SEMANTIC_DATASET_METADATA_STANDARD_ID_IDENTIFIER, SEMANTIC_DATASET_METADATA_STANDARD_ID_TYPE)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (Metadatum metadata: metadatas) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()) { + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (metadata.getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_METADATA_DESCRIPTION)){ + fieldModel.setTextValue(metadata.getDescription()); + valueFound = true; + } + if (metadata.getLanguage() != null && metadata.getLanguage().value() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_METADATA_LANGUAGE)){ + fieldModel.setTextValue(metadata.getLanguage().value()); + valueFound = true; + } + if (metadata.getMetadataStandardId() != null && metadata.getMetadataStandardId().getIdentifier() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_METADATA_STANDARD_ID_IDENTIFIER)){ + fieldModel.setTextValue(metadata.getMetadataStandardId().getIdentifier()); + valueFound = true; + } + if (metadata.getMetadataStandardId() != null && metadata.getMetadataStandardId().getType() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_METADATA_STANDARD_ID_TYPE)){ + fieldModel.setTextValue(metadata.getMetadataStandardId().getType().value()); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaSecurityAndPrivacy(DescriptionTemplateModel descriptionTemplateModel, List securityAndPrivacies){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (securityAndPrivacies == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_DATASET_SECURITY_AND_PRIVACY_TITLE, SEMANTIC_DATASET_SECURITY_AND_PRIVACY_DESCRIPTION)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (SecurityAndPrivacy securityAndPrivacy: securityAndPrivacies) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()) { + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (securityAndPrivacy.getTitle() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_SECURITY_AND_PRIVACY_TITLE)){ + fieldModel.setTextValue(securityAndPrivacy.getTitle()); + valueFound = true; + } + if (securityAndPrivacy.getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_SECURITY_AND_PRIVACY_DESCRIPTION)){ + fieldModel.setTextValue(securityAndPrivacy.getDescription()); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaTechnicalResource(DescriptionTemplateModel descriptionTemplateModel, List technicalResources){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (technicalResources == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_DATASET_TECHNICAL_RESOURCE_NAME, SEMANTIC_DATASET_TECHNICAL_RESOURCE_DESCRIPTION)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (TechnicalResource technicalResource: technicalResources) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()) { + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (technicalResource.getName() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_TECHNICAL_RESOURCE_NAME)){ + fieldModel.setTextValue(technicalResource.getName()); + valueFound = true; + } + if (technicalResource.getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_TECHNICAL_RESOURCE_DESCRIPTION)){ + fieldModel.setTextValue(technicalResource.getDescription()); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaCost(DescriptionTemplateModel descriptionTemplateModel, List costs){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (costs == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_COST_CURRENCY_CODE, SEMANTIC_COST_TITLE, + SEMANTIC_COST_VALUE, SEMANTIC_COST_DESCRIPTION)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (Cost cost: costs) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()) { + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (cost.getCurrencyCode() != null && templateFieldModel.getSemantics().contains(SEMANTIC_COST_CURRENCY_CODE)){ + fieldModel.setTextValue(cost.getCurrencyCode().value()); + valueFound = true; + } + if (cost.getTitle() != null && templateFieldModel.getSemantics().contains(SEMANTIC_COST_TITLE)){ + fieldModel.setTextValue(cost.getTitle()); + valueFound = true; + } + if (cost.getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_COST_DESCRIPTION)){ + fieldModel.setTextValue(cost.getDescription()); + valueFound = true; + } + if (cost.getValue() != null && templateFieldModel.getSemantics().contains(SEMANTIC_COST_VALUE)){ + fieldModel.setTextValue(cost.getValue().toString()); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaContributor(DescriptionTemplateModel descriptionTemplateModel, List contributors){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (contributors == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_DATASET_CONTRIBUTOR)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (Contributor contributor: contributors) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()){ + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (contributor.getContributorId() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_CONTRIBUTOR)){ + ReferenceModel referenceModel = new ReferenceModel(); + referenceModel.setReference(contributor.getContributorId().toString()); + referenceModel.setLabel(contributor.getName()); + ReferenceTypeDataModel referenceTypeDataModel = (ReferenceTypeDataModel) templateFieldModel.getData(); + if (referenceTypeDataModel != null) referenceModel.setType(referenceTypeDataModel.getReferenceType()); + fieldModel.setReferences(new ArrayList<>()); + fieldModel.getReferences().add(referenceModel); + valueFound = true; + } + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaDistribution(DescriptionTemplateModel descriptionTemplateModel, List distributions){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + if (distributions == null) return fieldSetModelMap; + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(SEMANTIC_DATASET_DISTRIBUTION_DESCRIPTION, SEMANTIC_DATASET_DISTRIBUTION_TITLE, + SEMANTIC_DATASET_DISTRIBUTION_DATA_ACCESS, SEMANTIC_DATASET_DISTRIBUTION_BYTE_SIZE, SEMANTIC_DATASET_DISTRIBUTION_ACCESS_URL, SEMANTIC_DATASET_DISTRIBUTION_AVAILABLE_UTIL, SEMANTIC_DATASET_DISTRIBUTION_DOWNLOAD_URL, + SEMANTIC_DATASET_DISTRIBUTION_LICENCE, SEMANTIC_DATASET_DISTRIBUTION_HOST_AVAILABILITY, SEMANTIC_DATASET_DISTRIBUTION_HOST_BACKUP_FREQUENCY, SEMANTIC_DATASET_DISTRIBUTION_HOST_BACKUP_TYPE, + SEMANTIC_DATASET_DISTRIBUTION_HOST_CERTIFIED_WITH, SEMANTIC_DATASET_DISTRIBUTION_HOST_DESCRIPTION, SEMANTIC_DATASET_DISTRIBUTION_HOST_PID_SYSTEM, SEMANTIC_DATASET_DISTRIBUTION_HOST_STORAGE_TYPE, + SEMANTIC_DATASET_DISTRIBUTION_HOST_TITLE, SEMANTIC_DATASET_DISTRIBUTION_HOST_URL, SEMANTIC_DATASET_DISTRIBUTION_HOST_GEO_LOCATION, SEMANTIC_DATASET_DISTRIBUTION_HOST_SUPPORT_VERSIONING)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (Distribution distribution: distributions) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + if (templateFieldModel.getSemantics() != null && !templateFieldModel.getSemantics().isEmpty()){ + boolean valueFound = false; + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (distribution.getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_DESCRIPTION)){ + fieldModel.setTextValue(distribution.getDescription()); + valueFound = true; + } + if (distribution.getTitle() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_TITLE)){ + fieldModel.setTextValue(distribution.getTitle()); + valueFound = true; + } + if (distribution.getDataAccess() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_DATA_ACCESS)){ + fieldModel.setTextValue(distribution.getDataAccess().value()); + valueFound = true; + } + if (distribution.getByteSize() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_BYTE_SIZE)){ + fieldModel.setTextValue(distribution.getByteSize().toString()); + valueFound = true; + } + if (distribution.getAccessUrl() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_ACCESS_URL)){ + fieldModel.setTextValue(distribution.getAccessUrl()); + valueFound = true; + } + if (distribution.getAvailableUntil() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_AVAILABLE_UTIL)){ + fieldModel.setDateValue(Instant.parse(distribution.getAvailableUntil())); + valueFound = true; + } + if (distribution.getDownloadUrl() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_DOWNLOAD_URL)){ + fieldModel.setTextValue(distribution.getDownloadUrl().toString()); + valueFound = true; + } + if (distribution.getLicense() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_LICENCE)){ + for (License license: distribution.getLicense()) { + ReferenceModel referenceModel = new ReferenceModel(); + referenceModel.setReference(license.getLicenseRef().toString()); + ReferenceTypeDataModel referenceTypeDataModel = (ReferenceTypeDataModel) templateFieldModel.getData(); + if (referenceTypeDataModel != null) referenceModel.setType(referenceTypeDataModel.getReferenceType()); + fieldModel.setReferences(new ArrayList<>()); + fieldModel.getReferences().add(referenceModel); + } + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getAvailability() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_AVAILABILITY)){ + fieldModel.setTextValue(distribution.getHost().getAvailability()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getBackupFrequency() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_BACKUP_FREQUENCY)){ + fieldModel.setTextValue(distribution.getHost().getBackupFrequency()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getBackupType() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_BACKUP_TYPE)){ + fieldModel.setTextValue(distribution.getHost().getBackupType()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getCertifiedWith() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_CERTIFIED_WITH)){ + fieldModel.setTextValue(distribution.getHost().getCertifiedWith().value()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getDescription() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_DESCRIPTION)){ + fieldModel.setTextValue(distribution.getHost().getDescription()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getPidSystem() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_PID_SYSTEM)){ + fieldModel.setTextListValue(distribution.getHost().getPidSystem().stream().map(x -> x.value()).collect(Collectors.toList())); + fieldModel.setTextValue(distribution.getHost().getPidSystem().stream().map(x -> x.value()).findFirst().orElse(null)); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getStorageType() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_STORAGE_TYPE)){ + fieldModel.setTextValue(distribution.getHost().getStorageType()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getTitle() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_TITLE)){ + fieldModel.setTextValue(distribution.getHost().getTitle()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getUrl() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_URL)){ + fieldModel.setTextValue(distribution.getHost().getUrl().toString()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getGeoLocation() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_GEO_LOCATION)){ + fieldModel.setTextValue(distribution.getHost().getGeoLocation().value()); + valueFound = true; + } + if (distribution.getFormat() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_FORMAT)){ + fieldModel.setTextValue(distribution.getFormat().getFirst()); + fieldModel.setTextListValue(distribution.getFormat()); + valueFound = true; + } + if (distribution.getHost() != null && distribution.getHost().getSupportVersioning() != null && templateFieldModel.getSemantics().contains(SEMANTIC_DATASET_DISTRIBUTION_HOST_SUPPORT_VERSIONING)){ + fieldModel.setTextValue(distribution.getHost().getSupportVersioning().value()); + valueFound = true; + } + + if (valueFound) fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; + } + + private Map buildFieldByRdaListValue(DescriptionTemplateModel descriptionTemplateModel, List values, String semantic){ + Map fieldSetModelMap = new HashMap<>(); + + if (descriptionTemplateModel == null) throw new MyApplicationException("description template is missing"); + + List fieldSetsWithSemantics = this.templateFieldSearcherService.searchFieldSetsBySemantics(descriptionTemplateModel, List.of(semantic)); + if (!fieldSetsWithSemantics.isEmpty()){ + for (String value: values) { + for (FieldSetModel templateFieldSetModel: fieldSetsWithSemantics) { + PropertyDefinitionFieldSetModel propertyDefinitionFieldSetModel = fieldSetModelMap.getOrDefault(templateFieldSetModel.getId(), null); + if(propertyDefinitionFieldSetModel == null) { + propertyDefinitionFieldSetModel = new PropertyDefinitionFieldSetModel(); + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + + PropertyDefinitionFieldSetItemModel propertyDefinitionFieldSetItemModel = new PropertyDefinitionFieldSetItemModel(); + Map fieldsMap = new HashMap<>(); + for (FieldModel templateFieldModel: templateFieldSetModel.getFields()) { + org.opencdmp.commonmodels.models.description.FieldModel fieldModel = new org.opencdmp.commonmodels.models.description.FieldModel(); + fieldModel.setId(templateFieldModel.getId()); + if (value != null) { + fieldModel.setTextValue(value); + fieldsMap.put(templateFieldModel.getId(), fieldModel); + } + } + if (propertyDefinitionFieldSetModel.getItems() == null){ + propertyDefinitionFieldSetModel.setItems(new ArrayList<>()); + } + propertyDefinitionFieldSetItemModel.setFields(fieldsMap); + propertyDefinitionFieldSetItemModel.setOrdinal(propertyDefinitionFieldSetModel.getItems().size()); + propertyDefinitionFieldSetModel.getItems().add(propertyDefinitionFieldSetItemModel); + + fieldSetModelMap.put(templateFieldSetModel.getId(), propertyDefinitionFieldSetModel); + } + } + + } + + return fieldSetModelMap; } @Override @@ -1255,4 +2159,69 @@ public class RdaFileTransformerService implements FileTransformerClient { configuration.setUseSharedStorage(this.configuration.getRdaFileTransformerServiceProperties().isUseSharedStorage()); return configuration; } + + //endregion + + //region preprocessing + @Override + public PreprocessingDmpModel preprocessingDmp(FileEnvelopeModel fileEnvelopeModel) { + + PreprocessingDmpModel preprocessingDmpModel = new PreprocessingDmpModel(); + byte[] bytes = null; + if (this.getConfiguration().isUseSharedStorage()) { + bytes = this.storageService.readFile(fileEnvelopeModel.getFileRef()); + } else { + bytes = fileEnvelopeModel.getFile(); + } + try { + RDAModel rdaModel = this.jsonHandlingService.fromJson(RDAModel.class, new String(bytes, StandardCharsets.UTF_8)); + if (rdaModel == null || rdaModel.getDmp() == null) throw new MyApplicationException("DMP is missing"); + + preprocessingDmpModel.setLabel(rdaModel.getDmp().getTitle()); + + if (rdaModel.getDmp().getDataset() != null){ + List preprocessingDescriptions = new ArrayList<>(); + for (Dataset dataset: rdaModel.getDmp().getDataset()) { + preprocessingDescriptions.add(this.preprocessingDescriptionFromDataset(dataset)); + } + preprocessingDmpModel.setPreprocessingDescriptionModels(preprocessingDescriptions); + } + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + return preprocessingDmpModel; + } + + @Override + public PreprocessingDescriptionModel preprocessingDescription(FileEnvelopeModel fileEnvelopeModel) { + + byte[] bytes = null; + if (this.getConfiguration().isUseSharedStorage()) { + bytes = this.storageService.readFile(fileEnvelopeModel.getFileRef()); + } else { + bytes = fileEnvelopeModel.getFile(); + } + try { + Dataset dataset = this.jsonHandlingService.fromJson(Dataset.class, new String(bytes, StandardCharsets.UTF_8)); + if (dataset == null) throw new MyApplicationException("description is missing"); + return this.preprocessingDescriptionFromDataset(dataset); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + + } + + public PreprocessingDescriptionModel preprocessingDescriptionFromDataset(Dataset dataset) { + PreprocessingDescriptionModel preprocessingDescriptionModel = new PreprocessingDescriptionModel(); + + if (dataset == null) return preprocessingDescriptionModel; + + preprocessingDescriptionModel.setLabel(dataset.getTitle()); + if (dataset.getDatasetId() != null) preprocessingDescriptionModel.setId(dataset.getDatasetId().getIdentifier()); + + return preprocessingDescriptionModel; + } + + //endregion } diff --git a/pom.xml b/pom.xml index 396bcee..15ed728 100644 --- a/pom.xml +++ b/pom.xml @@ -32,7 +32,7 @@ org.opencdmp file-transformer-base - 0.0.22 + 0.0.23 gr.cite diff --git a/web/src/main/java/org/opencdmp/filetransformer/rda/controller/FileTransformerController.java b/web/src/main/java/org/opencdmp/filetransformer/rda/controller/FileTransformerController.java index 7752784..08107b8 100644 --- a/web/src/main/java/org/opencdmp/filetransformer/rda/controller/FileTransformerController.java +++ b/web/src/main/java/org/opencdmp/filetransformer/rda/controller/FileTransformerController.java @@ -9,6 +9,10 @@ import org.opencdmp.commonmodels.models.dmp.DmpModel; import org.opencdmp.filetransformer.rda.audit.AuditableAction; import org.opencdmp.filetransformerbase.interfaces.FileTransformerClient; import org.opencdmp.filetransformerbase.interfaces.FileTransformerConfiguration; +import org.opencdmp.filetransformerbase.models.misc.DescriptionImportModel; +import org.opencdmp.filetransformerbase.models.misc.DmpImportModel; +import org.opencdmp.filetransformerbase.models.misc.PreprocessingDescriptionModel; +import org.opencdmp.filetransformerbase.models.misc.PreprocessingDmpModel; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; @@ -55,25 +59,46 @@ public class FileTransformerController implements org.opencdmp.filetransformerba return model; } - public DmpModel importFileToDmp(@RequestBody FileEnvelopeModel fileEnvelope) { + public PreprocessingDmpModel preprocessingDmp(FileEnvelopeModel fileEnvelopeModel) { + logger.debug(new MapLogEntry("preprocessingDmp " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", fileEnvelopeModel)); - logger.debug(new MapLogEntry("importFileToDmp " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", fileEnvelope)); - - DmpModel model = fileTransformerExecutor.importDmp(fileEnvelope); + PreprocessingDmpModel model = fileTransformerExecutor.preprocessingDmp(fileEnvelopeModel); this.auditService.track(AuditableAction.FileTransformer_ImportFileToDmp, Map.ofEntries( - new AbstractMap.SimpleEntry("fileEnvelope", fileEnvelope) + new AbstractMap.SimpleEntry("fileEnvelope", fileEnvelopeModel) )); return model; } - public DescriptionModel importFileToDescription(@RequestBody FileEnvelopeModel fileEnvelope) { - logger.debug(new MapLogEntry("importFileToDescription " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", fileEnvelope)); + public DmpModel importFileToDmp(@RequestBody DmpImportModel dmpImportModel) { + logger.debug(new MapLogEntry("importFileToDmp " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", dmpImportModel.getFile())); - DescriptionModel model = fileTransformerExecutor.importDescription(fileEnvelope); + DmpModel model = fileTransformerExecutor.importDmp(dmpImportModel); + + this.auditService.track(AuditableAction.FileTransformer_ImportFileToDmp, Map.ofEntries( + new AbstractMap.SimpleEntry("fileEnvelope", dmpImportModel.getFile()) + )); + return model; + } + + public PreprocessingDescriptionModel preprocessingDescription(FileEnvelopeModel fileEnvelopeModel) { + logger.debug(new MapLogEntry("preprocessingDescription " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", fileEnvelopeModel)); + + PreprocessingDescriptionModel model = fileTransformerExecutor.preprocessingDescription(fileEnvelopeModel); + + this.auditService.track(AuditableAction.FileTransformer_ImportFileToDmp, Map.ofEntries( + new AbstractMap.SimpleEntry("fileEnvelope", fileEnvelopeModel) + )); + return model; + } + + public DescriptionModel importFileToDescription(@RequestBody DescriptionImportModel descriptionImportModel) { + logger.debug(new MapLogEntry("importFileToDescription " + FileEnvelopeModel.class.getSimpleName()).And("fileEnvelope", descriptionImportModel.getFile())); + + DescriptionModel model = fileTransformerExecutor.importDescription(descriptionImportModel); this.auditService.track(AuditableAction.FileTransformer_ImportFileToDescription, Map.ofEntries( - new AbstractMap.SimpleEntry("importFileToDescription ", fileEnvelope) + new AbstractMap.SimpleEntry("importFileToDescription ", descriptionImportModel.getFile()) )); return model; }