From 15efa81131cfe76713fc15f74d908d2c4e8d5c4a Mon Sep 17 00:00:00 2001 From: "CITE\\amentis" Date: Thu, 29 Aug 2024 14:47:00 +0300 Subject: [PATCH] add blueprint description templates on import --- .../service/plan/PlanServiceImpl.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) 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 e8c0f267d..1aa26e97f 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 @@ -2475,12 +2475,32 @@ public class PlanServiceImpl implements PlanService { return persist; } - private List xmlPlanDescriptionTemplatesToPersist(PlanImportExport planXml){ + private List xmlPlanDescriptionTemplatesToPersist(PlanImportExport planXml) throws InvalidApplicationException { if (!this.conventionService.isListNullOrEmpty(planXml.getDescriptionTemplates())) { List descriptionTemplates = new ArrayList<>(); for (PlanDescriptionTemplateImportExport descriptionTemplate : planXml.getDescriptionTemplates()) { descriptionTemplates.add(this.xmlPlanDescriptionTemplateToPersist(descriptionTemplate)); } + // add description templates from blueprint + if (planXml.getBlueprint() != null && planXml.getBlueprint().getId() != null) { + PlanBlueprintEntity blueprintEntity = this.entityManager.find(PlanBlueprintEntity.class, planXml.getBlueprint().getId(), true); + if (blueprintEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{planXml.getBlueprint().getId(), PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); + org.opencdmp.commons.types.planblueprint.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(org.opencdmp.commons.types.planblueprint.DefinitionEntity.class, blueprintEntity.getDefinition()); + List sections = definition.getSections().stream().filter(SectionEntity::getHasTemplates).collect(Collectors.toList()); + for (SectionEntity section: sections) { + if (section.getHasTemplates() && !this.conventionService.isListNullOrEmpty(section.getDescriptionTemplates())){ + for (DescriptionTemplateEntity descriptionTemplateEntity: section.getDescriptionTemplates()){ + if (descriptionTemplates.stream().filter(x -> x.getDescriptionTemplateGroupId().equals(descriptionTemplateEntity.getDescriptionTemplateGroupId()) && x.getSectionId().equals(section.getId())).findFirst().orElse(null) == null) { + PlanDescriptionTemplateImportExport model = new PlanDescriptionTemplateImportExport(); + model.setDescriptionTemplateGroupId(descriptionTemplateEntity.getDescriptionTemplateGroupId()); + model.setSectionId(section.getId()); + descriptionTemplates.add(this.xmlPlanDescriptionTemplateToPersist(model)); + } + } + } + } + + } return descriptionTemplates.stream().filter(Objects::nonNull).toList(); } return null; @@ -2611,12 +2631,32 @@ public class PlanServiceImpl implements PlanService { return plan; } - private List commonModelPlanDescriptionTemplatesToPersist(PlanModel commonModel){ + private List commonModelPlanDescriptionTemplatesToPersist(PlanModel commonModel) throws InvalidApplicationException { if (!this.conventionService.isListNullOrEmpty(commonModel.getDescriptionTemplates())) { List descriptionTemplates = new ArrayList<>(); for (PlanDescriptionTemplateModel descriptionTemplate : commonModel.getDescriptionTemplates()) { descriptionTemplates.add(this.commonModelPlanDescriptionTemplateToPersist(descriptionTemplate)); } + // add description templates from blueprint + if (commonModel.getPlanBlueprint() != null && commonModel.getPlanBlueprint().getId() != null) { + PlanBlueprintEntity blueprintEntity = this.entityManager.find(PlanBlueprintEntity.class, commonModel.getPlanBlueprint().getId(), true); + if (blueprintEntity == null) throw new MyNotFoundException(this.messageSource.getMessage("General_ItemNotFound", new Object[]{commonModel.getPlanBlueprint().getId(), PlanBlueprint.class.getSimpleName()}, LocaleContextHolder.getLocale())); + org.opencdmp.commons.types.planblueprint.DefinitionEntity definition = this.xmlHandlingService.fromXmlSafe(org.opencdmp.commons.types.planblueprint.DefinitionEntity.class, blueprintEntity.getDefinition()); + List sections = definition.getSections().stream().filter(SectionEntity::getHasTemplates).collect(Collectors.toList()); + for (SectionEntity section: sections) { + if (section.getHasTemplates() && !this.conventionService.isListNullOrEmpty(section.getDescriptionTemplates())){ + for (DescriptionTemplateEntity descriptionTemplateEntity: section.getDescriptionTemplates()){ + if (descriptionTemplates.stream().filter(x -> x.getDescriptionTemplateGroupId().equals(descriptionTemplateEntity.getDescriptionTemplateGroupId()) && x.getSectionId().equals(section.getId())).findFirst().orElse(null) == null) { + PlanDescriptionTemplateModel model = new PlanDescriptionTemplateModel(); + model.setDescriptionTemplateGroupId(descriptionTemplateEntity.getDescriptionTemplateGroupId()); + model.setSectionId(section.getId()); + descriptionTemplates.add(this.commonModelPlanDescriptionTemplateToPersist(model)); + } + } + } + } + + } return descriptionTemplates.stream().filter(Objects::nonNull).toList(); } return null;