001: /*
002: * ============================================================================
003: * GNU Lesser General Public License
004: * ============================================================================
005: *
006: * JasperReports - Free Java report-generating library.
007: * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
008: *
009: * This library is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU Lesser General Public
011: * License as published by the Free Software Foundation; either
012: * version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: * Lesser General Public License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * JasperSoft Corporation
024: * 303 Second Street, Suite 450 North
025: * San Francisco, CA 94107
026: * http://www.jaspersoft.com
027: */
028: package net.sf.jasperreports.olap.xmla;
029:
030: import java.util.ArrayList;
031: import java.util.Iterator;
032: import java.util.List;
033:
034: import org.apache.commons.logging.Log;
035: import org.apache.commons.logging.LogFactory;
036:
037: import net.sf.jasperreports.engine.JRRuntimeException;
038: import net.sf.jasperreports.olap.result.JROlapCell;
039: import net.sf.jasperreports.olap.result.JROlapResult;
040: import net.sf.jasperreports.olap.result.JROlapResultAxis;
041:
042: /**
043: * @author Lucian Chirita (lucianc@users.sourceforge.net)
044: * @version $Id: JRXmlaResult.java 1446 2006-10-25 09:00:05Z lucianc $
045: */
046: public class JRXmlaResult implements JROlapResult {
047:
048: private final static Log log = LogFactory
049: .getLog(JRXmlaResult.class);
050:
051: private List axesList = new ArrayList();
052: private JRXmlaResultAxis[] axes;
053: private int[] cellOrdinalFactors;
054: private final List cells = new ArrayList();
055:
056: public JROlapResultAxis[] getAxes() {
057: return ensureAxisArray();
058: }
059:
060: public JROlapCell getCell(int[] axisPositions) {
061: int cellOrdinal = getCellOrdinal(axisPositions);
062: return (JROlapCell) cells.get(cellOrdinal);
063: }
064:
065: protected int getCellOrdinal(int[] axisPositions) {
066: ensureCellOrdinalFactors();
067:
068: if (axisPositions.length != axes.length) {
069: throw new JRRuntimeException("Number of axis positions ("
070: + axisPositions.length
071: + ") doesn't match the number of axes ("
072: + axes.length + ")");
073: }
074:
075: int ordinal = 0;
076: for (int i = 0; i < axisPositions.length; ++i) {
077: ordinal += cellOrdinalFactors[i] * axisPositions[i];
078: }
079: return ordinal;
080: }
081:
082: public void addAxis(JRXmlaResultAxis axis) {
083: axesList.add(axis);
084: resetAxisArray();
085: }
086:
087: public JRXmlaResultAxis getAxisByName(String name) {
088: JRXmlaResultAxis axis = null;
089: for (Iterator iter = axesList.iterator(); axis == null
090: && iter.hasNext();) {
091: JRXmlaResultAxis ax = (JRXmlaResultAxis) iter.next();
092: if (ax.getAxisName().equals(name)) {
093: axis = ax;
094: }
095: }
096: return axis;
097: }
098:
099: protected JRXmlaResultAxis[] ensureAxisArray() {
100: if (axes == null) {
101: axes = new JRXmlaResultAxis[axesList.size()];
102: axes = (JRXmlaResultAxis[]) axesList.toArray(axes);
103: }
104: return axes;
105: }
106:
107: protected void ensureCellOrdinalFactors() {
108: ensureAxisArray();
109:
110: if (cellOrdinalFactors == null) {
111: int axisCount = axes.length;
112: cellOrdinalFactors = new int[axisCount];
113:
114: cellOrdinalFactors[0] = 1;
115: for (int i = 1; i < axisCount; ++i) {
116: cellOrdinalFactors[i] = cellOrdinalFactors[i - 1]
117: * axes[i - 1].getTupleCount();
118: }
119: }
120: }
121:
122: protected void resetAxisArray() {
123: axes = null;
124: cellOrdinalFactors = null;
125: }
126:
127: public void setCell(JRXmlaCell cell, int cellOrdinal) {
128: int cellsCount = cells.size();
129: if (cellOrdinal == -1 || cellOrdinal == cellsCount) {
130: cells.add(cell);
131: } else if (cellOrdinal > cellsCount) {
132: for (int i = cellsCount; i < cellOrdinal; ++i) {
133: cells.add(null);
134: }
135: cells.add(cell);
136: } else {
137: if (log.isWarnEnabled()) {
138: log.warn("Overwriting cell at ordinal " + cellOrdinal);
139: }
140:
141: cells.set(cellOrdinal, cell);
142: }
143: }
144: }
|