This is an automated email from the ASF dual-hosted git repository.
mridulpathak pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/ofbiz-framework.git
The following commit(s) were added to refs/heads/trunk by this push:
new e9dd030756 Give a form its own copies of the field groups it inherits
e9dd030756 is described below
commit e9dd030756459a03977d5e1b091b2a3f2e6b3f8d
Author: Mridul Pathak <[email protected]>
AuthorDate: Wed Oct 28 21:38:51 2026 +0530
Give a form its own copies of the field groups it inherits
When a form extended another, it reused the parent's FieldGroup instances,
and each FieldGroup keeps the form that created it, so the child's inherited
groups opened and closed the single-form wrapper with the parent form and
decided whether to show a group from the parent's fields and use-when
conditions. The child now copies each inherited FieldGroup for itself, keeping
its id, style, title and collapsible flags, and maps the inherited field names
to the copies; banners hold no form [...]
---
.../org/apache/ofbiz/widget/model/ModelForm.java | 25 ++++++++++++++---
.../apache/ofbiz/widget/model/ModelFormTest.java | 31 ++++++++++++++++++++++
2 files changed, 52 insertions(+), 4 deletions(-)
diff --git
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelForm.java
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelForm.java
index 50ff92b36e..724a6517a8 100644
---
a/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelForm.java
+++
b/framework/widget/src/main/java/org/apache/ofbiz/widget/model/ModelForm.java
@@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
+import java.util.IdentityHashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
@@ -490,12 +491,19 @@ public abstract class ModelForm extends ModelWidget {
}
}
Map<String, FieldGroupBase> fieldGroupMap = new HashMap<>();
- if (parentModel != null) {
- fieldGroupMap.putAll(parentModel.fieldGroupMap);
- }
ArrayList<FieldGroupBase> fieldGroupList = new ArrayList<>();
if (parentModel != null) {
- fieldGroupList.addAll(parentModel.fieldGroupList);
+ // Copy the parent's field groups so that they render with this
form; banners hold no form and are shared
+ Map<FieldGroupBase, FieldGroupBase> inheritedFieldGroups = new
IdentityHashMap<>();
+ for (FieldGroupBase parentFieldGroup : parentModel.fieldGroupList)
{
+ FieldGroupBase fieldGroup = parentFieldGroup instanceof
FieldGroup
+ ? new FieldGroup((FieldGroup) parentFieldGroup, this)
: parentFieldGroup;
+ inheritedFieldGroups.put(parentFieldGroup, fieldGroup);
+ fieldGroupList.add(fieldGroup);
+ }
+ for (Map.Entry<String, FieldGroupBase> entry :
parentModel.fieldGroupMap.entrySet()) {
+ fieldGroupMap.put(entry.getKey(),
inheritedFieldGroups.getOrDefault(entry.getValue(), entry.getValue()));
+ }
}
ArrayList<String> lastOrderFields = new ArrayList<>();
if (parentModel != null) {
@@ -2142,6 +2150,15 @@ public abstract class ModelForm extends ModelWidget {
this.initiallyCollapsed = initiallyCollapsed;
}
+ private FieldGroup(FieldGroup parentFieldGroup, ModelForm modelForm) {
+ this.modelForm = modelForm;
+ this.id = parentFieldGroup.id;
+ this.style = parentFieldGroup.style;
+ this.title = parentFieldGroup.title;
+ this.collapsible = parentFieldGroup.collapsible;
+ this.initiallyCollapsed = parentFieldGroup.initiallyCollapsed;
+ }
+
/**
* Collapsible boolean.
* @return the boolean
diff --git
a/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
b/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
index a7e327cae8..3a2c2782bd 100644
---
a/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
+++
b/framework/widget/src/test/java/org/apache/ofbiz/widget/model/ModelFormTest.java
@@ -31,12 +31,16 @@ import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericValue;
import org.apache.ofbiz.webapp.control.JWTManager;
+import org.apache.ofbiz.widget.renderer.FormStringRenderer;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.w3c.dom.Element;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public final class ModelFormTest {
@@ -109,6 +113,33 @@ public final class ModelFormTest {
return UtilXml.firstChildElement(UtilXml.readXmlDocument(xml, false,
true).getDocumentElement(), tagName);
}
+ @Test
+ public void testInheritedFieldGroupRendersWithChildForm() throws Exception
{
+ String xml = "<forms>"
+ + "<form name=\"Parent\" type=\"single\">"
+ + "<field name=\"a\"><display/></field>"
+ + "<sort-order><field-group title=\"Group\"><sort-field
name=\"a\"/></field-group></sort-order>"
+ + "</form>"
+ + "<form name=\"Child\" type=\"single\" extends=\"Parent\"/>"
+ + "</forms>";
+ Element rootElement = UtilXml.readXmlDocument(xml, false,
true).getDocumentElement();
+ ModelSingleForm child = new
ModelSingleForm(UtilXml.childElementList(rootElement, "form").get(1), "test",
+ null, null, null);
+ ModelForm parent = child.getParentModelForm();
+ Assertions.assertNotNull(parent, "Child should have a parent");
+ ModelForm.FieldGroup fieldGroup = (ModelForm.FieldGroup)
child.getFieldGroupMap().get("a");
+ Assertions.assertTrue(child.getFieldGroupList().contains(fieldGroup),
"Field group should be in the child list");
+ Assertions.assertNotSame(parent.getFieldGroupMap().get("a"),
fieldGroup, "Field group should be the child's own");
+
+ FormStringRenderer renderer = Mockito.mock(FormStringRenderer.class);
+ StringBuilder writer = new StringBuilder();
+ fieldGroup.renderStartString(writer, context, renderer);
+ fieldGroup.renderEndString(writer, context, renderer);
+ verify(renderer).renderFormatSingleWrapperOpen(same(writer),
same(context), same(child));
+ verify(renderer).renderFormatSingleWrapperClose(same(writer),
same(context), same(child));
+ verify(renderer, never()).renderFormatSingleWrapperOpen(any(), any(),
same(parent));
+ }
+
private static CommonWidgetModels.Parameter
getParameterOnly(ModelForm.UpdateArea updateArea) {
Assertions.assertNotNull(updateArea.getParameterList());
Assertions.assertEquals(updateArea.getParameterList().size(), 1,
"Parameter size should be one");