1 package com.calipso.reportgenerator.reportmanager; 2 3 import com.calipso.reportgenerator.reportmanager.ReportData; 4 import com.calipso.reportgenerator.common.QueryDimension; 5 import com.calipso.reportgenerator.common.ReportQuery; 6 import com.calipso.reportgenerator.common.DimensionValueNode; 7 import com.calipso.reportgenerator.common.HeaderTableModel; 8 import com.calipso.reportgenerator.reportcalculator.SharedFloat; 9 10 import javax.swing.table.TableModel ; 11 import javax.swing.table.DefaultTableModel ; 12 import java.util.Vector ; 13 import java.util.Collection ; 14 import java.util.Iterator ; 15 import java.io.Serializable ; 16 17 public class DataVectorBuilder implements Serializable { 18 19 private DimensionValueNode rowsRoot; 20 private DimensionValueNode colsRoot; 21 private ReportData reportData; 22 private Vector dataVector; 23 private boolean withTotals; 24 private int rowCount; 25 private int colCount; 26 private int[] metrics; 27 private int[] groupingDimensions; 28 private Float tempFloat = new Float (0); 29 30 31 public DataVectorBuilder(DimensionValueNode rowsRoot, DimensionValueNode colsRoot, ReportQuery reportQuery, ReportData reportData, boolean withTotals) { 32 this.rowsRoot = rowsRoot; 33 this.colsRoot = colsRoot; 34 this.reportData = reportData; 35 this.withTotals = withTotals; 36 this.metrics = reportQuery.getCubeVisibleMetricFields(); 37 getDataVector(); 38 } 39 40 public Vector getDataVector() { 41 if (dataVector == null) { 42 dataVector = new Vector (); 43 fill(); 44 } 45 return dataVector; 46 } 47 48 private void fill() { 49 DimensionNodeIterator rowsIterator = new DimensionNodeIterator(rowsRoot, withTotals); 50 if (rowsRoot.getDimensionCount() == 0) { 51 Object [] rowHeaders = new Object [0]; 52 DimensionNodeIterator colsIterator = new DimensionNodeIterator(colsRoot, withTotals); 53 Vector dataValues = new Vector (); 54 if (colsRoot.getDimensionCount() == 0) { 55 fillValue(dataValues, rowHeaders, new Object [0]); 56 } 57 else { 58 while (colsIterator.hasNext()) { 59 Object [] colHeaders = (Object []) colsIterator.next(); 60 fillValue(dataValues, rowHeaders, colHeaders); 61 } 62 } 63 dataVector.add(dataValues); 64 rowCount++; 65 } 66 else { 67 while (rowsIterator.hasNext()) { 68 Object [] rowHeaders = (Object []) rowsIterator.next(); 69 DimensionNodeIterator colsIterator = new DimensionNodeIterator(colsRoot, withTotals); 70 Vector dataValues = new Vector (); 71 if (colsRoot.getDimensionCount() == 0) { 72 fillValue(dataValues, rowHeaders, new Object [0]); 73 } 74 else { 75 while (colsIterator.hasNext()) { 76 Object [] colHeaders = (Object []) colsIterator.next(); 77 fillValue(dataValues, rowHeaders, colHeaders); 78 } 79 } 80 dataVector.add(dataValues); 81 rowCount++; 82 } 83 } 84 } 85 86 private boolean isTotal(Object [] rowHeaders, Object [] colHeaders) { 87 return (rowHeaders.length > 0 && rowHeaders[rowHeaders.length - 1] == null) || 88 (colHeaders.length > 0 && colHeaders[colHeaders.length - 1] == null); 89 } 90 91 private int[] getMetrics() { 92 return metrics; 93 } 94 95 private void fillValue(Vector dataValues, Object [] rowHeaders, Object [] colHeaders) { 96 Object [] values = new Object [rowHeaders.length + colHeaders.length]; 97 System.arraycopy(rowHeaders, 0, values, 0, rowHeaders.length); 98 System.arraycopy(colHeaders, 0, values, rowHeaders.length, colHeaders.length); 99 Object [] metricValues; 100 if (isTotal(rowHeaders, colHeaders)) { 101 Object [] params = getNotNullParams(values); 103 int[] dimensions = (int[]) params[0]; 104 Object [] dimValues = (Object []) params[1]; 105 metricValues = reportData.getMetricValues(dimensions, dimValues); 107 } 108 else { 109 metricValues = reportData.getMetricValues(getGroupingDimensionsIndexArray(), values); 111 } 112 if(metricValues == null){ 113 for (int i = 0; i < getMetrics().length; i++) { 114 Object value = null; dataValues.add(value); 116 if (rowCount == 0) colCount++; 117 } 118 }else{ 119 for (int i = 0; i < getMetrics().length; i++) { 120 int metricIndex = getMetrics()[i] - this.reportData.getQuery().getReportSpec().getDimensions().length + this.getGroupingDimensionsIndexArray().length; 121 Object value = metricValues[metricIndex]; 122 dataValues.add(value); 123 if (rowCount == 0) colCount++; 124 } 125 } 126 } 127 128 private int[] getGroupingDimensionsIndexArray() { 129 if (groupingDimensions == null){ 130 groupingDimensions = ((CubeReportData)reportData).getCube().getIntegerArray(0, reportData.getQuery().getGroupingDimensionsIndexArray().length); 132 } 133 return groupingDimensions; 134 } 135 136 private Object [] getNotNullParams(Object [] values) { 137 Object [] result = new Object [2]; 138 Vector notNullDims = new Vector (); 139 Vector notNullValues = new Vector (); 140 for (int i = 0; i < values.length; i++) { 141 Object value = values[i]; 142 if (value != null) { 143 notNullValues.add(value); 144 notNullDims.add(dimensionFromValuesIndex(i)); 145 } 146 } 147 result[0] = toIntArray(notNullDims); 148 result[1] = notNullValues.toArray(); 149 return result; 150 } 151 152 private int[] toIntArray(Collection selectedDimensions) { 153 int[] result; 154 int index; 155 Iterator iterator; 156 int selectedDimensionsSize = selectedDimensions.size(); 157 result = new int[selectedDimensionsSize]; 158 iterator = selectedDimensions.iterator(); 159 for (index = 0; index < selectedDimensionsSize; index++) { 160 result[index] = ((Integer ) iterator.next()).intValue(); 161 } 162 return result; 163 } 164 165 private Integer dimensionFromValuesIndex(int index) { 166 return new Integer (index); 181 } 182 183 public void invalidate() { 184 dataVector = null; 185 colCount = 0; 186 rowCount = 0; 187 } 188 189 public Vector getNames() { 190 Vector names = new Vector (getColCount()); 191 for (int i = 0; i < getColCount(); i++) { 192 names.add(String.valueOf(i)); 193 } 194 return names; 195 } 196 197 public int getRowCount() { 198 return Math.max(rowCount, 1); 199 } 200 201 public int getColCount() { 202 return Math.max(colCount, 1); 203 } 204 205 public TableModel getDataModel() { 206 return new DefaultTableModel (getDataVector(), getNames()); 207 } 208 209 } 210 | Popular Tags |