DescriptionTemplate version status fixes
This commit is contained in:
parent
a55d7c3692
commit
d89d8d4aa8
|
@ -7,7 +7,7 @@ import java.util.Map;
|
|||
|
||||
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;
|
||||
|
||||
|
|
|
@ -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<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 {
|
||||
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<DescriptionTemplateEntity> 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<DescriptionTemplateEntity> 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);
|
||||
|
|
Loading…
Reference in New Issue