KickJava   Java API By Example, From Geeks To Geeks.

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


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

16
17 public class StaticReportTableModel extends ReportTableModel {
18
19   private StaticReportResult reportResult;
20
21   public StaticReportTableModel(StaticReportResult reportResult) throws InfoException {
22     super();
23     this.reportResult = reportResult;
24     initialize();
25   }
26
27   public StaticReportTableModel(ReportSpec reportSpec, ReportQuery query){
28     super();
29     initialize(reportSpec, query);
30   }
31
32   public StaticReportTableModel(ReportSpec reportSpec) throws InfoException{
33     super();
34     ReportQuery query = new ReportQuery(reportSpec);
35     initialize(reportSpec, query);
36   }
37
38   private void initialize(ReportSpec reportSpec, ReportQuery query){
39     Object JavaDoc [] groupingDimensions;
40     Object JavaDoc [] nonGroupingDimensions;
41     groupingDimensions = reportSpec.getVisibleDimensionsByGrouping(true);
42     nonGroupingDimensions = reportSpec.getVisibleDimensionsByGrouping(false);
43     Object JavaDoc [] totalMetrics = reportSpec.getMetrics();
44     setGroupingDimCount(groupingDimensions.length);
45     setNonGroupingDimCount(nonGroupingDimensions.length);
46     setCommonMetricsCount(query.getVisibleMetrics().size());
47     setAccMetricsCount(query.getVisibleAccumulableMetrics().size());
48     setTableColumnNames(getSpecTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics));
49 // setTableData(getTableModelData());
50
newNonDataTableModel();
51   }
52
53   protected Vector JavaDoc getSpecTableModelColumns(Object JavaDoc[] groupingDimensions, Object JavaDoc[] nonGroupingDimensions, Object JavaDoc[] metricsArray) {
54     Vector JavaDoc columnNames = new Vector JavaDoc();
55     for (int i = 0; i < groupingDimensions.length; i++) {
56       ReportDimensionSpec qd = (ReportDimensionSpec) groupingDimensions[i];
57       columnNames.add(qd.getName());
58     }
59     for (int i = 0; i < nonGroupingDimensions.length; i++) {
60       ReportDimensionSpec qd = (ReportDimensionSpec) nonGroupingDimensions[i];
61       columnNames.add(qd.getName());
62     }
63     for (int i = 0; i < metricsArray.length; i++) {
64       ReportMetricSpec qm = (ReportMetricSpec) metricsArray[i];
65       columnNames.add(qm.getName());
66     }
67     return columnNames;
68   }
69
70   private void initialize() throws InfoException {
71     Object JavaDoc [] groupingDimensions = getGroupingDimensions();
72     Object JavaDoc [] nonGroupingDimensions = getNonGroupingDimensions();
73     Object JavaDoc [] totalMetrics = getTotalMetrics();
74     setGroupingDimCount(groupingDimensions.length);
75     setNonGroupingDimCount(nonGroupingDimensions.length);
76     setCommonMetricsCount(reportResult.getReportQuery().getVisibleMetrics().size());
77     setAccMetricsCount(reportResult.getReportQuery().getVisibleAccumulableMetrics().size());
78     setTableColumnNames(getTableModelColumns(groupingDimensions, nonGroupingDimensions, totalMetrics));
79     setTableData(getTableModelData());
80     newDefaultTableModel();
81   }
82
83   /**
84    * Obtiene los valores de las Dimensiones que agrupan, es decir, las Row Dimensiones
85    * a partir de un reporte tipo Static.
86    * @return array que contiene objetos de tipo QueryDimension
87    * @throws com.calipso.reportgenerator.common.InfoException
88    */

89   private Object JavaDoc[] getGroupingDimensions() throws InfoException {
90     StaticQuery staticQuery = reportResult.getDataTree().getQuery();
91     int[] dimensionIndexes = staticQuery.getGroupDimensions();
92     return getQueryDimensionsArray(dimensionIndexes, reportResult);
93   }
94
95   /**
96    * Obtiene los valores de las Dimensiones que no agrupan, es decir, las Row Dimensiones
97    * a partir de un reporte tipo Static.
98    * @return array que contiene objetos de tipo QueryDimension
99    * @throws com.calipso.reportgenerator.common.InfoException
100    */

101   private Object JavaDoc[] getNonGroupingDimensions() throws InfoException {
102     StaticQuery staticQuery = reportResult.getDataTree().getQuery();
103     int[] dimensionIndexes = staticQuery.getNoGroupDimensions();
104     return getQueryDimensionsArray(dimensionIndexes, reportResult);
105   }
106
107   /**
108    * Inicializa un array con objetos de tipo QueryDimension.
109    * @param indexes array que contiene los valores de las dimensiones
110    * @param reportResult necesario para obtener los valores de las dimensions
111    * @return array que contiene objetos de tipo QueryDimension
112    * @throws com.calipso.reportgenerator.common.InfoException
113    */

