KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > connection > mdx > MDXResultSet


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Sep 12, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.data.connection.mdx;
18
19 import mondrian.olap.Axis;
20 import mondrian.olap.Connection;
21 import mondrian.olap.Result;
22
23 import org.pentaho.core.connection.IPentahoMetaData;
24 import org.pentaho.core.connection.IPentahoResultSet;
25 import org.pentaho.core.connection.memory.MemoryMetaData;
26 import org.pentaho.core.connection.memory.MemoryResultSet;
27
28 /**
29  * @author wseyler
30  *
31  * TODO To change the template for this generated type comment go to Window -
32  * Preferences - Java - Code Style - Code Templates
33  */

34 public class MDXResultSet implements IPentahoResultSet {
35     private static final int columnAxis = 0;
36
37     private static final int rowAxis = 1;
38
39     int columnCount = 0;
40
41     int rowCount = 0;
42
43     Result nativeResultSet;
44
45     Connection nativeConnection;
46
47     int rowIndex = 0;
48
49     MDXMetaData mdxMetaData = null;
50
51     /**
52      * @param result
53      */

54     public MDXResultSet(Result nativeResultSet, Connection nativeConnection) {
55         super();
56         
57         this.nativeResultSet = nativeResultSet;
58         this.nativeConnection = nativeConnection;
59
60         Axis[] axis = nativeResultSet.getAxes();
61         if (axis == null || axis.length == 0) {
62             columnCount = 0;
63             rowCount = 0;
64         } else if (axis.length == 1) {
65             columnCount = axis[columnAxis].positions.length;
66             rowCount = 0;
67         } else {
68             columnCount = axis[columnAxis].positions.length;
69             rowCount = axis[rowAxis].positions.length;
70         }
71
72         mdxMetaData = new MDXMetaData(this.nativeResultSet);
73     }
74
75     /*
76      * (non-Javadoc)
77      *
78      * @see org.pentaho.connection.IPentahoResultSet#getMetaData()
79      */

80     public IPentahoMetaData getMetaData() {
81         return mdxMetaData;
82     }
83
84     /*
85      * (non-Javadoc)
86      *
87      * @see org.pentaho.connection.IPentahoResultSet#next()
88      */

89     public Object JavaDoc[] next() {
90         Object JavaDoc[] result = null;
91
92         if (rowIndex < rowCount) {
93             result = new Object JavaDoc[columnCount];
94             for (int i = 0; i < columnCount; i++) {
95                 result[i] = getValueAt(rowIndex, i);
96             }
97             rowIndex++;
98         }
99         return result;
100     }
101
102     /*
103      * (non-Javadoc)
104      *
105      * @see org.pentaho.connection.IPentahoResultSet#close()
106      */

107     public void close() {
108         nativeResultSet.close();
109     }
110
111     /*
112      * (non-Javadoc)
113      *
114      * @see org.pentaho.connection.IPentahoResultSet#closeConnection()
115      */

116     public void closeConnection() {
117         nativeResultSet.close();
118         nativeConnection.close();
119     }
120
121     /*
122      * (non-Javadoc)
123      *
124      * @see org.pentaho.core.runtime.IDisposable#dispose()
125      */

126     public void dispose() {
127         closeConnection();
128     }
129
130     /*
131      * (non-Javadoc)
132      *
133      * @see org.pentaho.connection.IPentahoResultSet#isScrollable()
134      */

135     public boolean isScrollable() {
136         // TODO Auto-generated method stub
137
return false;
138     }
139
140     /*
141      * (non-Javadoc)
142      *
143      * @see org.pentaho.connection.IPentahoResultSet#getValueAt(int, int)
144      */

145     public Object JavaDoc getValueAt(int row, int column) {
146         int[] key = new int[2];
147         key[0] = column;
148         key[1] = row;
149         return nativeResultSet.getCell(key).getValue();
150     }
151
152     /*
153      * (non-Javadoc)
154      *
155      * @see org.pentaho.connection.IPentahoResultSet#getRowCount()
156      */

157     public int getRowCount() {
158         return mdxMetaData.getRowHeaders().length;
159     }
160
161     /*
162      * (non-Javadoc)
163      *
164      * @see org.pentaho.connection.IPentahoResultSet#getColumnCount()
165      */

166     public int getColumnCount() {
167         return mdxMetaData.getColumnCount();
168     }
169
170     public IPentahoResultSet memoryCopy() {
171         try {
172             IPentahoMetaData metadata = getMetaData();
173             Object JavaDoc columnHeaders[][] = metadata.getColumnHeaders();
174             Object JavaDoc rowHeaders[][] = metadata.getRowHeaders();
175
176             MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, rowHeaders);
177             MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
178
179             Object JavaDoc[] rowObjects = next();
180             while (rowObjects != null) {
181                 cachedResultSet.addRow(rowObjects);
182                 rowObjects = next();
183             }
184             return cachedResultSet;
185         } finally {
186             close();
187         }
188     }
189
190     public void beforeFirst() {
191         rowIndex = 0;
192     }
193
194     public Object JavaDoc[] getDataColumn(int column) {
195         int oldIndex = rowIndex; // save our current iteration location
196

197         beforeFirst();
198         Object JavaDoc[] result = new Object JavaDoc[getRowCount()];
199         int index = 0;
200         Object JavaDoc[] rowData = next();
201         while (rowData != null) {
202             result[index] = rowData[column];
203             index++;
204             rowData = next();
205         }
206
207         rowIndex = oldIndex; // restore the old iteration location
208

209         return result;
210     }
211
212     public Object JavaDoc[] getDataRow(int row) {
213         int oldIndex = rowIndex; // save our current iteration location
214

215         rowIndex = row;
216         Object JavaDoc[] rowData = next();
217         rowIndex = oldIndex;
218
219         return rowData;
220     }
221
222 }
223
Popular Tags