From d89d8d4aa8139e178f86c456d175419433d2e1ca Mon Sep 17 00:00:00 2001 From: sgiannopoulos Date: Fri, 26 Jan 2024 18:18:05 +0200 Subject: [PATCH] DescriptionTemplate version status fixes --- .../DescriptionTemplateVersionStatus.java | 2 +- .../DescriptionTemplateServiceImpl.java | 71 ++++++++++++------- 2 files changed, 47 insertions(+), 26 deletions(-) diff --git a/dmp-backend/core/src/main/java/eu/eudat/commons/enums/DescriptionTemplateVersionStatus.java b/dmp-backend/core/src/main/java/eu/eudat/commons/enums/DescriptionTemplateVersionStatus.java index 64f7bd22f..4deed73e9 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/commons/enums/DescriptionTemplateVersionStatus.java +++ b/dmp-backend/core/src/main/java/eu/eudat/commons/enums/DescriptionTemplateVersionStatus.java @@ -7,7 +7,7 @@ import java.util.Map; public enum DescriptionTemplateVersionStatus implements DatabaseEnum { - Current((short) 0), Previous ((short) 1); + Current((short) 0), Previous ((short) 1), NotFinalized ((short) 2); private final Short value; diff --git a/dmp-backend/core/src/main/java/eu/eudat/service/descriptiontemplate/DescriptionTemplateServiceImpl.java b/dmp-backend/core/src/main/java/eu/eudat/service/descriptiontemplate/DescriptionTemplateServiceImpl.java index e343d8681..cbb1dc6af 100644 --- a/dmp-backend/core/src/main/java/eu/eudat/service/descriptiontemplate/DescriptionTemplateServiceImpl.java +++ b/dmp-backend/core/src/main/java/eu/eudat/service/descriptiontemplate/DescriptionTemplateServiceImpl.java @@ -172,22 +172,21 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic DescriptionTemplateEntity data; if (isUpdate) { data = this.entityManager.find(DescriptionTemplateEntity.class, model.getId()); - if (data == null) - throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); - if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) - throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); - if (data.getStatus() == DescriptionTemplateStatus.Finalized) - throw new MyForbiddenException("Can not update finalized template"); + if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); + if (data.getStatus().equals(DescriptionTemplateStatus.Finalized)) throw new MyForbiddenException("Can not update finalized template"); } else { data = new DescriptionTemplateEntity(); data.setId(UUID.randomUUID()); data.setIsActive(IsActive.Active); data.setCreatedAt(Instant.now()); data.setGroupId(UUID.randomUUID()); - data.setVersionStatus(DescriptionTemplateVersionStatus.Current); + data.setVersionStatus(DescriptionTemplateVersionStatus.NotFinalized); data.setVersion((short) 1); } + DescriptionTemplateStatus previousStatus = data.getStatus(); + data.setDescription(model.getDescription()); data.setLabel(model.getLabel()); data.setTypeId(model.getType()); @@ -205,10 +204,37 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic this.addOwner(data); this.entityManager.flush(); + + this.updateVersionStatusAndSave(data, previousStatus, data.getStatus()); + this.entityManager.flush(); + return this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, DescriptionTemplate._id), data); } + + private void updateVersionStatusAndSave(DescriptionTemplateEntity data, DescriptionTemplateStatus previousStatus, DescriptionTemplateStatus newStatus){ + if (previousStatus.equals(newStatus)) return; + if (previousStatus.equals(DescriptionTemplateStatus.Finalized)) throw new MyForbiddenException("Can not update finalized template"); + if (newStatus.equals(DescriptionTemplateStatus.Finalized)) { + List latestVersionDescriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.Current).isActive(IsActive.Active).groupIds(data.getGroupId()).collect(); + if (latestVersionDescriptionTemplates.size() > 1) throw new MyValidationException("Multiple previous template found"); + DescriptionTemplateEntity oldDescriptionTemplateEntity = latestVersionDescriptionTemplates.getFirst(); + + data.setVersionStatus(DescriptionTemplateVersionStatus.Current); + + if (oldDescriptionTemplateEntity != null){ + data.setVersion((short) (oldDescriptionTemplateEntity.getVersion() + 1)); + + oldDescriptionTemplateEntity.setVersionStatus(DescriptionTemplateVersionStatus.Previous); + this.entityManager.merge(oldDescriptionTemplateEntity); + } else { + data.setVersion((short)1); + } + } + + } + private void persistUsers(UUID id, List users) throws InvalidApplicationException { if (users == null) users = new ArrayList<>(); @@ -521,27 +547,22 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic this.authorizationService.authorizeForce(Permission.CreateNewVersionDescriptionTemplate); DescriptionTemplateEntity oldDescriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getId()); - if (oldDescriptionTemplateEntity == null) - throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); - if (!this.conventionService.hashValue(oldDescriptionTemplateEntity.getUpdatedAt()).equals(model.getHash())) - throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); - - DescriptionTemplateQuery latestVersionDescriptionTemplateEntityQuery = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.Current).groupIds(oldDescriptionTemplateEntity.getGroupId()); - List latestVersionDescriptionTemplates = latestVersionDescriptionTemplateEntityQuery.collect(); - if (latestVersionDescriptionTemplates.isEmpty()) - throw new MyValidationException("Previous template not found"); - if (latestVersionDescriptionTemplates.size() > 1) - throw new MyValidationException("Multiple previous template found"); - if (!latestVersionDescriptionTemplates.get(0).getVersion().equals(oldDescriptionTemplateEntity.getVersion())) { - throw new MyValidationException(this.errors.getDescriptionTemplateNewVersionConflict().getCode(), this.errors.getDescriptionTemplateNewVersionConflict().getMessage()); - } + if (oldDescriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale())); + if (!this.conventionService.hashValue(oldDescriptionTemplateEntity.getUpdatedAt()).equals(model.getHash())) throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage()); + List latestVersionDescriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.Current).isActive(IsActive.Active).groupIds(oldDescriptionTemplateEntity.getGroupId()).collect(); + if (latestVersionDescriptionTemplates.isEmpty()) throw new MyValidationException("Previous template not found"); + if (latestVersionDescriptionTemplates.size() > 1) throw new MyValidationException("Multiple previous template found"); + if (!latestVersionDescriptionTemplates.getFirst().getVersion().equals(oldDescriptionTemplateEntity.getVersion())) throw new MyValidationException(this.errors.getDescriptionTemplateNewVersionConflict().getCode(), this.errors.getDescriptionTemplateNewVersionConflict().getMessage()); + Long notFinalizedCount = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.NotFinalized).groupIds(oldDescriptionTemplateEntity.getGroupId()).isActive(IsActive.Active).count(); + if (notFinalizedCount > 0) throw new MyValidationException("Already created draft for this template"); + DescriptionTemplateEntity data = new DescriptionTemplateEntity(); data.setId(UUID.randomUUID()); data.setIsActive(IsActive.Active); data.setCreatedAt(Instant.now()); data.setUpdatedAt(Instant.now()); - data.setVersionStatus(DescriptionTemplateVersionStatus.Current); + data.setVersionStatus(DescriptionTemplateVersionStatus.NotFinalized); data.setGroupId(oldDescriptionTemplateEntity.getGroupId()); data.setVersion((short) (oldDescriptionTemplateEntity.getVersion() + 1)); data.setDescription(model.getDescription()); @@ -554,12 +575,12 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic this.entityManager.persist(data); this.persistUsers(data.getId(), model.getUsers()); - //this.addOwner(data); - oldDescriptionTemplateEntity.setVersionStatus(DescriptionTemplateVersionStatus.Previous); - this.entityManager.merge(oldDescriptionTemplateEntity); + this.entityManager.flush(); + this.updateVersionStatusAndSave(data, DescriptionTemplateStatus.Draft, data.getStatus()); + this.entityManager.flush(); return this.builderFactory.builder(DescriptionTemplateBuilder.class).authorize(AuthorizationFlags.OwnerOrDmpAssociatedOrPermissionOrPublic).build(BaseFieldSet.build(fields, DescriptionTemplate._id), data);