KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > calipso > reportgenerator > userinterface > DinamicChartData


1 package com.calipso.reportgenerator.userinterface;
2
3 import com.calipso.reportgenerator.reportcalculator.*;
4 import com.calipso.reportgenerator.common.*;
5
6 import java.util.*;
7 import com.calipso.reportgenerator.common.InfoException;
8 import org.jfree.data.category.DefaultCategoryDataset;
9 import org.jfree.data.category.CategoryDataset;
10
11 /**
12  *
13  * User: soliveri
14  * Date: Jun 30, 2003
15  * Time: 2:05:07 PM
16  *
17  */

18
19 public class DinamicChartData {
20
21   private ReportQuery reportQuery;
22   private ReportResult reportResult;
23   private Vector datas;
24   private HeaderTableModel rowsModel;
25   private HeaderTableModel columnsModel;
26   private int metricsCount;
27
28   public DinamicChartData() {
29   }
30
31   public void updateDinamicChartData(ReportResult reportResult){
32     Object JavaDoc [] groupingDimensions = null;
33     Object JavaDoc [] nonGroupingDimensions = null;
34     Object JavaDoc [] metricsNames = null;
35     this.reportResult = reportResult;
36     this.reportQuery = reportResult.getReportQuery();
37     try {
38       groupingDimensions = getGroupingDimensions(reportResult);
39       nonGroupingDimensions = getNonGroupingDimensions(reportResult);
40       metricsNames = reportResult.getReportQuery().getVisibleMetrics().toArray();
41       //datas = getCubeTableModelData(groupingDimensions.length + nonGroupingDimensions.length + metricsNames.length);
42
datas = getTableModelData();
43     } catch (InfoException e) {
44       e.printStackTrace();
45     }
46   }
47
48   private Object JavaDoc[] getNonGroupingDimensions(ReportResult reportResult) throws InfoException {
49     ReportQuery query = reportResult.getReportQuery();
50     return getCubeNonGroupingDimensions(query, reportResult);
51   }
52
53   private Object JavaDoc[] getCubeNonGroupingDimensions(ReportQuery query, ReportResult reportResult) throws InfoException {
54     CubeQuery cubeQuery = query.getCubeQuery();
55     int[] dimensionIndexes = cubeQuery.getColumns();
56     return getQueryDimensionsArray(dimensionIndexes, reportResult);
57   }
58
59   private Object JavaDoc[] getGroupingDimensions(ReportResult reportResult) throws InfoException {
60     ReportQuery query = reportResult.getReportQuery();
61     return getCubeGroupingDimensions(query, reportResult);
62   }
63
64   private Object JavaDoc[] getCubeGroupingDimensions(ReportQuery query, ReportResult reportResult) throws InfoException {
65     CubeQuery cubeQuery = query.getCubeQuery();
66     int[] dimensionIndexes = cubeQuery.getRows();
67     return getQueryDimensionsArray(dimensionIndexes, reportResult);
68   }
69
70   private Object JavaDoc[] getQueryDimensionsArray(int[] indexes, ReportResult reportResult) throws InfoException {
71     ReportQuery query = reportResult.getReportQuery();
72     Object JavaDoc[] result = new Object JavaDoc[indexes.length];
73     for (int i = 0; i < indexes.length; i++) {
74       String JavaDoc name = reportResult.getReportSpec().getDimensionFromIndex(indexes[i]).getName();
75       QueryDimension dimension = query.getQueryDimensionFromName(name);
76       result[i] = dimension;
77     }
78     return result;
79   }
80
81     private Vector getTableModelData() {
82     rowsModel = ((CubeReportResult)reportResult).getRowsModel();
83     columnsModel = ((CubeReportResult)reportResult).getColumnsModel();
84     metricsCount = reportResult.getReportSpec().getMetrics().length;
85     Vector dataVector = ((CubeReportResult)reportResult).getDataVector();
86     Vector main = new Vector();
87     fillGroupingVectorFrom(new Vector(), main, rowsModel.getDimensionValueNode().getSubNodesList(), dataVector, 0);
88     return main;
89   }
90
91   private int fillGroupingVectorFrom(Vector first, Vector main, Collection subnodes, Vector dataVector, int row) {
92     if(subnodes.size() > 0) {
93       Iterator iterator = subnodes.iterator();
94       while(iterator.hasNext()) {
95         DimensionValueNode node = (DimensionValueNode) iterator.next();
96         first.add(node.getValue().toString());
97         if(node.getSubNodesList().size() == 0) {
98           fillNonGroupingVectorFrom(main, first, new Vector(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
99           row++;
100         } else {
101           row = fillGroupingVectorFrom(first, main, node.getSubNodesList(), dataVector, row);
102         }
103         first.removeElementAt(first.size() - 1);
104       }
105       return row;
106     } else {
107       fillNonGroupingVectorFrom(main, first, new Vector(), columnsModel.getDimensionValueNode().getSubNodesList(), dataVector, row, 0);
108       return 0;
109     }
110   }
111
112   private int fillNonGroupingVectorFrom(Vector main, Vector first, Vector second, Collection subNodes, Vector dataVector, int row, int col) {
113     if(subNodes.size() > 0) {
114       Iterator iterator = subNodes.iterator();
115       while(iterator.hasNext()) {
116         DimensionValueNode node = (DimensionValueNode) iterator.next();
117         second.add(node.getValue().toString());
118         if(node.getSubNodesList().size() == 0) {
119           col = getItemVectorFrom(main, first, second, dataVector, row, col, false);
120         } else {
121           col = fillNonGroupingVectorFrom(main, first, second, node.getSubNodesList(), dataVector, row, col);
122         }
123         second.removeElementAt(second.size() - 1);
124       }
125       return col;
126     } else {
127       getItemVectorFrom(main, first, second, dataVector, row, col, true);
128       return 0;
129     }
130   }
131
132   private int getItemVectorFrom(Vector main, Vector first, Vector second, Vector dataVector, int row, int col, boolean isColModelEmpty) {
133     Vector itemVector = new Vector();
134
135     for(int i = 0 ; i < first.size() ; i++) {
136       itemVector.add(first.elementAt(i));
137     }
138
139     for(int i = 0 ; i < second.size() ; i++) {
140       itemVector.add(second.elementAt(i));
141     }
142
143     int analizedCols = getDatasFrom(dataVector, row, col, itemVector, isColModelEmpty);
144
145     main.add(itemVector);
146     return analizedCols;
147   }
148
149   private int getDatasFrom(Vector dataVector, int row, int col, Vector itemVector, boolean isColModelEmpty) {
150     Vector vector = (Vector) dataVector.elementAt(row);
151     for(int i = 0 ; i < reportQuery.getVisibleMetrics().size() ; i++) {
152       SharedFloat sharedFloat = (SharedFloat) vector.elementAt(col);
153       itemVector.add(sharedFloat);
154       col++;
155     }
156     return col;
157   }
158
159   public CategoryDataset getCategoryDataset(boolean isByRow){
160     DefaultCategoryDataset dataset = new DefaultCategoryDataset();
161     int rowsDimensionsCount = reportQuery.getRowDimensions().size();
162     int columnsDimensionsCount = reportQuery.getColumnDimensions().size();
163     int metricsCount = reportQuery.getVisibleMetrics().size();
164     for(int j = 0 ; j < metricsCount ; j++){
165       for(int i = 0 ; i < datas.size() ; i++){
166         Vector element = (Vector) datas.elementAt(i);
167         double v;
168         if ((element.elementAt(element.size() - metricsCount + j))== null){
169           v = 0;
170         } else{
171           v = Double.valueOf(element.elementAt(element.size() - metricsCount + j).toString()).doubleValue();
172         }
173         if(isByRow){
174           //for(int j = element.size() - metricsCount ; j < element.size() ; j++){
175
dataset.addValue(v,
176                              getDescription(0, rowsDimensionsCount, element, null),
177                              getDescription(rowsDimensionsCount, rowsDimensionsCount + columnsDimensionsCount, element, (QueryMetric)reportQuery.getVisibleMetrics().get(j)));
178           //}
179
}else{
180           //for(int j = element.size() - metricsCount ; j < element.size() ; j++){
181
dataset.addValue(v,
182                            getDescription(rowsDimensionsCount , columnsDimensionsCount + rowsDimensionsCount, element, (QueryMetric)reportQuery.getVisibleMetrics().get(j)),
183                            getDescription(0, rowsDimensionsCount, element, null));
184
185           //}
186
}
187       }
188     }
189     return dataset;
190   }
191
192   private String JavaDoc getDescription(int from, int to, Vector element, QueryMetric desc){
193     String JavaDoc description = "";
194     for(int i = from ; i < to ; i++){
195       description = description + " " +element.elementAt(i);
196     }
197     if(desc != null){
198       return description + " " + desc.getName();
199     }
200     return description;
201   }
202 }
203
204
Popular Tags