DescriptionTemplate version status fixes

This commit is contained in:
Efstratios Giannopoulos 2024-01-26 18:18:05 +02:00
parent a55d7c3692
commit d89d8d4aa8
2 changed files with 47 additions and 26 deletions

View File

@ -7,7 +7,7 @@ import java.util.Map;
public enum DescriptionTemplateVersionStatus implements DatabaseEnum<Short> { public enum DescriptionTemplateVersionStatus implements DatabaseEnum<Short> {
Current((short) 0), Previous ((short) 1); Current((short) 0), Previous ((short) 1), NotFinalized ((short) 2);
private final Short value; private final Short value;

View File

@ -172,22 +172,21 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
DescriptionTemplateEntity data; DescriptionTemplateEntity data;
if (isUpdate) { if (isUpdate) {
data = this.entityManager.find(DescriptionTemplateEntity.class, model.getId()); data = this.entityManager.find(DescriptionTemplateEntity.class, model.getId());
if (data == null) if (data == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
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 (!this.conventionService.hashValue(data.getUpdatedAt()).equals(model.getHash())) if (data.getStatus().equals(DescriptionTemplateStatus.Finalized)) throw new MyForbiddenException("Can not update finalized template");
throw new MyValidationException(this.errors.getHashConflict().getCode(), this.errors.getHashConflict().getMessage());
if (data.getStatus() == DescriptionTemplateStatus.Finalized)
throw new MyForbiddenException("Can not update finalized template");
} else { } else {
data = new DescriptionTemplateEntity(); data = new DescriptionTemplateEntity();
data.setId(UUID.randomUUID()); data.setId(UUID.randomUUID());
data.setIsActive(IsActive.Active); data.setIsActive(IsActive.Active);
data.setCreatedAt(Instant.now()); data.setCreatedAt(Instant.now());
data.setGroupId(UUID.randomUUID()); data.setGroupId(UUID.randomUUID());
data.setVersionStatus(DescriptionTemplateVersionStatus.Current); data.setVersionStatus(DescriptionTemplateVersionStatus.NotFinalized);
data.setVersion((short) 1); data.setVersion((short) 1);
} }
DescriptionTemplateStatus previousStatus = data.getStatus();
data.setDescription(model.getDescription()); data.setDescription(model.getDescription());
data.setLabel(model.getLabel()); data.setLabel(model.getLabel());
data.setTypeId(model.getType()); data.setTypeId(model.getType());
@ -206,9 +205,36 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
this.entityManager.flush(); 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); 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<DescriptionTemplateEntity> 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<UserDescriptionTemplatePersist> users) throws InvalidApplicationException { private void persistUsers(UUID id, List<UserDescriptionTemplatePersist> users) throws InvalidApplicationException {
if (users == null) if (users == null)
users = new ArrayList<>(); users = new ArrayList<>();
@ -521,27 +547,22 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
this.authorizationService.authorizeForce(Permission.CreateNewVersionDescriptionTemplate); this.authorizationService.authorizeForce(Permission.CreateNewVersionDescriptionTemplate);
DescriptionTemplateEntity oldDescriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getId()); DescriptionTemplateEntity oldDescriptionTemplateEntity = this.entityManager.find(DescriptionTemplateEntity.class, model.getId());
if (oldDescriptionTemplateEntity == null) if (oldDescriptionTemplateEntity == null) throw new MyNotFoundException(messageSource.getMessage("General_ItemNotFound", new Object[]{model.getId(), DescriptionTemplate.class.getSimpleName()}, LocaleContextHolder.getLocale()));
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());
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<DescriptionTemplateEntity> latestVersionDescriptionTemplates = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.Current).isActive(IsActive.Active).groupIds(oldDescriptionTemplateEntity.getGroupId()).collect();
List<DescriptionTemplateEntity> latestVersionDescriptionTemplates = latestVersionDescriptionTemplateEntityQuery.collect(); if (latestVersionDescriptionTemplates.isEmpty()) throw new MyValidationException("Previous template not found");
if (latestVersionDescriptionTemplates.isEmpty()) if (latestVersionDescriptionTemplates.size() > 1) throw new MyValidationException("Multiple previous template found");
throw new MyValidationException("Previous template not found"); if (!latestVersionDescriptionTemplates.getFirst().getVersion().equals(oldDescriptionTemplateEntity.getVersion())) throw new MyValidationException(this.errors.getDescriptionTemplateNewVersionConflict().getCode(), this.errors.getDescriptionTemplateNewVersionConflict().getMessage());
if (latestVersionDescriptionTemplates.size() > 1) Long notFinalizedCount = this.queryFactory.query(DescriptionTemplateQuery.class).versionStatuses(DescriptionTemplateVersionStatus.NotFinalized).groupIds(oldDescriptionTemplateEntity.getGroupId()).isActive(IsActive.Active).count();
throw new MyValidationException("Multiple previous template found"); if (notFinalizedCount > 0) throw new MyValidationException("Already created draft for this template");
if (!latestVersionDescriptionTemplates.get(0).getVersion().equals(oldDescriptionTemplateEntity.getVersion())) {
throw new MyValidationException(this.errors.getDescriptionTemplateNewVersionConflict().getCode(), this.errors.getDescriptionTemplateNewVersionConflict().getMessage());
}
DescriptionTemplateEntity data = new DescriptionTemplateEntity(); DescriptionTemplateEntity data = new DescriptionTemplateEntity();
data.setId(UUID.randomUUID()); data.setId(UUID.randomUUID());
data.setIsActive(IsActive.Active); data.setIsActive(IsActive.Active);
data.setCreatedAt(Instant.now()); data.setCreatedAt(Instant.now());
data.setUpdatedAt(Instant.now()); data.setUpdatedAt(Instant.now());
data.setVersionStatus(DescriptionTemplateVersionStatus.Current); data.setVersionStatus(DescriptionTemplateVersionStatus.NotFinalized);
data.setGroupId(oldDescriptionTemplateEntity.getGroupId()); data.setGroupId(oldDescriptionTemplateEntity.getGroupId());
data.setVersion((short) (oldDescriptionTemplateEntity.getVersion() + 1)); data.setVersion((short) (oldDescriptionTemplateEntity.getVersion() + 1));
data.setDescription(model.getDescription()); data.setDescription(model.getDescription());
@ -554,11 +575,11 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic
this.entityManager.persist(data); this.entityManager.persist(data);
this.persistUsers(data.getId(), model.getUsers()); this.persistUsers(data.getId(), model.getUsers());
//this.addOwner(data); //this.addOwner(data);
oldDescriptionTemplateEntity.setVersionStatus(DescriptionTemplateVersionStatus.Previous); this.entityManager.flush();
this.entityManager.merge(oldDescriptionTemplateEntity);
this.updateVersionStatusAndSave(data, DescriptionTemplateStatus.Draft, data.getStatus());
this.entityManager.flush(); this.entityManager.flush();