001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.ejb;
012:
013: import com.versant.core.server.CompiledQuery;
014: import com.versant.core.server.QueryResultWrapper;
015: import com.versant.core.common.QueryResultContainer;
016: import com.versant.core.common.BindingSupportImpl;
017: import com.versant.core.jdo.JDOListIterator;
018: import com.versant.core.jdo.QueryResultBase;
019:
020: import java.util.NoSuchElementException;
021:
022: /**
023: * A ListIterator implemenation that supports forward iteration over the results.
024: */
025: public class EJBQueryIterator implements JDOListIterator {
026: private EMProxy pm;
027:
028: private Object[] data;
029: private int actualSize;
030: private int indexInData;
031: private QueryResultWrapper qrw;
032: private boolean noMoreDataInResultSet;
033: private boolean closed;
034: private int nextIndex;
035:
036: public EJBQueryIterator(EMProxy pm, CompiledQuery compiledQuery,
037: Object[] params, boolean doNotFlush) {
038: this .pm = pm;
039: if (!doNotFlush
040: && !compiledQuery.getQueryDetails().isIgnoreCache()) {
041: pm.flushIfDepOn(compiledQuery.getEvictionClassBits());
042: }
043: qrw = pm.executeQuery(compiledQuery, params);
044: }
045:
046: public void remove() {
047: throw BindingSupportImpl.getInstance().unsupported(
048: "Not allowed to modify");
049: }
050:
051: public boolean hasNext() {
052: if (closed)
053: return false;
054:
055: if (!noMoreDataInResultSet
056: && (data == null || indexInData == actualSize)) {
057: getMoreData();
058: }
059: return !(data == null || indexInData == actualSize);
060: }
061:
062: /**
063: * If we have not started yet then execute the query and return the first result.
064: * If we have already started and there is still data available then return the
065: * next data. If we are at the end of the last fetched data then get more
066: * and return the first data.
067: * @return
068: */
069: public Object next() {
070: if (closed) {
071: throw new NoSuchElementException();
072: }
073:
074: if (!noMoreDataInResultSet
075: && (data == null || indexInData == actualSize)) {
076: getMoreData();
077: }
078: return getNextData();
079: }
080:
081: private void getMoreData() {
082: //get data from server
083: QueryResultContainer container = pm.getNextQueryResult(qrw, 0);
084: pm.addToCache(container.container);
085:
086: data = container.getDataArray();
087: actualSize = container.size();
088: noMoreDataInResultSet = container.isqFinished();
089: indexInData = 0;
090: }
091:
092: private Object getNextData() {
093: if (indexInData == actualSize)
094: throw new NoSuchElementException();
095: nextIndex++;
096: return QueryResultBase.resolveRow(data[indexInData++], pm);
097: }
098:
099: /**
100: * Cleanup all resources.
101: */
102: public void close() {
103: if (closed)
104: return;
105:
106: if (qrw != null) {
107: pm.closeQuery(qrw);
108: qrw = null;
109: }
110:
111: pm = null;
112: data = null;
113: qrw = null;
114:
115: closed = true;
116: }
117:
118: public int nextIndex() {
119: return nextIndex;
120: }
121:
122: public int previousIndex() {
123: return nextIndex - 1;
124: }
125:
126: public boolean hasPrevious() {
127: return nextIndex != 0;
128: }
129:
130: public Object previous() {
131: throw BindingSupportImpl.getInstance().unsupportedOperation(
132: null);
133: }
134:
135: public void add(Object o) {
136: throw BindingSupportImpl.getInstance().unsupported(
137: "Not allowed to modify");
138: }
139:
140: public void set(Object o) {
141: throw BindingSupportImpl.getInstance().unsupported(
142: "Not allowed to modify");
143: }
144: }
145:
146: /* CVS change log - do not edit
147: *
148: * $Log: EJBQueryIterator.java,v $
149: * Revision 1.1 2005/06/20 09:54:35 carl_rosenberger
150: * OpenAccess now supplied as Source only
151: *
152: * Revision 1.1 2005/04/19 09:43:44 jaco
153: * updated for ejb3 support
154: *
155: * Revision 1.1.1.1 2005/03/08 08:36:03 david
156: * Source from jdo2 project
157: *
158: * Revision 1.2 2005/03/07 10:07:07 jaco
159: * refactored query stuff
160: *
161: * Revision 1.1 2005/03/02 16:33:14 jaco
162: * updated for horizontal inheritance
163: *
164: */
|