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 803c82a3c..7428d5f81 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 @@ -479,90 +479,90 @@ public class PlanServiceImpl implements PlanService { this.entityManager.flush(); - PlanBlueprintEntity blueprintEntity = this.entityManager.find(PlanBlueprintEntity.class, model.getBlueprintId(), true); - if (blueprintEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getBlueprintId(), PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); + PlanBlueprintEntity blueprintEntity = this.entityManager.find(PlanBlueprintEntity.class, model.getBlueprintId(), true); + if (blueprintEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{model.getBlueprintId(), PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); - org.opencdmp.commons.types.planblueprint.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(org.opencdmp.commons.types.planblueprint.DefinitionEntity.class, blueprintEntity.getDefinition()); + org.opencdmp.commons.types.planblueprint.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(org.opencdmp.commons.types.planblueprint.DefinitionEntity.class, blueprintEntity.getDefinition()); - List descriptionEntities = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(model.getDescriptions().stream().map(NewVersionPlanDescriptionPersist::getDescriptionId).distinct().collect(Collectors.toList())).isActive(IsActive.Active).collect(); + List descriptionEntities = this.queryFactory.query(DescriptionQuery.class).disableTracking().ids(model.getDescriptions().stream().map(NewVersionPlanDescriptionPersist::getDescriptionId).distinct().collect(Collectors.toList())).isActive(IsActive.Active).collect(); - FieldSet fieldSet = new BaseFieldSet(Description._id, BaseFieldSet.asIndexer(Description._descriptionTemplate, DescriptionTemplate._groupId)); - List models = this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(fieldSet, descriptionEntities); + FieldSet fieldSet = new BaseFieldSet(Description._id, BaseFieldSet.asIndexer(Description._descriptionTemplate, DescriptionTemplate._groupId)); + List models = this.builderFactory.builder(DescriptionBuilder.class).authorize(AuthorizationFlags.AllExceptPublic).build(fieldSet, descriptionEntities); - if (!this.conventionService.isListNullOrEmpty(models) && !this.conventionService.isListNullOrEmpty(model.getDescriptions())){ - for (NewVersionPlanDescriptionPersist newVersionPlanDescriptionPersist : model.getDescriptions()) { - Description description = models.stream().filter(x -> x.getId().equals(newVersionPlanDescriptionPersist.getDescriptionId())).findFirst().orElse(null); - if (description != null){ - PlanDescriptionTemplateEntity existingPlanDescriptionTemplateEntity = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).sectionIds(newVersionPlanDescriptionPersist.getBlueprintSectionId()).descriptionTemplateGroupIds(description.getDescriptionTemplate().getGroupId()).first(); - if (existingPlanDescriptionTemplateEntity == null){ - PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); - newTemplate.setId(UUID.randomUUID()); - newTemplate.setPlanId(newPlan.getId()); - newTemplate.setDescriptionTemplateGroupId(description.getDescriptionTemplate().getGroupId()); - newTemplate.setSectionId(newVersionPlanDescriptionPersist.getBlueprintSectionId()); - newTemplate.setCreatedAt(Instant.now()); - newTemplate.setUpdatedAt(Instant.now()); - newTemplate.setIsActive(IsActive.Active); - this.entityManager.persist(newTemplate); - this.entityManager.flush(); - this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), newTemplate.getId()); - } else{ - this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), existingPlanDescriptionTemplateEntity.getId()); - } - } - } - } - - List newPlanDescriptionTemplateEntities = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).collect(); - - 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())){ - 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); - isDescriptionTemplateAlreadyInPlan = existingBlueprintDescriptionTemplateEntity != null; - } - - if (!isDescriptionTemplateAlreadyInPlan){ - PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); - newTemplate.setId(UUID.randomUUID()); - newTemplate.setPlanId(newPlan.getId()); - newTemplate.setDescriptionTemplateGroupId(blueprintDescriptionTemplate.getDescriptionTemplateGroupId()); - newTemplate.setSectionId(section.getId()); - newTemplate.setCreatedAt(Instant.now()); - newTemplate.setUpdatedAt(Instant.now()); - newTemplate.setIsActive(IsActive.Active); - this.entityManager.persist(newTemplate); - this.entityManager.flush(); - } - } - } - } - } - } else { - - for (PlanDescriptionTemplateEntity oldPlanDescriptionTemplate : planDescriptionTemplates) { - if (newPlanDescriptionTemplateEntities.stream().filter(x -> x.getSectionId().equals(oldPlanDescriptionTemplate.getSectionId()) && x.getDescriptionTemplateGroupId().equals(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId())).findFirst().orElse(null) == null){ + if (!this.conventionService.isListNullOrEmpty(models) && !this.conventionService.isListNullOrEmpty(model.getDescriptions())){ + for (NewVersionPlanDescriptionPersist newVersionPlanDescriptionPersist : model.getDescriptions()) { + Description description = models.stream().filter(x -> x.getId().equals(newVersionPlanDescriptionPersist.getDescriptionId())).findFirst().orElse(null); + if (description != null){ + PlanDescriptionTemplateEntity existingPlanDescriptionTemplateEntity = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).sectionIds(newVersionPlanDescriptionPersist.getBlueprintSectionId()).descriptionTemplateGroupIds(description.getDescriptionTemplate().getGroupId()).first(); + if (existingPlanDescriptionTemplateEntity == null){ PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); newTemplate.setId(UUID.randomUUID()); newTemplate.setPlanId(newPlan.getId()); - newTemplate.setDescriptionTemplateGroupId(oldPlanDescriptionTemplate.getDescriptionTemplateGroupId()); - newTemplate.setSectionId(oldPlanDescriptionTemplate.getSectionId()); + newTemplate.setDescriptionTemplateGroupId(description.getDescriptionTemplate().getGroupId()); + newTemplate.setSectionId(newVersionPlanDescriptionPersist.getBlueprintSectionId()); newTemplate.setCreatedAt(Instant.now()); newTemplate.setUpdatedAt(Instant.now()); newTemplate.setIsActive(IsActive.Active); this.entityManager.persist(newTemplate); + this.entityManager.flush(); + this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), newTemplate.getId()); + } else{ + this.cloneDescription(newPlan.getId(), null, newVersionPlanDescriptionPersist.getDescriptionId(), existingPlanDescriptionTemplateEntity.getId()); } } } + } + + List newPlanDescriptionTemplateEntities = this.queryFactory.query(PlanDescriptionTemplateQuery.class).disableTracking().planIds(newPlan.getId()).isActive(IsActive.Active).collect(); + + 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())){ + 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); + isDescriptionTemplateAlreadyInPlan = existingBlueprintDescriptionTemplateEntity != null; + } + + if (!isDescriptionTemplateAlreadyInPlan){ + PlanDescriptionTemplateEntity newTemplate = new PlanDescriptionTemplateEntity(); + newTemplate.setId(UUID.randomUUID()); + newTemplate.setPlanId(newPlan.getId()); + newTemplate.setDescriptionTemplateGroupId(blueprintDescriptionTemplate.getDescriptionTemplateGroupId()); + newTemplate.setSectionId(section.getId()); + newTemplate.setCreatedAt(Instant.now()); + newTemplate.setUpdatedAt(Instant.now()); + newTemplate.setIsActive(IsActive.Active); + this.entityManager.persist(newTemplate); + this.entityManager.flush(); + } + } + } + } + } + } + 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();