KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > common > CubeReportTableModel


1 package com.calipso.reportgenerator.common;
2
3 import com.calipso.reportgenerator.reportcalculator.SharedFloat;
4 import com.calipso.reportgenerator.reportcalculator.CubeQuery;
5 import com.calipso.reportgenerator.reportcalculator.CubeValue;
6
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Vector JavaDoc;
10
11 /**
12  *
13  * User: soliveri
14  * Date: Dec 16, 2003
15  * Time: 3:02:55 PM
16  *
17  */

18
19 public class CubeReportTableModel extends ReportTableModel {
20
21   private CubeReportResult reportResult;
22   private HeaderTableModel rowsModel;
23   private HeaderTableModel columnsModel;
24   private int metricsCount;
25
26
27   public CubeReportTableModel(CubeReportResult reportResult) throws InfoException {
28     super();
29     this.reportResult = reportResult;
30     initialize();
31   }
32
33   private void initialize() throws InfoException {
34     Object JavaDoc [] groupingDimensions = getGroupingDimensions();
35     Object JavaDoc [] nonGroupingDimensions = getNonGroupingDimensions();
36     Object JavaDoc [] totalMetrics = getTotalMetrics();
37     setGroupingDimCount(groupingDimensions.length);
38     setNonGroupingDimCount(nonGroupingDimensions.length);
39     setCommonMetricsCount(reportResult.getReportQuery().getVisibleMetrics().size());
40     setAccMetricsCount(reportResult.getReportQuery().getVisibleAccumulableMetrics().size());
41     setTableData(getTableModelData());
42     setTableColumnNames(getTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics));
43     newDefaultTableModel();
44   }
45
46   /**
47    * Obtiene los valores de las Dimensiones que no agrupan, es decir, las Row Dimensions
48    * a partir de un reporte tipo Cube.
49    * @return array que contiene objetos de tipo QueryDimension
50    * @throws com.calipso.reportgenerator.common.InfoException
51    */

52   private Object JavaDoc[] getNonGroupingDimensions() throws InfoException {
53     CubeQuery cubeQuery = reportResult.getReportQuery().getCubeQuery();
54     int[] dimensionIndexes = cubeQuery.getColumns();
55     return getQueryDimensionsArray(dimensionIndexes, reportResult);
56   }
57
58   /**
59    * Obtiene los valores de las Dimensiones que agrupan, es decir, las Row Dimensions
60    * a partir de un reporte tipo Cube.
61    * @return array que contiene objetos de tipo QueryDimension
62    * @throws com.calipso.reportgenerator.common.InfoException
63    */

64   private Object JavaDoc[] getGroupingDimensions() throws InfoException {
65     CubeQuery cubeQuery = reportResult.getReportQuery().getCubeQuery();
66     int[] dimensionIndexes = cubeQuery.getRows();
67     return getQueryDimensionsArray(dimensionIndexes, reportResult);
68   }
69
70   /**
71    * Inicializa un array con objetos de tipo QueryDimension.
72    * @param indexes array que contiene los valores de las dimensiones
73    * @param reportResult necesario para obtener los valores de las dimensions
74    * @return array que contiene objetos de tipo QueryDimension
75    * @throws com.calipso.reportgenerator.common.InfoException
76    */

