您的位置:首页 > 数据库

分类统计数据库数据

2008-08-23 23:24 381 查看
/**double 数据类型*/
private static final String STR_DOUBLE="double";

/**INT 数据类型*/
private static final String STR_INT="int";

/**long 数据类型*/
private static final String STR_LONG="long";

/**标志位*/
private static final String TOTAL_PROPERTY_NAME="sumFlag";

/**统计所有数据*/
public static final String SUM_FLAG_TOTAL="SUM_FLAG_TOTAL";

/**分组统计数据*/
public static final String SUM_FLAG_GROUP = "SUM_FLAG_GROUP";

private static final String STR_EMPTY = "";

private static final String STR_ZERO = "0.0";

private static final String SITE_FIRST = "first";

private static final String SITE_LAST = "last";

private static final Logger log = Logger.getLogger(GroupUtil.class);

public GroupUtil() {
super();
}

public static Collection appendTotal(Collection datas,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}
ArrayList coll = new ArrayList();
try{
coll.addAll(datas);
appendTotalObj(coll,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);

}catch(Exception e){
log.error(e.getMessage());
coll = null;
}
return coll;
}

public static Collection group(Collection datas,String groupProperty,String[] sumPropertys){
if(datas==null){
return null;
}
if(datas.size()==0){
return datas;
}

Collection resultVals = groupBy4Datas(datas,groupProperty,sumPropertys,SITE_LAST);

return resultVals;
}

public static Collection group(Collection datas,
String groupProperty,
String[] sumPropertys, boolean sumRowAtFirst) {

if (datas == null) {
return null;
}
if (datas.size() == 0) {
return datas;
}

if (sumRowAtFirst) {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_FIRST);
}
else {
return groupBy4Datas(datas, groupProperty, sumPropertys,
SITE_LAST);
}

}

public static void appendTotalObj(List datas,String[] sumPropertys,String totalFlag,String site)
throws Exception{
BigDecimal bigDecimal = null;
String properTyp = null;
String strVal = null;
Object obj = null;
Map map = new HashMap();

for(int i=0;i<sumPropertys.length;i++){
bigDecimal = new BigDecimal(STR_ZERO);
for(int j=0;j<datas.size();j++){
obj = datas.get(j);
if(properTyp==null){
properTyp = getPropertyTypName(obj,sumPropertys[i]);
}
strVal = BeanUtils.getProperty(obj,sumPropertys[i]);
bigDecimal = bigDecimal.add(new BigDecimal(strVal));
}
if(properTyp.equals(STR_DOUBLE)){
map.put(sumPropertys[i],bigDecimal.doubleValue()+STR_EMPTY);
}else if(properTyp.equals(STR_INT)){
map.put(sumPropertys[i],bigDecimal.intValue()+STR_EMPTY);
}else if(properTyp.equals(STR_LONG)){
map.put(sumPropertys[i],bigDecimal.longValue()+STR_EMPTY);
}
properTyp = null;
}
Object newObj = getNewObj(obj,totalFlag);
BeanUtils.copyProperties(newObj,map);
if(SITE_LAST.equals(site)){
datas.add(newObj);
}else{
((List)datas).add(0,newObj);
}
}

protected static String getPropertyTypName(Object obj,String property) throws Exception{
String name = STR_EMPTY;
PropertyDescriptor proDes = PropertyUtils.getPropertyDescriptor(obj,property);
Class propertyClass = proDes.getPropertyType();
name = propertyClass.getName();
return name;
}

protected static Object getNewObj(Object obj,String totalFlag) throws Exception{
Object newObj = obj.getClass().newInstance();
Map map = new HashMap();
map.put(TOTAL_PROPERTY_NAME,totalFlag);
BeanUtils.copyProperties(newObj,obj);
BeanUtils.copyProperties(newObj,map);
return newObj;
}

protected static Collection groupBy4Datas(Collection datas,String groupProperty,
String[] sumPropertys, String site){
ArrayList totalList = new ArrayList();
Collection resultVals = null;
try{
totalList.addAll(datas);
appendTotalObj(totalList,sumPropertys,SUM_FLAG_TOTAL,SITE_LAST);
Object obj = totalList.remove(totalList.size()-1);
Map splitMap = splitDatas(totalList,groupProperty);
ArrayList keyList = new ArrayList();
keyList.addAll(splitMap.keySet());
Iterator it = keyList.iterator();
List coll = null;
while(it.hasNext()){
coll = (List)splitMap.get(it.next());
appendTotalObj(coll,sumPropertys,SUM_FLAG_GROUP,site);
}
Collections.sort(keyList);
resultVals = new ArrayList();
it = keyList.iterator();
while(it.hasNext()){
coll = (List) splitMap.get(it.next());
resultVals.addAll(coll);
}
resultVals.add(obj);
}catch(Exception ex){
log.error(ex.getMessage());
resultVals = null;
}

return resultVals;
}

protected static Map splitDatas(List datas,String groupProerty)throws Exception{
Map splitDatas = new HashMap();
Iterator it = datas.iterator();
Object obj = null;
String key = null;
List splitList = null;
while(it.hasNext()){
obj = it.next();
key = BeanUtils.getProperty(obj,groupProerty);
if(splitDatas.containsKey(key)){
splitList = (List)splitDatas.get(key);
splitList.add(obj);
}else{
splitList = new ArrayList();
splitList.add(obj);
splitDatas.put(key,splitList);
}
}
return splitDatas;
}

String[] sumPropertys = {"accuDepr","mdeprqu"};
return GroupUtil.group(fiaValChaDAO.query4Excel(para),"assetTyp",sumPropertys);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: