001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportmanager.ReportData;
004: import com.calipso.reportgenerator.common.ReportQuery;
005: import com.calipso.reportgenerator.common.QueryDimension;
006: import com.calipso.reportgenerator.common.DimensionValueNode;
007: import java.util.ArrayList;
008: import java.util.Iterator;
009: import java.util.List;
010: import java.util.Map;
011:
012: /**
013: * Esta clase se encarga de generar la estructura de nodos para poder dibujar y cargar los datos en las tablas
014: */
015:
016: public class DimensionValueTreeBuilder {
017: private ReportData reportData;
018:
019: /**
020: * Crea un objeto DimensionValueTreeBuilder a partir fde un ReportResult
021: * @param reportData
022: */
023: public DimensionValueTreeBuilder(ReportData reportData) {
024: setReportData(reportData);
025: initCubeProccess();
026: }
027:
028: /**
029: *
030: */
031: private void initCubeProccess() {
032: List rows = getReportData().getQuery().getRowDimensions();
033: List cols = getReportData().getQuery().getColumnDimensions();
034: ArrayList prevDimensions;
035: ArrayList values;
036: prevDimensions = new ArrayList();
037: values = new ArrayList();
038: rowsRoot = new DimensionValueNode("ROWS");
039: rowsRoot.setDimensionCount(rows.size());
040: if (rows.size() > 0) {
041: fillSubNodes(rowsRoot, rows, prevDimensions, values, -1);
042: }
043: columnsRoot = new DimensionValueNode("COLUMNS");
044: columnsRoot.setDimensionCount(cols.size());
045: prevDimensions = new ArrayList();
046: values = new ArrayList();
047: if (cols.size() > 0) {
048: fillSubNodes(columnsRoot, cols, prevDimensions, values,
049: rows.size() - 1);
050: }
051: }
052:
053: private DimensionValueNode rowsRoot;
054: private DimensionValueNode columnsRoot;
055:
056: public DimensionValueNode getRowsRoot() {
057: return rowsRoot;
058: }
059:
060: public DimensionValueNode getColumnsRoot() {
061: return columnsRoot;
062: }
063:
064: private void fillSubNodes(DimensionValueNode parentNode,
065: List dimensions, ArrayList prevDimensions,
066: ArrayList prevDimensionValues, int index) {
067: index++;
068: int currentDimension = index /*((QueryDimension)dimensions.get(prevDimensions.size())).getIndex()*/;
069: Iterator iterator = getReportData().valuesFor(currentDimension,
070: arrayListToIntArray(prevDimensions),
071: prevDimensionValues.toArray());
072: boolean lastDimension = prevDimensions.size() == dimensions
073: .size() - 1;
074: while (iterator.hasNext()) {
075: Map.Entry current = (Map.Entry) iterator.next();
076: Object currentValue = current.getKey();
077: DimensionValueNode currentNode = new DimensionValueNode(
078: currentValue, parentNode);
079: currentNode.setDimensionIndex(prevDimensions.size());
080: currentNode.setDimensionCount(parentNode
081: .getDimensionCount() - 1);
082: parentNode.addSubNode(currentNode);
083: prevDimensions.add(new Integer(currentDimension));
084: // prevDimensionValues.add(this.getReportData().getDefinition().getDimensionFromIndex(currentDimension).getVisualValue(currentValue));
085: prevDimensionValues.add(currentValue);
086: if (!lastDimension) {
087: fillSubNodes(currentNode, dimensions, prevDimensions,
088: prevDimensionValues, index);
089: }
090: prevDimensions.remove(prevDimensions.size() - 1);
091: prevDimensionValues.remove(prevDimensionValues.size() - 1);
092: }
093: }
094:
095: private int[] arrayListToIntArray(ArrayList list) {
096: int[] result = new int[list.size()];
097: for (int i = 0; i < list.size(); i++) {
098: result[i] = ((Integer) list.get(i)).intValue();
099: }
100: return result;
101: }
102:
103: public ReportData getReportData() {
104: return reportData;
105: }
106:
107: public ReportQuery getQuery() {
108: return getReportData().getQuery();
109: }
110:
111: public void setReportData(ReportData reportData) {
112: this.reportData = reportData;
113: }
114: }
|