001    package org.apache.ojb.soda;
002    
003    /* Copyright 2002-2005 The Apache Software Foundation
004     *
005     * Licensed under the Apache License, Version 2.0 (the "License");
006     * you may not use this file except in compliance with the License.
007     * You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    import java.util.Collection;
019    import java.util.Iterator;
020    import java.util.Vector;
021    
022    import org.apache.ojb.broker.PersistenceBroker;
023    import org.apache.ojb.broker.accesslayer.RsIterator;
024    import org.apache.ojb.broker.query.Query;
025    import org.odbms.ObjectSet;
026    
027    /**
028     * @version $Id: ObjectSetImpl.java,v 1.1 2007-08-24 22:17:42 ewestfal Exp $
029     */
030    public class ObjectSetImpl implements ObjectSet
031    {
032        protected Iterator ojbIterator;
033        protected int length;
034        protected Vector elements;
035        protected int position;
036        protected int scrolled;
037        protected boolean resultSetClosed;
038    
039        /**
040         * Constructor for ObjectSetImpl. Builds up an ObjectSet from an OJB Query object
041         */
042        public ObjectSetImpl(PersistenceBroker broker, Query query, int limit)
043        {
044            super();
045            position = 0;
046            scrolled = 0;
047    
048            // avoid double query
049    //        length = broker.getCount(query);
050    //        if (limit >= 0)
051    //        {
052    //              length = Math.min(length,limit);
053    //        }
054    //        elements = new Vector(length);
055    
056            // thma:
057            // unfortunately Iterators are currently not extent-ware
058            // we have to use getCollectionBy Query () thus!
059            //ojbIterator = ojbBroker.getIteratorByQuery(query);
060            Collection col = broker.getCollectionByQuery(query);
061            ojbIterator = col.iterator();
062    
063            length = col.size();
064            if (limit >= 0)
065            {
066                    length = Math.min(length,limit);
067            }
068            elements = new Vector(length);
069    
070            setResultSetClosed(false);
071        }
072    
073        /*
074         * @see ObjectSet#hasNext()
075         */
076        public synchronized boolean hasNext()
077        {
078            if (position < length)
079            {
080                if (position < scrolled)
081                {
082                    return true;
083                }
084                else
085                {
086                    boolean result = ojbIterator.hasNext();
087                    return result;
088                }
089            }
090            else
091            {
092                            releaseJdbcResources();
093                return false;
094            }
095    
096        }
097    
098        protected void releaseJdbcResources()
099        {
100            if (!isResultSetClosed())
101            {
102                if (ojbIterator instanceof RsIterator)
103                {
104                    ((RsIterator) ojbIterator).releaseDbResources();
105                }
106                setResultSetClosed(true);
107            }
108        }
109    
110        /*
111         * @see ObjectSet#next()
112         */
113        public synchronized Object next()
114        {
115            if (position < scrolled)
116            {
117                position++;
118                return elements.get(position - 1);
119            }
120            else
121            {
122                Object next = ojbIterator.next();
123                elements.add(next);
124                position++;
125                scrolled++;
126                return next;
127            }
128        }
129    
130        /*
131         * @see ObjectSet#reset()
132         */
133        public synchronized void reset()
134        {
135            position = 0;
136        }
137    
138        /*
139         * @see ObjectSet#size()
140         */
141        public int size()
142        {
143            return length;
144        }
145    
146        /**
147         * Gets the resultSetClosed.
148         * @return Returns a boolean
149         */
150        public boolean isResultSetClosed()
151        {
152            return resultSetClosed;
153        }
154    
155        /**
156         * Sets the resultSetClosed.
157         * @param resultSetClosed The resultSetClosed to set
158         */
159        public void setResultSetClosed(boolean resultSetClosed)
160        {
161            this.resultSetClosed = resultSetClosed;
162        }
163    }