Author: adrianc
Date: Sun Nov  2 17:48:20 2014
New Revision: 1636158

URL: http://svn.apache.org/r1636158
Log:
Bug fix and code cleanup in form widget:

1. Cache keys must include delegator name. Multiple forms based on the same 
entity name might be completely different due to the delegator being used.

2. Do not hold references to ModelReader and DispatchContext after construction.

Modified:
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
    ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java

Modified: 
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java?rev=1636158&r1=1636157&r2=1636158&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java 
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/FormFactory.java Sun 
Nov  2 17:48:20 2014
@@ -59,7 +59,9 @@ public class FormFactory {
 
     public static ModelForm getFormFromLocation(String resourceName, String 
formName, ModelReader entityModelReader, DispatchContext dispatchContext)
             throws IOException, SAXException, ParserConfigurationException {
-        String cacheKey = resourceName + "#" + formName;
+        StringBuilder sb = new 
StringBuilder(dispatchContext.getDelegator().getDelegatorName());
+        sb.append(":").append(resourceName).append("#").append(formName);
+        String cacheKey = sb.toString();
         ModelForm modelForm = formLocationCache.get(cacheKey);
         if (modelForm == null) {
             URL formFileUrl = FlexibleLocation.resolveLocation(resourceName);

Modified: ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelForm.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelForm.java?rev=1636158&r1=1636157&r2=1636158&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelForm.java 
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelForm.java Sun 
Nov  2 17:48:20 2014
@@ -72,9 +72,6 @@ public class ModelForm extends ModelWidg
     public static final String module = ModelForm.class.getName();
     public static final String DEFAULT_FORM_RESULT_LIST_NAME = 
"defaultFormResultList";
 
-    protected ModelReader entityModelReader;
-    protected DispatchContext dispatchContext;
-
     protected String formLocation;
     protected String parentFormName;
     protected String parentFormLocation;
@@ -207,21 +204,14 @@ public class ModelForm extends ModelWidg
     /** XML Constructor */
     public ModelForm(Element formElement, ModelReader entityModelReader, 
DispatchContext dispatchContext) {
         super(formElement);
-        this.entityModelReader = entityModelReader;
-        this.dispatchContext = dispatchContext;
         try {
-            initForm(formElement);
+            initForm(formElement, entityModelReader, dispatchContext);
         } catch (RuntimeException e) {
             Debug.logError(e, "Error parsing form [" + 
formElement.getAttribute("name") + "]: " + e.toString(), module);
             throw e;
         }
     }
 
-    public ModelForm(Element formElement) {
-        super(formElement);
-        initForm(formElement);
-    }
-
     public String getTarget() {
         return target.getOriginal();
     }
@@ -246,7 +236,7 @@ public class ModelForm extends ModelWidg
         return autoFieldsServices;
     }
 
-    public void initForm(Element formElement) {
+    public void initForm(Element formElement, ModelReader entityModelReader, 
DispatchContext dispatchContext) {
 
         
setDefaultViewSize(UtilProperties.getPropertyValue("widget.properties", 
"widget.form.defaultViewSize"));
         // check if there is a parent form to inherit from
@@ -534,19 +524,19 @@ public class ModelForm extends ModelWidg
         // auto-fields-service
         for (Element autoFieldsServiceElement: 
UtilXml.childElementList(formElement, "auto-fields-service")) {
             AutoFieldsService autoFieldsService = new 
AutoFieldsService(autoFieldsServiceElement);
-            this.addAutoFieldsFromService(autoFieldsService);
+            this.addAutoFieldsFromService(autoFieldsService, 
entityModelReader, dispatchContext);
         }
 
         // auto-fields-entity
         for (Element autoFieldsEntityElement: 
UtilXml.childElementList(formElement, "auto-fields-entity")) {
             AutoFieldsEntity autoFieldsEntity = new 
AutoFieldsEntity(autoFieldsEntityElement);
-            this.addAutoFieldsFromEntity(autoFieldsEntity);
+            this.addAutoFieldsFromEntity(autoFieldsEntity, entityModelReader);
         }
 
         // read in add field defs, add/override one by one using the fieldList 
and fieldMap
         String thisType = this.getType();
         for (Element fieldElement: UtilXml.childElementList(formElement, 
"field")) {
-            ModelFormField modelFormField = new ModelFormField(fieldElement, 
this);
+            ModelFormField modelFormField = new ModelFormField(fieldElement, 
this, entityModelReader, dispatchContext);
             ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo();
             if (thisType.equals("multi") && fieldInfo instanceof 
ModelFormField.SubmitField) {
                multiSubmitFields.add(modelFormField);
@@ -760,13 +750,13 @@ public class ModelForm extends ModelWidg
         }
     }
 
-    public void addAutoFieldsFromService(AutoFieldsService autoFieldsService) {
+    private void addAutoFieldsFromService(AutoFieldsService autoFieldsService, 
ModelReader entityModelReader, DispatchContext dispatchContext) {
         autoFieldsServices.add(autoFieldsService);
 
         // read service def and auto-create fields
         ModelService modelService = null;
         try {
-            modelService = 
this.dispatchContext.getModelService(autoFieldsService.serviceName);
+            modelService = 
dispatchContext.getModelService(autoFieldsService.serviceName);
         } catch (GenericServiceException e) {
             String errmsg = "Error finding Service with name " + 
autoFieldsService.serviceName + " for auto-fields-service in a form widget";
             Debug.logError(e, errmsg, module);
@@ -782,7 +772,7 @@ public class ModelForm extends ModelWidg
                 if (UtilValidate.isNotEmpty(modelParam.entityName) && 
UtilValidate.isNotEmpty(modelParam.fieldName)) {
                     ModelEntity modelEntity;
                     try {
-                        modelEntity = 
this.entityModelReader.getModelEntity(modelParam.entityName);
+                        modelEntity = 
entityModelReader.getModelEntity(modelParam.entityName);
                         if (modelEntity != null) {
                             ModelField modelField = 
modelEntity.getField(modelParam.fieldName);
                             if (modelField != null) {
@@ -809,7 +799,7 @@ public class ModelForm extends ModelWidg
         }
     }
 
-    public ModelFormField addFieldFromServiceParam(ModelService modelService, 
ModelParam modelParam, String defaultFieldType, int defaultPosition) {
+    private ModelFormField addFieldFromServiceParam(ModelService modelService, 
ModelParam modelParam, String defaultFieldType, int defaultPosition) {
         // create field def from service param def
         ModelFormField newFormField = new ModelFormField(this);
         newFormField.setName(modelParam.name);
@@ -822,12 +812,12 @@ public class ModelForm extends ModelWidg
         return this.addUpdateField(newFormField);
     }
 
-    public void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity) {
+    private void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, 
ModelReader entityModelReader) {
         autoFieldsEntities.add(autoFieldsEntity);
         // read entity def and auto-create fields
         ModelEntity modelEntity = null;
         try {
-            modelEntity = 
this.entityModelReader.getModelEntity(autoFieldsEntity.entityName);
+            modelEntity = 
entityModelReader.getModelEntity(autoFieldsEntity.entityName);
         } catch (GenericEntityException e) {
             Debug.logError(e, module);
         }
@@ -849,7 +839,7 @@ public class ModelForm extends ModelWidg
         }
     }
 
-    public ModelFormField addFieldFromEntityField(ModelEntity modelEntity, 
ModelField modelField, String defaultFieldType, int defaultPosition) {
+    private ModelFormField addFieldFromEntityField(ModelEntity modelEntity, 
ModelField modelField, String defaultFieldType, int defaultPosition) {
         // create field def from entity field def
         ModelFormField newFormField = new ModelFormField(this);
         newFormField.setName(modelField.getName());

Modified: 
ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java
URL: 
http://svn.apache.org/viewvc/ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java?rev=1636158&r1=1636157&r2=1636158&view=diff
==============================================================================
--- ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java 
(original)
+++ ofbiz/trunk/framework/widget/src/org/ofbiz/widget/form/ModelFormField.java 
Sun Nov  2 17:48:20 2014
@@ -129,7 +129,7 @@ public class ModelFormField {
     }
 
     /** XML Constructor */
-    public ModelFormField(Element fieldElement, ModelForm modelForm) {
+    public ModelFormField(Element fieldElement, ModelForm modelForm, 
ModelReader entityModelReader, DispatchContext dispatchContext) {
         this.modelForm = modelForm;
         this.name = fieldElement.getAttribute("name");
         this.setMapName(fieldElement.getAttribute("map-name"));
@@ -182,7 +182,7 @@ public class ModelFormField {
 
             if (UtilValidate.isEmpty(subElementName)) {
                 this.fieldInfo = null;
-                this.induceFieldInfo(null); //no defaultFieldType specified 
here, will default to edit
+                this.induceFieldInfo(null, entityModelReader, 
dispatchContext); //no defaultFieldType specified here, will default to edit
             } else if ("display".equals(subElementName)) this.fieldInfo = new 
DisplayField(subElement, this);
             else if ("display-entity".equals(subElementName)) this.fieldInfo = 
new DisplayEntityField(subElement, this);
             else if ("hyperlink".equals(subElementName)) this.fieldInfo = new 
HyperlinkField(subElement, this);
@@ -209,20 +209,20 @@ public class ModelFormField {
         }
     }
 
-    public void addOnEventUpdateArea(UpdateArea updateArea) {
+    private void addOnEventUpdateArea(UpdateArea updateArea) {
         // Event types are sorted as a convenience for the rendering classes
         Debug.logInfo(this.modelForm.getName() + ":" + this.name + " adding 
UpdateArea type " + updateArea.getEventType(), module);
         if ("change".equals(updateArea.getEventType()))  
addOnChangeUpdateArea(updateArea);
         else if ("click".equals(updateArea.getEventType())) 
addOnClickUpdateArea(updateArea);
     }
 
-    protected void addOnChangeUpdateArea(UpdateArea updateArea) {
+    private void addOnChangeUpdateArea(UpdateArea updateArea) {
         if (onChangeUpdateAreas == null) onChangeUpdateAreas = new 
ArrayList<UpdateArea>();
         onChangeUpdateAreas.add(updateArea);
         Debug.logInfo(this.modelForm.getName() + ":" + this.name + " 
onChangeUpdateAreas size = " + onChangeUpdateAreas.size(), module);
     }
 
-    protected void addOnClickUpdateArea(UpdateArea updateArea) {
+    private void addOnClickUpdateArea(UpdateArea updateArea) {
         if (onClickUpdateAreas == null) onClickUpdateAreas = new 
ArrayList<UpdateArea>();
         onClickUpdateAreas.add(updateArea);
     }
@@ -261,16 +261,14 @@ public class ModelFormField {
         this.encodeOutput = overrideFormField.encodeOutput;
     }
 
-    public boolean induceFieldInfo(String defaultFieldType) {
-        if (this.induceFieldInfoFromEntityField(defaultFieldType)) return true;
-        if (this.induceFieldInfoFromServiceParam(defaultFieldType)) return 
true;
+    private boolean induceFieldInfo(String defaultFieldType, ModelReader 
entityModelReader, DispatchContext dispatchContext) {
+        if (this.induceFieldInfoFromEntityField(defaultFieldType, 
entityModelReader)) return true;
+        if (this.induceFieldInfoFromServiceParam(defaultFieldType, 
entityModelReader, dispatchContext)) return true;
         return false;
     }
 
-    public boolean induceFieldInfoFromServiceParam(String defaultFieldType) {
+    private boolean induceFieldInfoFromServiceParam(String defaultFieldType, 
ModelReader entityModelReader, DispatchContext dispatchContext) {
         if (UtilValidate.isEmpty(this.getServiceName()) || 
UtilValidate.isEmpty(this.getAttributeName()))  return false;
-
-        DispatchContext dispatchContext = this.getModelForm().dispatchContext;
         try {
             ModelService modelService = 
dispatchContext.getModelService(this.getServiceName());
             if (modelService != null) {
@@ -279,7 +277,7 @@ public class ModelFormField {
                     if (UtilValidate.isNotEmpty(modelParam.entityName) && 
UtilValidate.isNotEmpty(modelParam.fieldName)) {
                         this.entityName = modelParam.entityName;
                         this.fieldName = modelParam.fieldName;
-                        if 
(this.induceFieldInfoFromEntityField(defaultFieldType)) {
+                        if 
(this.induceFieldInfoFromEntityField(defaultFieldType, entityModelReader)) {
                             return true;
                         }
                     }
@@ -357,10 +355,8 @@ public class ModelFormField {
         return true;
     }
 
-    public boolean induceFieldInfoFromEntityField(String defaultFieldType) {
+    private boolean induceFieldInfoFromEntityField(String defaultFieldType, 
ModelReader entityModelReader) {
         if (UtilValidate.isEmpty(this.getEntityName()) || 
UtilValidate.isEmpty(this.getFieldName())) return false;
-        
-        ModelReader entityModelReader = this.getModelForm().entityModelReader;
         try {
             ModelEntity modelEntity = 
entityModelReader.getModelEntity(this.getEntityName());
             if (modelEntity != null) {


Reply via email to