From 066d8c8d24d680fb67e616d1dde01cb738cbcf9c Mon Sep 17 00:00:00 2001 From: amentis Date: Thu, 16 May 2024 14:32:56 +0300 Subject: [PATCH] fix new version flow, sort other dmp versions --- .../model/builder/PublicDmpBuilder.java | 8 +++++-- .../model/builder/dmp/DmpBuilder.java | 8 +++++-- .../descriptionproperties/FieldPersist.java | 2 +- .../opencdmp/service/dmp/DmpServiceImpl.java | 24 ++++++++++++++++--- 4 files changed, 34 insertions(+), 8 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/PublicDmpBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/PublicDmpBuilder.java index 7ad8a3586..b9ed5f07a 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/PublicDmpBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/PublicDmpBuilder.java @@ -13,6 +13,7 @@ import org.opencdmp.commons.enums.IsActive; import org.opencdmp.convention.ConventionService; import org.opencdmp.data.DmpEntity; import org.opencdmp.model.*; +import org.opencdmp.model.dmp.Dmp; import org.opencdmp.query.*; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -88,7 +89,10 @@ public class PublicDmpBuilder extends BaseBuilder { if (dmpUsersMap != null && !dmpUsersMap.isEmpty() && dmpUsersMap.containsKey(d.getId())) m.setDmpUsers(dmpUsersMap.get(d.getId())); if (descriptionsMap != null && !descriptionsMap.isEmpty() && descriptionsMap.containsKey(d.getId())) m.setDescriptions(descriptionsMap.get(d.getId())); if (entityDoisMap != null && !entityDoisMap.isEmpty() && entityDoisMap.containsKey(d.getId())) m.setEntityDois(entityDoisMap.get(d.getId())); - if (otherDmpVersionsMap != null && !otherDmpVersionsMap.isEmpty() && otherDmpVersionsMap.containsKey(d.getGroupId())) m.setOtherDmpVersions(otherDmpVersionsMap.get(d.getGroupId())); + if (otherDmpVersionsMap != null && !otherDmpVersionsMap.isEmpty() && otherDmpVersionsMap.containsKey(d.getGroupId())){ + m.setOtherDmpVersions(otherDmpVersionsMap.get(d.getGroupId())); + m.getOtherDmpVersions().sort(Comparator.comparing(PublicDmp::getVersion)); + } models.add(m); } @@ -178,7 +182,7 @@ public class PublicDmpBuilder extends BaseBuilder { DmpQuery query = this.queryFactory.query(DmpQuery.class).disableTracking().authorize(this.authorize).groupIds(data.stream().map(DmpEntity::getGroupId).distinct().collect(Collectors.toList())).isActive(IsActive.Active); itemMap = this.builderFactory.builder(PublicDmpBuilder.class).authorize(this.authorize).asMasterKey(query, clone, PublicDmp::getGroupId); - if (!fields.hasField(this.asIndexer(PublicDmp._otherDmpVersions, PublicDmp._id))) { + if (!fields.hasField(PublicDmp._id)) { itemMap.values().stream().flatMap(List::stream).filter(Objects::nonNull).forEach(x -> { x.setId(null); }); diff --git a/backend/core/src/main/java/org/opencdmp/model/builder/dmp/DmpBuilder.java b/backend/core/src/main/java/org/opencdmp/model/builder/dmp/DmpBuilder.java index 621ee47c4..0c06bd05c 100644 --- a/backend/core/src/main/java/org/opencdmp/model/builder/dmp/DmpBuilder.java +++ b/backend/core/src/main/java/org/opencdmp/model/builder/dmp/DmpBuilder.java @@ -32,6 +32,7 @@ import org.opencdmp.model.dmpblueprint.DmpBlueprint; import org.opencdmp.model.dmpreference.DmpReference; import org.opencdmp.model.user.User; import org.opencdmp.query.*; +import org.opencdmp.service.externalfetcher.config.entities.SourceBaseConfiguration; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.config.ConfigurableBeanFactory; @@ -137,7 +138,10 @@ public class DmpBuilder extends BaseBuilder { if (dmpUsersMap != null && !dmpUsersMap.isEmpty() && dmpUsersMap.containsKey(d.getId())) m.setDmpUsers(dmpUsersMap.get(d.getId())); if (descriptionsMap != null && !descriptionsMap.isEmpty() && descriptionsMap.containsKey(d.getId())) m.setDescriptions(descriptionsMap.get(d.getId())); if (dmpDescriptionTemplatesMap != null && !dmpDescriptionTemplatesMap.isEmpty() && dmpDescriptionTemplatesMap.containsKey(d.getId())) m.setDmpDescriptionTemplates(dmpDescriptionTemplatesMap.get(d.getId())); - if (otherDmpVersionsMap != null && !otherDmpVersionsMap.isEmpty() && otherDmpVersionsMap.containsKey(d.getGroupId())) m.setOtherDmpVersions(otherDmpVersionsMap.get(d.getGroupId())); + if (otherDmpVersionsMap != null && !otherDmpVersionsMap.isEmpty() && otherDmpVersionsMap.containsKey(d.getGroupId())) { + m.setOtherDmpVersions(otherDmpVersionsMap.get(d.getGroupId())); + m.getOtherDmpVersions().sort(Comparator.comparing(Dmp::getVersion)); + } if (!propertiesFields.isEmpty() && d.getProperties() != null){ DmpPropertiesEntity propertyDefinition = this.jsonHandlingService.fromJsonSafe(DmpPropertiesEntity.class, d.getProperties()); m.setProperties(this.builderFactory.builder(DmpPropertiesBuilder.class).authorize(this.authorize).build(propertiesFields, propertyDefinition)); @@ -310,7 +314,7 @@ public class DmpBuilder extends BaseBuilder { DmpQuery query = this.queryFactory.query(DmpQuery.class).disableTracking().authorize(this.authorize).groupIds(data.stream().map(DmpEntity::getGroupId).distinct().collect(Collectors.toList())); itemMap = this.builderFactory.builder(DmpBuilder.class).authorize(this.authorize).asMasterKey(query, clone, Dmp::getGroupId); - if (!fields.hasField(this.asIndexer(Dmp._otherDmpVersions, Dmp._id))) { + if (!fields.hasField(Dmp._id)) { itemMap.values().stream().flatMap(List::stream).filter(Objects::nonNull).forEach(x -> { x.setId(null); }); diff --git a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionproperties/FieldPersist.java b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionproperties/FieldPersist.java index aaa3a2fa0..d87ecc736 100644 --- a/backend/core/src/main/java/org/opencdmp/model/persist/descriptionproperties/FieldPersist.java +++ b/backend/core/src/main/java/org/opencdmp/model/persist/descriptionproperties/FieldPersist.java @@ -136,7 +136,7 @@ public class FieldPersist { .must(() -> !this.isEmpty(item.getTextValue())) .failOn(FieldPersist._textValue).failWith(this.messageSource.getMessage("Validation_Required", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())), this.spec() - .iff(()-> fieldType.equals(FieldType.FREE_TEXT) && DescriptionStatus.Finalized.equals(this.status) && isVisible && isUrlRequired) + .iff(()-> fieldType.equals(FieldType.FREE_TEXT) && DescriptionStatus.Finalized.equals(this.status) && isVisible && isUrlRequired && (required || !this.isEmpty(item.getTextValue()))) .must(() -> this.isValidURL(item.getTextValue())) .failOn(FieldPersist._textValue).failWith(this.messageSource.getMessage("Validation_UrlRequired", new Object[]{FieldPersist._textValue}, LocaleContextHolder.getLocale())), this.spec() 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 4cfb4c263..7b0c48759 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 @@ -343,7 +343,7 @@ public class DmpServiceImpl implements DmpService { .groupIds(data.getGroupId()); dmpQuery.setOrder(new Ordering().addDescending(Dmp._version)); - previousDmp = dmpQuery.first(); + previousDmp = dmpQuery.collect().get(0); if (previousDmp != null){ if (previousDmp.getStatus().equals(DmpStatus.Finalized)) previousDmp.setVersionStatus(DmpVersionStatus.Current); else previousDmp.setVersionStatus(DmpVersionStatus.NotFinalized); @@ -566,7 +566,8 @@ public class DmpServiceImpl implements DmpService { if (newStatus.equals(DmpStatus.Finalized)) { List latestVersionDmps = this.queryFactory.query(DmpQuery.class) - .versionStatuses(DmpVersionStatus.Current).isActive(IsActive.Active).groupIds(data.getGroupId()).collect(); + .versionStatuses(DmpVersionStatus.Current).excludedIds(data.getId()) + .isActive(IsActive.Active).groupIds(data.getGroupId()).collect(); if (latestVersionDmps.size() > 1) throw new MyValidationException("Multiple previous template found"); DmpEntity oldDmpEntity = latestVersionDmps.stream().findFirst().orElse(null); @@ -1086,11 +1087,28 @@ public class DmpServiceImpl implements DmpService { dmp.setStatus(DmpStatus.Draft); dmp.setUpdatedAt(Instant.now()); - dmp.setVersionStatus(DmpVersionStatus.NotFinalized); this.entityManager.merge(dmp); this.entityManager.flush(); + this.updateVersionStatusAndSave(dmp, DmpStatus.Finalized, dmp.getStatus()); + this.entityManager.flush(); + + DmpQuery dmpQuery = this.queryFactory.query(DmpQuery.class).disableTracking() + .versionStatuses(DmpVersionStatus.Previous) + .excludedIds(dmp.getId()) + .isActive(IsActive.Active) + .groupIds(dmp.getGroupId()); + + dmpQuery.setOrder(new Ordering().addDescending(Dmp._version)); + DmpEntity previousDmp = dmpQuery.collect().get(0); + if (previousDmp != null){ + if (previousDmp.getStatus().equals(DmpStatus.Finalized)) previousDmp.setVersionStatus(DmpVersionStatus.Current); + else previousDmp.setVersionStatus(DmpVersionStatus.NotFinalized); + this.entityManager.merge(previousDmp); + } + this.entityManager.flush(); + this.annotationEntityTouchedIntegrationEventHandler.handleDmp(dmp.getId()); this.sendNotification(dmp); }