KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > ejb > EJBQueryIterator


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.ejb;
13
14 import com.versant.core.server.CompiledQuery;
15 import com.versant.core.server.QueryResultWrapper;
16 import com.versant.core.common.QueryResultContainer;
17 import com.versant.core.common.BindingSupportImpl;
18 import com.versant.core.jdo.JDOListIterator;
19 import com.versant.core.jdo.QueryResultBase;
20
21 import java.util.NoSuchElementException JavaDoc;
22
23 /**
24  * A ListIterator implemenation that supports forward iteration over the results.
25  */

26 public class EJBQueryIterator implements JDOListIterator {
27     private EMProxy pm;
28
29     private Object JavaDoc[] data;
30     private int actualSize;
31     private int indexInData;
32     private QueryResultWrapper qrw;
33     private boolean noMoreDataInResultSet;
34     private boolean closed;
35     private int nextIndex;
36
37     public EJBQueryIterator(EMProxy pm, CompiledQuery compiledQuery, Object JavaDoc[] params,
38             boolean doNotFlush) {
39         this.pm = pm;
40         if (!doNotFlush && !compiledQuery.getQueryDetails().isIgnoreCache()) {
41             pm.flushIfDepOn(compiledQuery.getEvictionClassBits());
42         }
43         qrw = pm.executeQuery(compiledQuery, params);
44     }
45
46     public void remove() {
47         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
48     }
49
50     public boolean hasNext() {
51         if (closed) return false;
52
53         if (!noMoreDataInResultSet && (data == null || indexInData == actualSize)) {
54             getMoreData();
55         }
56         return !(data == null || indexInData == actualSize);
57     }
58
59     /**
60      * If we have not started yet then execute the query and return the first result.
61      * If we have already started and there is still data available then return the
62      * next data. If we are at the end of the last fetched data then get more
63      * and return the first data.
64      * @return
65      */

66     public Object JavaDoc next() {
67         if (closed) {
68             throw new NoSuchElementException JavaDoc();
69         }
70
71         if (!noMoreDataInResultSet && (data == null || indexInData == actualSize)) {
72             getMoreData();
73         }
74         return getNextData();
75     }
76
77     private void getMoreData() {
78         //get data from server
79
QueryResultContainer container = pm.getNextQueryResult(qrw, 0);
80         pm.addToCache(container.container);
81
82         data = container.getDataArray();
83         actualSize = container.size();
84         noMoreDataInResultSet = container.isqFinished();
85         indexInData = 0;
86     }
87
88     private Object JavaDoc getNextData() {
89         if (indexInData == actualSize) throw new NoSuchElementException JavaDoc();
90         nextIndex++;
91         return QueryResultBase.resolveRow(data[indexInData++], pm);
92     }
93
94     /**
95      * Cleanup all resources.
96      */

97     public void close() {
98         if (closed) return;
99
100         if (qrw != null) {
101             pm.closeQuery(qrw);
102             qrw = null;
103         }
104         
105         pm = null;
106         data = null;
107         qrw = null;
108
109         closed = true;
110     }
111
112     public int nextIndex() {
113         return nextIndex;
114     }
115
116     public int previousIndex() {
117         return nextIndex - 1;
118     }
119
120     public boolean hasPrevious() {
121         return nextIndex != 0;
122     }
123
124     public Object JavaDoc previous() {
125         throw BindingSupportImpl.getInstance().unsupportedOperation(null);
126     }
127
128     public void add(Object JavaDoc o) {
129         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
130     }
131
132     public void set(Object JavaDoc o) {
133         throw BindingSupportImpl.getInstance().unsupported("Not allowed to modify");
134     }
135 }
136
137 /* CVS change log - do not edit
138  *
139  * $Log: EJBQueryIterator.java,v $
140  * Revision 1.1 2005/06/20 09:54:35 carl_rosenberger
141  * OpenAccess now supplied as Source only
142  *
143  * Revision 1.1 2005/04/19 09:43:44 jaco
144  * updated for ejb3 support
145  *
146  * Revision 1.1.1.1 2005/03/08 08:36:03 david
147  * Source from jdo2 project
148  *
149  * Revision 1.2 2005/03/07 10:07:07 jaco
150  * refactored query stuff
151  *
152  * Revision 1.1 2005/03/02 16:33:14 jaco
153  * updated for horizontal inheritance
154  *
155  */

156
Popular Tags