MyInquirySumByGroupTotal.java 5.46 KB
Newer Older
TongZuu committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
package com.csc.library.database;

import java.rmi.RemoteException;

import com.csc.library.utilities.MyLog;

public class MyInquirySumByGroupTotal extends MyInquiry {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public void preRefresh() {
		try {
			if (this.param.containsKey("__ORDERBY")){
				this.setOrderBy(this.param.get("__ORDERBY").toString());
			}	
			if (this.param.containsKey("__GROUPBY")){
				this.setGroupBy(this.param.get("__GROUPBY").toString());
			}	
			if (this.param.containsKey("__SUMCOLUMN")){
				this.setSumColumn(this.param.get("__SUMCOLUMN").toString());
			}	
			if (this.param.containsKey("__AVGCOLUMN")){
				this.setAVGColumn(this.param.get("__AVGCOLUMN").toString());
			}	
			if (this.param.containsKey("__MAXCOLUMN")){
				this.setMaxColumn(this.param.get("__MAXCOLUMN").toString());
			}	
			if (this.param.containsKey("__MINCOLUMN")){
				this.setMinColumn(this.param.get("__MINCOLUMN").toString());
			}	
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}	

	protected void groupDataProcess() throws RemoteException {
		if (this.getGroupSize()>0) {	
			this.dataProcessing((MyHashMap)this.getGroupData().get(String.valueOf(this.getGroupSize()-1).trim()));
		}
	}
	
	protected void groupChange(int level)  {  	// summary data from group total to supergroup total
		if (level >= 0) {
			for (int i = this.getGroupSize()-1; i >= level; i-- ) {
				try {
					if (i==0) {
						this.getGroupData().put("all", this.dataProcessing((MyHashMap)this.getGroupData().get("all"), (MyHashMap)this.getGroupData().get(String.valueOf(i).trim())));
					} else {
						this.getGroupData().put(String.valueOf(i-1).trim(), this.dataProcessing((MyHashMap)this.getGroupData().get(String.valueOf(i-1).trim()), (MyHashMap)this.getGroupData().get(String.valueOf(i).trim())));
					}
				} catch (RemoteException e) {
					MyLog.warn(this, e);
				}
			}
		}
	}
	
	protected MyHashMap dataProcessing(MyHashMap hm, MyHashMap hmGroup) throws RemoteException {
		MyHashMap hmGroupTotal;
		int count = hm.getInt("count")+1;
		hm.put("count", count);
		if (this.sumColumnVector.size()>0) { 
			MyHashMap hmSUM = (MyHashMap)hm.get("sum");
			hmGroupTotal = (MyHashMap)hmGroup.get("sum");
			for (int i=0; i < this.sumColumnVector.size();i++ ) {
				double d = hmSUM.getDouble(this.sumColumnVector.elementAt(i));
				d += hmGroupTotal.getDouble(this.sumColumnVector.elementAt(i).toString());
				hmSUM.put(this.sumColumnVector.elementAt(i),d);
			}
		}
		if (this.avgColumnVector.size()>0) { 
			MyHashMap hmAVG = (MyHashMap)hm.get("avg");
			hmGroupTotal = (MyHashMap)hmGroup.get("avg");
			for (int i=0; i < this.avgColumnVector.size();i++ ) {
				double d = hmAVG.getDouble("sum_"+this.avgColumnVector.elementAt(i));
				double value = hmGroupTotal.getDouble(this.avgColumnVector.elementAt(i).toString());
				d += value;
				hmAVG.put("sum_"+this.avgColumnVector.elementAt(i),d);
				hmAVG.put(this.avgColumnVector.elementAt(i),d/count);
			}
		}
		if (this.minColumnVector.size()>0) { 
			MyHashMap hmMIN = (MyHashMap)hm.get("min");
			hmGroupTotal = (MyHashMap)hmGroup.get("min");
			for (int i=0; i < this.minColumnVector.size();i++ ) {
				String oldValue = hmMIN.get(this.minColumnVector.elementAt(i)).toString();
				String newValue = hmGroupTotal.getString(this.minColumnVector.elementAt(i).toString());
				if (newValue.compareTo(oldValue)<0) {
					hmMIN.put(this.minColumnVector.elementAt(i), newValue);
				}
			}
		}
		if (this.maxColumnVector.size()>0) { 
			MyHashMap hmMAX = (MyHashMap)hm.get("max");
			hmGroupTotal = (MyHashMap)hmGroup.get("max");
			for (int i=0; i < this.maxColumnVector.size();i++ ) {
				String oldValue = hmMAX.get(this.maxColumnVector.elementAt(i)).toString();
				String newValue = hmGroupTotal.getString(this.maxColumnVector.elementAt(i).toString());
				if (newValue.compareTo(oldValue)>0) {
					hmMAX.put(this.maxColumnVector.elementAt(i), newValue);
				}
			}
		}
		if (this.udfColumnVector.size()>0) { 
			MyHashMap hmUDF = (MyHashMap)hm.get("udf");
			hmGroupTotal = (MyHashMap)hmGroup.get("udf");
			for (int i=0; i < this.udfColumnVector.size();i++ ) {
				Object oldValue = hmUDF.get(this.udfColumnVector.elementAt(i));
				Object newValue = this.calculateSumGroup(this.udfColumnVector.elementAt(i).toString(), oldValue, hmGroupTotal.get(this.udfColumnVector.elementAt(i)));
				if (newValue!=null && newValue.toString().equals("")) {
					newValue = "0";
				} else if (newValue==null) {
					newValue = new String();
					newValue = "0";
				}
				hmUDF.put(this.udfColumnVector.elementAt(i), newValue);
			}
		}
		return hm;
	}
	
	public Object calculateSumGroup(String FieldName, Object oldValue, Object newValue) {
		/*if (oldValue==null) {
			oldValue = "PIAK1";
		} else {
			oldValue = oldValue.toString().substring(4);
			Integer i = Integer.valueOf(oldValue.toString());
			int k = i.intValue()+1;
			oldValue = "PIAK"+String.valueOf(k);
		}*/
		return (Object)oldValue;
	}
	
	public String getString(String fieldName) {
		String result="";		
		if (this.getGroupBy().length()>0 && fieldName.indexOf(":")>-1) {
			result = super.getSimString(fieldName);
			if (result==null || result.equals("")) {
				result = "0";
			}
			//MyLog.debug(this, "SimString fieldName ------ "+fieldName+ ": "+super.getSimString(fieldName));
			return result;
		} else {
			return super.getString(fieldName);
		}
	}
	
}