KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > print > PrintDataGroup


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.print;
15
16 import java.util.*;
17 import java.math.*;
18
19 import org.compiere.util.*;
20
21 /**
22  * Group By Management
23  *
24  * @author Jorg Janke
25  * @version $Id: PrintDataGroup.java,v 1.1 2002/08/19 05:02:02 jjanke Exp $
26  */

27 public class PrintDataGroup
28 {
29     /**
30      * Constructor
31      */

32     public PrintDataGroup ()
33     {
34     } // PrintDataGroup
35

36
37     static public final String JavaDoc TOTAL = "GRANDTOTAL_";
38     /** NULL substitute value */
39     static private final Object JavaDoc NULL = new String JavaDoc();
40
41     /** List of group columns */
42     private ArrayList m_groups = new ArrayList();
43     /** Map of group column & value */
44     private HashMap m_groupMap = new HashMap();
45     /** List of column_function */
46     private ArrayList m_functions = new ArrayList();
47     /** Map of group_function column & function */
48     private HashMap m_groupFunction = new HashMap();
49
50     /*************************************************************************/
51
52     /**
53      * Add Group Column
54      * @param groupColumnName group column
55      */

56     public void addGroupColumn (String JavaDoc groupColumnName)
57     {
58         m_groups.add(groupColumnName);
59     } // addGroup
60

61     /**
62      * Get Grouyp Column Count.
63      * TOTAL is included as a column
64      * @return number of groups
65      */

66     public int getGroupColumnCount()
67     {
68         return m_groups.size();
69     } // getGroupColumnCount
70

71     /**
72      * Column has a function
73      * @param columnName column name or TOTAL
74      * @return true if column has function
75      */

76     public boolean isGroupColumn (String JavaDoc columnName)
77     {
78         if (columnName == null || m_groups.size() == 0)
79             return false;
80         for (int i = 0; i < m_groups.size(); i++)
81         {
82             if (columnName.equals(m_groups.get(i)))
83                 return true;
84         }
85         return false;
86     } // isGroupColumn
87

88     /**
89      * Check for Group Change
90      * @param groupColumnName column name
91      * @param value column value
92      * @return null if no group change otherwise old value
93      */

94     public Object JavaDoc groupChange (String JavaDoc groupColumnName, Object JavaDoc value)
95     {
96         if (!isGroupColumn(groupColumnName))
97             return null;
98         Object JavaDoc newValue = value;
99         if (newValue == null)
100             newValue = NULL;
101         //
102
if (m_groupMap.containsKey(groupColumnName))
103         {
104             Object JavaDoc oldValue = m_groupMap.get(groupColumnName);
105             if (newValue.equals(oldValue))
106                 return null;
107             m_groupMap.put(groupColumnName, newValue);
108             return oldValue;
109         }
110         m_groupMap.put(groupColumnName, newValue);
111         return null;
112     } // groupChange
113

114     /*************************************************************************/
115
116     /**
117      * Add Function Column
118      * @param functionColumnName column name
119      * @param function function
120      */

121     public void addFunction (String JavaDoc functionColumnName, char function)
122     {
123         m_functions.add(functionColumnName + "_" + function);
124         if (!m_groups.contains(TOTAL))
125             m_groups.add(TOTAL);
126     } // addFunction
127

128     /**
129      * Column has a function
130      * @param columnName column name
131      * @return true if column has function
132      */

133     public boolean isFunctionColumn (String JavaDoc columnName)
134     {
135         if (columnName == null || m_functions.size() == 0)
136             return false;
137         for (int i = 0; i < m_functions.size(); i++)
138         {
139             String JavaDoc f = (String JavaDoc)m_functions.get(i);
140             if (f.startsWith(columnName))
141                 return true;
142         }
143         return false;
144     } // isFunctionColumn
145

146     /**
147      * Get calculated functions of column
148      * @param columnName column name or TOTAL
149      * @return array of functions
150      */

151     public char[] getFunctions(String JavaDoc columnName)
152     {
153         ArrayList list = new ArrayList(); // the final function List
154
Iterator it = m_groupFunction.keySet().iterator();
155         while(it.hasNext())
156         {
157             String JavaDoc group_function = (String JavaDoc)it.next(); // GRANDTOTAL__LoadSeq
158
if (group_function.startsWith(columnName))
159             {
160                 group_function = group_function.substring(group_function.lastIndexOf('_')+1); // LoadSeq
161
for (int i = 0; i < m_functions.size(); i++)
162                 {
163                     String JavaDoc col_function = ((String JavaDoc)m_functions.get(i)); // LoadSeq_A
164
if (col_function.startsWith(group_function))
165                     {
166                         String JavaDoc function = col_function.substring(col_function.lastIndexOf('_')+1);
167                         if (!list.contains(function))
168                             list.add(function);
169                     }
170                 }
171             }
172         }
173         // Return Value
174
char[] retValue = new char[list.size()];
175         for (int i = 0; i < retValue.length; i++)
176             retValue[i] = ((String JavaDoc)list.get(i)).charAt(0);
177     // Log.trace(9, "PrintDataGroup.getFunctions for " + columnName + "/" + retValue.length, new String(retValue));
178
return retValue;
179     } // getFunctions
180

181     /**
182      * Column has a function
183      * @param columnName column name
184      * @param function function
185      * @return true if column has function
186      */

187     public boolean isFunctionColumn (String JavaDoc columnName, char function)
188     {
189         if (columnName == null || m_functions.size() == 0)
190             return false;
191         String JavaDoc key = columnName + "_" + function;
192         for (int i = 0; i < m_functions.size(); i++)
193         {
194             String JavaDoc f = (String JavaDoc)m_functions.get(i);
195             if (f.equals(key))
196                 return true;
197         }
198         return false;
199     } // isFunctionColumn
200

201     /*************************************************************************/
202
203     /**
204      * Add Value to groups
205      * @param functionColumnName column name
206      * @param functionValue value
207      */

208     public void addValue (String JavaDoc functionColumnName, BigDecimal functionValue)
209     {
210         if (!isFunctionColumn(functionColumnName))
211             return;
212         // Group Breaks
213
for (int i = 0; i < m_groups.size(); i++)
214         {
215             String JavaDoc groupColumnName = (String JavaDoc)m_groups.get(i);
216             String JavaDoc key = groupColumnName + "_" + functionColumnName;
217             PrintDataFunction pdf = (PrintDataFunction)m_groupFunction.get(key);
218             if (pdf == null)
219                 pdf = new PrintDataFunction();
220             pdf.addValue(functionValue);
221             m_groupFunction.put(key, pdf);
222         }
223     } // addValue
224

225     /**
226      * Get Value
227      * @param groupColumnName group column name (or TOTAL)
228      * @param functionColumnName function column name
229      * @param function function
230      * @param reset true if function should be reset
231      * @return value
232      */

233     public BigDecimal getValue (String JavaDoc groupColumnName, String JavaDoc functionColumnName,
234         char function, boolean reset)
235     {
236         String JavaDoc key = groupColumnName + "_" + functionColumnName;
237         PrintDataFunction pdf = (PrintDataFunction)m_groupFunction.get(key);
238         if (pdf == null)
239             return null;
240         BigDecimal retValue = pdf.getValue(function);
241         if (reset)
242             pdf.reset();
243         return retValue;
244     } // getValue
245

246     /*************************************************************************/
247
248     /**
249      * String Representation
250      * @return info
251      */

252     public String JavaDoc toString ()
253     {
254         return toString(false);
255     } // toString
256

257     /**
258      * String Representation
259      * @param withData with data
260      * @return info
261      */

262     public String JavaDoc toString (boolean withData)
263     {
264         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("PrintDataGroup[");
265         sb.append("Groups=");
266         for (int i = 0; i < m_groups.size(); i++)
267         {
268             if (i != 0)
269                 sb.append(",");
270             sb.append(m_groups.get(i));
271         }
272         if (withData)
273         {
274             Iterator it = m_groupMap.keySet().iterator();
275             while(it.hasNext())
276             {
277                 Object JavaDoc key = it.next();
278                 Object JavaDoc value = m_groupMap.get(key);
279                 sb.append(":").append(key).append("=").append(value);
280             }
281         }
282         sb.append(";Functions=");
283         for (int i = 0; i < m_functions.size(); i++)
284         {
285             if (i != 0)
286                 sb.append(",");
287             sb.append(m_functions.get(i));
288         }
289         if (withData)
290         {
291             Iterator it = m_groupFunction.keySet().iterator();
292             while(it.hasNext())
293             {
294                 Object JavaDoc key = it.next();
295                 Object JavaDoc value = m_groupFunction.get(key);
296                 sb.append(":").append(key).append("=").append(value);
297             }
298         }
299         sb.append("]");
300         return sb.toString();
301     } // toString
302

303 } // PrintDataGroup
304

305
Popular Tags