KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > data > connection > xquery > XQResultSet


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 15, 2005
14  * @author wseyler
15  */

16 package org.pentaho.data.connection.xquery;
17
18 import java.math.BigDecimal JavaDoc;
19 import java.sql.Date JavaDoc;
20 import java.sql.Timestamp JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.Map JavaDoc;
23 import net.sf.saxon.om.Axis;
24 import net.sf.saxon.om.AxisIterator;
25 import net.sf.saxon.query.DynamicQueryContext;
26 import net.sf.saxon.query.XQueryExpression;
27 import net.sf.saxon.tinytree.TinyNodeImpl;
28 import net.sf.saxon.trans.XPathException;
29 import net.sf.saxon.type.Type;
30 import org.apache.commons.collections.OrderedMap;
31 import org.apache.commons.collections.map.ListOrderedMap;
32 import org.pentaho.core.connection.IPentahoMetaData;
33 import org.pentaho.core.connection.IPentahoResultSet;
34 import org.pentaho.core.connection.memory.MemoryMetaData;
35 import org.pentaho.core.connection.memory.MemoryResultSet;
36
37 /**
38  * @author wseyler
39  *
40  * TODO To change the template for this generated type comment go to Window -
41  * Preferences - Java - Code Style - Code Templates
42  */

43 public class XQResultSet implements IPentahoResultSet {
44     protected XQueryExpression exp = null;
45
46     protected DynamicQueryContext dynamicContext = null;
47
48     protected XQMetaData metaData = null;
49
50     protected static final String JavaDoc DELIM = ", "; //$NON-NLS-1$
51

52     protected static final String JavaDoc EMPTY_STR = ""; //$NON-NLS-1$
53

54     Iterator JavaDoc iter = null;
55
56     protected String JavaDoc columnTypes[] = null;
57
58     /**
59      * @param exp
60      * @param dynamicContext
61      * @param columnTypes
62      * @throws XPathException
63      */

64     public XQResultSet(XQueryExpression exp, DynamicQueryContext dynamicContext, String JavaDoc columnTypes[]) throws XPathException {
65         super();
66         this.columnTypes = columnTypes;
67         this.exp = exp;
68         this.dynamicContext = dynamicContext;
69         init();
70     }
71
72     protected void init() throws XPathException {
73         iter = this.exp.evaluate(this.dynamicContext).iterator();
74         metaData = new XQMetaData(iter);
75         // reset the iterator for the data
76
iter = this.exp.evaluate(this.dynamicContext).iterator();
77     }
78
79     /*
80      * (non-Javadoc)
81      *
82      * @see org.pentaho.connection.IPentahoResultSet#getMetaData()
83      */

84     public IPentahoMetaData getMetaData() {
85         return metaData;
86     }
87
88     /*
89      * (non-Javadoc)
90      *
91      * @see org.pentaho.connection.IPentahoResultSet#next()
92      */

93     public Object JavaDoc[] next() {
94         // Create a map of the headers and assign empty string to them
95
OrderedMap resultList = new ListOrderedMap();
96         for (int i = 0; i < metaData.getColumnCount(); i++) {
97             resultList.put(metaData.getColumnHeaders()[0][i], EMPTY_STR);
98         }
99         // Get the next row of data
100
if (iter.hasNext()) {
101             Object JavaDoc o = iter.next();
102             decodeNode(o, resultList);
103         }
104         // get the values
105
Object JavaDoc[] retArray = new Object JavaDoc[resultList.size()];
106         Iterator JavaDoc keyIter = resultList.keySet().iterator();
107         int i = 0;
108         while (keyIter.hasNext()) {
109             retArray[i] = resultList.get(keyIter.next());
110             i++;
111         }
112         // if all the values are the empty string then we're done.
113
boolean done = true;
114         for (int j = 0; j < retArray.length; j++) {
115             if (!("".equals(retArray[j]))) { //$NON-NLS-1$
116
done = false;
117             }
118         }
119         if (done) {
120             return null;
121         }
122         return retArray;
123     }
124
125     protected void decodeNode(Object JavaDoc obj, Map JavaDoc retValue) {
126         if (obj instanceof TinyNodeImpl) {
127             AxisIterator aIter = ((TinyNodeImpl) obj).iterateAxis(Axis.DESCENDANT);
128             Object JavaDoc descendent = aIter.next();
129             boolean processedChildren = false;
130             int columnIndex = 0;
131             while (descendent != null) {
132                 if (descendent instanceof TinyNodeImpl && ((TinyNodeImpl) descendent).getNodeKind() == Type.ELEMENT) {
133                     TinyNodeImpl descNode = (TinyNodeImpl) descendent;
134                     Object JavaDoc value = retValue.get(descNode.getDisplayName());
135                     if (value == null) {
136                         value = EMPTY_STR;
137                     }
138                     if (!(EMPTY_STR.equals(value))) {
139                         value = value.toString() + DELIM;
140                     }
141                     value = value.toString() + descNode.getStringValue();
142                     if (value != null && !value.equals("") && columnTypes != null && columnIndex >= 0 && columnIndex < columnTypes.length) { //$NON-NLS-1$
143
String JavaDoc columnType = columnTypes[columnIndex].trim();
144                         if (columnType.equals("java.math.BigDecimal")) { //$NON-NLS-1$
145
value = new BigDecimal JavaDoc(value.toString());
146                         } else if (columnType.equals("java.sql.Timestamp")) { //$NON-NLS-1$
147
value = new Timestamp JavaDoc(Long.parseLong(value.toString()));
148                         } else if (columnType.equals("java.sql.Date")) { //$NON-NLS-1$
149
value = new Date JavaDoc(Long.parseLong(value.toString()));
150                         } else if (columnType.equals("java.lang.Integer")) { //$NON-NLS-1$
151
value = new Integer JavaDoc(Integer.parseInt(value.toString()));
152                         } else if (columnType.equals("java.lang.Double")) { //$NON-NLS-1$
153
value = new Double JavaDoc(Double.parseDouble(value.toString()));
154                         } else if (columnType.equals("java.lang.Long")) { //$NON-NLS-1$
155
value = new Long JavaDoc(Long.parseLong(value.toString()));
156                         }
157                     }
158                     retValue.put(descNode.getDisplayName(), value);
159                     processedChildren = true;
160                     columnIndex++;
161                 }
162                 descendent = aIter.next();
163             }
164             if (!processedChildren) {
165                 Object JavaDoc key = ((TinyNodeImpl) obj).getDisplayName();
166                 Object JavaDoc value = ((TinyNodeImpl) obj).getStringValue();
167                 retValue.put(key, value);
168             }
169         } else {
170             retValue.put(XQMetaData.DEFAULT_COLUMN_NAME, obj.toString());
171         }
172     }
173
174     /*
175      * (non-Javadoc)
176      *
177      * @see org.pentaho.connection.IPentahoResultSet#close()
178      */

179     public void close() {
180         // TODO Auto-generated method stub
181
}
182
183     /*
184      * (non-Javadoc)
185      *
186      * @see org.pentaho.connection.IPentahoResultSet#closeConnection()
187      */

188     public void closeConnection() {
189         // TODO Auto-generated method stub
190
}
191
192     /*
193      * (non-Javadoc)
194      *
195      * @see org.pentaho.connection.IPentahoResultSet#isScrollable()
196      */

197     public boolean isScrollable() {
198         // TODO Auto-generated method stub
199
return false;
200     }
201
202     /*
203      * (non-Javadoc)
204      *
205      * @see org.pentaho.connection.IPentahoResultSet#getValueAt(int, int)
206      */

207     public Object JavaDoc getValueAt(int row, int column) {
208         // TODO Auto-generated method stub
209
return null;
210     }
211
212     /*
213      * (non-Javadoc)
214      *
215      * @see org.pentaho.connection.IPentahoResultSet#getRowCount()
216      */

217     public int getRowCount() {
218         return metaData.getRowCount();
219     }
220
221     /*
222      * (non-Javadoc)
223      *
224      * @see org.pentaho.connection.IPentahoResultSet#getColumnCount()
225      */

226     public int getColumnCount() {
227         return metaData.getColumnCount();
228     }
229
230     /*
231      * (non-Javadoc)
232      *
233      * @see org.pentaho.core.runtime.IDisposable#dispose()
234      */

235     public void dispose() {
236         // TODO Auto-generated method stub
237
}
238
239     public IPentahoResultSet memoryCopy() {
240         try {
241             IPentahoMetaData metadata = getMetaData();
242             Object JavaDoc columnHeaders[][] = metadata.getColumnHeaders();
243             MemoryMetaData cachedMetaData = new MemoryMetaData(columnHeaders, null);
244             MemoryResultSet cachedResultSet = new MemoryResultSet(cachedMetaData);
245             Object JavaDoc[] rowObjects = next();
246             while (rowObjects != null) {
247                 cachedResultSet.addRow(rowObjects);
248                 rowObjects = next();
249             }
250             return cachedResultSet;
251         } finally {
252             close();
253         }
254     }
255
256     public void beforeFirst() {
257         try {
258             init();
259         } catch (Throwable JavaDoc t) {
260             t.printStackTrace();
261         }
262     }
263
264     /*
265      * (non-Javadoc)
266      *
267      * @see org.pentaho.connection.IPentahoResultSet#getDataColumn(int)
268      *
269      * NOTE: calling this will move the cursor to the top of the result stack
270      */

271     public Object JavaDoc[] getDataColumn(int column) {
272         beforeFirst(); // go to top just in case we called this after some
273
// next()s
274
Object JavaDoc[] result = new Object JavaDoc[getRowCount()];
275         int rowIndex = 0;
276         Object JavaDoc[] rowData = next();
277         while (rowData != null) {
278             result[rowIndex] = rowData[column];
279             rowIndex++;
280             rowData = next();
281         }
282         beforeFirst();
283         return result;
284     }
285
286     public Object JavaDoc[] getDataRow(int row) {
287         beforeFirst(); // go to top
288
int count = 0;
289         while (count < row) {
290             next();
291         }
292         Object JavaDoc[] dataRow = next();
293         beforeFirst();
294         return dataRow;
295     }
296 }
297
Popular Tags