001: package com.calipso.reportgenerator.userinterface;
002:
003: import com.calipso.reportgenerator.reportcalculator.*;
004: import com.calipso.reportgenerator.common.*;
005:
006: import java.util.*;
007: import com.calipso.reportgenerator.common.InfoException;
008: import org.jfree.data.category.DefaultCategoryDataset;
009: import org.jfree.data.category.CategoryDataset;
010:
011: /**
012: *
013: * User: soliveri
014: * Date: Jun 30, 2003
015: * Time: 2:05:07 PM
016: *
017: */
018:
019: public class DinamicChartData {
020:
021: private ReportQuery reportQuery;
022: private ReportResult reportResult;
023: private Vector datas;
024: private HeaderTableModel rowsModel;
025: private HeaderTableModel columnsModel;
026: private int metricsCount;
027:
028: public DinamicChartData() {
029: }
030:
031: public void updateDinamicChartData(ReportResult reportResult) {
032: Object[] groupingDimensions = null;
033: Object[] nonGroupingDimensions = null;
034: Object[] metricsNames = null;
035: this .reportResult = reportResult;
036: this .reportQuery = reportResult.getReportQuery();
037: try {
038: groupingDimensions = getGroupingDimensions(reportResult);
039: nonGroupingDimensions = getNonGroupingDimensions(reportResult);
040: metricsNames = reportResult.getReportQuery()
041: .getVisibleMetrics().toArray();
042: //datas = getCubeTableModelData(groupingDimensions.length + nonGroupingDimensions.length + metricsNames.length);
043: datas = getTableModelData();
044: } catch (InfoException e) {
045: e.printStackTrace();
046: }
047: }
048:
049: private Object[] getNonGroupingDimensions(ReportResult reportResult)
050: throws InfoException {
051: ReportQuery query = reportResult.getReportQuery();
052: return getCubeNonGroupingDimensions(query, reportResult);
053: }
054:
055: private Object[] getCubeNonGroupingDimensions(ReportQuery query,
056: ReportResult reportResult) throws InfoException {
057: CubeQuery cubeQuery = query.getCubeQuery();
058: int[] dimensionIndexes = cubeQuery.getColumns();
059: return getQueryDimensionsArray(dimensionIndexes, reportResult);
060: }
061:
062: private Object[] getGroupingDimensions(ReportResult reportResult)
063: throws InfoException {
064: ReportQuery query = reportResult.getReportQuery();
065: return getCubeGroupingDimensions(query, reportResult);
066: }
067:
068: private Object[] getCubeGroupingDimensions(ReportQuery query,
069: ReportResult reportResult) throws InfoException {
070: CubeQuery cubeQuery = query.getCubeQuery();
071: int[] dimensionIndexes = cubeQuery.getRows();
072: return getQueryDimensionsArray(dimensionIndexes, reportResult);
073: }
074:
075: private Object[] getQueryDimensionsArray(int[] indexes,
076: ReportResult reportResult) throws InfoException {
077: ReportQuery query = reportResult.getReportQuery();
078: Object[] result = new Object[indexes.length];
079: for (int i = 0; i < indexes.length; i++) {
080: String name = reportResult.getReportSpec()
081: .getDimensionFromIndex(indexes[i]).getName();
082: QueryDimension dimension = query
083: .getQueryDimensionFromName(name);
084: result[i] = dimension;
085: }
086: return result;
087: }
088:
089: private Vector getTableModelData() {
090: rowsModel = ((CubeReportResult) reportResult).getRowsModel();
091: columnsModel = ((CubeReportResult) reportResult)
092: .getColumnsModel();
093: metricsCount = reportResult.getReportSpec().getMetrics().length;
094: Vector dataVector = ((CubeReportResult) reportResult)
095: .getDataVector();
096: Vector main = new Vector();
097: fillGroupingVectorFrom(new Vector(), main, rowsModel
098: .getDimensionValueNode().getSubNodesList(), dataVector,
099: 0);
100: return main;
101: }
102:
103: private int fillGroupingVectorFrom(Vector first, Vector main,
104: Collection subnodes, Vector dataVector, int row) {
105: if (subnodes.size() > 0) {
106: Iterator iterator = subnodes.iterator();
107: while (iterator.hasNext()) {
108: DimensionValueNode node = (DimensionValueNode) iterator
109: .next();
110: first.add(node.getValue().toString());
111: if (node.getSubNodesList().size() == 0) {
112: fillNonGroupingVectorFrom(main, first,
113: new Vector(), columnsModel
114: .getDimensionValueNode()
115: .getSubNodesList(), dataVector,
116: row, 0);
117: row++;
118: } else {
119: row = fillGroupingVectorFrom(first, main, node
120: .getSubNodesList(), dataVector, row);
121: }
122: first.removeElementAt(first.size() - 1);
123: }
124: return row;
125: } else {
126: fillNonGroupingVectorFrom(main, first, new Vector(),
127: columnsModel.getDimensionValueNode()
128: .getSubNodesList(), dataVector, row, 0);
129: return 0;
130: }
131: }
132:
133: private int fillNonGroupingVectorFrom(Vector main, Vector first,
134: Vector second, Collection subNodes, Vector dataVector,
135: int row, int col) {
136: if (subNodes.size() > 0) {
137: Iterator iterator = subNodes.iterator();
138: while (iterator.hasNext()) {
139: DimensionValueNode node = (DimensionValueNode) iterator
140: .next();
141: second.add(node.getValue().toString());
142: if (node.getSubNodesList().size() == 0) {
143: col = getItemVectorFrom(main, first, second,
144: dataVector, row, col, false);
145: } else {
146: col = fillNonGroupingVectorFrom(main, first,
147: second, node.getSubNodesList(), dataVector,
148: row, col);
149: }
150: second.removeElementAt(second.size() - 1);
151: }
152: return col;
153: } else {
154: getItemVectorFrom(main, first, second, dataVector, row,
155: col, true);
156: return 0;
157: }
158: }
159:
160: private int getItemVectorFrom(Vector main, Vector first,
161: Vector second, Vector dataVector, int row, int col,
162: boolean isColModelEmpty) {
163: Vector itemVector = new Vector();
164:
165: for (int i = 0; i < first.size(); i++) {
166: itemVector.add(first.elementAt(i));
167: }
168:
169: for (int i = 0; i < second.size(); i++) {
170: itemVector.add(second.elementAt(i));
171: }
172:
173: int analizedCols = getDatasFrom(dataVector, row, col,
174: itemVector, isColModelEmpty);
175:
176: main.add(itemVector);
177: return analizedCols;
178: }
179:
180: private int getDatasFrom(Vector dataVector, int row, int col,
181: Vector itemVector, boolean isColModelEmpty) {
182: Vector vector = (Vector) dataVector.elementAt(row);
183: for (int i = 0; i < reportQuery.getVisibleMetrics().size(); i++) {
184: SharedFloat sharedFloat = (SharedFloat) vector
185: .elementAt(col);
186: itemVector.add(sharedFloat);
187: col++;
188: }
189: return col;
190: }
191:
192: public CategoryDataset getCategoryDataset(boolean isByRow) {
193: DefaultCategoryDataset dataset = new DefaultCategoryDataset();
194: int rowsDimensionsCount = reportQuery.getRowDimensions().size();
195: int columnsDimensionsCount = reportQuery.getColumnDimensions()
196: .size();
197: int metricsCount = reportQuery.getVisibleMetrics().size();
198: for (int j = 0; j < metricsCount; j++) {
199: for (int i = 0; i < datas.size(); i++) {
200: Vector element = (Vector) datas.elementAt(i);
201: double v;
202: if ((element.elementAt(element.size() - metricsCount
203: + j)) == null) {
204: v = 0;
205: } else {
206: v = Double.valueOf(
207: element.elementAt(
208: element.size() - metricsCount + j)
209: .toString()).doubleValue();
210: }
211: if (isByRow) {
212: //for(int j = element.size() - metricsCount ; j < element.size() ; j++){
213: dataset
214: .addValue(
215: v,
216: getDescription(0,
217: rowsDimensionsCount,
218: element, null),
219: getDescription(
220: rowsDimensionsCount,
221: rowsDimensionsCount
222: + columnsDimensionsCount,
223: element,
224: (QueryMetric) reportQuery
225: .getVisibleMetrics()
226: .get(j)));
227: //}
228: } else {
229: //for(int j = element.size() - metricsCount ; j < element.size() ; j++){
230: dataset.addValue(v, getDescription(
231: rowsDimensionsCount, columnsDimensionsCount
232: + rowsDimensionsCount, element,
233: (QueryMetric) reportQuery
234: .getVisibleMetrics().get(j)),
235: getDescription(0, rowsDimensionsCount,
236: element, null));
237:
238: //}
239: }
240: }
241: }
242: return dataset;
243: }
244:
245: private String getDescription(int from, int to, Vector element,
246: QueryMetric desc) {
247: String description = "";
248: for (int i = from; i < to; i++) {
249: description = description + " " + element.elementAt(i);
250: }
251: if (desc != null) {
252: return description + " " + desc.getName();
253: }
254: return description;
255: }
256: }
|