From 93cbd788f5ae95eb69a5471037940f8188e1acf6 Mon Sep 17 00:00:00 2001 From: amentis Date: Thu, 18 Jul 2024 16:34:40 +0300 Subject: [PATCH] fix reassign visibility rules on description template clone --- .../model/descriptiontemplate/Definition.java | 29 ++++++++++++ .../model/descriptiontemplate/FieldSet.java | 9 ++++ .../model/descriptiontemplate/Page.java | 21 +++++++++ .../model/descriptiontemplate/Section.java | 29 ++++++++++++ .../DescriptionTemplateServiceImpl.java | 46 +++++++++++++++---- 5 files changed, 124 insertions(+), 10 deletions(-) diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Definition.java b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Definition.java index 7c70b162b..426ed846d 100644 --- a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Definition.java +++ b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Definition.java @@ -1,5 +1,6 @@ package org.opencdmp.model.descriptiontemplate; +import java.util.ArrayList; import java.util.List; public class Definition { @@ -14,4 +15,32 @@ public class Definition { public void setPages(List pages) { this.pages = pages; } + + public List getAllField(){ + List fields = new ArrayList<>(); + if (this.getPages() != null){ + for (Page page: this.getPages()) { + fields.addAll(page.getAllField()); + } + } + return fields; + } + + public List
getAllFieldSets(){ + List
fieldSets = new ArrayList<>(); + if (this.getPages() != null){ + for (Page page: this.getPages()) { + fieldSets.addAll(page.getAllFieldSets()); + } + } + return fieldSets; + } + + public List
getFieldSetById(String id) { + return this.getAllFieldSets().stream().filter(x-> id.equals(x.getId())).toList(); + } + + public List getFieldById(String id) { + return this.getAllField().stream().filter(x-> id.equals(x.getId())).toList(); + } } diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/FieldSet.java b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/FieldSet.java index 6127fa76b..b80a94efd 100644 --- a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/FieldSet.java +++ b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/FieldSet.java @@ -1,5 +1,6 @@ package org.opencdmp.model.descriptiontemplate; +import java.util.ArrayList; import java.util.List; public class FieldSet { @@ -113,4 +114,12 @@ public class FieldSet { public void setFields(List fields) { this.fields = fields; } + + public List getAllField() { + return this.getFields() == null ? new ArrayList<>() : this.getFields(); + } + + public List getFieldById(String id) { + return this.getAllField().stream().filter(x-> id.equals(x.getId())).toList(); + } } diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Page.java b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Page.java index a7e6e90b1..cf72f317d 100644 --- a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Page.java +++ b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Page.java @@ -1,6 +1,7 @@ package org.opencdmp.model.descriptiontemplate; +import java.util.ArrayList; import java.util.List; public class Page { @@ -52,6 +53,26 @@ public class Page { public void setSections(List
sections) { this.sections = sections; } + + public List getAllField(){ + List fields = new ArrayList<>(); + if (this.getSections() != null){ + for (Section section: this.getSections()) { + fields.addAll(section.getAllField()); + } + } + return fields; + } + + public List
getAllFieldSets(){ + List
fieldSets = new ArrayList<>(); + if (this.getSections() != null){ + for (Section section: this.getSections()) { + fieldSets.addAll(section.getAllFieldSets()); + } + } + return fieldSets; + } } diff --git a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Section.java b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Section.java index 0613040d6..62ae2f60c 100644 --- a/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Section.java +++ b/backend/core/src/main/java/org/opencdmp/model/descriptiontemplate/Section.java @@ -1,6 +1,7 @@ package org.opencdmp.model.descriptiontemplate; +import java.util.ArrayList; import java.util.List; public class Section { @@ -81,6 +82,34 @@ public class Section { public void setFieldSets(List
fieldSets) { this.fieldSets = fieldSets; } + + public List getAllField(){ + List fields = new ArrayList<>(); + if (this.getFieldSets() != null){ + for (FieldSet fieldSet: this.getFieldSets()) { + fields.addAll(fieldSet.getAllField()); + } + } + if (this.getSections() != null){ + for (Section sectionEntity: this.getSections()) { + fields.addAll(sectionEntity.getAllField()); + } + } + return fields; + } + + public List
getAllFieldSets(){ + List
fieldSet = new ArrayList<>(); + if (this.getFieldSets() != null){ + fieldSet.addAll(this.getFieldSets()); + } + if (this.getSections() != null){ + for (Section section: this.getSections()) { + fieldSet.addAll(section.getAllFieldSets()); + } + } + return fieldSet; + } } diff --git a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java index d69176492..6fa290663 100644 --- a/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java +++ b/backend/core/src/main/java/org/opencdmp/service/descriptiontemplate/DescriptionTemplateServiceImpl.java @@ -558,59 +558,85 @@ public class DescriptionTemplateServiceImpl implements DescriptionTemplateServic if (model == null) return; + Map visibilityRulesMap = new HashMap<>(); + List fields = model.getAllField(); + if (!this.conventionService.isListNullOrEmpty(fields)){ + for (Field field: fields) { + if (!this.conventionService.isListNullOrEmpty(field.getVisibilityRules())){ + field.getVisibilityRules().stream().map(x -> x.getTarget()).collect(Collectors.toList()).forEach(y -> { + visibilityRulesMap.put(y, null); + }); + } + } + + } if (model.getPages() != null) { for (Page page : model.getPages()) { - this.reassignPage(page); + this.reassignPage(page, visibilityRulesMap); + } + } + + for (Field field: model.getAllField()) { + if (!this.conventionService.isListNullOrEmpty(field.getVisibilityRules())){ + for (Rule rule: field.getVisibilityRules()) { + if (visibilityRulesMap.containsKey(rule.getTarget())) rule.setTarget(visibilityRulesMap.get(rule.getTarget())); + } } } } - private void reassignPage(Page model) { + private void reassignPage(Page model, Map visibilityRulesMap) { if (model == null) return; model.setId(UUID.randomUUID().toString()); if (model.getSections() != null) { for (Section section : model.getSections()) { - this.reassignSection(section); + this.reassignSection(section, visibilityRulesMap); } } } - private void reassignSection(Section model) { + private void reassignSection(Section model, Map visibilityRulesMap) { if (model == null) return; model.setId(UUID.randomUUID().toString()); if (model.getSections() != null) { for (Section section : model.getSections()) { - this.reassignSection(section); + this.reassignSection(section, visibilityRulesMap); } } if (model.getFieldSets() != null) { for (org.opencdmp.model.descriptiontemplate.FieldSet fieldSet : model.getFieldSets()) { - this.reassignFieldSet(fieldSet); + this.reassignFieldSet(fieldSet, visibilityRulesMap); } } } - private void reassignFieldSet(org.opencdmp.model.descriptiontemplate.FieldSet model) { + private void reassignFieldSet(org.opencdmp.model.descriptiontemplate.FieldSet model, Map visibilityRulesMap) { if (model == null) return; model.setId(UUID.randomUUID().toString()); if (model.getFields() != null) { for (Field field : model.getFields()) { - this.reassignField(field); + this.reassignField(field, visibilityRulesMap); } } } - private void reassignField(Field model) { + private void reassignField(Field model, Map visibilityRulesMap) { if (model == null) return; - model.setId(UUID.randomUUID().toString()); + String oldFieldId = model.getId(); + if (visibilityRulesMap != null && visibilityRulesMap.containsKey(oldFieldId)){ + model.setId(UUID.randomUUID().toString()); + visibilityRulesMap.put(oldFieldId, model.getId()); + } else { + model.setId(UUID.randomUUID().toString()); + } } //endregion