details: http://code.openbravo.com/erp/devel/pi/rev/794612123584 changeset: 4495:794612123584 user: Stefan Hühner <stefan.huehner <at> openbravo.com> date: Fri Aug 14 11:38:51 2009 +0200 summary: [NumberFormat] integrate needed framework change from cyclone project to support different input number formats - This only add the needed framework changes, no changes to use the new functionality yet - NumberFilter/VariablesBase: add new getNumeric* getters to be used when reading numeric parameter values - HttpSecureAppServlet,DynamicJS: add all info from Format.xml to DynamicJS response (to be used by js code) - web/ needed js changes to support nuemric widgets with varying number formats
diffstat: src-core/src/org/openbravo/base/VariablesBase.java | 130 +++ src-core/src/org/openbravo/base/filter/NumberFilter.java | 35 + src/org/openbravo/base/secureApp/HttpSecureAppServlet.java | 9 +- src/org/openbravo/erpCommon/utility/DynamicJS.java | 37 +- web/js/calculator.js | 30 +- web/js/dojo/dojo.js | 281 ++++-- web/js/utils.js | 770 ++++++++++++++++++- web/popups/calculator.html | 2 +- 8 files changed, 1168 insertions(+), 126 deletions(-) diffs (truncated from 1469 to 300 lines): diff -r 62b1542a7ccf -r 794612123584 src-core/src/org/openbravo/base/VariablesBase.java --- a/src-core/src/org/openbravo/base/VariablesBase.java Fri Aug 14 11:33:06 2009 +0200 +++ b/src-core/src/org/openbravo/base/VariablesBase.java Fri Aug 14 11:38:51 2009 +0200 @@ -29,6 +29,7 @@ import org.apache.commons.fileupload.servlet.ServletFileUpload; import org.apache.commons.fileupload.servlet.ServletRequestContext; import org.apache.log4j.Logger; +import org.openbravo.base.filter.NumberFilter; import org.openbravo.base.filter.RequestFilter; import org.openbravo.utils.FormatUtilities; @@ -46,6 +47,7 @@ private List<String> sortedParameters = null; public boolean isMultipart = false; List<FileItem> items; + private final String DEFAULT_FORMAT_NAME = "qtyEdition"; static Logger log4j = Logger.getLogger(VariablesBase.class); @@ -665,6 +667,134 @@ } } + public String getNumericParameter(String parameter) throws ServletException { + String value = getStringParameter(parameter); + + if (value.equals("")) { + return value; + } + + String newValue = transformNumber(value); + + if (!NumberFilter.instance.accept(newValue)) { + log4j.error("Input: " + parameter + " not accepted by filter: " + NumberFilter.instance); + throw new ServletException("Input: " + parameter + " is not an accepted input"); + } + + return newValue; + } + + public String getNumericParameter(String parameter, String defaultValue) throws ServletException { + String value = getStringParameter(parameter, false, defaultValue); + + if (value.equals("")) { + return value; + } + + String newValue = transformNumber(value); + + if (!NumberFilter.instance.accept(newValue)) { + log4j.error("Input: " + parameter + " not accepted by filter: " + NumberFilter.instance); + throw new ServletException("Input: " + parameter + " is not an accepted input"); + } + + return newValue; + } + + public String getRequiredNumericParameter(String parameter) throws ServletException { + String value = getRequiredStringParameter(parameter); + + if (value.equals("")) { + return value; + } + + String newValue = transformNumber(value); + + if (!NumberFilter.instance.accept(newValue)) { + log4j.error("Input: " + parameter + " not accepted by filter: " + NumberFilter.instance); + throw new ServletException("Input: " + parameter + " is not an accepted input"); + } + + return newValue; + } + + public String getRequiredNumericParameter(String parameter, String defaultValue) + throws ServletException { + String value = getStringParameter(parameter, true, defaultValue); + + if (value.equals("")) { + return value; + } + + String newValue = transformNumber(value); + + if (!NumberFilter.instance.accept(newValue)) { + log4j.error("Input: " + parameter + " not accepted by filter: " + NumberFilter.instance); + throw new ServletException("Input: " + parameter + " is not an accepted input"); + } + + return newValue; + } + + /** + * @see #getGlobalVariable(String,String,String) + */ + public String getNumericGlobalVariable(String requestParameter, String sessionAttribute, + String defaultValue) throws ServletException { + String res = getNumericParameter(requestParameter); + if (!res.equals("")) { + setSessionValue(sessionAttribute, res); + } else { + res = getSessionValue(sessionAttribute); + if (res.equals("")) { + res = defaultValue; + setSessionValue(sessionAttribute, res); + } + } + return res; + } + + /** + * @see #getRequestGlobalVariable(String,String) + */ + public String getNumericRequestGlobalVariable(String requestParameter, String sessionAttribute) + throws ServletException { + String res = getNumericParameter(requestParameter); + if (!res.equals("")) { + setSessionValue(sessionAttribute, res); + } else { + res = getSessionValue(sessionAttribute); + if (!res.equals("")) { + removeSessionValue(sessionAttribute); + } + } + return res; + } + + private String transformNumber(String number) throws ServletException { + String value = number; + String groupSeparator = getSessionValue("#GROUPSEPARATOR|" + DEFAULT_FORMAT_NAME); + String decimalSeparator = getSessionValue("#DECIMALSEPARATOR|" + DEFAULT_FORMAT_NAME); + + if (groupSeparator.equals("") || decimalSeparator.equals("")) { + log4j.error("Error while trying to transform number: groupSeparator = " + groupSeparator + + " ,decimalSeparator = " + decimalSeparator); + throw new ServletException("Error while trying to transform numeric input"); + } + + if (groupSeparator.equals(".")) { + groupSeparator = "\\."; + } + + value = value.replaceAll(groupSeparator, ""); + + if (!decimalSeparator.equals(".")) { + value = value.replaceAll(decimalSeparator, "."); + } + + return value; + } + /** * @see #getStringParameter(String,RequestFilter) */ diff -r 62b1542a7ccf -r 794612123584 src-core/src/org/openbravo/base/filter/NumberFilter.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src-core/src/org/openbravo/base/filter/NumberFilter.java Fri Aug 14 11:38:51 2009 +0200 @@ -0,0 +1,35 @@ +/* + ************************************************************************************ + * Copyright (C) 2009 Openbravo S.L. + * Licensed under the Apache Software License version 2.0 + * 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 KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + ************************************************************************************ + */ +package org.openbravo.base.filter; + +import java.math.BigDecimal; + +/** + * Filter to check if a value can be parsed into a BigDecimal. + * + * @author iperdomo + * + */ +public class NumberFilter implements RequestFilter { + + public static final NumberFilter instance = new NumberFilter(); + + @Override + public boolean accept(String value) { + try { + new BigDecimal(value); + } catch (Exception e) { + return false; + } + return true; + } +} diff -r 62b1542a7ccf -r 794612123584 src/org/openbravo/base/secureApp/HttpSecureAppServlet.java --- a/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java Fri Aug 14 11:33:06 2009 +0200 +++ b/src/org/openbravo/base/secureApp/HttpSecureAppServlet.java Fri Aug 14 11:38:51 2009 +0200 @@ -903,6 +903,8 @@ String strGroupingSeparator = ","; // Default grouping separator String strDecimalSeparator = "."; // Default decimal separator final String strName = "euroInform"; // Name of the format to use + final HashMap<String, String> formatMap = new HashMap<String, String>(); + try { // Reading number format configuration final DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); @@ -918,8 +920,10 @@ final String strNumberName = NumberElement.getAttributes().getNamedItem("name") .getNodeValue(); // store in session all the formats - vars.setSessionValue("#FormatOutput|" + strNumberName, NumberElement.getAttributes() - .getNamedItem("formatOutput").getNodeValue()); + final String strFormatOutput = NumberElement.getAttributes().getNamedItem("formatOutput") + .getNodeValue(); + formatMap.put(strNumberName, strFormatOutput); + vars.setSessionValue("#FormatOutput|" + strNumberName, strFormatOutput); vars.setSessionValue("#DecimalSeparator|" + strNumberName, NumberElement.getAttributes() .getNamedItem("decimal").getNodeValue()); vars.setSessionValue("#GroupSeparator|" + strNumberName, NumberElement.getAttributes() @@ -935,6 +939,7 @@ } catch (final Exception e) { log4j.error("error reading number format", e); } + vars.setSessionObject("#FormatMap", formatMap); vars.setSessionValue("#AD_ReportNumberFormat", strNumberFormat); vars.setSessionValue("#AD_ReportGroupingSeparator", strGroupingSeparator); vars.setSessionValue("#AD_ReportDecimalSeparator", strDecimalSeparator); diff -r 62b1542a7ccf -r 794612123584 src/org/openbravo/erpCommon/utility/DynamicJS.java --- a/src/org/openbravo/erpCommon/utility/DynamicJS.java Fri Aug 14 11:33:06 2009 +0200 +++ b/src/org/openbravo/erpCommon/utility/DynamicJS.java Fri Aug 14 11:38:51 2009 +0200 @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.PrintWriter; +import java.util.HashMap; import javax.servlet.ServletConfig; import javax.servlet.ServletException; @@ -34,8 +35,8 @@ /** * @author Fernando Iriazabal * - * Servlet that prints a javascript with dynamic functions such as the confirmation messages for the check javascript - * of the window. + * Servlet that prints a javascript with dynamic functions such as the confirmation messages + * for the check javascript of the window. */ public class DynamicJS extends HttpSecureAppServlet { private static final long serialVersionUID = 1L; @@ -61,6 +62,7 @@ * @throws IOException * @throws ServletException */ + @SuppressWarnings("unchecked") private void printPageDataSheet(HttpServletResponse response, VariablesSecureApp vars) throws IOException, ServletException { if (log4j.isDebugEnabled()) @@ -113,6 +115,37 @@ if (log4j.isDebugEnabled()) log4j.debug(arrayType.toString() + array.toString()); out.println(arrayType.toString() + array.toString()); + + String globals = ""; + + globals += "var decSeparator_global = '" + + vars.getSessionValue("#DECIMALSEPARATOR|EUROEDITION") + "';\n"; + globals += "var groupSeparator_global = '" + + vars.getSessionValue("#GROUPSEPARATOR|EUROEDITION") + "';\n"; + globals += "var groupInterval_global = '3';\n"; + globals += "var maskNumeric_default = '" + vars.getSessionValue("#FORMATOUTPUT|EUROEDITION") + + "';\n"; + + out.print(globals); + + final HashMap<String, String> formatMap = (HashMap<String, String>) vars + .getSessionObject("#formatMap"); + + out.print("var F = {\"formats\": ["); + boolean first = true; + for (String key : formatMap.keySet()) { + if (!first) { + out.print(","); + } else { + first = false; + } + out.print("{\"name\":\"" + key + "\","); + out.print("\"output\":\"" + formatMap.get(key) + "\"}"); + } + out.println("]};"); + out + .println("F.getFormat=function(name){if(typeof name==='undefined'||name===''){return'qtyEdition';}for(var i=0;i<this.formats.length;i++){if(this.formats[i].name===name){return this.formats[i].output;}}return'qtyEdtion';}"); + out.close(); } } diff -r 62b1542a7ccf -r 794612123584 web/js/calculator.js --- a/web/js/calculator.js Fri Aug 14 11:33:06 2009 +0200 +++ b/web/js/calculator.js Fri Aug 14 11:38:51 2009 +0200 @@ -98,6 +98,8 @@ ------------------------------------------------------------------------------ Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day trial. Simplify your report design, integration and deployment - and focus on what you do best, core application coding. Discover what's new with Crystal Reports now. http://p.sf.net/sfu/bobj-july _______________________________________________ Openbravo-commits mailing list Openbravo-commits@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/openbravo-commits