KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > olap > xmla > JRXmlaResult


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2006 JasperSoft Corporation http://www.jaspersoft.com
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * JasperSoft Corporation
24  * 303 Second Street, Suite 450 North
25  * San Francisco, CA 94107
26  * http://www.jaspersoft.com
27  */

28 package net.sf.jasperreports.olap.xmla;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Iterator JavaDoc;
32 import java.util.List JavaDoc;
33
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36
37 import net.sf.jasperreports.engine.JRRuntimeException;
38 import net.sf.jasperreports.olap.result.JROlapCell;
39 import net.sf.jasperreports.olap.result.JROlapResult;
40 import net.sf.jasperreports.olap.result.JROlapResultAxis;
41
42
43 /**
44  * @author Lucian Chirita (lucianc@users.sourceforge.net)
45  * @version $Id: JRXmlaResult.java 1446 2006-10-25 09:00:05Z lucianc $
46  */

47 public class JRXmlaResult implements JROlapResult
48 {
49
50     private final static Log log = LogFactory.getLog(JRXmlaResult.class);
51     
52     private List JavaDoc axesList = new ArrayList JavaDoc();
53     private JRXmlaResultAxis[] axes;
54     private int[] cellOrdinalFactors;
55     private final List JavaDoc cells = new ArrayList JavaDoc();
56
57     public JROlapResultAxis[] getAxes()
58     {
59         return ensureAxisArray();
60     }
61
62     public JROlapCell getCell(int[] axisPositions)
63     {
64         int cellOrdinal = getCellOrdinal(axisPositions);
65         return (JROlapCell) cells.get(cellOrdinal);
66     }
67     
68     protected int getCellOrdinal(int[] axisPositions)
69     {
70         ensureCellOrdinalFactors();
71
72         if (axisPositions.length != axes.length)
73         {
74             throw new JRRuntimeException("Number of axis positions (" + axisPositions.length
75                     + ") doesn't match the number of axes (" + axes.length + ")");
76         }
77         
78         int ordinal = 0;
79         for (int i = 0; i < axisPositions.length; ++i)
80         {
81             ordinal += cellOrdinalFactors[i] * axisPositions[i];
82         }
83         return ordinal;
84     }
85
86     public void addAxis(JRXmlaResultAxis axis)
87     {
88         axesList.add(axis);
89         resetAxisArray();
90     }
91
92     public JRXmlaResultAxis getAxisByName(String JavaDoc name)
93     {
94         JRXmlaResultAxis axis = null;
95         for (Iterator JavaDoc iter = axesList.iterator(); axis == null && iter.hasNext();)
96         {
97             JRXmlaResultAxis ax = (JRXmlaResultAxis) iter.next();
98             if (ax.getAxisName().equals(name))
99             {
100                 axis = ax;
101             }
102         }
103         return axis;
104     }
105
106     protected JRXmlaResultAxis[] ensureAxisArray()
107     {
108         if (axes == null)
109         {
110             axes = new JRXmlaResultAxis[axesList.size()];
111             axes = (JRXmlaResultAxis[]) axesList.toArray(axes);
112         }
113         return axes;
114     }
115
116     protected void ensureCellOrdinalFactors()
117     {
118         ensureAxisArray();
119         
120         if (cellOrdinalFactors == null)
121         {
122             int axisCount = axes.length;
123             cellOrdinalFactors = new int[axisCount];
124             
125             cellOrdinalFactors[0] = 1;
126             for (int i = 1; i < axisCount; ++i)
127             {
128                 cellOrdinalFactors[i] = cellOrdinalFactors[i - 1] * axes[i - 1].getTupleCount();
129             }
130         }
131     }
132
133     protected void resetAxisArray()
134     {
135         axes = null;
136         cellOrdinalFactors = null;
137     }
138     
139     public void setCell(JRXmlaCell cell, int cellOrdinal)
140     {
141         int cellsCount = cells.size();
142         if (cellOrdinal == -1 || cellOrdinal == cellsCount)
143         {
144             cells.add(cell);
145         }
146         else if (cellOrdinal > cellsCount)
147         {
148             for (int i = cellsCount; i < cellOrdinal; ++i)
149             {
150                 cells.add(null);
151             }
152             cells.add(cell);
153         }
154         else
155         {
156             if (log.isWarnEnabled())
157             {
158                 log.warn("Overwriting cell at ordinal " + cellOrdinal);
159             }
160             
161             cells.set(cellOrdinal, cell);
162         }
163     }
164 }
165
Popular Tags