Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/artifact/ArtifactInfoGatherer.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/artifact/ArtifactInfoGatherer.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/artifact/ArtifactInfoGatherer.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/artifact/ArtifactInfoGatherer.java Mon Jan 5 08:50:30 2015 @@ -265,16 +265,6 @@ public final class ArtifactInfoGatherer } @Override - public void visit(ModelFormAction.EntityAnd entityAnd) { - infoContext.addEntityName(entityAnd.getFinder().getEntityName()); - } - - @Override - public void visit(ModelFormAction.EntityCondition entityCondition) { - infoContext.addEntityName(entityCondition.getFinder().getEntityName()); - } - - @Override public void visit(ModelFormAction.Service service) { infoContext.addServiceName(service.getServiceName()); // TODO: Look for entityName in performFind service call Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FieldInfo.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FieldInfo.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FieldInfo.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FieldInfo.java Mon Jan 5 08:50:30 2015 @@ -31,7 +31,6 @@ import org.w3c.dom.Element; */ public abstract class FieldInfo { - public static final String module = FieldInfo.class.getName(); public static final int DISPLAY = 1; public static final int HYPERLINK = 2; public static final int TEXT = 3; Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FormRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FormRenderer.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FormRenderer.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/FormRenderer.java Mon Jan 5 08:50:30 2015 @@ -206,10 +206,6 @@ public class FormRenderer { * itemIndex (Integer, for lists only, otherwise null), bshInterpreter, * formName (String, optional alternate name for form, defaults to the * value of the name attribute) - * @param formStringRenderer An implementation of the FormStringRenderer - * interface that is responsible for the actual text generation for - * different form elements; implementing your own makes it possible to - * use the same form definitions for many types of form UIs */ public void render(Appendable writer, Map<String, Object> context) throws Exception { @@ -237,13 +233,7 @@ public class FormRenderer { positions = curPos; } FieldInfo currentFieldInfo = modelFormField.getFieldInfo(); - if (currentFieldInfo != null) { - ModelFormField fieldInfoFormField = currentFieldInfo.getModelFormField(); - if (fieldInfoFormField != null) { - // FIXME - //fieldInfoFormField.setModelForm(this); - } - } else { + if (currentFieldInfo == null) { throw new IllegalArgumentException( "Error rendering form, a field has no FieldInfo, ie no sub-element for the type of field for field named: " + modelFormField.getName()); Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/MacroFormRenderer.java Mon Jan 5 08:50:30 2015 @@ -40,6 +40,7 @@ import javax.servlet.http.HttpServletRes import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilCodec; import org.ofbiz.base.util.UtilFormatOut; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilHttp; @@ -92,7 +93,7 @@ public final class MacroFormRenderer imp public static final String module = MacroFormRenderer.class.getName(); private final Template macroLibrary; private final WeakHashMap<Appendable, Environment> environments = new WeakHashMap<Appendable, Environment>(); - private final StringUtil.SimpleEncoder internalEncoder; + private final UtilCodec.SimpleEncoder internalEncoder; private final RequestHandler rh; private final HttpServletRequest request; private final HttpServletResponse response; @@ -107,7 +108,7 @@ public final class MacroFormRenderer imp ServletContext ctx = (ServletContext) request.getAttribute("servletContext"); this.rh = (RequestHandler) ctx.getAttribute("_REQUEST_HANDLER_"); this.javaScriptEnabled = UtilHttp.isJavaScriptEnabled(request); - internalEncoder = StringUtil.getEncoder("string"); + internalEncoder = UtilCodec.getEncoder("string"); } @Deprecated @@ -157,7 +158,7 @@ public final class MacroFormRenderer imp if (UtilValidate.isEmpty(value)) { return value; } - StringUtil.SimpleEncoder encoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + UtilCodec.SimpleEncoder encoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (modelFormField.getEncodeOutput() && encoder != null) { value = encoder.encode(value); } else { @@ -355,7 +356,7 @@ public final class MacroFormRenderer imp mask = textField.getMask(); } String ajaxUrl = createAjaxParamsFromUpdateAreas(updateAreas, "", context); - boolean disabled = textField.disabled; + boolean disabled = textField.getDisabled(); StringWriter sr = new StringWriter(); sr.append("<@renderTextField "); sr.append("name=\""); @@ -541,17 +542,21 @@ public final class MacroFormRenderer imp localizedInputTitle = uiLabelMap.get("CommonFormatDateTime"); } } - String contextValue = null; - // If time-dropdown deactivate encodingOutput for found hour and minutes + /* + * FIXME: Using a builder here is a hack. Replace the builder with appropriate code. + */ + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(modelFormField); boolean memEncodeOutput = modelFormField.getEncodeOutput(); if (useTimeDropDown) - // FIXME: This is not thread-safe! Never modify a model's state! - modelFormField.setEncodeOutput(false); + // If time-dropdown deactivate encodingOutput for found hour and minutes + // FIXME: Encoding should be controlled by the renderer, not by the model. + builder.setEncodeOutput(false); // FIXME: modelFormField.getEntry ignores shortDateInput when converting Date objects to Strings. - // Object type conversion should be done by the renderer, not by the model. - contextValue = modelFormField.getEntry(context, dateTimeField.getDefaultValue(context)); - if (useTimeDropDown) - modelFormField.setEncodeOutput(memEncodeOutput); + if (useTimeDropDown) { + builder.setEncodeOutput(memEncodeOutput); + } + modelFormField = builder.build(); + String contextValue = modelFormField.getEntry(context, dateTimeField.getDefaultValue(context)); String value = contextValue; if (UtilValidate.isNotEmpty(value)) { if (value.length() > maxlength) { @@ -2024,7 +2029,7 @@ public final class MacroFormRenderer imp } String event = modelFormField.getEvent(); String action = modelFormField.getAction(context); - boolean readonly = lookupField.readonly; + boolean readonly = lookupField.getReadonly(); // add lookup pop-up button String descriptionFieldName = lookupField.getDescriptionFieldName(); ModelForm modelForm = modelFormField.getModelForm(); @@ -2937,9 +2942,9 @@ public final class MacroFormRenderer imp String encodedDescription = encode(description, modelFormField, context); // get the parameterized pagination index and size fields int paginatorNumber = WidgetWorker.getPaginatorNumber(context); - String viewIndexField = modelFormField.modelForm.getMultiPaginateIndexField(context); - String viewSizeField = modelFormField.modelForm.getMultiPaginateSizeField(context); - ModelForm modelForm = modelFormField.modelForm; + ModelForm modelForm = modelFormField.getModelForm(); + String viewIndexField = modelForm.getMultiPaginateIndexField(context); + String viewSizeField = modelForm.getMultiPaginateSizeField(context); int viewIndex = Paginator.getViewIndex(modelForm, context); int viewSize = Paginator.getViewSize(modelForm, context); if (viewIndexField.equals("viewIndex" + "_" + paginatorNumber)) { @@ -2951,7 +2956,7 @@ public final class MacroFormRenderer imp if ("hidden-form".equals(realLinkType)) { parameterMap.put(viewIndexField, Integer.toString(viewIndex)); parameterMap.put(viewSizeField, Integer.toString(viewSize)); - if (modelFormField != null && "multi".equals(modelFormField.getModelForm().getType())) { + if (modelFormField != null && "multi".equals(modelForm.getType())) { WidgetWorker.makeHiddenFormLinkAnchor(writer, linkStyle, encodedDescription, confirmation, modelFormField, request, response, context); // this is a bit trickier, since we can't do a nested form we'll have to put the link to submit the form in place, but put the actual form def elsewhere, ie after the big form is closed Map<String, Object> wholeFormContext = UtilGenerics.checkMap(context.get("wholeFormContext")); @@ -3083,7 +3088,7 @@ public final class MacroFormRenderer imp parameters.append(parameter.getName()); parameters.append("'"); parameters.append(",'value':'"); - parameters.append(StringUtil.htmlEncoder.encode(parameter.getValue(context))); + parameters.append(UtilCodec.getEncoder("html").encode(parameter.getValue(context))); parameters.append("'}"); } parameters.append("]"); Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelForm.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelForm.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelForm.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelForm.java Mon Jan 5 08:50:30 2015 @@ -34,6 +34,7 @@ import java.util.concurrent.atomic.Atomi import org.ofbiz.base.util.BshUtil; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.StringUtil; +import org.ofbiz.base.util.UtilCodec; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; @@ -560,15 +561,15 @@ public class ModelForm extends ModelWidg if (parentModelForm != null) { useWhenFields.addAll(parentModelForm.useWhenFields); } - ArrayList<ModelFormField> fieldList = new ArrayList<ModelFormField>(); - Map<String, ModelFormField> fieldMap = new HashMap<String, ModelFormField>(); + ArrayList<ModelFormFieldBuilder> fieldBuilderList = new ArrayList<ModelFormFieldBuilder>(); + Map<String, ModelFormFieldBuilder> fieldBuilderMap = new HashMap<String, ModelFormFieldBuilder>(); if (parentModelForm != null) { // Create this fieldList/Map from clones of parentModelForm's for (ModelFormField parentChildField : parentModelForm.fieldList) { - ModelFormField childField = new ModelFormField(this); - childField.mergeOverrideModelFormField(parentChildField); - fieldList.add(childField); - fieldMap.put(childField.getName(), childField); + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(parentChildField); + builder.setModelForm(this); + fieldBuilderList.add(builder); + fieldBuilderMap.put(builder.getName(), builder); } } Map<String, FieldGroupBase> fieldGroupMap = new HashMap<String, FieldGroupBase>(); @@ -731,7 +732,7 @@ public class ModelForm extends ModelWidg rowCountExdr = parentModelForm.rowCountExdr; } this.rowCountExdr = paginate; - ArrayList<ModelFormField> multiSubmitFields = new ArrayList<ModelFormField>(); + ArrayList<ModelFormFieldBuilder> multiSubmitBuilders = new ArrayList<ModelFormFieldBuilder>(); ArrayList<AutoFieldsService> autoFieldsServices = new ArrayList<AutoFieldsService>(); ArrayList<AutoFieldsEntity> autoFieldsEntities = new ArrayList<AutoFieldsEntity>(); ArrayList<SortField> sortOrderFields = new ArrayList<SortField>(); @@ -739,21 +740,21 @@ public class ModelForm extends ModelWidg for (Element autoFieldsServiceElement : UtilXml.childElementList(formElement, "auto-fields-service")) { AutoFieldsService autoFieldsService = new AutoFieldsService(autoFieldsServiceElement); autoFieldsServices.add(autoFieldsService); - addAutoFieldsFromService(autoFieldsService, entityModelReader, dispatchContext, useWhenFields, fieldList, fieldMap); + addAutoFieldsFromService(autoFieldsService, entityModelReader, dispatchContext, useWhenFields, fieldBuilderList, fieldBuilderMap); } for (Element autoFieldsEntityElement : UtilXml.childElementList(formElement, "auto-fields-entity")) { AutoFieldsEntity autoFieldsEntity = new AutoFieldsEntity(autoFieldsEntityElement); autoFieldsEntities.add(autoFieldsEntity); - addAutoFieldsFromEntity(autoFieldsEntity, entityModelReader, useWhenFields, fieldList, fieldMap); + addAutoFieldsFromEntity(autoFieldsEntity, entityModelReader, useWhenFields, fieldBuilderList, fieldBuilderMap); } String thisType = this.getType(); for (Element fieldElement : UtilXml.childElementList(formElement, "field")) { - ModelFormField modelFormField = new ModelFormField(fieldElement, this, entityModelReader, dispatchContext); - FieldInfo fieldInfo = modelFormField.getFieldInfo(); + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(fieldElement, this, entityModelReader, dispatchContext); + FieldInfo fieldInfo = builder.getFieldInfo(); if (thisType.equals("multi") && fieldInfo instanceof ModelFormField.SubmitField) { - multiSubmitFields.add(modelFormField); + multiSubmitBuilders.add(builder); } else { - modelFormField = addUpdateField(modelFormField, useWhenFields, fieldList, fieldMap); + addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap); } } // get the sort-order @@ -787,61 +788,67 @@ public class ModelForm extends ModelWidg } } if (sortOrderFields.size() > 0) { - ArrayList<ModelFormField> sortedFields = new ArrayList<ModelFormField>(); + ArrayList<ModelFormFieldBuilder> sortedFields = new ArrayList<ModelFormFieldBuilder>(); for (SortField sortField : sortOrderFields) { String fieldName = sortField.getFieldName(); if (UtilValidate.isEmpty(fieldName)) { continue; } // get all fields with the given name from the existing list and put them in the sorted list - Iterator<ModelFormField> fieldIter = fieldList.iterator(); + Iterator<ModelFormFieldBuilder> fieldIter = fieldBuilderList.iterator(); while (fieldIter.hasNext()) { - ModelFormField modelFormField = fieldIter.next(); - if (fieldName.equals(modelFormField.getName())) { + ModelFormFieldBuilder builder = fieldIter.next(); + if (fieldName.equals(builder.getName())) { // matched the name; remove from the original last and add to the sorted list if (UtilValidate.isNotEmpty(sortField.getPosition())) { - modelFormField.setPosition(sortField.getPosition()); + builder.setPosition(sortField.getPosition()); } fieldIter.remove(); - sortedFields.add(modelFormField); + sortedFields.add(builder); } } } // now add all of the rest of the fields from fieldList, ie those that were not explicitly listed in the sort order - sortedFields.addAll(fieldList); + sortedFields.addAll(fieldBuilderList); // sortedFields all done, set fieldList - fieldList = sortedFields; + fieldBuilderList = sortedFields; } if (UtilValidate.isNotEmpty(lastOrderFields)) { - List<ModelFormField> lastedFields = new LinkedList<ModelFormField>(); + List<ModelFormFieldBuilder> lastedFields = new LinkedList<ModelFormFieldBuilder>(); for (String fieldName : lastOrderFields) { if (UtilValidate.isEmpty(fieldName)) { continue; } // get all fields with the given name from the existing list and put them in the lasted list - Iterator<ModelFormField> fieldIter = fieldList.iterator(); + Iterator<ModelFormFieldBuilder> fieldIter = fieldBuilderList.iterator(); while (fieldIter.hasNext()) { - ModelFormField modelFormField = fieldIter.next(); - if (fieldName.equals(modelFormField.getName())) { + ModelFormFieldBuilder builder = fieldIter.next(); + if (fieldName.equals(builder.getName())) { // matched the name; remove from the original last and add to the lasted list fieldIter.remove(); - lastedFields.add(modelFormField); + lastedFields.add(builder); } } } //now put all lastedFields at the field list end - fieldList.addAll(lastedFields); + fieldBuilderList.addAll(lastedFields); + } + List<ModelFormField> fieldList = new ArrayList<ModelFormField>(fieldBuilderList.size()); + for (ModelFormFieldBuilder builder : fieldBuilderList) { + fieldList.add(builder.build()); } - this.useWhenFields = Collections.unmodifiableSet(useWhenFields); - fieldList.trimToSize(); this.fieldList = Collections.unmodifiableList(fieldList); + List<ModelFormField> multiSubmitFields = new ArrayList<ModelFormField>(multiSubmitBuilders.size()); + for (ModelFormFieldBuilder builder : multiSubmitBuilders) { + multiSubmitFields.add(builder.build()); + } + this.multiSubmitFields = Collections.unmodifiableList(multiSubmitFields); + this.useWhenFields = Collections.unmodifiableSet(useWhenFields); this.fieldGroupMap = Collections.unmodifiableMap(fieldGroupMap); fieldGroupList.trimToSize(); this.fieldGroupList = Collections.unmodifiableList(fieldGroupList); lastOrderFields.trimToSize(); this.lastOrderFields = Collections.unmodifiableList(lastOrderFields); - multiSubmitFields.trimToSize(); - this.multiSubmitFields = Collections.unmodifiableList(multiSubmitFields); autoFieldsServices.trimToSize(); this.autoFieldsServices = Collections.unmodifiableList(autoFieldsServices); autoFieldsEntities.trimToSize(); @@ -866,7 +873,7 @@ public class ModelForm extends ModelWidg } private void addAutoFieldsFromEntity(AutoFieldsEntity autoFieldsEntity, ModelReader entityModelReader, - Set<String> useWhenFields, List<ModelFormField> fieldList, Map<String, ModelFormField> fieldMap) { + Set<String> useWhenFields, List<ModelFormFieldBuilder> fieldBuilderList, Map<String, ModelFormFieldBuilder> fieldBuilderMap) { // read entity def and auto-create fields ModelEntity modelEntity = null; try { @@ -878,7 +885,6 @@ public class ModelForm extends ModelWidg throw new IllegalArgumentException("Error finding Entity with name " + autoFieldsEntity.entityName + " for auto-fields-entity in a form widget"); } - Iterator<ModelField> modelFieldIter = modelEntity.getFieldsIterator(); while (modelFieldIter.hasNext()) { ModelField modelField = modelFieldIter.next(); @@ -886,18 +892,23 @@ public class ModelForm extends ModelWidg // don't ever auto-add these, should only be added if explicitly referenced continue; } - ModelFormField modelFormField = this.addFieldFromEntityField(modelEntity, modelField, - autoFieldsEntity.defaultFieldType, autoFieldsEntity.defaultPosition, useWhenFields, fieldList, fieldMap); + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(); + builder.setModelForm(this); + builder.setName(modelField.getName()); + builder.setEntityName(modelEntity.getEntityName()); + builder.setFieldName(modelField.getName()); + builder.induceFieldInfoFromEntityField(modelEntity, modelField, autoFieldsEntity.defaultFieldType); + builder.setPosition(autoFieldsEntity.defaultPosition); if (UtilValidate.isNotEmpty(autoFieldsEntity.mapName)) { - modelFormField.setMapName(autoFieldsEntity.mapName); + builder.setMapName(autoFieldsEntity.mapName); } + addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap); } } private void addAutoFieldsFromService(AutoFieldsService autoFieldsService, ModelReader entityModelReader, - DispatchContext dispatchContext, Set<String> useWhenFields, List<ModelFormField> fieldList, - Map<String, ModelFormField> fieldMap) { - + DispatchContext dispatchContext, Set<String> useWhenFields, List<ModelFormFieldBuilder> fieldBuilderList, + Map<String, ModelFormFieldBuilder> fieldBuilderMap) { // read service def and auto-create fields ModelService modelService = null; try { @@ -908,11 +919,9 @@ public class ModelForm extends ModelWidg Debug.logError(e, errmsg, module); throw new IllegalArgumentException(errmsg); } - for (ModelParam modelParam : modelService.getInModelParamList()) { - // skip auto params that the service engine populates... - if ("userLogin".equals(modelParam.name) || "locale".equals(modelParam.name) || "timeZone".equals(modelParam.name) - || "login.username".equals(modelParam.name) || "login.password".equals(modelParam.name)) { + if (modelParam.internal) { + // skip auto params that the service engine populates... continue; } if (modelParam.formDisplay) { @@ -924,13 +933,17 @@ public class ModelForm extends ModelWidg ModelField modelField = modelEntity.getField(modelParam.fieldName); if (modelField != null) { // okay, populate using the entity field info... - ModelFormField modelFormField = addFieldFromEntityField(modelEntity, modelField, - autoFieldsService.defaultFieldType, autoFieldsService.defaultPosition, useWhenFields, - fieldList, fieldMap); + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(); + builder.setModelForm(this); + builder.setName(modelField.getName()); + builder.setEntityName(modelEntity.getEntityName()); + builder.setFieldName(modelField.getName()); + builder.induceFieldInfoFromEntityField(modelEntity, modelField, autoFieldsService.defaultFieldType); if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { - modelFormField.setMapName(autoFieldsService.mapName); + builder.setMapName(autoFieldsService.mapName); } - modelFormField.setRequiredField(!modelParam.optional); + builder.setRequiredField(!modelParam.optional); + addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap); // continue to skip creating based on service param continue; } @@ -939,79 +952,52 @@ public class ModelForm extends ModelWidg Debug.logError(e, module); } } - - ModelFormField modelFormField = this - .addFieldFromServiceParam(modelService, modelParam, autoFieldsService.defaultFieldType, - autoFieldsService.defaultPosition, useWhenFields, fieldList, fieldMap); + ModelFormFieldBuilder builder = new ModelFormFieldBuilder(); + builder.setModelForm(this); + builder.setName(modelParam.name); + builder.setServiceName(modelService.name); + builder.setAttributeName(modelParam.name); + builder.setTitle(modelParam.formLabel); + builder.setRequiredField(!modelParam.optional); + builder.induceFieldInfoFromServiceParam(modelService, modelParam, autoFieldsService.defaultFieldType); + builder.setPosition(autoFieldsService.defaultPosition); if (UtilValidate.isNotEmpty(autoFieldsService.mapName)) { - modelFormField.setMapName(autoFieldsService.mapName); + builder.setMapName(autoFieldsService.mapName); } + addUpdateField(builder, useWhenFields, fieldBuilderList, fieldBuilderMap); } } } - private ModelFormField addFieldFromEntityField(ModelEntity modelEntity, ModelField modelField, String defaultFieldType, - int defaultPosition, Set<String> useWhenFields, List<ModelFormField> fieldList, Map<String, ModelFormField> fieldMap) { - // create field def from entity field def - ModelFormField newFormField = new ModelFormField(this); - newFormField.setName(modelField.getName()); - newFormField.setEntityName(modelEntity.getEntityName()); - newFormField.setFieldName(modelField.getName()); - newFormField.induceFieldInfoFromEntityField(modelEntity, modelField, defaultFieldType); - newFormField.setPosition(defaultPosition); - return this.addUpdateField(newFormField, useWhenFields, fieldList, fieldMap); - } - - private ModelFormField addFieldFromServiceParam(ModelService modelService, ModelParam modelParam, String defaultFieldType, - int defaultPosition, Set<String> useWhenFields, List<ModelFormField> fieldList, Map<String, ModelFormField> fieldMap) { - // create field def from service param def - ModelFormField newFormField = new ModelFormField(this); - newFormField.setName(modelParam.name); - newFormField.setServiceName(modelService.name); - newFormField.setAttributeName(modelParam.name); - newFormField.setTitle(modelParam.formLabel); - newFormField.setRequiredField(!modelParam.optional); - newFormField.induceFieldInfoFromServiceParam(modelService, modelParam, defaultFieldType); - newFormField.setPosition(defaultPosition); - return this.addUpdateField(newFormField, useWhenFields, fieldList, fieldMap); - } - - /** - * add/override modelFormField using the fieldList and fieldMap - * - * @return The same ModelFormField, or if merged with an existing field, the existing field. - */ - private ModelFormField addUpdateField(ModelFormField modelFormField, Set<String> useWhenFields, - List<ModelFormField> fieldList, Map<String, ModelFormField> fieldMap) { - if (!modelFormField.isUseWhenEmpty() || useWhenFields.contains(modelFormField.getName())) { - useWhenFields.add(modelFormField.getName()); + private void addUpdateField(ModelFormFieldBuilder builder, Set<String> useWhenFields, + List<ModelFormFieldBuilder> fieldBuilderList, Map<String, ModelFormFieldBuilder> fieldBuilderMap) { + if (!builder.getUseWhen().isEmpty() || useWhenFields.contains(builder.getName())) { + useWhenFields.add(builder.getName()); // is a conditional field, add to the List but don't worry about the Map //for adding to list, see if there is another field with that name in the list and if so, put it before that one boolean inserted = false; - for (int i = 0; i < fieldList.size(); i++) { - ModelFormField curField = fieldList.get(i); - if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { - fieldList.add(i, modelFormField); + for (int i = 0; i < fieldBuilderList.size(); i++) { + ModelFormFieldBuilder curField = fieldBuilderList.get(i); + if (curField.getName() != null && curField.getName().equals(builder.getName())) { + fieldBuilderList.add(i, builder); inserted = true; break; } } if (!inserted) { - fieldList.add(modelFormField); + fieldBuilderList.add(builder); } - return modelFormField; + return; } else { // not a conditional field, see if a named field exists in Map - ModelFormField existingField = fieldMap.get(modelFormField.getName()); + ModelFormFieldBuilder existingField = fieldBuilderMap.get(builder.getName()); if (existingField != null) { // does exist, update the field by doing a merge/override - existingField.mergeOverrideModelFormField(modelFormField); - return existingField; + existingField.mergeOverrideModelFormField(builder); } else { // does not exist, add to List and Map - fieldList.add(modelFormField); - fieldMap.put(modelFormField.getName(), modelFormField); - return modelFormField; + fieldBuilderList.add(builder); + fieldBuilderMap.put(builder.getName(), builder); } } } @@ -1518,7 +1504,6 @@ public class ModelForm extends ModelWidg Debug.logError(e, errmsg, module); throw new IllegalArgumentException(errmsg); } - return styles; } @@ -1531,11 +1516,10 @@ public class ModelForm extends ModelWidg */ public String getTarget(Map<String, Object> context, String targetType) { Map<String, Object> expanderContext = context; - StringUtil.SimpleEncoder simpleEncoder = (StringUtil.SimpleEncoder) context.get("simpleEncoder"); + UtilCodec.SimpleEncoder simpleEncoder = (UtilCodec.SimpleEncoder) context.get("simpleEncoder"); if (simpleEncoder != null) { - expanderContext = StringUtil.HtmlEncodingMapWrapper.getHtmlEncodingMapWrapper(context, simpleEncoder); + expanderContext = UtilCodec.HtmlEncodingMapWrapper.getHtmlEncodingMapWrapper(context, simpleEncoder); } - try { // use the same Interpreter (ie with the same context setup) for all evals Interpreter bsh = this.getBshInterpreter(context); @@ -1561,7 +1545,6 @@ public class ModelForm extends ModelWidg Debug.logError(e, errmsg, module); throw new IllegalArgumentException(errmsg); } - return target.expandString(expanderContext); } Modified: ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java URL: http://svn.apache.org/viewvc/ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java?rev=1649482&r1=1649481&r2=1649482&view=diff ============================================================================== --- ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java (original) +++ ofbiz/branches/OFBIZ-5312-ofbiz-ecommerce-seo-2013-10-23/framework/widget/src/org/ofbiz/widget/form/ModelFormAction.java Mon Jan 5 08:50:30 2015 @@ -27,14 +27,11 @@ import java.util.Map; import java.util.regex.PatternSyntaxException; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilGenerics; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.base.util.UtilXml; import org.ofbiz.base.util.collections.FlexibleMapAccessor; import org.ofbiz.base.util.string.FlexibleStringExpander; -import org.ofbiz.entity.finder.ByAndFinder; -import org.ofbiz.entity.finder.ByConditionFinder; import org.ofbiz.entity.finder.EntityFinderUtil; import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.ModelService; @@ -44,7 +41,7 @@ import org.ofbiz.widget.WidgetWorker; import org.w3c.dom.Element; /** - * Widget Library - Screen model class + * Abstract form action. */ public abstract class ModelFormAction { @@ -56,92 +53,166 @@ public abstract class ModelFormAction { for (Element actionElement : UtilXml.childElementList(parentElement)) { if ("service".equals(actionElement.getNodeName())) { actions.add(new Service(modelForm, actionElement)); - } else if ("entity-and".equals(actionElement.getNodeName())) { - actions.add(new EntityAnd(modelForm, actionElement)); - } else if ("entity-condition".equals(actionElement.getNodeName())) { - actions.add(new EntityCondition(modelForm, actionElement)); + } else if ("entity-and".equals(actionElement.getNodeName()) || "entity-condition".equals(actionElement.getNodeName()) + || "get-related".equals(actionElement.getNodeName())) { + if (!actionElement.hasAttribute("list")) { + String listName = modelForm.getListName(); + if (UtilValidate.isEmpty(listName)) { + listName = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; + } + actionElement.setAttribute("list", listName); + } + actions.add(ModelWidgetAction.newInstance(modelForm, actionElement)); } else if ("call-parent-actions".equals(actionElement.getNodeName())) { actions.add(new CallParentActions(modelForm, actionElement)); } else { - actions.add(ModelWidgetAction.toModelWidgetAction(modelForm, actionElement)); + actions.add(ModelWidgetAction.newInstance(modelForm, actionElement)); } } return Collections.unmodifiableList(actions); } + /** + * Models the <call-parent-actions> element. + * + * @see <code>widget-form.xsd</code> + */ + @SuppressWarnings("serial") + public static class CallParentActions extends ModelWidgetAction { + private final ActionsKind kind;; + private final ModelForm modelForm; + + public CallParentActions(ModelForm modelForm, Element callParentActionsElement) { + super(modelForm, callParentActionsElement); + String parentName = callParentActionsElement.getParentNode().getNodeName(); + if ("actions".equals(parentName)) { + kind = ActionsKind.ACTIONS; + } else if ("row-actions".equals(parentName)) { + kind = ActionsKind.ROW_ACTIONS; + } else { + throw new IllegalArgumentException("Action element not supported for call-parent-actions : " + parentName); + } + ModelForm parentModel = modelForm.getParentModelForm(); + if (parentModel == null) { + throw new IllegalArgumentException("call-parent-actions can only be used with form extending another form"); + } + this.modelForm = modelForm; + } + + @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + @Override + public void runAction(Map<String, Object> context) { + ModelForm parentModel = modelForm.getParentModelForm(); + switch (kind) { + case ACTIONS: + parentModel.runFormActions(context); + break; + case ROW_ACTIONS: + ModelWidgetAction.runSubActions(parentModel.getRowActions(), context); + break; + } + } + + protected static enum ActionsKind { + ACTIONS, ROW_ACTIONS + } + } + + /** + * Models the <service> element. + * + * @see <code>widget-form.xsd</code> + */ @SuppressWarnings("serial") public static class Service extends ModelWidgetAction { - protected FlexibleStringExpander serviceNameExdr; - protected FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr; - protected FlexibleStringExpander autoFieldMapExdr; - protected FlexibleStringExpander resultMapListNameExdr; - protected Map<FlexibleMapAccessor<Object>, Object> fieldMap; - protected boolean ignoreError = false; + private final FlexibleStringExpander autoFieldMapExdr; + private final Map<FlexibleMapAccessor<Object>, Object> fieldMap; + private final boolean ignoreError; + private final FlexibleStringExpander resultMapListNameExdr; + private final FlexibleMapAccessor<Map<String, Object>> resultMapNameAcsr; + private final FlexibleStringExpander serviceNameExdr; public Service(ModelForm modelForm, Element serviceElement) { - super (modelForm, serviceElement); + super(modelForm, serviceElement); this.serviceNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("service-name")); this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map")); - if (this.resultMapNameAcsr.isEmpty()) this.resultMapNameAcsr = FlexibleMapAccessor.getInstance(serviceElement.getAttribute("result-map-name")); this.autoFieldMapExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("auto-field-map")); - if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list")) && UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-name"))) { - if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-iterator")) && UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-iterator-name"))) { + FlexibleStringExpander resultMapListNameExdr = FlexibleStringExpander.getInstance(""); + if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list")) + && UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-name"))) { + if (UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-iterator")) + && UtilValidate.isEmpty(serviceElement.getAttribute("result-map-list-iterator-name"))) { String lstNm = modelForm.getListName(); if (UtilValidate.isEmpty(lstNm)) { lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; } - this.resultMapListNameExdr = FlexibleStringExpander.getInstance(lstNm); + resultMapListNameExdr = FlexibleStringExpander.getInstance(lstNm); } else { // this is deprecated, but support it for now anyway - this.resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("result-map-list-iterator")); - if (this.resultMapListNameExdr.isEmpty()) this.resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("result-map-list-iterator-name")); + resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement + .getAttribute("result-map-list-iterator")); + if (resultMapListNameExdr.isEmpty()) + resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement + .getAttribute("result-map-list-iterator-name")); } } else { - this.resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("result-map-list")); - if (this.resultMapListNameExdr.isEmpty()) this.resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("result-map-list-name")); + resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement.getAttribute("result-map-list")); + if (resultMapListNameExdr.isEmpty()) + resultMapListNameExdr = FlexibleStringExpander.getInstance(serviceElement + .getAttribute("result-map-list-name")); } - + this.resultMapListNameExdr = resultMapListNameExdr; this.fieldMap = EntityFinderUtil.makeFieldMap(serviceElement); this.ignoreError = "true".equals(serviceElement.getAttribute("ignore-error")); } @Override + public void accept(ModelActionVisitor visitor) { + visitor.visit(this); + } + + public String getServiceName() { + return serviceNameExdr.getOriginal(); + } + + @Override public void runAction(Map<String, Object> context) { String serviceNameExpanded = this.serviceNameExdr.expandString(context); if (UtilValidate.isEmpty(serviceNameExpanded)) { throw new IllegalArgumentException("Service name was empty, expanded from: " + this.serviceNameExdr.getOriginal()); } - String autoFieldMapString = this.autoFieldMapExdr.expandString(context); boolean autoFieldMapBool = !"false".equals(autoFieldMapString); - try { Map<String, Object> serviceContext = null; if (autoFieldMapBool) { - if (! "true".equals(autoFieldMapString)) { + if (!"true".equals(autoFieldMapString)) { Map<String, Object> autoFieldMap = UtilGenerics.checkMap(context.get(autoFieldMapString)); - serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap); + serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext() + .makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, autoFieldMap); } else { - serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext().makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context); + serviceContext = WidgetWorker.getDispatcher(context).getDispatchContext() + .makeValidContext(serviceNameExpanded, ModelService.IN_PARAM, context); } } else { serviceContext = new HashMap<String, Object>(); } - if (this.fieldMap != null) { EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, serviceContext); } - Map<String, Object> result = null; if (this.ignoreError) { result = WidgetWorker.getDispatcher(context).runSync(serviceNameExpanded, serviceContext, -1, true); } else { result = WidgetWorker.getDispatcher(context).runSync(serviceNameExpanded, serviceContext); } - if (!this.resultMapNameAcsr.isEmpty()) { this.resultMapNameAcsr.put(context, result); - String queryString = (String)result.get("queryString"); + String queryString = (String) result.get("queryString"); context.put("queryString", queryString); context.put("queryStringMap", result.get("queryStringMap")); if (UtilValidate.isNotEmpty(queryString)) { @@ -159,189 +230,21 @@ public abstract class ModelFormAction { Object listObj = result.get(listName); if (listObj != null) { if (!(listObj instanceof List<?>) && !(listObj instanceof ListIterator<?>)) { - throw new IllegalArgumentException("Error in form [" + this.modelWidget.getName() + "] calling service with name [" + serviceNameExpanded + "]: the result that is supposed to be a List or ListIterator and is not."); + throw new IllegalArgumentException("Error in form [" + this.getModelWidget().getName() + + "] calling service with name [" + serviceNameExpanded + + "]: the result that is supposed to be a List or ListIterator and is not."); } context.put("listName", listName); context.put(listName, listObj); } } catch (GenericServiceException e) { - String errMsg = "Error in form [" + this.modelWidget.getName() + "] calling service with name [" + serviceNameExpanded + "]: " + e.toString(); + String errMsg = "Error in form [" + this.getModelWidget().getName() + "] calling service with name [" + + serviceNameExpanded + "]: " + e.toString(); Debug.logError(e, errMsg, module); if (!this.ignoreError) { throw new IllegalArgumentException(errMsg); } } } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - - public String getServiceName() { - return serviceNameExdr.getOriginal(); - } - } - - @SuppressWarnings("serial") - public static class EntityAnd extends ModelWidgetAction { - protected ByAndFinder finder; - - public EntityAnd(ModelForm modelForm, Element entityAndElement) { - super (modelForm, entityAndElement); - - //don't want to default to the iterator, should be specified explicitly, not the default - // Document ownerDoc = entityAndElement.getOwnerDocument(); - // boolean useCache = "true".equalsIgnoreCase(entityAndElement.getAttribute("use-cache")); - // if (!useCache) UtilXml.addChildElement(entityAndElement, "use-iterator", ownerDoc); - - // make list-name optional - if (UtilValidate.isEmpty(entityAndElement.getAttribute("list")) && UtilValidate.isEmpty(entityAndElement.getAttribute("list-name"))) { - String lstNm = modelForm.getListName(); - if (UtilValidate.isEmpty(lstNm)) { - lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; - } - entityAndElement.setAttribute("list", lstNm); - } - finder = new ByAndFinder(entityAndElement); - } - - @Override - public void runAction(Map<String, Object> context) { - try { - // don't want to do this: context.put("defaultFormResultList", null); - finder.runFind(context, WidgetWorker.getDelegator(context)); - - /* NOTE DEJ20100925: this should not be running any more as it causes actions in a list or multi - * form definition to overwrite the desired list elsewhere, this was the really old way of doing - * it that was removed a long time ago and needs to stay gone to avoid issues; the form's list - * should be found by explicitly matching the name: - Object obj = context.get(this.actualListName); - if (obj != null && ((obj instanceof List) || (obj instanceof EntityListIterator))) { - String modelFormListName = modelForm.getListName(); - context.put(modelFormListName, obj); - } - */ - } catch (GeneralException e) { - String errMsg = "Error doing entity query by condition: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); - } - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - - public ByAndFinder getFinder() { - return finder; - } - } - - @SuppressWarnings("serial") - public static class EntityCondition extends ModelWidgetAction { - ByConditionFinder finder; - String actualListName; - - public EntityCondition(ModelForm modelForm, Element entityConditionElement) { - super (modelForm, entityConditionElement); - - //don't want to default to the iterator, should be specified explicitly, not the default - // Document ownerDoc = entityConditionElement.getOwnerDocument(); - // boolean useCache = "true".equalsIgnoreCase(entityConditionElement.getAttribute("use-cache")); - // if (!useCache) UtilXml.addChildElement(entityConditionElement, "use-iterator", ownerDoc); - - // make list-name optional - if (UtilValidate.isEmpty(entityConditionElement.getAttribute("list")) && UtilValidate.isEmpty(entityConditionElement.getAttribute("list-name"))) { - String lstNm = modelForm.getListName(); - if (UtilValidate.isEmpty(lstNm)) { - lstNm = ModelForm.DEFAULT_FORM_RESULT_LIST_NAME; - } - entityConditionElement.setAttribute("list", lstNm); - } - this.actualListName = entityConditionElement.getAttribute("list"); - if (UtilValidate.isEmpty(this.actualListName)) this.actualListName = entityConditionElement.getAttribute("list-name"); - finder = new ByConditionFinder(entityConditionElement); - } - - @Override - public void runAction(Map<String, Object> context) { - try { - // don't want to do this: context.put("defaultFormResultList", null); - finder.runFind(context, WidgetWorker.getDelegator(context)); - - /* NOTE DEJ20100925: this should not be running any more as it causes actions in a list or multi - * form definition to overwrite the desired list elsewhere, this was the really old way of doing - * it that was removed a long time ago and needs to stay gone to avoid issues; the form's list - * should be found by explicitly matching the name: - Object obj = context.get(this.actualListName); - if (obj != null && ((obj instanceof List) || (obj instanceof EntityListIterator))) { - String modelFormListName = modelForm.getListName(); - context.put(modelFormListName, obj); - } - */ - } catch (GeneralException e) { - String errMsg = "Error doing entity query by condition: " + e.toString(); - Debug.logError(e, errMsg, module); - throw new IllegalArgumentException(errMsg); - } - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } - - public ByConditionFinder getFinder() { - return finder; - } - } - - @SuppressWarnings("serial") - public static class CallParentActions extends ModelWidgetAction { - protected static enum ActionsKind { - ACTIONS, - ROW_ACTIONS - }; - - protected ActionsKind kind; - private final ModelForm modelForm; - - public CallParentActions(ModelForm modelForm, Element callParentActionsElement) { - super(modelForm, callParentActionsElement); - String parentName = callParentActionsElement.getParentNode().getNodeName(); - if ("actions".equals(parentName)) { - kind = ActionsKind.ACTIONS; - } else if ("row-actions".equals(parentName)) { - kind = ActionsKind.ROW_ACTIONS; - } else { - throw new IllegalArgumentException("Action element not supported for call-parent-actions : " + parentName); - } - - ModelForm parentModel = modelForm.getParentModelForm(); - if (parentModel == null) { - throw new IllegalArgumentException("call-parent-actions can only be used with form extending another form"); - } - this.modelForm = modelForm; - } - - @Override - public void runAction(Map<String, Object> context) { - ModelForm parentModel = modelForm.getParentModelForm(); - switch (kind) { - case ACTIONS: - parentModel.runFormActions(context); - break; - case ROW_ACTIONS: - ModelWidgetAction.runSubActions(parentModel.getRowActions(), context); - break; - } - } - - @Override - public void accept(ModelActionVisitor visitor) { - visitor.visit(this); - } } } |
Free forum by Nabble | Edit this page |