114   private Object JavaDoc[] getQueryDimensionsArray(int[] indexes, ReportResult reportResult) throws InfoException {
115     ReportQuery query = reportResult.getReportQuery();
116     Object JavaDoc[] result = new Object JavaDoc[indexes.length];
117     for (int i = 0; i < indexes.length; i++) {
118       String JavaDoc name = reportResult.getReportSpec().getDimensionFromIndex(indexes[i]).getName();
119       QueryDimension dimension = query.getQueryDimensionFromName(name);
120       result[i] = dimension;
121     }
122     return result;
123   }
124
125   private Object JavaDoc[] getTotalMetrics() {
126     Object JavaDoc [] metricsNames = reportResult.getReportQuery().getVisibleMetrics().toArray();
127     Object JavaDoc [] accuMetricsNames = reportResult.getReportQuery().getAccumulableMetrics().toArray();
128     Object JavaDoc [] allMetrics = new Object JavaDoc[metricsNames.length + accuMetricsNames.length];
129     int i = 0;
130     for( ; i < metricsNames.length ; i++) {
131       allMetrics[i] = metricsNames[i];
132     }
133     for(int j = 0 ; j < accuMetricsNames.length ; j++, i++) {
134       QueryMetric queryMetric = (QueryMetric) accuMetricsNames[j];
135       queryMetric.setName(queryMetric.getName() + "_ACCUM");//LanguageTraslator.traslate("314")
136
allMetrics[i] = queryMetric;
137     }
138     return allMetrics;
139   }
140
141   /**
142    * Devuelve un vector que contiene los datos para el model de la tabla
143    * para un reporte de tipo estatico
144    * @return datos para el model de la tabla
145    */

146   private Vector JavaDoc getTableModelData() {
147     DataTree dataTree = reportResult.getDataTree();
148     return getDataVector(dataTree);
149   }
150
151   /**
152    * Se ejecuta en caso de que el tipo de Reporte sea Static. Devuelve un vector
153    * que contiene los datos de la tabla necesarios para generar el modelo
154    * @param dataTree arbol de datos
155    * @return datos de la tabla
156    */

157   private Vector JavaDoc getDataVector(DataTree dataTree) {
158     Vector JavaDoc dataVector = new Vector JavaDoc();
159     Vector JavaDoc previousValues = new Vector JavaDoc();
160     fillFromNode(dataVector, dataTree.getRoot(), previousValues);
161     return dataVector;
162   }
163
164   /**
165    * Itera el arbol de datos y llena un vector que contiene los datos
166    * en formato plano para generar el modelo de la table
167    * @param dataVector datos necesarios para generar la tabla
168    * @param node nodo del arbol de datos
169    * @param previousValues
170    */

171   private void fillFromNode(Vector JavaDoc dataVector, DataTreeNode node, Vector JavaDoc previousValues) {
172     if (node.getSubNodes().size() == 0) {
173       Iterator JavaDoc iterator = node.getSubItems().iterator();
174       while (iterator.hasNext()) {
175         DataTreeSubItem subItem = (DataTreeSubItem) iterator.next();
176         dataVector.add(getItemVector(previousValues, subItem));
177       }
178     }
179     else {
180       Iterator JavaDoc iterator = node.getSubNodes().values().iterator();
181       while (iterator.hasNext()) {
182         DataTreeNode subNode = (DataTreeNode) iterator.next();
183         previousValues.add(subNode.getValue());
184         fillFromNode(dataVector, subNode, previousValues);
185         previousValues.remove(previousValues.size() - 1);
186       }
187     }
188   }
189
190   /**
191    * Devuelve un vector que contiene los datos de una fila.
192    * @param previousValues
193    * @param subItem
194    * @return
195    */

196   private Vector JavaDoc getItemVector(Vector JavaDoc previousValues, DataTreeSubItem subItem) {
197     Vector JavaDoc itemVector = new Vector JavaDoc(previousValues);
198     String JavaDoc[] ngValues = subItem.getNonGroupingDimensionValues();
199     for (int i = 0; i < ngValues.length; i++) {
200       String JavaDoc ngValue = ngValues[i];
201       itemVector.add(ngValue);
202     }
203
204     SharedFloat[] metricValues = subItem.getMetricValues();
205     for (int i = 0; i < metricValues.length; i++) {
206       SharedFloat metricValue = metricValues[i];
207       itemVector.add(new Float JavaDoc(metricValue.floatValue()));
208     }
209
210
211     SharedFloat[] accMetricValues = subItem.getAccumulableMetricValues();
212     for (int i = 0; i < accMetricValues.length; i++) {
213       SharedFloat accMetricValue = accMetricValues[i];
214       itemVector.add(new Float JavaDoc(accMetricValue.floatValue()));
215     }
216
217     return itemVector;
218   }
219 }
220
Popular Tags