diff --git a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java index 7428d5f81..2451ea6c6 100644 --- a/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/plan/PlanServiceImpl.java @@ -446,7 +446,7 @@ public class PlanServiceImpl implements PlanService { .planIds(model.getId()) .isActives(IsActive.Active) .collect(); - List planDescriptionTemplates = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking() + List oldPlanDescriptionTemplates = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking() .planIds(model.getId()) .isActive(IsActive.Active) .collect(); @@ -514,20 +514,50 @@ public class PlanServiceImpl implements PlanService { } } - List newPlanDescriptionTemplateEntities = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).collect(); + List newPlanDescriptionTemplateEntitiesEntities = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).collect(); + List sections = definition.getSections().stream().filter(SectionEntity::getHasTemplates).collect(Collectors.toList()); + if (!this.conventionService.isListNullOrEmpty(oldPlanDescriptionTemplates)) { + for (PlanDescriptionTemplateEntity oldPlanDescriptionTemplate : oldPlanDescriptionTemplates) { + for (SectionEntity section : sections) { + if (section.getId().equals(oldPlanDescriptionTemplate.getSectionId())) { + PlanDescriptionTemplateEntity oldDescriptionTemplateEntity = oldPlanDescriptionTemplates.stream().filter(x -> x.getSectionId().equals(section.getId()) && x.getDescriptionTemplateGroupId().equals(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null); + boolean isDescriptionTemplateAlreadyInPlan = false; + + if (!this.conventionService.isListNullOrEmpty(newPlanDescriptionTemplateEntitiesEntities)) { + PlanDescriptionTemplateEntity existingBlueprintDescriptionTemplateEntity = newPlanDescriptionTemplateEntitiesEntities.stream().filter(x -> x.getSectionId().equals(section.getId()) && x.getDescriptionTemplateGroupId().equals(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null); + isDescriptionTemplateAlreadyInPlan = existingBlueprintDescriptionTemplateEntity != null; + } + + if (oldDescriptionTemplateEntity != null && !isDescriptionTemplateAlreadyInPlan) { + PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); + newTemplate.setId(UUID.randomUUID()); + newTemplate.setPlanId(newPlan.getId()); + newTemplate.setDescriptionTemplateGroupId(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId()); + newTemplate.setSectionId(section.getId()); + newTemplate.setCreatedAt(Instant.now()); + newTemplate.setUpdatedAt(Instant.now()); + newTemplate.setIsActive(IsActive.Active); + this.entityManager.persist(newTemplate); + this.entityManager.flush(); + newPlanDescriptionTemplateEntitiesEntities.add(newTemplate); + } + } + } + } + } if (!oldPlanEntity.getBlueprintId().equals(blueprintEntity.getId())){ // add description templates if exists in new blueprint - List sections = definition.getSections().stream().filter(SectionEntity::getHasTemplates).collect(Collectors.toList()); if (!this.conventionService.isListNullOrEmpty(sections)){ for (SectionEntity section: sections) { - if (!this.conventionService.isListNullOrEmpty(section.getDescriptionTemplates())){ + if (!this.conventionService.isListNullOrEmpty(section.getDescriptionTemplates()) && section.getHasTemplates()){ + // new blueprint templates for (DescriptionTemplateEntity blueprintDescriptionTemplate: section.getDescriptionTemplates()) { boolean isDescriptionTemplateAlreadyInPlan = false; - if (!this.conventionService.isListNullOrEmpty(newPlanDescriptionTemplateEntities)) { - PlanDescriptionTemplateEntity existingBlueprintDescriptionTemplateEntity = newPlanDescriptionTemplateEntities.stream().filter(x -> x.getSectionId().equals(section.getId()) && x.getDescriptionTemplateGroupId().equals(blueprintDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null); + if (!this.conventionService.isListNullOrEmpty(newPlanDescriptionTemplateEntitiesEntities)) { + PlanDescriptionTemplateEntity existingBlueprintDescriptionTemplateEntity = newPlanDescriptionTemplateEntitiesEntities.stream().filter(x -> x.getSectionId().equals(section.getId()) && x.getDescriptionTemplateGroupId().equals(blueprintDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null); isDescriptionTemplateAlreadyInPlan = existingBlueprintDescriptionTemplateEntity != null; } @@ -542,27 +572,13 @@ public class PlanServiceImpl implements PlanService { newTemplate.setIsActive(IsActive.Active); this.entityManager.persist(newTemplate); this.entityManager.flush(); + newPlanDescriptionTemplateEntitiesEntities.add(newTemplate); } } } } } } - else { - for (PlanDescriptionTemplateEntity oldPlanDescriptionTemplate : planDescriptionTemplates) { - if (newPlanDescriptionTemplateEntities.stream().filter(x -> x.getSectionId().equals(oldPlanDescriptionTemplate.getSectionId()) && x.getDescriptionTemplateGroupId().equals(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null) == null){ - PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); - newTemplate.setId(UUID.randomUUID()); - newTemplate.setPlanId(newPlan.getId()); - newTemplate.setDescriptionTemplateGroupId(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId()); - newTemplate.setSectionId(oldPlanDescriptionTemplate.getSectionId()); - newTemplate.setCreatedAt(Instant.now()); - newTemplate.setUpdatedAt(Instant.now()); - newTemplate.setIsActive(IsActive.Active); - this.entityManager.persist(newTemplate); - } - } - } this.entityManager.flush();