Hi,
I try to make more precise the form use-when clause interpreter, particularly for the single form. I attach the patch related Thanks Marco Index: framework/widget/src/org/ofbiz/widget/form/ModelForm.java =================================================================== --- framework/widget/src/org/ofbiz/widget/form/ModelForm.java (revision 735630) +++ framework/widget/src/org/ofbiz/widget/form/ModelForm.java (working copy) @@ -6,9 +6,9 @@ * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.Iterator; import java.util.List; +import java.util.ListIterator; import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; @@ -48,6 +49,7 @@ import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericDelegator; import org.ofbiz.entity.GenericEntityException; +import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.model.ModelEntity; import org.ofbiz.entity.model.ModelField; import org.ofbiz.entity.model.ModelReader; @@ -115,9 +117,9 @@ protected boolean useRowSubmit = false; protected FlexibleStringExpander targetWindowExdr; protected String defaultRequiredFieldStyle; - protected String defaultSortFieldStyle; + protected String defaultSortFieldStyle; protected String defaultSortFieldAscStyle; - protected String defaultSortFieldDescStyle; + protected String defaultSortFieldDescStyle; protected String oddRowStyle; protected String evenRowStyle; protected String defaultTableStyle; @@ -155,17 +157,17 @@ * Can also include Banner objects. */ protected List<FieldGroupBase> fieldGroupList = FastList.newInstance(); - + /** This Map is keyed with the field name and has a FieldGroup for the value. * Can also include Banner objects. */ protected Map<String, FieldGroupBase> fieldGroupMap = FastMap.newInstance(); - + /** This field group will be the "catch-all" group for fields that are not * included in an explicit field-group. */ protected FieldGroup defaultFieldGroup; - + /** Default hyperlink target. */ public static String DEFAULT_TARGET_TYPE = "intra-app"; @@ -179,18 +181,18 @@ public static String DEFAULT_PAG_PREV_STYLE = "nav-previous"; public static String DEFAULT_PAG_NEXT_STYLE = "nav-next"; public static String DEFAULT_PAG_LAST_STYLE = "nav-last"; - + /** Sort field default styles. */ public static String DEFAULT_SORT_FIELD_STYLE = "sort-order"; public static String DEFAULT_SORT_FIELD_ASC_STYLE = "sort-order-asc"; public static String DEFAULT_SORT_FIELD_DESC_STYLE = "sort-order-desc"; - + protected List<ModelFormAction> actions; protected List<ModelFormAction> rowActions; protected FlexibleStringExpander rowCountExdr; protected List<ModelFormField> multiSubmitFields = FastList.newInstance(); protected int rowCount = 0; - + /** On Submit areas to be updated. */ protected List<UpdateArea> onSubmitUpdateAreas; /** On Paginate areas to be updated. */ @@ -207,12 +209,12 @@ this.dispatchContext = dispatchContext; initForm(formElement); } - + public ModelForm(Element formElement) { super(formElement); initForm(formElement); } - + public void initForm(Element formElement) { setDefaultViewSize(UtilProperties.getPropertyValue("widget.properties", "widget.form.defaultViewSize")); @@ -293,7 +295,7 @@ this.onSubmitUpdateAreas = parent.onSubmitUpdateAreas; this.onPaginateUpdateAreas = parent.onPaginateUpdateAreas; this.altRowStyles = parent.altRowStyles; - + //these are done below in a special way... //this.fieldList = parent.fieldList; //this.fieldMap = parent.fieldMap; @@ -399,13 +401,13 @@ } if (this.defaultSortFieldStyle == null || formElement.hasAttribute("default-sort-field-style")) { this.defaultSortFieldStyle = formElement.getAttribute("default-sort-field-style"); - } + } if (this.defaultSortFieldAscStyle == null || formElement.hasAttribute("default-sort-field-asc-style")) { this.defaultSortFieldAscStyle = formElement.getAttribute("default-sort-field-asc-style"); } if (this.defaultSortFieldDescStyle == null || formElement.hasAttribute("default-sort-field-desc-style")) { this.defaultSortFieldDescStyle = formElement.getAttribute("default-sort-field-desc-style"); - } + } // pagination settings if (this.paginateTarget == null || formElement.hasAttribute("paginate-target")) { @@ -438,7 +440,7 @@ if (this.paginateStyle == null || formElement.hasAttribute("paginate-style")) { setPaginateStyle(formElement.getAttribute("paginate-style")); } - + this.paginate = "true".equals(formElement.getAttribute("paginate")); this.skipStart = "true".equals(formElement.getAttribute("skip-start")); this.skipEnd = "true".equals(formElement.getAttribute("skip-end")); @@ -466,7 +468,7 @@ AltRowStyle altRowStyle = new AltRowStyle(altRowStyleElement); this.altRowStyles.add(altRowStyle); } - + // alt-target List altTargetElements = UtilXml.childElementList(formElement, "alt-target"); Iterator altTargetElementIter = altTargetElements.iterator(); @@ -475,14 +477,14 @@ AltTarget altTarget = new AltTarget(altTargetElement); this.addAltTarget(altTarget); } - + // on-event-update-area List<? extends Element> updateAreaElements = UtilXml.childElementList(formElement, "on-event-update-area"); for (Element updateAreaElement : updateAreaElements) { UpdateArea updateArea = new UpdateArea(updateAreaElement); this.addOnEventUpdateArea(updateArea); } - + // auto-fields-service List autoFieldsServiceElements = UtilXml.childElementList(formElement, "auto-fields-service"); Iterator autoFieldsServiceElementIter = autoFieldsServiceElements.iterator(); @@ -537,13 +539,13 @@ } else if (tagName.equals("banner")) { Banner thisBanner = new Banner(sortFieldElement, this); this.fieldGroupList.add(thisBanner); - + lastFieldGroup = new FieldGroup(null, this); this.fieldGroupList.add(lastFieldGroup); } else if (tagName.equals("field-group")) { FieldGroup thisFieldGroup = new FieldGroup(sortFieldElement, this); this.fieldGroupList.add(thisFieldGroup); - + lastFieldGroup = new FieldGroup(null, this); this.fieldGroupList.add(lastFieldGroup); } @@ -779,6 +781,47 @@ ModelFormAction.runSubActions(this.actions, context); } + protected List<ModelFormField> filterFieldToShow(List<ModelFormField> originList, Map<String, Object> context) { + List<ModelFormField> tempFieldList = FastList.newInstance(); + tempFieldList.addAll(originList); + + // Check to see if there is a field, same name and same use-when (could come from extended form) + if ("single".equals(this.getType())) { + for (int j = 0; j < tempFieldList.size(); j++) { + ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); + if (!modelFormField.isUseWhenEmpty()) { + boolean shouldUse1 = modelFormField.shouldUse(context); + for (int i = j+1; i < tempFieldList.size(); i++) { + ModelFormField curField = (ModelFormField) tempFieldList.get(i); + boolean shouldUse2 = curField.shouldUse(context); + if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { + if (!shouldUse2 || shouldUse1 == shouldUse2) { + tempFieldList.remove(i--); + } + } else { + if (!shouldUse2) { + tempFieldList.remove(i--); + }; + } + } + if (!shouldUse1) + tempFieldList.remove(j--); + } + } + } else { + for (int j = 0; j < tempFieldList.size(); j++) { + ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); + for (int i = j+1; i < tempFieldList.size(); i++) { + ModelFormField curField = (ModelFormField) tempFieldList.get(i); + if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { + tempFieldList.remove(i--); + } + } + } + } + return tempFieldList; + } + /** * Renders this form to a String, i.e. in a text format, as defined with the * FormStringRenderer implementation. @@ -796,7 +839,7 @@ */ public void renderFormString(Appendable writer, Map<String, Object> context, FormStringRenderer formStringRenderer) throws IOException { runFormActions(context); - + setWidgetBoundaryComments(context); // if this is a list form, don't useRequestParameters @@ -838,27 +881,7 @@ } public void renderSingleFormString(Appendable writer, Map<String, Object> context, FormStringRenderer formStringRenderer, int positions) throws IOException { - List<ModelFormField> tempFieldList = FastList.newInstance(); - tempFieldList.addAll(this.fieldList); - - // Check to see if there is a field, same name and same use-when (could come from extended form) - for (int j = 0; j < tempFieldList.size(); j++) { - ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); - if (!modelFormField.isUseWhenEmpty()) { - boolean shouldUse1 = modelFormField.shouldUse(context); - for (int i = j+1; i < tempFieldList.size(); i++) { - ModelFormField curField = (ModelFormField) tempFieldList.get(i); - if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { - boolean shouldUse2 = curField.shouldUse(context); - if (shouldUse1 == shouldUse2) { - tempFieldList.remove(i--); - } - } else { - continue; - } - } - } - } + List<ModelFormField> tempFieldList = filterFieldToShow(this.fieldList, context); Set<String> alreadyRendered = new TreeSet<String>(); FieldGroup lastFieldGroup = null; @@ -884,21 +907,21 @@ if (fieldIter.hasNext()) { nextFormField = (ModelFormField) fieldIter.next(); } - + FieldGroup currentFieldGroup = null; String currentFieldGroupName = null; String lastFieldGroupName = null; if (currentFormField != null) { - currentFieldGroup = (FieldGroup)fieldGroupMap.get(currentFormField.getFieldName()); + currentFieldGroup = (FieldGroup)fieldGroupMap.get(currentFormField.getFieldName()); if (currentFieldGroup == null) { currentFieldGroup = defaultFieldGroup; } if (currentFieldGroup != null) { currentFieldGroupName = currentFieldGroup.getId(); - } + } } - - + + boolean isFirstPass = true; boolean haveRenderedOpenFieldRow = false; while (currentFormField != null) { @@ -906,16 +929,25 @@ // don't do it on the first pass though... if (isFirstPass) { isFirstPass = false; - List inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); - Iterator iter = inbetweenList.iterator(); - while (iter.hasNext()) { - Object obj = iter.next(); - if (obj instanceof ModelForm.Banner) { - ((ModelForm.Banner) obj).renderString(writer, context, formStringRenderer); + if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { + formStringRenderer.renderFieldGroupOpen(writer, context, currentFieldGroup); + List<ModelForm.Banner> bannerList = new ArrayList<Banner>(); + List<FieldGroupBase> inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); + Iterator<FieldGroupBase> iter = inbetweenList.iterator(); + while (iter.hasNext()) { + Object obj = iter.next(); + if (obj instanceof ModelForm.Banner) { + ModelForm.Banner banner = (ModelForm.Banner) obj; + if (banner.shouldUse(context)) + bannerList.add(banner); + } } - } - if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { - currentFieldGroup.renderStartString(writer, context, formStringRenderer); + if (UtilValidate.isNotEmpty(bannerList)) { + for (Banner banner : bannerList) { + banner.renderString(writer, context, formStringRenderer); + } + } + formStringRenderer.renderFormatSingleWrapperOpen(writer, context, this); lastFieldGroup = currentFieldGroup; } } else { @@ -938,32 +970,44 @@ } currentFieldGroup = null; if (currentFormField != null) { - currentFieldGroup = (FieldGroup) fieldGroupMap.get(currentFormField.getName()); + currentFieldGroup = (FieldGroup) fieldGroupMap.get(currentFormField.getName()); } if (currentFieldGroup == null) { currentFieldGroup = defaultFieldGroup; } currentFieldGroupName = currentFieldGroup.getId(); - + if (lastFieldGroup != null ) { lastFieldGroupName = lastFieldGroup.getId(); if (!lastFieldGroupName.equals(currentFieldGroupName)) { + // render row formatting close + formStringRenderer.renderFormatFieldRowClose(writer, context, this); + haveRenderedOpenFieldRow = false; + lastFieldGroup.renderEndString(writer, context, formStringRenderer); - - List inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); - Iterator iter = inbetweenList.iterator(); - while (iter.hasNext()) { - Object obj = iter.next(); - if (obj instanceof ModelForm.Banner) { - ((ModelForm.Banner) obj).renderString(writer, context, formStringRenderer); - } - } } } - + if (currentFieldGroup != null && (lastFieldGroup == null || !lastFieldGroupName.equals(currentFieldGroupName))) { - currentFieldGroup.renderStartString(writer, context, formStringRenderer); - lastFieldGroup = currentFieldGroup; + formStringRenderer.renderFieldGroupOpen(writer, context, currentFieldGroup); + List<ModelForm.Banner> bannerList = new ArrayList<Banner>(); + List<FieldGroupBase> inbetweenList = getInbetweenList(lastFieldGroup, currentFieldGroup); + Iterator<FieldGroupBase> iter = inbetweenList.iterator(); + while (iter.hasNext()) { + Object obj = iter.next(); + if (obj instanceof ModelForm.Banner) { + ModelForm.Banner banner = (ModelForm.Banner) obj; + if (banner.shouldUse(context)) + bannerList.add(banner); + } + } + if (UtilValidate.isNotEmpty(bannerList)) { + for (Banner banner : bannerList) { + banner.renderString(writer, context, formStringRenderer); + } + } + formStringRenderer.renderFormatSingleWrapperOpen(writer, context, this); + lastFieldGroup = currentFieldGroup; } } @@ -1102,7 +1146,7 @@ this.renderItemRows(writer, context, formStringRenderer, false, numOfColumns); formStringRenderer.renderFormatListWrapperClose(writer, context, this); - + if (!skipEnd) { formStringRenderer.renderMultiFormClose(writer, context, this); } @@ -1116,17 +1160,7 @@ // in this model: we can have more fields with the same name when use-when // conditions are used or when a form is extended or when the fields are // automatically retrieved by a service or entity definition. - List<ModelFormField> tempFieldList = FastList.newInstance(); - tempFieldList.addAll(this.fieldList); - for (int j = 0; j < tempFieldList.size(); j++) { - ModelFormField modelFormField = (ModelFormField) tempFieldList.get(j); - for (int i = j+1; i < tempFieldList.size(); i++) { - ModelFormField curField = (ModelFormField) tempFieldList.get(i); - if (curField.getName() != null && curField.getName().equals(modelFormField.getName())) { - tempFieldList.remove(i--); - } - } - } + List<ModelFormField> tempFieldList = filterFieldToShow(this.fieldList, context); // =========================== // Preprocessing @@ -1162,7 +1196,7 @@ ModelFormField modelFormField = (ModelFormField) displayHyperlinkFieldIter.next(); ModelFormField.FieldInfo fieldInfo = modelFormField.getFieldInfo(); - // if the field's title is explicitly set to "" (title="") then + // if the field's title is explicitly set to "" (title="") then // the header is not created for it; this is useful for position list // where one line can be rendered with more than one row, and we // only want to display the title header for the main row @@ -1214,7 +1248,7 @@ if (maxNumOfColumns < numOfColumns) { maxNumOfColumns = numOfColumns; } - + fieldRowsByPosition.add(UtilMisc.toMap("displayBefore", innerDisplayHyperlinkFieldsBegin, "inputFields", innerFormFields, "displayAfter", innerDisplayHyperlinkFieldsEnd)); @@ -1228,9 +1262,9 @@ List innerDisplayHyperlinkFieldsBegin = (List)listsMap.get("displayBefore"); List innerFormFields = (List)listsMap.get("inputFields"); List innerDisplayHyperlinkFieldsEnd = (List)listsMap.get("displayAfter"); - + int numOfCells = innerDisplayHyperlinkFieldsBegin.size() + - innerDisplayHyperlinkFieldsEnd.size() + + innerDisplayHyperlinkFieldsEnd.size() + (innerFormFields.size() > 0? 1: 0); int numOfColumnsToSpan = maxNumOfColumns - numOfCells + 1; if (numOfColumnsToSpan < 1) { @@ -1306,7 +1340,7 @@ } public void preparePager(Map<String, Object> context) { - + // increment the paginator this.incrementPaginatorNumber(context); this.rowCount = 0; @@ -1324,7 +1358,7 @@ Iterator iter = null; List items = null; if (obj instanceof Iterator) { - iter = (Iterator) obj; + iter = (Iterator) obj; setPaginate(true); } else if (obj instanceof List) { items = (List) obj; @@ -1391,7 +1425,7 @@ Iterator iter = null; List items = null; if (obj instanceof Iterator) { - iter = (Iterator) obj; + iter = (Iterator) obj; setPaginate(true); } else if (obj instanceof List) { items = (List) obj; @@ -1429,7 +1463,7 @@ if (itemIndex < lowIndex) { continue; } - + Map<String, Object> itemMap = UtilGenerics.checkMap(item); MapStack<String> localContext = MapStack.create(context); if (UtilValidate.isNotEmpty(this.getListEntryName())) { @@ -1471,7 +1505,7 @@ } } - // Each single item is rendered in one or more rows if its fields have + // Each single item is rendered in one or more rows if its fields have // different "position" attributes. All the fields with the same position // are rendered in the same row. // The default position is 1, and represents the main row: @@ -1582,7 +1616,7 @@ context.put("highIndex", Integer.valueOf(isOverridenListSize() ? listSize : highIndex)); } context.put("actualPageSize", Integer.valueOf(highIndex - lowIndex)); - + if (iter instanceof EntityListIterator) { try { ((EntityListIterator) iter).close(); @@ -1598,7 +1632,7 @@ // of one row (corresponding to one position). public void renderItemRow(Appendable writer, Map<String, Object> localContext, FormStringRenderer formStringRenderer, boolean formPerItem, List hiddenIgnoredFieldList, List innerDisplayHyperlinkFieldsBegin, List innerFormFields, List innerDisplayHyperlinkFieldsEnd, int position, int numOfColumns) throws IOException { int numOfCells = innerDisplayHyperlinkFieldsBegin.size() + - innerDisplayHyperlinkFieldsEnd.size() + + innerDisplayHyperlinkFieldsEnd.size() + (innerFormFields.size() > 0? 1: 0); int numOfColumnsToSpan = numOfColumns - numOfCells + 1; if (numOfColumnsToSpan < 1) { @@ -1776,9 +1810,9 @@ } public String getTargetType() { - return this.targetType; + return this.targetType; } - + public String getParentFormName() { return this.parentFormName; } @@ -1801,22 +1835,22 @@ public Map<String, ? extends Object> getDefaultMap(Map<String, ? extends Object> context) { return this.defaultMapName.get(context); } - + public String getDefaultRequiredFieldStyle() { return this.defaultRequiredFieldStyle; } - + public String getDefaultSortFieldStyle() { return (UtilValidate.isEmpty(this.defaultSortFieldStyle) ? DEFAULT_SORT_FIELD_STYLE : this.defaultSortFieldStyle); } - + public String getDefaultSortFieldAscStyle() { return (UtilValidate.isEmpty(this.defaultSortFieldAscStyle) ? DEFAULT_SORT_FIELD_ASC_STYLE : this.defaultSortFieldAscStyle); } - + public String getDefaultSortFieldDescStyle() { return (UtilValidate.isEmpty(this.defaultSortFieldDescStyle) ? DEFAULT_SORT_FIELD_DESC_STYLE : this.defaultSortFieldDescStyle); - } + } public String getDefaultServiceName() { @@ -1842,7 +1876,7 @@ public String getOddRowStyle() { return this.oddRowStyle; } - + public String getEvenRowStyle() { return this.evenRowStyle; } @@ -1850,11 +1884,11 @@ public String getDefaultTableStyle() { return this.defaultTableStyle; } - + public String getHeaderRowStyle() { return this.headerRowStyle; } - + public String getDefaultTitleStyle() { return this.defaultTitleStyle; } @@ -1971,11 +2005,11 @@ public String getBoundaryCommentName() { return formLocation + "#" + name; } - + public void resetBshInterpreter(Map<String, Object> context) { context.remove("bshInterpreter"); } - + public Interpreter getBshInterpreter(Map<String, Object> context) throws EvalError { Interpreter bsh = (Interpreter) context.get("bshInterpreter"); if (bsh == null) { @@ -2033,35 +2067,35 @@ public void setDefaultWidgetAreaStyle(String string) { this.defaultWidgetAreaStyle = string; } - + /** * @param string */ public void setOddRowStyle(String string) { this.oddRowStyle = string; } - + /** * @param string */ public void setEvenRowStyle(String string) { this.evenRowStyle = string; } - + /** * @param string */ public void setDefaultTableStyle(String string) { this.defaultTableStyle = string; } - + /** * @param string */ public void setHeaderRowStyle(String string) { this.headerRowStyle = string; } - + /** * @param string */ @@ -2100,7 +2134,7 @@ public String getFormLocation() { return this.formLocation; } - + /** * @param string */ @@ -2182,7 +2216,7 @@ public String getPaginateTargetAnchor() { return this.paginateTargetAnchor; } - + public String getPaginateIndexField(Map<String, Object> context) { String field = this.paginateIndexField.expandString(context); if (UtilValidate.isEmpty(field)) { @@ -2195,7 +2229,7 @@ public int getPaginateIndex(Map<String, Object> context) { String field = this.getPaginateIndexField(context); - + int viewIndex = 0; try { Object value = context.get(field); @@ -2205,22 +2239,22 @@ Map parameters = (Map) context.get("parameters"); if (parameters != null) { value = parameters.get("VIEW_INDEX" + "_" + getPaginatorNumber(context)); - + if (value == null) { value = parameters.get(field); } } } - if (value instanceof Integer) { + if (value instanceof Integer) { viewIndex = ((Integer) value).intValue(); - } else if (value instanceof String) { + } else if (value instanceof String) { viewIndex = Integer.parseInt((String) value); } } catch (Exception e) { Debug.logWarning(e, "Error getting paginate view index: " + e.toString(), module); } - + return viewIndex; } @@ -2324,7 +2358,7 @@ public String getTargetWindow(Map<String, Object> context) { return this.targetWindowExdr.expandString(context); } - + public void setTargetWindow( String val ) { this.targetWindowExdr = FlexibleStringExpander.getInstance(val); } @@ -2340,7 +2374,7 @@ public boolean getSkipStart() { return this.skipStart; } - + public boolean getSkipEnd() { return this.skipEnd; } @@ -2352,11 +2386,11 @@ public void setSkipStart(boolean val) { this.skipStart = val; } - + public void setSkipEnd(boolean val) { this.skipEnd = val; } - + public boolean getHideHeader() { return this.hideHeader; } @@ -2398,10 +2432,10 @@ Integer sz = Integer.valueOf(val); defaultViewSize = sz.intValue(); } catch(NumberFormatException e) { - defaultViewSize = DEFAULT_PAGE_SIZE; + defaultViewSize = DEFAULT_PAGE_SIZE; } } - + public int getListSize(Map<String, Object> context) { Integer value = (Integer) context.get("listSize"); return value != null ? value.intValue() : 0; @@ -2458,7 +2492,7 @@ if (listSize > 0) { setOverridenListSize(true); } else if (entryList instanceof EntityListIterator) { - EntityListIterator iter = (EntityListIterator) entryList; + EntityListIterator iter = (EntityListIterator) entryList; try { iter.last(); listSize = iter.currentIndex(); @@ -2471,11 +2505,11 @@ List items = (List) entryList; listSize = items.size(); } - + if (paginate) { viewIndex = this.getPaginateIndex(context); viewSize = this.getPaginateSize(context); - + lowIndex = viewIndex * viewSize; highIndex = (viewIndex + 1) * viewSize; } else { @@ -2484,18 +2518,18 @@ lowIndex = 0; highIndex = defaultViewSize; } - + context.put("listSize", Integer.valueOf(listSize)); context.put("viewIndex", Integer.valueOf(viewIndex)); context.put("viewSize", Integer.valueOf(viewSize)); context.put("lowIndex", Integer.valueOf(lowIndex)); context.put("highIndex", Integer.valueOf(highIndex)); } - + public String getPassedRowCount(Map<String, Object> context) { return rowCountExdr.expandString(context); } - + public int getRowCount() { return this.rowCount; } @@ -2508,24 +2542,24 @@ return this.multiSubmitFields; } - public List getInbetweenList(FieldGroup startFieldGroup, FieldGroup endFieldGroup) { - ArrayList<Object> inbetweenList = new ArrayList<Object>(); + public List<FieldGroupBase> getInbetweenList(FieldGroup startFieldGroup, FieldGroup endFieldGroup) { + ArrayList<FieldGroupBase> inbetweenList = new ArrayList<FieldGroupBase>(); boolean firstFound = false; String startFieldGroupId = null; String endFieldGroupId = null; if (endFieldGroup != null) { - endFieldGroupId = endFieldGroup.getId(); + endFieldGroupId = endFieldGroup.getId(); } if (startFieldGroup == null) { firstFound = true; } else { - startFieldGroupId = startFieldGroup.getId(); + startFieldGroupId = startFieldGroup.getId(); } - Iterator iter = fieldGroupList.iterator(); + Iterator<FieldGroupBase> iter = fieldGroupList.iterator(); while (iter.hasNext()) { - Object obj = iter.next(); + FieldGroupBase obj = iter.next(); if (obj instanceof ModelForm.Banner) { - if (firstFound) inbetweenList.add(obj); + if (firstFound) inbetweenList.add(obj); } else { FieldGroup fieldGroup = (FieldGroup)obj; String fieldGroupId = fieldGroup.getId(); @@ -2539,14 +2573,14 @@ if (fieldGroupId.equals(endFieldGroupId)) { break; } else { - inbetweenList.add(fieldGroup); + inbetweenList.add(fieldGroup); } } } } return inbetweenList; } - + public String getSortField(Map<String, Object> context) { String field = "sortField"; String value = null; @@ -2562,16 +2596,16 @@ } catch (Exception e) { Debug.logWarning(e, "Error getting sortField: " + e.toString(), module); } - + return value; - } - + } + /* Returns the list of ModelForm.UpdateArea objects. */ public List<UpdateArea> getOnSubmitUpdateAreas() { return this.onSubmitUpdateAreas; } - + public static class AltRowStyle { public String useWhen; public String style; @@ -2580,7 +2614,7 @@ this.style = altRowStyleElement.getAttribute("style"); } } - + /** * iterate through alt-row-styles list to see if should be used, then add style * @return The style for item row @@ -2611,7 +2645,7 @@ return styles; } - + public static class AltTarget { public String useWhen; public String target; @@ -2716,7 +2750,7 @@ this.defaultPosition = position; } } - + public static interface FieldGroupBase {} public static class FieldGroup implements FieldGroupBase { @@ -2729,7 +2763,7 @@ protected static int baseSeqNo = 0; protected static String baseId = "_G"; public FieldGroup(Element sortOrderElement, ModelForm modelForm) { - + this.modelForm = modelForm; if (sortOrderElement != null) { this.id = sortOrderElement.getAttribute("id"); @@ -2744,7 +2778,7 @@ if (this.initiallyCollapsed) { this.collapsible = true; } - + List sortFieldElements = UtilXml.childElementList(sortOrderElement, "sort-field"); Iterator sortFieldElementIter = sortFieldElements.iterator(); while (sortFieldElementIter.hasNext()) { @@ -2757,36 +2791,36 @@ this.setId(lastGroupId); } } - + public String getId() { - return this.id; + return this.id; } - + public void setId( String id) { - this.id = id; + this.id = id; } - + public String getStyle() { - return this.style; + return this.style; } - + public String getTitle() { - return this.title; + return this.title; } public Boolean collapsible() { - return this.collapsible; + return this.collapsible; } public Boolean initiallyCollapsed() { - return this.initiallyCollapsed; + return this.initiallyCollapsed; } public void renderStartString(Appendable writer, Map<String, Object> context, FormStringRenderer formStringRenderer) throws IOException { formStringRenderer.renderFieldGroupOpen(writer, context, this); formStringRenderer.renderFormatSingleWrapperOpen(writer, context, modelForm); } - + public void renderEndString(Appendable writer, Map<String, Object> context, FormStringRenderer formStringRenderer) throws IOException { formStringRenderer.renderFormatSingleWrapperClose(writer, context, modelForm); formStringRenderer.renderFieldGroupClose(writer, context, this); @@ -2802,7 +2836,8 @@ public FlexibleStringExpander leftTextStyle; public FlexibleStringExpander rightText; public FlexibleStringExpander rightTextStyle; - + public FlexibleStringExpander useWhen; + public Banner(Element sortOrderElement, ModelForm modelForm) { this.modelForm = modelForm; this.style = FlexibleStringExpander.getInstance(sortOrderElement.getAttribute("style")); @@ -2812,8 +2847,9 @@ this.leftTextStyle = FlexibleStringExpander.getInstance(sortOrderElement.getAttribute("left-text-style")); this.rightText = FlexibleStringExpander.getInstance(sortOrderElement.getAttribute("right-text")); this.rightTextStyle = FlexibleStringExpander.getInstance(sortOrderElement.getAttribute("right-text-style")); + this.useWhen = FlexibleStringExpander.getInstance(sortOrderElement.getAttribute("use-when")); } - + public String getStyle(Map<String, Object> context) { return this.style.expandString(context); } public String getText(Map<String, Object> context) { return this.text.expandString(context); } public String getTextStyle(Map<String, Object> context) { return this.textStyle.expandString(context); } @@ -2821,7 +2857,41 @@ public String getLeftTextStyle(Map<String, Object> context) { return this.leftTextStyle.expandString(context); } public String getRightText(Map<String, Object> context) { return this.rightText.expandString(context); } public String getRightTextStyle(Map<String, Object> context) { return this.rightTextStyle.expandString(context); } - + public String getUseWhen(Map<String, Object> context) { + if (this.useWhen != null && !this.useWhen.isEmpty()) { + return this.useWhen.expandString(context); + } else { + return ""; + } + } + public boolean shouldUse(Map<String, Object> context) { + String useWhenStr = this.getUseWhen(context); + if (UtilValidate.isEmpty(useWhenStr)) { + return true; + } else { + try { + Interpreter bsh = this.modelForm.getBshInterpreter(context); + Object retVal = bsh.eval(useWhenStr); + boolean condTrue = false; + // retVal should be a Boolean, if not something weird is up... + if (retVal instanceof Boolean) { + Boolean boolVal = (Boolean) retVal; + condTrue = boolVal.booleanValue(); + } else { + throw new IllegalArgumentException("Return value from use-when condition eval was not a Boolean: " + + retVal.getClass().getName() + " [" + retVal + "] on the banner of form " + this.modelForm.getName()); + } + + return condTrue; + } catch (EvalError e) { + String errMsg = "Error evaluating BeanShell use-when condition [" + useWhenStr + "] on the banner of form " + this.modelForm.getName() + ": " + e.toString(); + Debug.logError(e, errMsg, module); + //Debug.logError("For use-when eval error context is: " + context, module); + throw new IllegalArgumentException(errMsg); + } + } + } + public void renderString(Appendable writer, Map<String, Object> context, FormStringRenderer formStringRenderer) throws IOException { formStringRenderer.renderBanner(writer, context, this); } @@ -2910,7 +2980,7 @@ String target = link.getTarget(null); String urlMode = link.getTargetType(); // Debug.logInfo("In findRequestNamesLinkedtoInWidget found link [" + link.rawString() + "] with target [" + target + "]", module); - + Set<String> controllerLocAndRequestSet = ConfigXMLReader.findControllerRequestUniqueForTargetType(target, urlMode); if (controllerLocAndRequestSet != null) { allRequestsUsed.addAll(controllerLocAndRequestSet); @@ -2968,14 +3038,14 @@ for (AltTarget altTarget: this.altTargets) { String target = altTarget.target; String urlMode = "intra-app"; - + Set<String> controllerLocAndRequestSet = ConfigXMLReader.findControllerRequestUniqueForTargetType(target, urlMode); if (controllerLocAndRequestSet != null) { allRequestsUsed.addAll(controllerLocAndRequestSet); } } } - + if (!this.target.isEmpty()) { String target = this.target.getOriginal(); String urlMode = UtilValidate.isNotEmpty(this.targetType) ? this.targetType : "intra-app"; @@ -2986,7 +3056,7 @@ } } } - + return allRequestsUsed; } } |
http://docs.ofbiz.org/display/OFBADMIN/OFBiz+Contributors+Best+Practices -David On Jan 20, 2009, at 4:49 AM, Marco Ruocco wrote: > Hi, > > I try to make more precise the form use-when clause interpreter, > particularly for the single form. > I attach the patch related > Thanks > > Marco |
Free forum by Nabble | Edit this page |