77   private Object JavaDoc[] getQueryDimensionsArray(int[] indexes, ReportResult reportResult) throws InfoException {
78     ReportQuery query = reportResult.getReportQuery();
79     Object JavaDoc[] result = new Object JavaDoc[indexes.length];
80     for (int i = 0; i < indexes.length; i++) {
81       String JavaDoc name = reportResult.getReportSpec().getDimensionFromIndex(indexes[i]).getName();
82       QueryDimension dimension = query.getQueryDimensionFromName(name);
83       result[i] = dimension;
84     }
85     return result;
86   }
87
88   private Object JavaDoc[] getTotalMetrics() {
89     Object JavaDoc [] metricsNames = reportResult.getReportQuery().getVisibleMetrics().toArray();
90     Object JavaDoc [] accuMetricsNames = reportResult.getReportQuery().getAccumulableMetrics().toArray();
91     Object JavaDoc [] allMetrics = new Object JavaDoc[metricsNames.length + accuMetricsNames.length];
92     int i = 0;
93     for( ; i < metricsNames.length ; i++) {
94       allMetrics[i] = metricsNames[i];
95     }
96     for(int j = 0 ; j < accuMetricsNames.length ; j++, i++) {
97       QueryMetric queryMetric = (QueryMetric) accuMetricsNames[j];
98       queryMetric.setName(queryMetric.getName() + "_ACCUM");//LanguageTraslator.traslate("314")
99
allMetrics[i] = queryMetric;
100     }
101     return allMetrics;
102   }
103
104   private Vector JavaDoc getTableModelData() {
105     rowsModel = reportResult.getRowsModel();
106     columnsModel = reportResult.getColumnsModel();
107     //metricsCount = reportResult.getReportSpec().getMetrics().length;
108
metricsCount = reportResult.getReportQuery().getVisibleMetrics().size();
109     Vector JavaDoc dataVector = reportResult.getDataVector();
110     Vector JavaDoc main = new Vector JavaDoc();
111     if(super.getNonGroupingDimCount() == 0) {
112       fillGroupingVectorFrom2(new Vector JavaDoc(), main, rowsModel.getDimensionValueNode().getSubNodesList(), dataVector, 0);
113     } else {
114       fillGroupingVectorFrom(new Vector JavaDoc(), main, rowsModel.getDimensionValueNode().getSubNodesList(), dataVector, 0);
115     }
116     return main;
117   }
118
119   private int fillGroupingVectorFrom(Vector JavaDoc first, Vector JavaDoc main, Collection JavaDoc subnodes, Vector JavaDoc dataVector, int row) {
120     if(subnodes.size() > 0) {
121       Iterator JavaDoc iterator = subnodes.iterator();
122       while(iterator.hasNext()) {
123         DimensionValueNode node = (DimensionValueNode) iterator.next();
124         first.add(node.getValue().toString());
125         if(node.getSubNodesList().size() == 0) {
126           while(rowsModel.isTotalRow(row)){
127             row++;
128           }
129           /*while(rowsModel.getValueFrom(row) [rowsModel.getColumnCount() - 1] == null) {
130             row++;
131           }*/

132           fillNonGroupingVectorFrom(main, first, new Vector JavaDoc(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
133           row++;
134         } else {
135           row = fillGroupingVectorFrom(first, main, node.getSubNodesList(), dataVector, row);
136         }
137         first.removeElementAt(first.size() - 1);
138       }
139       return row;
140     } else {
141       fillNonGroupingVectorFrom(main, first, new Vector JavaDoc(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
142       return 0;
143     }
144   }
145
146   private int fillNonGroupingVectorFrom(Vector JavaDoc main, Vector JavaDoc first, Vector JavaDoc second, Collection JavaDoc subNodes, Vector JavaDoc dataVector, int row, int col) {
147     boolean leafReached = false;
148     DimensionValueNode node = null;
149     if(subNodes.size() > 0) {
150       Iterator JavaDoc iterator = subNodes.iterator();
151       while(iterator.hasNext() && !leafReached) {
152         node = (DimensionValueNode) iterator.next();
153         if(node.getSubNodesList().size() == 0) {
154           leafReached = true;
155           col = getItemVectorFrom(main, first, second, dataVector, row, col, false, node);
156         } else {
157           second.add(node.getValue().toString());
158           col = fillNonGroupingVectorFrom(main, first, second, node.getSubNodesList(), dataVector, row, col);
159           leafReached = false;
160           second.removeElementAt(second.size() - 1);
161         }
162       }
163       return col;
164     } else {
165       getItemVectorFrom(main, first, second, dataVector, row, col, true, node);
166       return 0;
167     }
168   }
169
170   private int getItemVectorFrom(Vector JavaDoc main, Vector JavaDoc first, Vector JavaDoc second, Vector JavaDoc dataVector, int row, int col, boolean isColModelEmpty, DimensionValueNode node) {
171     Vector JavaDoc itemVector = new Vector JavaDoc();
172
173     for(int i = 0 ; i < first.size() ; i++) {
174       itemVector.add(first.elementAt(i));
175     }
176
177     for(int i = 0 ; i < second.size() ; i++) {
178       itemVector.add(second.elementAt(i));
179     }
180
181     int analizedCols = getDatasFrom(dataVector, row, col, itemVector, isColModelEmpty, node);
182
183     main.add(itemVector);
184     return analizedCols;
185   }
186
187   private int getDatasFrom(Vector JavaDoc dataVector, int row, int col, Vector JavaDoc itemVector, boolean isColModelEmpty, DimensionValueNode node) {
188     int i = 0, p = 0;
189     Vector JavaDoc ocurrences = new Vector JavaDoc();
190     fillOcurrences(ocurrences, columnsModel.getDimensionValueNode().getSubNodesList());
191
192     //Determinar si hay algun valor de dimension previo que no se muestra en la tabla
193
for(int x = 0 ; x < ocurrences.size() ; x++) {
194       String JavaDoc currentOcurrence = (String JavaDoc) ocurrences.elementAt(x);
195       if(currentOcurrence.equals(node.getValue().toString())) {
196         break;
197       } else {
198         for(int z = 0 ; z < metricsCount ; z++) {
199           SharedFloat sharedFloat = SharedFloat.newFrom(0);
200           itemVector.add(new Float JavaDoc(sharedFloat.floatValue()));
201           p++;
202         }
203       }
204     }
205
206     //Agregamos los datos que realmente estan en la tabla
207
Vector JavaDoc vector = (Vector JavaDoc) dataVector.elementAt(row);
208     for( ; i < (metricsCount * node.getParentNode().getSubNodesList().size()) ; i++) {
209       if(!isColModelEmpty) {
210         while(columnsModel.isTotalCol(col)){
211           col++;
212         }
213         /*while((columnsModel.getValueFrom(col) [columnsModel.getRowCount() - 1] == null)) {
214           col++;
215         }*/

216       }
217       SharedFloat sharedFloat = (SharedFloat) vector.elementAt(col);
218       if (sharedFloat==null){
219         itemVector.add(new Float JavaDoc(0));
220       }else{
221         itemVector.add(new Float JavaDoc(sharedFloat.floatValue()));
222       }
223       col++;
224     }
225
226     //Determinar si hay algun valor de dimension posterior que no se muestra en la tabla
227
while((i + p) < (ocurrences.size() * metricsCount)) {
228       SharedFloat sharedFloat = SharedFloat.newFrom(0);
229       itemVector.add(new Float JavaDoc(sharedFloat.floatValue()));
230       i++;
231     }
232     //Agrega los totales para una fila
233
if(reportResult.getReportQuery().isVisibleTotals()){
234       int index = col;
235       for(int j = 0 ; j < metricsCount ; j++) {
236         SharedFloat sharedFloat = (SharedFloat) vector.elementAt(index);
237         itemVector.add(new Float JavaDoc(sharedFloat.floatValue()));
238         index++;
239       }
240     }
241
242     return col;
243   }
244
245
246   protected Vector JavaDoc getTableModelColumns(Object JavaDoc[] groupingDimensions, Object JavaDoc[] nonGroupingDimensions, Object JavaDoc[] metricsArray) {
247     Vector JavaDoc columnNames = new Vector JavaDoc();
248     for (int i = 0; i < groupingDimensions.length; i++) {
249       QueryDimension qd = (QueryDimension) groupingDimensions[i];
250       columnNames.add(qd.getName());
251     }
252
253     occurrences = new Vector JavaDoc();
254     fillOcurrences(occurrences, reportResult.getColumnsModel().getDimensionValueNode().getSubNodesList());
255
256     if(nonGroupingDimensions.length > 0) {
257       for (int i = 0; i < nonGroupingDimensions.length; i++) {
258         QueryDimension qd = (QueryDimension) nonGroupingDimensions[i];
259         if(i + 1 == nonGroupingDimensions.length) {
260           for(int j = 0 ; j < occurrences.size() ; j++) {
261             for (int z = 0; z < metricsArray.length; z++) {
262               QueryMetric qm = (QueryMetric) metricsArray[z];
263               columnNames.add(j + "_" + qm.getName());
264             }
265           }
266           //Nombra las filas de totales
267
if(reportResult.getReportQuery().isVisibleTotals()){
268             for (int j = 0; j < metricsArray.length; j++) {
269               QueryMetric qm = (QueryMetric) metricsArray[j];
270               columnNames.add("TOTAL_" + qm.getName());
271             }
272           }
273         } else {
274           columnNames.add(qd.getName());
275         }
276       }
277     } else {
278       for (int i = 0; i < metricsArray.length; i++) {
279         QueryMetric qm = (QueryMetric) metricsArray[i];
280         columnNames.add(qm.getName());
281       }
282
283 // for (int i = 0; i < metricsArray.length; i++) {
284
// QueryMetric qm = (QueryMetric) metricsArray[i];
285
// columnNames.add("TOTAL" + qm.getName());
286
// }
287
}
288     return columnNames;
289   }
290
291   private String JavaDoc getWellFormedString(String JavaDoc s) {
292     StringBuffer JavaDoc buffer = null;
293     String JavaDoc [] strings = null;
294
295     if(s.split("-").length > 1) {
296       buffer = new StringBuffer JavaDoc();
297       strings = s.split("-");
298     } else {
299       if(s.split("/").length > 1) {
300         buffer = new StringBuffer JavaDoc();
301         strings = s.split("/");
302       } else {
303         int i = s.indexOf(46);
304         if(i != -1) {
305           buffer = new StringBuffer JavaDoc();
306           for(int j = 0 ; j < s.length() ; j++) {
307             if(j != i) {
308               String JavaDoc tmp = String.valueOf(s.charAt(j));
309               buffer.append(tmp);
310             }
311           }
312           return buffer.toString();
313         }
314       }
315     }
316
317     if(strings != null) {
318       for(int i = 0 ; i < strings.length ; i++) {
319         buffer.append(strings [i]);
320       }
321       return buffer.toString();
322     } else {
323       return s;
324     }
325   }
326
327   private void fillOcurrences(Vector JavaDoc occurrences, Collection JavaDoc subNodesList) {
328     Iterator JavaDoc childs = subNodesList.iterator();
329     while(childs.hasNext()) {
330       DimensionValueNode currentNode = (DimensionValueNode) childs.next();
331       if(currentNode.getSubNodesList().size() == 0) {
332         if(!(occurrences.contains(currentNode.getValue().toString()))) {
333           occurrences.add(currentNode.getValue().toString());
334         }
335       } else {
336         fillOcurrences(occurrences, currentNode.getSubNodesList());
337       }
338     }
339   }
340
341   private int fillGroupingVectorFrom2(Vector JavaDoc first, Vector JavaDoc main, Collection JavaDoc subnodes, Vector JavaDoc dataVector, int row) {
342     if(subnodes.size() > 0) {
343       Iterator JavaDoc iterator = subnodes.iterator();
344       while(iterator.hasNext()) {
345         DimensionValueNode node = (DimensionValueNode) iterator.next();
346         Object JavaDoc value = node.getValue();
347         if(value instanceof CubeValue){
348           value = ((CubeValue)value).getValue();
349         }
350         first.add(value);
351         if(node.getSubNodesList().size() == 0) {
352           while(rowsModel.isTotalRow(row)){
353             row++;
354           }
355           fillNonGroupingVectorFrom2(main, first, new Vector JavaDoc(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
356           row++;
357         } else {
358           row = fillGroupingVectorFrom2(first, main, node.getSubNodesList(), dataVector, row);
359         }
360         first.removeElementAt(first.size() - 1);
361       }
362       return row;
363     } else {
364       fillNonGroupingVectorFrom2(main, first, new Vector JavaDoc(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
365       return 0;
366     }
367   }
368
369   private int fillNonGroupingVectorFrom2(Vector JavaDoc main, Vector JavaDoc first, Vector JavaDoc second, Collection JavaDoc subNodes, Vector JavaDoc dataVector, int row, int col) {
370     if(subNodes.size() > 0) {
371       Iterator JavaDoc iterator = subNodes.iterator();
372       while(iterator.hasNext()) {
373         DimensionValueNode node = (DimensionValueNode) iterator.next();
374         second.add(node.getValue().toString());
375         if(node.getSubNodesList().size() == 0) {
376           col = getItemVectorFrom2(main, first, second, dataVector, row, col, false);
377         } else {
378           col = fillNonGroupingVectorFrom2(main, first, second, node.getSubNodesList(), dataVector, row, col);
379         }
380         second.removeElementAt(second.size() - 1);
381       }
382       return col;
383     } else {
384       getItemVectorFrom2(main, first, second, dataVector, row, col, true);
385       return 0;
386     }
387   }
388
389   private int getItemVectorFrom2(Vector JavaDoc main, Vector JavaDoc first, Vector JavaDoc second, Vector JavaDoc dataVector, int row, int col, boolean isColModelEmpty) {
390     Vector JavaDoc itemVector = new Vector JavaDoc();
391
392     for(int i = 0 ; i < first.size() ; i++) {
393       itemVector.add(first.elementAt(i));
394     }
395
396     for(int i = 0 ; i < second.size() ; i++) {
397       itemVector.add(second.elementAt(i));
398     }
399
400     int analizedCols = getDatasFrom2(dataVector, row, col, itemVector, isColModelEmpty);
401
402     main.add(itemVector);
403     return analizedCols;
404   }
405
406   private int getDatasFrom2(Vector JavaDoc dataVector, int row, int col, Vector JavaDoc itemVector, boolean isColModelEmpty) {
407     Vector JavaDoc vector = (Vector JavaDoc) dataVector.elementAt(row);
408     for(int i = 0 ; i < metricsCount ; i++) {
409       if(!isColModelEmpty) {
410         while(columnsModel.isTotalCol(col)){
411           col++;
412         }
413       }
414       SharedFloat sharedFloat = (SharedFloat) vector.elementAt(col);
415       itemVector.add(new Float JavaDoc(sharedFloat.floatValue()));
416       col++;
417     }
418     return col;
419   }
420
421 }
422
Popular Tags