From 37a0fb18a6cd67529b7e975fbd581208dd09053e Mon Sep 17 00:00:00 2001 From: amentis Date: Mon, 22 Jul 2024 16:41:13 +0300 Subject: [PATCH] fix blueprint update version status on delete --- .../PlanBlueprintServiceImpl.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java index 1b545190b..f10c30432 100644 --- a/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/planblueprint/PlanBlueprintServiceImpl.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import gr.cite.commons.web.authz.service.AuthorizationService; import gr.cite.tools.data.builder.BuilderFactory; import gr.cite.tools.data.deleter.DeleterFactory; +import gr.cite.tools.data.query.Ordering; import gr.cite.tools.data.query.QueryFactory; import gr.cite.tools.exception.MyApplicationException; import gr.cite.tools.exception.MyForbiddenException; @@ -23,12 +24,10 @@ import org.opencdmp.commons.XmlHandlingService; import org.opencdmp.commons.enums.*; import org.opencdmp.commons.scope.tenant.TenantScope; import org.opencdmp.commons.types.planblueprint.*; +import org.opencdmp.commons.types.planblueprint.DescriptionTemplateEntity; import org.opencdmp.commons.types.planblueprint.importexport.*; import org.opencdmp.convention.ConventionService; -import org.opencdmp.data.PlanBlueprintEntity; -import org.opencdmp.data.PrefillingSourceEntity; -import org.opencdmp.data.ReferenceTypeEntity; -import org.opencdmp.data.TenantEntityManager; +import org.opencdmp.data.*; import org.opencdmp.errorcode.ErrorThesaurusProperties; import org.opencdmp.model.builder.planblueprint.PlanBlueprintBuilder; import org.opencdmp.model.deleter.PlanBlueprintDeleter; @@ -42,10 +41,7 @@ import org.opencdmp.model.planblueprint.PlanBlueprint; import org.opencdmp.model.planblueprint.Section; import org.opencdmp.model.prefillingsource.PrefillingSource; import org.opencdmp.model.referencetype.ReferenceType; -import org.opencdmp.query.DescriptionTemplateQuery; -import org.opencdmp.query.PlanBlueprintQuery; -import org.opencdmp.query.PrefillingSourceQuery; -import org.opencdmp.query.ReferenceTypeQuery; +import org.opencdmp.query.*; import org.opencdmp.service.accounting.AccountingService; import org.opencdmp.service.responseutils.ResponseUtilsService; import org.opencdmp.service.usagelimit.UsageLimitService; @@ -318,6 +314,27 @@ public class PlanBlueprintServiceImpl implements PlanBlueprintService { this.authorizationService.authorizeForce(Permission.DeletePlanBlueprint); + PlanBlueprintEntity data = this.entityManager.find(PlanBlueprintEntity.class, id); + if (data == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{id, PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); + + if (!data.getVersionStatus().equals(PlanBlueprintVersionStatus.Previous)){ + PlanBlueprintQuery planBlueprintQuery = this.queryFactory.query(PlanBlueprintQuery.class) + .excludedIds(data.getId()) + .isActive(IsActive.Active) + .groupIds(data.getGroupId()); + + planBlueprintQuery.setOrder(new Ordering().addDescending(PlanBlueprint._version)); + PlanBlueprintEntity previousPlanBlueprint = planBlueprintQuery.count() > 0 ? planBlueprintQuery.collect().getFirst() : null; + if (previousPlanBlueprint != null){ + if (previousPlanBlueprint.getStatus().equals(PlanBlueprintStatus.Finalized)) previousPlanBlueprint.setVersionStatus(PlanBlueprintVersionStatus.Current); + else previousPlanBlueprint.setVersionStatus(PlanBlueprintVersionStatus.NotFinalized); + this.entityManager.merge(previousPlanBlueprint); + data.setVersionStatus(PlanBlueprintVersionStatus.NotFinalized); + } + this.entityManager.merge(data); + this.entityManager.flush(); + } + this.deleterFactory.deleter(PlanBlueprintDeleter.class).deleteAndSaveByIds(List.of(id)); }