From 112c09d5ab354228cf8c298685deefa462258646 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Thu, 13 Jun 2024 11:24:04 +0300 Subject: [PATCH 1/2] dmp import changes --- .../opencdmp/service/dmp/DmpServiceImpl.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java index af3b863ae..1094124a9 100644 --- a/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/dmp/DmpServiceImpl.java @@ -92,6 +92,7 @@ import org.opencdmp.model.referencetype.ReferenceType; import org.opencdmp.query.*; import org.opencdmp.service.actionconfirmation.ActionConfirmationService; import org.opencdmp.service.description.DescriptionService; +import org.opencdmp.service.descriptiontemplate.DescriptionTemplateService; import org.opencdmp.service.dmpblueprint.DmpBlueprintService; import org.opencdmp.service.elastic.ElasticService; import org.opencdmp.service.filetransformer.FileTransformerService; @@ -164,6 +165,7 @@ public class DmpServiceImpl implements DmpService { private final gr.cite.tools.validation.ValidatorFactory validatorFactory; private final ElasticService elasticService; + private final DescriptionTemplateService descriptionTemplateService; private final AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler; @@ -193,7 +195,7 @@ public class DmpServiceImpl implements DmpService { ActionConfirmationService actionConfirmationService, FileTransformerService fileTransformerService, ValidatorFactory validatorFactory, - ElasticService elasticService, + ElasticService elasticService, DescriptionTemplateService descriptionTemplateService, AnnotationEntityTouchedIntegrationEventHandler annotationEntityTouchedIntegrationEventHandler, AnnotationEntityRemovalIntegrationEventHandler annotationEntityRemovalIntegrationEventHandler, AuthorizationContentResolver authorizationContentResolver, TenantScope tenantScope, ResponseUtilsService responseUtilsService, DmpBlueprintService dmpBlueprintService) { this.entityManager = entityManager; this.authorizationService = authorizationService; @@ -214,7 +216,8 @@ public class DmpServiceImpl implements DmpService { this.actionConfirmationService = actionConfirmationService; this.validatorFactory = validatorFactory; this.elasticService = elasticService; - this.annotationEntityTouchedIntegrationEventHandler = annotationEntityTouchedIntegrationEventHandler; + this.descriptionTemplateService = descriptionTemplateService; + this.annotationEntityTouchedIntegrationEventHandler = annotationEntityTouchedIntegrationEventHandler; this.annotationEntityRemovalIntegrationEventHandler = annotationEntityRemovalIntegrationEventHandler; this.authorizationContentResolver = authorizationContentResolver; this.tenantScope = tenantScope; @@ -1792,14 +1795,13 @@ public class DmpServiceImpl implements DmpService { logger.debug(new MapLogEntry("import data").And("bytes", bytes).And("label", label).And("fields", fields)); this.authorizationService.authorizeForce(Permission.NewDmp); + DmpImportExport dmpXml = this.xmlHandlingService.fromXml(DmpImportExport.class, new String(bytes, StandardCharsets.UTF_8)); + + this.xmlEnsureDescriptionTemplates(dmpXml); DmpPersist persist = new DmpPersist(); - persist.setLabel(label); persist.setStatus(DmpStatus.Draft); - - DmpImportExport dmpXml = this.xmlHandlingService.fromXml(DmpImportExport.class, new String(bytes, StandardCharsets.UTF_8)); - persist.setDescription(dmpXml.getDescription()); persist.setAccessType(dmpXml.getAccess()); persist.setLanguage(dmpXml.getLanguage()); @@ -1814,8 +1816,6 @@ public class DmpServiceImpl implements DmpService { if (dmp == null) throw new MyApplicationException("Error creating dmp"); if (!this.conventionService.isListNullOrEmpty(dmpXml.getDescriptions())){ - - for (DescriptionImportExport description: dmpXml.getDescriptions()){ this.descriptionService.importXml(description, dmp.getId(), fields != null ? fields.extractPrefixed(this.conventionService.asPrefix(Dmp._description)) : null); } @@ -1824,6 +1824,20 @@ public class DmpServiceImpl implements DmpService { return dmp; } + private void xmlEnsureDescriptionTemplates(DmpImportExport dmpImportExport) throws JAXBException, InvalidApplicationException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException { + if (!this.conventionService.isListNullOrEmpty(dmpImportExport.getDescriptions())){ + for (DescriptionImportExport descriptionXml: dmpImportExport.getDescriptions()) { + if (descriptionXml.getDescriptionTemplate() != null) { + org.opencdmp.data.DescriptionTemplateEntity descriptionTemplateEntity = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().ids(descriptionXml.getDescriptionTemplate().getId()).first(); + if (descriptionTemplateEntity == null) descriptionTemplateEntity = this.queryFactory.query(DescriptionTemplateQuery.class).disableTracking().groupIds(descriptionXml.getDescriptionTemplate().getGroupId()).versionStatuses(DescriptionTemplateVersionStatus.Current).isActive(IsActive.Active).statuses(DescriptionTemplateStatus.Finalized).first(); + if (descriptionTemplateEntity == null) { + this.descriptionTemplateService.importXml(descriptionXml.getDescriptionTemplate(), null, descriptionXml.getDescriptionTemplate().getDescription(), new BaseFieldSet().ensure(DmpBlueprint._label).ensure(DmpBlueprint._hash)); + } + } + } + } + } + private UUID xmlDmpBlueprintToPersist(DmpImportExport dmpXml) throws JAXBException, InvalidApplicationException, ParserConfigurationException, IOException, TransformerException, InstantiationException, IllegalAccessException, SAXException { if (dmpXml.getBlueprint() != null){ DmpBlueprintEntity dmpBlueprintEntity = this.queryFactory.query(DmpBlueprintQuery.class).disableTracking().ids(dmpXml.getBlueprint().getId()).first(); From d42fb1896d8d44e404f422e19d627473e7e05ae0 Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Thu, 13 Jun 2024 11:24:39 +0300 Subject: [PATCH 2/2] migration fixes --- .../migration/DatasetMigrationService.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java index 8167773f1..76a5def3b 100644 --- a/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java +++ b/dmp-migration-tool/web/src/main/java/eu/old/eudat/migration/DatasetMigrationService.java @@ -262,19 +262,30 @@ public class DatasetMigrationService { } this.addMultipleField(item, properties, descriptionTemplateDefinitionEntity, propertyDefinitionEntity, referenceTypeDefinitionEntityMap, referenceMap); - + + List descriptionReferenceEntities = this.queryFactory.query(DescriptionReferenceQuery.class).descriptionIds(item.getId()).collect(); + Map descriptionReferenceEntitiesMap = descriptionReferenceEntities.stream().collect(Collectors.toMap(DescriptionReferenceEntity::getId, x-> x)); + Map descriptionReferenceDataEntityById = new HashMap<>(); + for (DescriptionReferenceEntity descriptionReferenceEntity : descriptionReferenceEntities){ + DescriptionReferenceDataEntity propertyDefinition = this.jsonHandlingService.fromJsonSafe(DescriptionReferenceDataEntity.class, descriptionReferenceEntity.getData()); + if (propertyDefinition != null) descriptionReferenceDataEntityById.put(descriptionReferenceEntity.getId(), propertyDefinition); + } for (PropertyDefinitionFieldSetEntity fieldSetEntity : propertyDefinitionEntity.getFieldSets().values()){ int newOrdinal = 0; PropertyDefinitionFieldSetItemEntity firstFieldSet = fieldSetEntity.getItems().stream().findFirst().filter(x-> x.getOrdinal() == SimpleFieldSetOrdinal).orElse(null); if (firstFieldSet != null){ + this.ensureDescriptionReferenceOrdinal(firstFieldSet, descriptionReferenceEntitiesMap, descriptionReferenceDataEntityById, newOrdinal); firstFieldSet.setOrdinal(newOrdinal); newOrdinal++; + } for (PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity.getItems().stream().filter(x-> x.getOrdinal() >= 0 && x.getOrdinal() != SimpleFieldSetOrdinal).sorted(Comparator.comparingInt(PropertyDefinitionFieldSetItemEntity::getOrdinal)).toList()){ + this.ensureDescriptionReferenceOrdinal(propertyDefinitionFieldSetItemEntity, descriptionReferenceEntitiesMap, descriptionReferenceDataEntityById, newOrdinal); propertyDefinitionFieldSetItemEntity.setOrdinal(newOrdinal); newOrdinal++; } for (PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity : fieldSetEntity.getItems().stream().filter(x-> x.getOrdinal() < 0 && x.getOrdinal() != SimpleFieldSetOrdinal).sorted(Comparator.comparingInt(PropertyDefinitionFieldSetItemEntity::getOrdinal)).toList()){ + this.ensureDescriptionReferenceOrdinal(propertyDefinitionFieldSetItemEntity, descriptionReferenceEntitiesMap, descriptionReferenceDataEntityById, newOrdinal); propertyDefinitionFieldSetItemEntity.setOrdinal(newOrdinal); newOrdinal++; } @@ -282,6 +293,19 @@ public class DatasetMigrationService { return propertyDefinitionEntity; } + private void ensureDescriptionReferenceOrdinal(PropertyDefinitionFieldSetItemEntity propertyDefinitionFieldSetItemEntity, Map descriptionReferenceEntitiesMap, Map descriptionReferenceDataEntityById, int newOrdinal){ + for (String key : propertyDefinitionFieldSetItemEntity.getFields().keySet()){ + for (Map.Entry entityMap : descriptionReferenceDataEntityById.entrySet()){ + if (entityMap.getValue().getOrdinal() == propertyDefinitionFieldSetItemEntity.getOrdinal() && entityMap.getValue().getFieldId().equals(key)){ + DescriptionReferenceEntity descriptionReferenceEntity = descriptionReferenceEntitiesMap.get(entityMap.getKey()); + descriptionReferenceEntity.setData(this.jsonHandlingService.toJsonSafe(this.buildDescriptionReferenceDataEntity(key, newOrdinal))); + this.entityManager.merge(descriptionReferenceEntity); + } + } + } + this.entityManager.flush(); + } + private void addMultipleField(Dataset item, Map properties, org.opencdmp.commons.types.descriptiontemplate.DefinitionEntity descriptionTemplateDefinitionEntity, PropertyDefinitionEntity propertyDefinitionEntity, Map referenceTypeDefinitionEntityMap, Map referenceMap){ Map groupOrdinalMapping = new HashMap<>(); for (String key : properties.keySet()) { @@ -1054,13 +1078,13 @@ public class DatasetMigrationService { data.setCreatedAt(Instant.now()); data.setUpdatedAt(Instant.now()); data.setIsActive(IsActive.Active); - data.setData(this.jsonHandlingService.toJsonSafe(this.buildDescriptionReferenceDataEntity(currentField, ordinal))); + data.setData(this.jsonHandlingService.toJsonSafe(this.buildDescriptionReferenceDataEntity(currentField.getId(), ordinal))); this.entityManager.persist(data); } - private @NotNull DescriptionReferenceDataEntity buildDescriptionReferenceDataEntity(org.opencdmp.commons.types.descriptiontemplate.FieldEntity currentField, int ordinal){ + private @NotNull DescriptionReferenceDataEntity buildDescriptionReferenceDataEntity(String currentField, int ordinal){ DescriptionReferenceDataEntity data = new DescriptionReferenceDataEntity(); - data.setFieldId(currentField.getId().trim()); + data.setFieldId(currentField.trim()); data.setOrdinal(ordinal); return data; }