1 package com.calipso.reportgenerator.reportmanager; 2 import com.calipso.reportgenerator.reportmanager.ReportData; 3 import com.calipso.reportgenerator.common.ReportQuery; 4 import com.calipso.reportgenerator.common.QueryDimension; 5 import com.calipso.reportgenerator.common.DimensionValueNode; 6 import java.util.ArrayList ; 7 import java.util.Iterator ; 8 import java.util.List ; 9 import java.util.Map ; 10 13 14 public class DimensionValueTreeBuilder { 15 private ReportData reportData; 16 17 21 public DimensionValueTreeBuilder(ReportData reportData) { 22 setReportData(reportData); 23 initCubeProccess(); 24 } 25 28 private void initCubeProccess() { 29 List rows = getReportData().getQuery().getRowDimensions(); 30 List cols = getReportData().getQuery().getColumnDimensions(); 31 ArrayList prevDimensions; 32 ArrayList values; 33 prevDimensions = new ArrayList (); 34 values = new ArrayList (); 35 rowsRoot = new DimensionValueNode("ROWS"); 36 rowsRoot.setDimensionCount(rows.size()); 37 if (rows.size() > 0) { 38 fillSubNodes(rowsRoot, rows, prevDimensions, values, -1); 39 } 40 columnsRoot = new DimensionValueNode("COLUMNS"); 41 columnsRoot.setDimensionCount(cols.size()); 42 prevDimensions = new ArrayList (); 43 values = new ArrayList (); 44 if (cols.size() > 0) { 45 fillSubNodes(columnsRoot, cols, prevDimensions, values, rows.size() -1); 46 } 47 } 48 private DimensionValueNode rowsRoot; 49 private DimensionValueNode columnsRoot; 50 public DimensionValueNode getRowsRoot(){ 51 return rowsRoot; 52 } 53 54 public DimensionValueNode getColumnsRoot(){ 55 return columnsRoot; 56 } 57 58 private void fillSubNodes(DimensionValueNode parentNode, List dimensions, ArrayList prevDimensions, ArrayList prevDimensionValues, int index){ 59 index++; 60 int currentDimension = index ; 61 Iterator iterator = getReportData().valuesFor(currentDimension, arrayListToIntArray(prevDimensions), prevDimensionValues.toArray()); 62 boolean lastDimension = prevDimensions.size() == dimensions.size() -1; 63 while (iterator.hasNext()){ 64 Map.Entry current = (Map.Entry ) iterator.next(); 65 Object currentValue = current.getKey(); 66 DimensionValueNode currentNode = new DimensionValueNode(currentValue, parentNode); 67 currentNode.setDimensionIndex(prevDimensions.size()); 68 currentNode.setDimensionCount(parentNode.getDimensionCount() -1); 69 parentNode.addSubNode(currentNode); 70 prevDimensions.add(new Integer (currentDimension)); 71 prevDimensionValues.add(currentValue); 73 if (!lastDimension){ 74 fillSubNodes(currentNode, dimensions, prevDimensions, prevDimensionValues, index); 75 } 76 prevDimensions.remove(prevDimensions.size()-1); 77 prevDimensionValues.remove(prevDimensionValues.size()-1); 78 } 79 } 80 81 private int[] arrayListToIntArray(ArrayList list){ 82 int[] result = new int[list.size()]; 83 for (int i = 0; i < list.size(); i++){ 84 result[i] = ((Integer )list.get(i)).intValue(); 85 } 86 return result; 87 } 88 89 90 public ReportData getReportData() { 91 return reportData; 92 } 93 94 public ReportQuery getQuery(){ 95 return getReportData().getQuery(); 96 } 97 98 public void setReportData(ReportData reportData) { 99 this.reportData = reportData; 100 } 101 } | Popular Tags |