From: <den...@us...> - 2010-09-23 23:52:15
|
Revision: 4761 http://jaffa.svn.sourceforge.net/jaffa/?rev=4761&view=rev Author: dennisl-jaffa Date: 2010-09-23 23:52:09 +0000 (Thu, 23 Sep 2010) Log Message: ----------- Bug 15233: Added setNum(), getNum(), and addNum() methods. Since Velocity Templates can only add integers, there needed to be some helper methods to cumulate decimal form values such as sheets totals. Modified Paths: -------------- trunk/JaffaComponentsPrinting/source/java/org/jaffa/modules/printing/services/FormFormatHelper.java Modified: trunk/JaffaComponentsPrinting/source/java/org/jaffa/modules/printing/services/FormFormatHelper.java =================================================================== --- trunk/JaffaComponentsPrinting/source/java/org/jaffa/modules/printing/services/FormFormatHelper.java 2010-09-23 22:43:07 UTC (rev 4760) +++ trunk/JaffaComponentsPrinting/source/java/org/jaffa/modules/printing/services/FormFormatHelper.java 2010-09-23 23:52:09 UTC (rev 4761) @@ -49,13 +49,18 @@ package org.jaffa.modules.printing.services; import org.jaffa.util.StringHelper; +import org.jaffa.datatypes.Formatter; import org.apache.log4j.Logger; import java.util.List; import java.util.Iterator; import java.util.ArrayList; +import java.util.HashMap; +import java.math.BigDecimal; /** - * + * This class is a helper class to be used by velocity template scripts to + * format data. This class can be added to the VelocityContext and then + * utilized within the velocity template script. * @author DennisL */ public class FormFormatHelper { @@ -65,8 +70,89 @@ } /** Logger reference */ private final static Logger log = Logger.getLogger(FormFormatHelper.class); + private HashMap numberMap = new HashMap(); + private BigDecimal m_bigDecimal = new BigDecimal("0.00"); + private String str_num; /** + * Initialize the numeric value of the key. + * @param key The key with which the numeric value is associated. + * @param num The numeric value to associate with the key. + */ + public void setNum(String key, int num) { + if (key != null) { + numberMap.put(key, new BigDecimal("" + num)); + } + } + + /** + * Initialize the numeric value of the key. + * @param key The key with which the numeric value is associated. + * @param num The numeric value to associate with the key. + */ + public void setNum(String key, Double num) { + if (key != null && num != null) { + numberMap.put(key, new BigDecimal("" + num)); + } + } + + /** + * Get the formatted String representation of the numeric value associated + * with the key. + * @param key The key with which the numeric value is associated. + * @return The formatted String representation of the numeric value associated + * with the key. Applies the standard formatting. + */ + public String getNum(String key) { + return (getNum(key, null)); + } + + /** + * Get the formatted String representation of the numeric value associated + * with the key. + * @param key The key with which the numeric value is associated. + * @param format The format to apply to the returned numeric value. + * @return The formatted String representation of the numeric value associated + * with the key. Applies the format identified by the input param. + */ + public String getNum(String key, String format) { + str_num = ""; + if (key != null) { + m_bigDecimal = (BigDecimal) numberMap.get(key); + if (m_bigDecimal != null) { + if (format != null) { + // Apply format + str_num = Formatter.format(m_bigDecimal.doubleValue(), format); + } else { + // Use standard formatting + str_num = Formatter.format(m_bigDecimal.doubleValue()); + } + + } + } + return (str_num); + } + + /** + * Adds the input numeric value to the the current value associated with + * the key. If there is no current value, then the input numeric value + * is set as the initial key value. + * @param key The key with which the numeric value is associated. + * @param num The numeric value to add to the current value associated + * with the key. + */ + public void addNum(String key, Double num) { + if (key != null && num != null) { + m_bigDecimal = (BigDecimal) numberMap.get(key); + if (m_bigDecimal != null) { + numberMap.put(key, m_bigDecimal.add(new BigDecimal("" + num))); + } else { + numberMap.put(key, new BigDecimal("" + num)); + } + } + } + + /** * Returns a substring of the input string starting at the beginning Index for the specified length. * The output string is always of the specified length and if necessary right padded with spaces. * @param str The input string to be acted upon. This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |