📄 printdatagroup.java
字号:
/******************************************************************************
* The contents of this file are subject to the Compiere License Version 1.1
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the License at http://www.compiere.org/license.html
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Original Code is Compiere ERP & CRM Business Solution
* The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
* Portions created by Jorg Janke are Copyright (C) 1999-2002 Jorg Janke, parts
* created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
* Contributor(s): ______________________________________.
*****************************************************************************/
package org.compiere.print;
import java.util.*;
import java.math.*;
import org.compiere.util.*;
/**
* Group By Management
*
* @author Jorg Janke
* @version $Id: PrintDataGroup.java,v 1.1 2002/08/19 05:02:02 jjanke Exp $
*/
public class PrintDataGroup
{
/**
* Constructor
*/
public PrintDataGroup ()
{
} // PrintDataGroup
static public final String TOTAL = "GRANDTOTAL_";
/** NULL substitute value */
static private final Object NULL = new String();
/** List of group columns */
private ArrayList m_groups = new ArrayList();
/** Map of group column & value */
private HashMap m_groupMap = new HashMap();
/** List of column_function */
private ArrayList m_functions = new ArrayList();
/** Map of group_function column & function */
private HashMap m_groupFunction = new HashMap();
/*************************************************************************/
/**
* Add Group Column
* @param groupColumnName group column
*/
public void addGroupColumn (String groupColumnName)
{
m_groups.add(groupColumnName);
} // addGroup
/**
* Get Grouyp Column Count.
* TOTAL is included as a column
* @return number of groups
*/
public int getGroupColumnCount()
{
return m_groups.size();
} // getGroupColumnCount
/**
* Column has a function
* @param columnName column name or TOTAL
* @return true if column has function
*/
public boolean isGroupColumn (String columnName)
{
if (columnName == null || m_groups.size() == 0)
return false;
for (int i = 0; i < m_groups.size(); i++)
{
if (columnName.equals(m_groups.get(i)))
return true;
}
return false;
} // isGroupColumn
/**
* Check for Group Change
* @param groupColumnName column name
* @param value column value
* @return null if no group change otherwise old value
*/
public Object groupChange (String groupColumnName, Object value)
{
if (!isGroupColumn(groupColumnName))
return null;
Object newValue = value;
if (newValue == null)
newValue = NULL;
//
if (m_groupMap.containsKey(groupColumnName))
{
Object oldValue = m_groupMap.get(groupColumnName);
if (newValue.equals(oldValue))
return null;
m_groupMap.put(groupColumnName, newValue);
return oldValue;
}
m_groupMap.put(groupColumnName, newValue);
return null;
} // groupChange
/*************************************************************************/
/**
* Add Function Column
* @param functionColumnName column name
* @param function function
*/
public void addFunction (String functionColumnName, char function)
{
m_functions.add(functionColumnName + "_" + function);
if (!m_groups.contains(TOTAL))
m_groups.add(TOTAL);
} // addFunction
/**
* Column has a function
* @param columnName column name
* @return true if column has function
*/
public boolean isFunctionColumn (String columnName)
{
if (columnName == null || m_functions.size() == 0)
return false;
for (int i = 0; i < m_functions.size(); i++)
{
String f = (String)m_functions.get(i);
if (f.startsWith(columnName))
return true;
}
return false;
} // isFunctionColumn
/**
* Get calculated functions of column
* @param columnName column name or TOTAL
* @return array of functions
*/
public char[] getFunctions(String columnName)
{
ArrayList list = new ArrayList(); // the final function List
Iterator it = m_groupFunction.keySet().iterator();
while(it.hasNext())
{
String group_function = (String)it.next(); // GRANDTOTAL__LoadSeq
if (group_function.startsWith(columnName))
{
group_function = group_function.substring(group_function.lastIndexOf('_')+1); // LoadSeq
for (int i = 0; i < m_functions.size(); i++)
{
String col_function = ((String)m_functions.get(i)); // LoadSeq_A
if (col_function.startsWith(group_function))
{
String function = col_function.substring(col_function.lastIndexOf('_')+1);
if (!list.contains(function))
list.add(function);
}
}
}
}
// Return Value
char[] retValue = new char[list.size()];
for (int i = 0; i < retValue.length; i++)
retValue[i] = ((String)list.get(i)).charAt(0);
// Log.trace(9, "PrintDataGroup.getFunctions for " + columnName + "/" + retValue.length, new String(retValue));
return retValue;
} // getFunctions
/**
* Column has a function
* @param columnName column name
* @param function function
* @return true if column has function
*/
public boolean isFunctionColumn (String columnName, char function)
{
if (columnName == null || m_functions.size() == 0)
return false;
String key = columnName + "_" + function;
for (int i = 0; i < m_functions.size(); i++)
{
String f = (String)m_functions.get(i);
if (f.equals(key))
return true;
}
return false;
} // isFunctionColumn
/*************************************************************************/
/**
* Add Value to groups
* @param functionColumnName column name
* @param functionValue value
*/
public void addValue (String functionColumnName, BigDecimal functionValue)
{
if (!isFunctionColumn(functionColumnName))
return;
// Group Breaks
for (int i = 0; i < m_groups.size(); i++)
{
String groupColumnName = (String)m_groups.get(i);
String key = groupColumnName + "_" + functionColumnName;
PrintDataFunction pdf = (PrintDataFunction)m_groupFunction.get(key);
if (pdf == null)
pdf = new PrintDataFunction();
pdf.addValue(functionValue);
m_groupFunction.put(key, pdf);
}
} // addValue
/**
* Get Value
* @param groupColumnName group column name (or TOTAL)
* @param functionColumnName function column name
* @param function function
* @param reset true if function should be reset
* @return value
*/
public BigDecimal getValue (String groupColumnName, String functionColumnName,
char function, boolean reset)
{
String key = groupColumnName + "_" + functionColumnName;
PrintDataFunction pdf = (PrintDataFunction)m_groupFunction.get(key);
if (pdf == null)
return null;
BigDecimal retValue = pdf.getValue(function);
if (reset)
pdf.reset();
return retValue;
} // getValue
/*************************************************************************/
/**
* String Representation
* @return info
*/
public String toString ()
{
return toString(false);
} // toString
/**
* String Representation
* @param withData with data
* @return info
*/
public String toString (boolean withData)
{
StringBuffer sb = new StringBuffer("PrintDataGroup[");
sb.append("Groups=");
for (int i = 0; i < m_groups.size(); i++)
{
if (i != 0)
sb.append(",");
sb.append(m_groups.get(i));
}
if (withData)
{
Iterator it = m_groupMap.keySet().iterator();
while(it.hasNext())
{
Object key = it.next();
Object value = m_groupMap.get(key);
sb.append(":").append(key).append("=").append(value);
}
}
sb.append(";Functions=");
for (int i = 0; i < m_functions.size(); i++)
{
if (i != 0)
sb.append(",");
sb.append(m_functions.get(i));
}
if (withData)
{
Iterator it = m_groupFunction.keySet().iterator();
while(it.hasNext())
{
Object key = it.next();
Object value = m_groupFunction.get(key);
sb.append(":").append(key).append("=").append(value);
}
}
sb.append("]");
return sb.toString();
} // toString
} // PrintDataGroup
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -