001: package com.calipso.reportgenerator.common;
002:
003: import com.calipso.reportgenerator.reportcalculator.SharedFloat;
004: import com.calipso.reportgenerator.reportcalculator.CubeQuery;
005: import com.calipso.reportgenerator.reportcalculator.CubeValue;
006:
007: import java.util.Collection;
008: import java.util.Iterator;
009: import java.util.Vector;
010:
011: /**
012: *
013: * User: soliveri
014: * Date: Dec 16, 2003
015: * Time: 3:02:55 PM
016: *
017: */
018:
019: public class CubeReportTableModel extends ReportTableModel {
020:
021: private CubeReportResult reportResult;
022: private HeaderTableModel rowsModel;
023: private HeaderTableModel columnsModel;
024: private int metricsCount;
025:
026: public CubeReportTableModel(CubeReportResult reportResult)
027: throws InfoException {
028: super ();
029: this .reportResult = reportResult;
030: initialize();
031: }
032:
033: private void initialize() throws InfoException {
034: Object[] groupingDimensions = getGroupingDimensions();
035: Object[] nonGroupingDimensions = getNonGroupingDimensions();
036: Object[] totalMetrics = getTotalMetrics();
037: setGroupingDimCount(groupingDimensions.length);
038: setNonGroupingDimCount(nonGroupingDimensions.length);
039: setCommonMetricsCount(reportResult.getReportQuery()
040: .getVisibleMetrics().size());
041: setAccMetricsCount(reportResult.getReportQuery()
042: .getVisibleAccumulableMetrics().size());
043: setTableData(getTableModelData());
044: setTableColumnNames(getTableModelColumns(groupingDimensions,
045: nonGroupingDimensions, totalMetrics));
046: newDefaultTableModel();
047: }
048:
049: /**
050: * Obtiene los valores de las Dimensiones que no agrupan, es decir, las Row Dimensions
051: * a partir de un reporte tipo Cube.
052: * @return array que contiene objetos de tipo QueryDimension
053: * @throws com.calipso.reportgenerator.common.InfoException
054: */
055: private Object[] getNonGroupingDimensions() throws InfoException {
056: CubeQuery cubeQuery = reportResult.getReportQuery()
057: .getCubeQuery();
058: int[] dimensionIndexes = cubeQuery.getColumns();
059: return getQueryDimensionsArray(dimensionIndexes, reportResult);
060: }
061:
062: /**
063: * Obtiene los valores de las Dimensiones que agrupan, es decir, las Row Dimensions
064: * a partir de un reporte tipo Cube.
065: * @return array que contiene objetos de tipo QueryDimension
066: * @throws com.calipso.reportgenerator.common.InfoException
067: */
068: private Object[] getGroupingDimensions() throws InfoException {
069: CubeQuery cubeQuery = reportResult.getReportQuery()
070: .getCubeQuery();
071: int[] dimensionIndexes = cubeQuery.getRows();
072: return getQueryDimensionsArray(dimensionIndexes, reportResult);
073: }
074:
075: /**
076: * Inicializa un array con objetos de tipo QueryDimension.
077: * @param indexes array que contiene los valores de las dimensiones
078: * @param reportResult necesario para obtener los valores de las dimensions
079: * @return array que contiene objetos de tipo QueryDimension
080: * @throws com.calipso.reportgenerator.common.InfoException
081: */
082: private Object[] getQueryDimensionsArray(int[] indexes,
083: ReportResult reportResult) throws InfoException {
084: ReportQuery query = reportResult.getReportQuery();
085: Object[] result = new Object[indexes.length];
086: for (int i = 0; i < indexes.length; i++) {
087: String name = reportResult.getReportSpec()
088: .getDimensionFromIndex(indexes[i]).getName();
089: QueryDimension dimension = query
090: .getQueryDimensionFromName(name);
091: result[i] = dimension;
092: }
093: return result;
094: }
095:
096: private Object[] getTotalMetrics() {
097: Object[] metricsNames = reportResult.getReportQuery()
098: .getVisibleMetrics().toArray();
099: Object[] accuMetricsNames = reportResult.getReportQuery()
100: .getAccumulableMetrics().toArray();
101: Object[] allMetrics = new Object[metricsNames.length
102: + accuMetricsNames.length];
103: int i = 0;
104: for (; i < metricsNames.length; i++) {
105: allMetrics[i] = metricsNames[i];
106: }
107: for (int j = 0; j < accuMetricsNames.length; j++, i++) {
108: QueryMetric queryMetric = (QueryMetric) accuMetricsNames[j];
109: queryMetric.setName(queryMetric.getName() + "_ACCUM");//LanguageTraslator.traslate("314")
110: allMetrics[i] = queryMetric;
111: }
112: return allMetrics;
113: }
114:
115: private Vector getTableModelData() {
116: rowsModel = reportResult.getRowsModel();
117: columnsModel = reportResult.getColumnsModel();
118: //metricsCount = reportResult.getReportSpec().getMetrics().length;
119: metricsCount = reportResult.getReportQuery()
120: .getVisibleMetrics().size();
121: Vector dataVector = reportResult.getDataVector();
122: Vector main = new Vector();
123: if (super .getNonGroupingDimCount() == 0) {
124: fillGroupingVectorFrom2(new Vector(), main, rowsModel
125: .getDimensionValueNode().getSubNodesList(),
126: dataVector, 0);
127: } else {
128: fillGroupingVectorFrom(new Vector(), main, rowsModel
129: .getDimensionValueNode().getSubNodesList(),
130: dataVector, 0);
131: }
132: return main;
133: }
134:
135: private int fillGroupingVectorFrom(Vector first, Vector main,
136: Collection subnodes, Vector dataVector, int row) {
137: if (subnodes.size() > 0) {
138: Iterator iterator = subnodes.iterator();
139: while (iterator.hasNext()) {
140: DimensionValueNode node = (DimensionValueNode) iterator
141: .next();
142: first.add(node.getValue().toString());
143: if (node.getSubNodesList().size() == 0) {
144: while (rowsModel.isTotalRow(row)) {
145: row++;
146: }
147: /*while(rowsModel.getValueFrom(row) [rowsModel.getColumnCount() - 1] == null) {
148: row++;
149: }*/
150: fillNonGroupingVectorFrom(main, first,
151: new Vector(), columnsModel
152: .getDimensionValueNode()
153: .getSubNodesList(), dataVector,
154: row, 0);
155: row++;
156: } else {
157: row = fillGroupingVectorFrom(first, main, node
158: .getSubNodesList(), dataVector, row);
159: }
160: first.removeElementAt(first.size() - 1);
161: }
162: return row;
163: } else {
164: fillNonGroupingVectorFrom(main, first, new Vector(),
165: columnsModel.getDimensionValueNode()
166: .getSubNodesList(), dataVector, row, 0);
167: return 0;
168: }
169: }
170:
171: private int fillNonGroupingVectorFrom(Vector main, Vector first,
172: Vector second, Collection subNodes, Vector dataVector,
173: int row, int col) {
174: boolean leafReached = false;
175: DimensionValueNode node = null;
176: if (subNodes.size() > 0) {
177: Iterator iterator = subNodes.iterator();
178: while (iterator.hasNext() && !leafReached) {
179: node = (DimensionValueNode) iterator.next();
180: if (node.getSubNodesList().size() == 0) {
181: leafReached = true;
182: col = getItemVectorFrom(main, first, second,
183: dataVector, row, col, false, node);
184: } else {
185: second.add(node.getValue().toString());
186: col = fillNonGroupingVectorFrom(main, first,
187: second, node.getSubNodesList(), dataVector,
188: row, col);
189: leafReached = false;
190: second.removeElementAt(second.size() - 1);
191: }
192: }
193: return col;
194: } else {
195: getItemVectorFrom(main, first, second, dataVector, row,
196: col, true, node);
197: return 0;
198: }
199: }
200:
201: private int getItemVectorFrom(Vector main, Vector first,
202: Vector second, Vector dataVector, int row, int col,
203: boolean isColModelEmpty, DimensionValueNode node) {
204: Vector itemVector = new Vector();
205:
206: for (int i = 0; i < first.size(); i++) {
207: itemVector.add(first.elementAt(i));
208: }
209:
210: for (int i = 0; i < second.size(); i++) {
211: itemVector.add(second.elementAt(i));
212: }
213:
214: int analizedCols = getDatasFrom(dataVector, row, col,
215: itemVector, isColModelEmpty, node);
216:
217: main.add(itemVector);
218: return analizedCols;
219: }
220:
221: private int getDatasFrom(Vector dataVector, int row, int col,
222: Vector itemVector, boolean isColModelEmpty,
223: DimensionValueNode node) {
224: int i = 0, p = 0;
225: Vector ocurrences = new Vector();
226: fillOcurrences(ocurrences, columnsModel.getDimensionValueNode()
227: .getSubNodesList());
228:
229: //Determinar si hay algun valor de dimension previo que no se muestra en la tabla
230: for (int x = 0; x < ocurrences.size(); x++) {
231: String currentOcurrence = (String) ocurrences.elementAt(x);
232: if (currentOcurrence.equals(node.getValue().toString())) {
233: break;
234: } else {
235: for (int z = 0; z < metricsCount; z++) {
236: SharedFloat sharedFloat = SharedFloat.newFrom(0);
237: itemVector.add(new Float(sharedFloat.floatValue()));
238: p++;
239: }
240: }
241: }
242:
243: //Agregamos los datos que realmente estan en la tabla
244: Vector vector = (Vector) dataVector.elementAt(row);
245: for (; i < (metricsCount * node.getParentNode()
246: .getSubNodesList().size()); i++) {
247: if (!isColModelEmpty) {
248: while (columnsModel.isTotalCol(col)) {
249: col++;
250: }
251: /*while((columnsModel.getValueFrom(col) [columnsModel.getRowCount() - 1] == null)) {
252: col++;
253: }*/
254: }
255: SharedFloat sharedFloat = (SharedFloat) vector
256: .elementAt(col);
257: if (sharedFloat == null) {
258: itemVector.add(new Float(0));
259: } else {
260: itemVector.add(new Float(sharedFloat.floatValue()));
261: }
262: col++;
263: }
264:
265: //Determinar si hay algun valor de dimension posterior que no se muestra en la tabla
266: while ((i + p) < (ocurrences.size() * metricsCount)) {
267: SharedFloat sharedFloat = SharedFloat.newFrom(0);
268: itemVector.add(new Float(sharedFloat.floatValue()));
269: i++;
270: }
271: //Agrega los totales para una fila
272: if (reportResult.getReportQuery().isVisibleTotals()) {
273: int index = col;
274: for (int j = 0; j < metricsCount; j++) {
275: SharedFloat sharedFloat = (SharedFloat) vector
276: .elementAt(index);
277: itemVector.add(new Float(sharedFloat.floatValue()));
278: index++;
279: }
280: }
281:
282: return col;
283: }
284:
285: protected Vector getTableModelColumns(Object[] groupingDimensions,
286: Object[] nonGroupingDimensions, Object[] metricsArray) {
287: Vector columnNames = new Vector();
288: for (int i = 0; i < groupingDimensions.length; i++) {
289: QueryDimension qd = (QueryDimension) groupingDimensions[i];
290: columnNames.add(qd.getName());
291: }
292:
293: occurrences = new Vector();
294: fillOcurrences(occurrences, reportResult.getColumnsModel()
295: .getDimensionValueNode().getSubNodesList());
296:
297: if (nonGroupingDimensions.length > 0) {
298: for (int i = 0; i < nonGroupingDimensions.length; i++) {
299: QueryDimension qd = (QueryDimension) nonGroupingDimensions[i];
300: if (i + 1 == nonGroupingDimensions.length) {
301: for (int j = 0; j < occurrences.size(); j++) {
302: for (int z = 0; z < metricsArray.length; z++) {
303: QueryMetric qm = (QueryMetric) metricsArray[z];
304: columnNames.add(j + "_" + qm.getName());
305: }
306: }
307: //Nombra las filas de totales
308: if (reportResult.getReportQuery().isVisibleTotals()) {
309: for (int j = 0; j < metricsArray.length; j++) {
310: QueryMetric qm = (QueryMetric) metricsArray[j];
311: columnNames.add("TOTAL_" + qm.getName());
312: }
313: }
314: } else {
315: columnNames.add(qd.getName());
316: }
317: }
318: } else {
319: for (int i = 0; i < metricsArray.length; i++) {
320: QueryMetric qm = (QueryMetric) metricsArray[i];
321: columnNames.add(qm.getName());
322: }
323:
324: // for (int i = 0; i < metricsArray.length; i++) {
325: // QueryMetric qm = (QueryMetric) metricsArray[i];
326: // columnNames.add("TOTAL" + qm.getName());
327: // }
328: }
329: return columnNames;
330: }
331:
332: private String getWellFormedString(String s) {
333: StringBuffer buffer = null;
334: String[] strings = null;
335:
336: if (s.split("-").length > 1) {
337: buffer = new StringBuffer();
338: strings = s.split("-");
339: } else {
340: if (s.split("/").length > 1) {
341: buffer = new StringBuffer();
342: strings = s.split("/");
343: } else {
344: int i = s.indexOf(46);
345: if (i != -1) {
346: buffer = new StringBuffer();
347: for (int j = 0; j < s.length(); j++) {
348: if (j != i) {
349: String tmp = String.valueOf(s.charAt(j));
350: buffer.append(tmp);
351: }
352: }
353: return buffer.toString();
354: }
355: }
356: }
357:
358: if (strings != null) {
359: for (int i = 0; i < strings.length; i++) {
360: buffer.append(strings[i]);
361: }
362: return buffer.toString();
363: } else {
364: return s;
365: }
366: }
367:
368: private void fillOcurrences(Vector occurrences,
369: Collection subNodesList) {
370: Iterator childs = subNodesList.iterator();
371: while (childs.hasNext()) {
372: DimensionValueNode currentNode = (DimensionValueNode) childs
373: .next();
374: if (currentNode.getSubNodesList().size() == 0) {
375: if (!(occurrences.contains(currentNode.getValue()
376: .toString()))) {
377: occurrences.add(currentNode.getValue().toString());
378: }
379: } else {
380: fillOcurrences(occurrences, currentNode
381: .getSubNodesList());
382: }
383: }
384: }
385:
386: private int fillGroupingVectorFrom2(Vector first, Vector main,
387: Collection subnodes, Vector dataVector, int row) {
388: if (subnodes.size() > 0) {
389: Iterator iterator = subnodes.iterator();
390: while (iterator.hasNext()) {
391: DimensionValueNode node = (DimensionValueNode) iterator
392: .next();
393: Object value = node.getValue();
394: if (value instanceof CubeValue) {
395: value = ((CubeValue) value).getValue();
396: }
397: first.add(value);
398: if (node.getSubNodesList().size() == 0) {
399: while (rowsModel.isTotalRow(row)) {
400: row++;
401: }
402: fillNonGroupingVectorFrom2(main, first,
403: new Vector(), columnsModel
404: .getDimensionValueNode()
405: .getSubNodesList(), dataVector,
406: row, 0);
407: row++;
408: } else {
409: row = fillGroupingVectorFrom2(first, main, node
410: .getSubNodesList(), dataVector, row);
411: }
412: first.removeElementAt(first.size() - 1);
413: }
414: return row;
415: } else {
416: fillNonGroupingVectorFrom2(main, first, new Vector(),
417: columnsModel.getDimensionValueNode()
418: .getSubNodesList(), dataVector, row, 0);
419: return 0;
420: }
421: }
422:
423: private int fillNonGroupingVectorFrom2(Vector main, Vector first,
424: Vector second, Collection subNodes, Vector dataVector,
425: int row, int col) {
426: if (subNodes.size() > 0) {
427: Iterator iterator = subNodes.iterator();
428: while (iterator.hasNext()) {
429: DimensionValueNode node = (DimensionValueNode) iterator
430: .next();
431: second.add(node.getValue().toString());
432: if (node.getSubNodesList().size() == 0) {
433: col = getItemVectorFrom2(main, first, second,
434: dataVector, row, col, false);
435: } else {
436: col = fillNonGroupingVectorFrom2(main, first,
437: second, node.getSubNodesList(), dataVector,
438: row, col);
439: }
440: second.removeElementAt(second.size() - 1);
441: }
442: return col;
443: } else {
444: getItemVectorFrom2(main, first, second, dataVector, row,
445: col, true);
446: return 0;
447: }
448: }
449:
450: private int getItemVectorFrom2(Vector main, Vector first,
451: Vector second, Vector dataVector, int row, int col,
452: boolean isColModelEmpty) {
453: Vector itemVector = new Vector();
454:
455: for (int i = 0; i < first.size(); i++) {
456: itemVector.add(first.elementAt(i));
457: }
458:
459: for (int i = 0; i < second.size(); i++) {
460: itemVector.add(second.elementAt(i));
461: }
462:
463: int analizedCols = getDatasFrom2(dataVector, row, col,
464: itemVector, isColModelEmpty);
465:
466: main.add(itemVector);
467: return analizedCols;
468: }
469:
470: private int getDatasFrom2(Vector dataVector, int row, int col,
471: Vector itemVector, boolean isColModelEmpty) {
472: Vector vector = (Vector) dataVector.elementAt(row);
473: for (int i = 0; i < metricsCount; i++) {
474: if (!isColModelEmpty) {
475: while (columnsModel.isTotalCol(col)) {
476: col++;
477: }
478: }
479: SharedFloat sharedFloat = (SharedFloat) vector
480: .elementAt(col);
481: itemVector.add(new Float(sharedFloat.floatValue()));
482: col++;
483: }
484: return col;
485: }
486:
487: }
|