001: package com.calipso.reportgenerator.reportmanager;
002:
003: import com.calipso.reportgenerator.reportmanager.ReportData;
004: import com.calipso.reportgenerator.common.QueryDimension;
005: import com.calipso.reportgenerator.common.ReportQuery;
006: import com.calipso.reportgenerator.common.DimensionValueNode;
007: import com.calipso.reportgenerator.common.HeaderTableModel;
008: import com.calipso.reportgenerator.reportcalculator.SharedFloat;
009:
010: import javax.swing.table.TableModel;
011: import javax.swing.table.DefaultTableModel;
012: import java.util.Vector;
013: import java.util.Collection;
014: import java.util.Iterator;
015: import java.io.Serializable;
016:
017: public class DataVectorBuilder implements Serializable {
018:
019: private DimensionValueNode rowsRoot;
020: private DimensionValueNode colsRoot;
021: private ReportData reportData;
022: private Vector dataVector;
023: private boolean withTotals;
024: private int rowCount;
025: private int colCount;
026: private int[] metrics;
027: private int[] groupingDimensions;
028: private Float tempFloat = new Float(0);
029:
030: public DataVectorBuilder(DimensionValueNode rowsRoot,
031: DimensionValueNode colsRoot, ReportQuery reportQuery,
032: ReportData reportData, boolean withTotals) {
033: this .rowsRoot = rowsRoot;
034: this .colsRoot = colsRoot;
035: this .reportData = reportData;
036: this .withTotals = withTotals;
037: this .metrics = reportQuery.getCubeVisibleMetricFields();
038: getDataVector();
039: }
040:
041: public Vector getDataVector() {
042: if (dataVector == null) {
043: dataVector = new Vector();
044: fill();
045: }
046: return dataVector;
047: }
048:
049: private void fill() {
050: DimensionNodeIterator rowsIterator = new DimensionNodeIterator(
051: rowsRoot, withTotals);
052: if (rowsRoot.getDimensionCount() == 0) {
053: Object[] rowHeaders = new Object[0];
054: DimensionNodeIterator colsIterator = new DimensionNodeIterator(
055: colsRoot, withTotals);
056: Vector dataValues = new Vector();
057: if (colsRoot.getDimensionCount() == 0) {
058: fillValue(dataValues, rowHeaders, new Object[0]);
059: } else {
060: while (colsIterator.hasNext()) {
061: Object[] colHeaders = (Object[]) colsIterator
062: .next();
063: fillValue(dataValues, rowHeaders, colHeaders);
064: }
065: }
066: dataVector.add(dataValues);
067: rowCount++;
068: } else {
069: while (rowsIterator.hasNext()) {
070: Object[] rowHeaders = (Object[]) rowsIterator.next();
071: DimensionNodeIterator colsIterator = new DimensionNodeIterator(
072: colsRoot, withTotals);
073: Vector dataValues = new Vector();
074: if (colsRoot.getDimensionCount() == 0) {
075: fillValue(dataValues, rowHeaders, new Object[0]);
076: } else {
077: while (colsIterator.hasNext()) {
078: Object[] colHeaders = (Object[]) colsIterator
079: .next();
080: fillValue(dataValues, rowHeaders, colHeaders);
081: }
082: }
083: dataVector.add(dataValues);
084: rowCount++;
085: }
086: }
087: }
088:
089: private boolean isTotal(Object[] rowHeaders, Object[] colHeaders) {
090: return (rowHeaders.length > 0 && rowHeaders[rowHeaders.length - 1] == null)
091: || (colHeaders.length > 0 && colHeaders[colHeaders.length - 1] == null);
092: }
093:
094: private int[] getMetrics() {
095: return metrics;
096: }
097:
098: private void fillValue(Vector dataValues, Object[] rowHeaders,
099: Object[] colHeaders) {
100: Object[] values = new Object[rowHeaders.length
101: + colHeaders.length];
102: System.arraycopy(rowHeaders, 0, values, 0, rowHeaders.length);
103: System.arraycopy(colHeaders, 0, values, rowHeaders.length,
104: colHeaders.length);
105: Object[] metricValues;
106: if (isTotal(rowHeaders, colHeaders)) {
107: //llenar con dimensiones y valores que no son null
108: Object[] params = getNotNullParams(values);
109: int[] dimensions = (int[]) params[0];
110: Object[] dimValues = (Object[]) params[1];
111: //metricValues = reportData.measuresAtDimensionsValues(dimensions, dimValues);
112: metricValues = reportData.getMetricValues(dimensions,
113: dimValues);
114: } else {
115: //metricValues = reportData.measuresAtDimensionsValues(getGroupingDimensionsIndexArray(), values);
116: metricValues = reportData.getMetricValues(
117: getGroupingDimensionsIndexArray(), values);
118: }
119: if (metricValues == null) {
120: for (int i = 0; i < getMetrics().length; i++) {
121: Object value = null;//SharedFloat.newFrom(tempFloat);
122: dataValues.add(value);
123: if (rowCount == 0)
124: colCount++;
125: }
126: } else {
127: for (int i = 0; i < getMetrics().length; i++) {
128: int metricIndex = getMetrics()[i]
129: - this .reportData.getQuery().getReportSpec()
130: .getDimensions().length
131: + this .getGroupingDimensionsIndexArray().length;
132: Object value = metricValues[metricIndex];
133: dataValues.add(value);
134: if (rowCount == 0)
135: colCount++;
136: }
137: }
138: }
139:
140: private int[] getGroupingDimensionsIndexArray() {
141: if (groupingDimensions == null) {
142: //groupingDimensions = reportData.getQuery().getGroupingDimensionsIndexArray();
143: groupingDimensions = ((CubeReportData) reportData)
144: .getCube()
145: .getIntegerArray(
146: 0,
147: reportData.getQuery()
148: .getGroupingDimensionsIndexArray().length);
149: }
150: return groupingDimensions;
151: }
152:
153: private Object[] getNotNullParams(Object[] values) {
154: Object[] result = new Object[2];
155: Vector notNullDims = new Vector();
156: Vector notNullValues = new Vector();
157: for (int i = 0; i < values.length; i++) {
158: Object value = values[i];
159: if (value != null) {
160: notNullValues.add(value);
161: notNullDims.add(dimensionFromValuesIndex(i));
162: }
163: }
164: result[0] = toIntArray(notNullDims);
165: result[1] = notNullValues.toArray();
166: return result;
167: }
168:
169: private int[] toIntArray(Collection selectedDimensions) {
170: int[] result;
171: int index;
172: Iterator iterator;
173: int selectedDimensionsSize = selectedDimensions.size();
174: result = new int[selectedDimensionsSize];
175: iterator = selectedDimensions.iterator();
176: for (index = 0; index < selectedDimensionsSize; index++) {
177: result[index] = ((Integer) iterator.next()).intValue();
178: }
179: return result;
180: }
181:
182: private Integer dimensionFromValuesIndex(int index) {
183: return new Integer(index); /*int rowsCount = rowsRoot.getDimensionCount();
184: if (rowsCount == 0) {
185: //return new Integer(cube.getQuery().getColumns()[index]);
186: return new Integer(((QueryDimension)reportData.getQuery().getColumnDimensions().get(index)).getIndex());
187: }
188: else {
189: if (index >= rowsCount) {
190: //return new Integer(cube.getQuery().getColumns()[index - Math.max(rowsCount, 1)]);
191: return new Integer(((QueryDimension)reportData.getQuery().getColumnDimensions().get(index - Math.max(rowsCount, 1))).getIndex());
192: }
193: else {
194: //return new Integer(cube.getQuery().getRows()[index]);
195: return new Integer(((QueryDimension)reportData.getQuery().getRowDimensions().get(index)).getIndex());
196: }
197: }*/
198: }
199:
200: public void invalidate() {
201: dataVector = null;
202: colCount = 0;
203: rowCount = 0;
204: }
205:
206: public Vector getNames() {
207: Vector names = new Vector(getColCount());
208: for (int i = 0; i < getColCount(); i++) {
209: names.add(String.valueOf(i));
210: }
211: return names;
212: }
213:
214: public int getRowCount() {
215: return Math.max(rowCount, 1);
216: }
217:
218: public int getColCount() {
219: return Math.max(colCount, 1);
220: }
221:
222: public TableModel getDataModel() {
223: return new DefaultTableModel(getDataVector(), getNames());
224: }
225:
226: }
|