KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > reportmanager > DataVectorBuilder


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 JavaDoc;
11 import javax.swing.table.DefaultTableModel JavaDoc;
12 import java.util.Vector JavaDoc;
13 import java.util.Collection JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.io.Serializable JavaDoc;
16
17 public class DataVectorBuilder implements Serializable JavaDoc {
18
19   private DimensionValueNode rowsRoot;
20   private DimensionValueNode colsRoot;
21   private ReportData reportData;
22   private Vector JavaDoc dataVector;
23   private boolean withTotals;
24   private int rowCount;
25   private int colCount;
26   private int[] metrics;
27   private int[] groupingDimensions;
28   private Float JavaDoc tempFloat = new Float JavaDoc(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 JavaDoc getDataVector() {
41     if (dataVector == null) {
42       dataVector = new Vector JavaDoc();
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 JavaDoc[] rowHeaders = new Object JavaDoc[0];
52       DimensionNodeIterator colsIterator = new DimensionNodeIterator(colsRoot, withTotals);
53       Vector JavaDoc dataValues = new Vector JavaDoc();
54       if (colsRoot.getDimensionCount() == 0) {
55         fillValue(dataValues, rowHeaders, new Object JavaDoc[0]);
56       }
57       else {
58         while (colsIterator.hasNext()) {
59           Object JavaDoc[] colHeaders = (Object JavaDoc[]) colsIterator.next();
60           fillValue(dataValues, rowHeaders, colHeaders);
61         }
62       }
63       dataVector.add(dataValues);
64       rowCount++;
65     }
66     else {
67       while (rowsIterator.hasNext()) {
68         Object JavaDoc[] rowHeaders = (Object JavaDoc[]) rowsIterator.next();
69         DimensionNodeIterator colsIterator = new DimensionNodeIterator(colsRoot, withTotals);
70         Vector JavaDoc dataValues = new Vector JavaDoc();
71         if (colsRoot.getDimensionCount() == 0) {
72           fillValue(dataValues, rowHeaders, new Object JavaDoc[0]);
73         }
74         else {
75           while (colsIterator.hasNext()) {
76             Object JavaDoc[] colHeaders = (Object JavaDoc[]) colsIterator.next();
77             fillValue(dataValues, rowHeaders, colHeaders);
78           }
79         }
80         dataVector.add(dataValues);
81         rowCount++;
82       }
83     }
84   }
85
86   private boolean isTotal(Object JavaDoc[] rowHeaders, Object JavaDoc[] 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 JavaDoc dataValues, Object JavaDoc[] rowHeaders, Object JavaDoc[] colHeaders) {
96     Object JavaDoc[] values = new Object JavaDoc[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 JavaDoc[] metricValues;
100     if (isTotal(rowHeaders, colHeaders)) {
101       //llenar con dimensiones y valores que no son null
102
Object JavaDoc[] params = getNotNullParams(values);
103       int[] dimensions = (int[]) params[0];
104       Object JavaDoc[] dimValues = (Object JavaDoc[]) params[1];
105       //metricValues = reportData.measuresAtDimensionsValues(dimensions, dimValues);
106
metricValues = reportData.getMetricValues(dimensions, dimValues);
107     }
108     else {
109       //metricValues = reportData.measuresAtDimensionsValues(getGroupingDimensionsIndexArray(), values);
110
metricValues = reportData.getMetricValues(getGroupingDimensionsIndexArray(), values);
111     }
112     if(metricValues == null){
113       for (int i = 0; i < getMetrics().length; i++) {
114         Object JavaDoc value = null;//SharedFloat.newFrom(tempFloat);
115
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 JavaDoc 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 = reportData.getQuery().getGroupingDimensionsIndexArray();
131
groupingDimensions = ((CubeReportData)reportData).getCube().getIntegerArray(0, reportData.getQuery().getGroupingDimensionsIndexArray().length);
132     }
133     return groupingDimensions;
134   }
135
136   private Object JavaDoc[] getNotNullParams(Object JavaDoc[] values) {
137     Object JavaDoc[] result = new Object JavaDoc[2];
138     Vector JavaDoc notNullDims = new Vector JavaDoc();
139     Vector JavaDoc notNullValues = new Vector JavaDoc();
140     for (int i = 0; i < values.length; i++) {
141       Object JavaDoc 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 JavaDoc selectedDimensions) {
153     int[] result;
154     int index;
155     Iterator JavaDoc 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 JavaDoc) iterator.next()).intValue();
161     }
162     return result;
163   }
164
165   private Integer JavaDoc dimensionFromValuesIndex(int index) {
166     return new Integer JavaDoc(index); /*int rowsCount = rowsRoot.getDimensionCount();
167     if (rowsCount == 0) {
168       //return new Integer(cube.getQuery().getColumns()[index]);
169       return new Integer(((QueryDimension)reportData.getQuery().getColumnDimensions().get(index)).getIndex());
170     }
171     else {
172     if (index >= rowsCount) {
173       //return new Integer(cube.getQuery().getColumns()[index - Math.max(rowsCount, 1)]);
174       return new Integer(((QueryDimension)reportData.getQuery().getColumnDimensions().get(index - Math.max(rowsCount, 1))).getIndex());
175     }
176     else {
177       //return new Integer(cube.getQuery().getRows()[index]);
178       return new Integer(((QueryDimension)reportData.getQuery().getRowDimensions().get(index)).getIndex());
179     }
180     }*/

181   }
182
183   public void invalidate() {
184     dataVector = null;
185     colCount = 0;
186     rowCount = 0;
187   }
188
189   public Vector JavaDoc getNames() {
190     Vector JavaDoc names = new Vector JavaDoc(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 JavaDoc(getDataVector(), getNames());
207   }
208
209 }
210
Popular Tags