1 14 package org.compiere.print; 15 16 import java.util.*; 17 import java.math.*; 18 19 import org.compiere.util.*; 20 21 27 public class PrintDataGroup 28 { 29 32 public PrintDataGroup () 33 { 34 } 36 37 static public final String TOTAL = "GRANDTOTAL_"; 38 39 static private final Object NULL = new String (); 40 41 42 private ArrayList m_groups = new ArrayList(); 43 44 private HashMap m_groupMap = new HashMap(); 45 46 private ArrayList m_functions = new ArrayList(); 47 48 private HashMap m_groupFunction = new HashMap(); 49 50 51 52 56 public void addGroupColumn (String groupColumnName) 57 { 58 m_groups.add(groupColumnName); 59 } 61 66 public int getGroupColumnCount() 67 { 68 return m_groups.size(); 69 } 71 76 public boolean isGroupColumn (String 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 } 88 94 public Object groupChange (String groupColumnName, Object value) 95 { 96 if (!isGroupColumn(groupColumnName)) 97 return null; 98 Object newValue = value; 99 if (newValue == null) 100 newValue = NULL; 101 if (m_groupMap.containsKey(groupColumnName)) 103 { 104 Object 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 } 114 115 116 121 public void addFunction (String functionColumnName, char function) 122 { 123 m_functions.add(functionColumnName + "_" + function); 124 if (!m_groups.contains(TOTAL)) 125 m_groups.add(TOTAL); 126 } 128 133 public boolean isFunctionColumn (String 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 f = (String )m_functions.get(i); 140 if (f.startsWith(columnName)) 141 return true; 142 } 143 return false; 144 } 146 151 public char[] getFunctions(String columnName) 152 { 153 ArrayList list = new ArrayList(); Iterator it = m_groupFunction.keySet().iterator(); 155 while(it.hasNext()) 156 { 157 String group_function = (String )it.next(); if (group_function.startsWith(columnName)) 159 { 160 group_function = group_function.substring(group_function.lastIndexOf('_')+1); for (int i = 0; i < m_functions.size(); i++) 162 { 163 String col_function = ((String )m_functions.get(i)); if (col_function.startsWith(group_function)) 165 { 166 String function = col_function.substring(col_function.lastIndexOf('_')+1); 167 if (!list.contains(function)) 168 list.add(function); 169 } 170 } 171 } 172 } 173 char[] retValue = new char[list.size()]; 175 for (int i = 0; i < retValue.length; i++) 176 retValue[i] = ((String )list.get(i)).charAt(0); 177 return retValue; 179 } 181 187 public boolean isFunctionColumn (String columnName, char function) 188 { 189 if (columnName == null || m_functions.size() == 0) 190 return false; 191 String key = columnName + "_" + function; 192 for (int i = 0; i < m_functions.size(); i++) 193 { 194 String f = (String )m_functions.get(i); 195 if (f.equals(key)) 196 return true; 197 } 198 return false; 199 } 201 202 203 208 public void addValue (String functionColumnName, BigDecimal functionValue) 209 { 210 if (!isFunctionColumn(functionColumnName)) 211 return; 212 for (int i = 0; i < m_groups.size(); i++) 214 { 215 String groupColumnName = (String )m_groups.get(i); 216 String 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 } 225 233 public BigDecimal getValue (String groupColumnName, String functionColumnName, 234 char function, boolean reset) 235 { 236 String 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 } 246 247 248 252 public String toString () 253 { 254 return toString(false); 255 } 257 262 public String toString (boolean withData) 263 { 264 StringBuffer sb = new StringBuffer ("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 key = it.next(); 278 Object 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 key = it.next(); 295 Object value = m_groupFunction.get(key); 296 sb.append(":").append(key).append("=").append(value); 297 } 298 } 299 sb.append("]"); 300 return sb.toString(); 301 } 303 } 305
| Popular Tags
|