001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */
053:
054: /**
055: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
056: *
057: * ===================================================================
058: * The Apache Software License, Version 1.1
059: *
060: * Redistribution and use in source and binary forms, with or without
061: * modification, are permitted provided that the following conditions
062: * are met:
063: *
064: * 1. Redistributions of source code must retain the above copyright
065: * notice, this list of conditions and the following disclaimer.
066: *
067: * 2. Redistributions in binary form must reproduce the above copyright
068: * notice, this list of conditions and the following disclaimer in
069: * the documentation and/or other materials provided with the
070: * distribution.
071: *
072: * 3. The end-user documentation included with the redistribution,
073: * if any, must include the following acknowledgment:
074: * "This product includes software developed by
075: * CoolServlets.com (http://www.coolservlets.com)."
076: * Alternately, this acknowledgment may appear in the software itself,
077: * if and wherever such third-party acknowledgments normally appear.
078: *
079: * 4. The names "Jive" and "CoolServlets.com" must not be used to
080: * endorse or promote products derived from this software without
081: * prior written permission. For written permission, please
082: * contact webmaster@coolservlets.com.
083: *
084: * 5. Products derived from this software may not be called "Jive",
085: * nor may "Jive" appear in their name, without prior written
086: * permission of CoolServlets.com.
087: *
088: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
089: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
090: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
091: * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
092: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
093: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
094: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
095: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
096: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
097: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
098: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
099: * SUCH DAMAGE.
100: * ====================================================================
101: *
102: * This software consists of voluntary contributions made by many
103: * individuals on behalf of CoolServlets.com. For more information
104: * on CoolServlets.com, please see <http://www.coolservlets.com>.
105: */package com.Yasna.forum.database;
106:
107: import java.util.Iterator;
108: import java.util.ListIterator;
109: import java.util.ArrayList;
110: import java.sql.*;
111: import com.Yasna.forum.*;
112:
113: /**
114: * An Iterator for all the users in the system. Those wishing to integrate
115: * Yazd into their own User system should also modify this class.
116: */
117: public class DbUserIterator implements Iterator, ListIterator {
118:
119: /** DATABASE QUERIES **/
120: private static final String ALL_USERS = "SELECT userID from yazdUser";
121:
122: private int currentIndex = -1;
123: private int[] users;
124:
125: private ProfileManager profileManager;
126:
127: protected DbUserIterator(ProfileManager profileManager) {
128: this .profileManager = profileManager;
129: //We don't know how many results will be returned, so store them
130: //in an ArrayList.
131: ArrayList tempUsers = new ArrayList();
132: Connection con = null;
133: PreparedStatement pstmt = null;
134: try {
135: con = DbConnectionManager.getConnection();
136: pstmt = con.prepareStatement(ALL_USERS);
137: ResultSet rs = pstmt.executeQuery();
138: while (rs.next()) {
139: tempUsers.add(new Integer(rs.getInt("userID")));
140: }
141: } catch (SQLException sqle) {
142: System.err.println("Error in DbUserIterator:constructor()-"
143: + sqle);
144: } finally {
145: try {
146: pstmt.close();
147: } catch (Exception e) {
148: e.printStackTrace();
149: }
150: try {
151: con.close();
152: } catch (Exception e) {
153: e.printStackTrace();
154: }
155: }
156: //Now copy into an array.
157: users = new int[tempUsers.size()];
158: for (int i = 0; i < users.length; i++) {
159: users[i] = ((Integer) tempUsers.get(i)).intValue();
160: }
161: }
162:
163: protected DbUserIterator(ProfileManager profileManager,
164: int startIndex, int numResults) {
165: this .profileManager = profileManager;
166:
167: int[] tempUsers = new int[numResults];
168: //It's very possible that there might not be as many results to get
169: //as we requested. Therefore, we keep track of how many results we
170: //get by keeping a count.
171: int userCount = 0;
172:
173: Connection con = null;
174: PreparedStatement pstmt = null;
175:
176: try {
177: con = DbConnectionManager.getConnection();
178: pstmt = con.prepareStatement(ALL_USERS);
179: ResultSet rs = pstmt.executeQuery();
180: //Move to start of index
181: for (int i = 0; i < startIndex; i++) {
182: rs.next();
183: }
184: //Now read in desired number of results
185: for (int i = 0; i < numResults; i++) {
186: if (rs.next()) {
187: tempUsers[userCount] = rs.getInt("userID");
188: userCount++;
189: } else {
190: break;
191: }
192: }
193: } catch (SQLException sqle) {
194: System.err.println("Error in DbUserIterator:constructor()-"
195: + sqle);
196: } finally {
197: try {
198: pstmt.close();
199: } catch (Exception e) {
200: e.printStackTrace();
201: }
202: try {
203: con.close();
204: } catch (Exception e) {
205: e.printStackTrace();
206: }
207: }
208: users = new int[userCount];
209: for (int i = 0; i < userCount; i++) {
210: users[i] = tempUsers[i];
211: }
212: }
213:
214: /**
215: * Returns true if there are more users left to iteratate through forwards.
216: */
217: public boolean hasNext() {
218: return (currentIndex + 1 < users.length);
219: }
220:
221: /**
222: * Returns true if there are more users left to iteratore through backwards.
223: */
224: public boolean hasPrevious() {
225: return (currentIndex > 0);
226: }
227:
228: /**
229: * Returns the next User.
230: *
231: * @return the next User.
232: * @throws NoSuchElementException if there are no more elements to return.
233: */
234: public Object next() throws java.util.NoSuchElementException {
235: User user = null;
236: currentIndex++;
237: if (currentIndex >= users.length) {
238: throw new java.util.NoSuchElementException();
239: }
240: try {
241: user = profileManager.getUser(users[currentIndex]);
242: } catch (UserNotFoundException gnfe) {
243: gnfe.printStackTrace();
244: }
245: return user;
246: }
247:
248: /**
249: * For security reasons, the add operation is not supported. Use
250: * ProfileManager instead.
251: *
252: * @see ProfileManager
253: */
254: public void add(Object o) throws UnsupportedOperationException {
255: throw new UnsupportedOperationException();
256: }
257:
258: /**
259: * For security reasons, the remove operation is not supported. Use
260: * ProfileManager instead.
261: *
262: * @see ProfileManager
263: */
264: public void remove() {
265: throw new UnsupportedOperationException();
266: }
267:
268: /**
269: * For security reasons, the set operation is not supported. Use
270: * ProfileManager instead.
271: *
272: * @see ProfileManager
273: */
274: public void set(Object o) throws UnsupportedOperationException {
275: throw new UnsupportedOperationException();
276: }
277:
278: /**
279: * Returns the index number that would be returned with a call to next().
280: */
281: public int nextIndex() {
282: return currentIndex + 1;
283: }
284:
285: /**
286: * Returns the previous user.
287: */
288: public Object previous() throws java.util.NoSuchElementException {
289: User user = null;
290: currentIndex--;
291: if (currentIndex < 0) {
292: currentIndex++;
293: throw new java.util.NoSuchElementException();
294: }
295: try {
296: user = profileManager.getUser(users[currentIndex]);
297: } catch (UserNotFoundException gnfe) {
298: System.err.println(gnfe);
299: }
300: return user;
301: }
302:
303: /**
304: * Returns the index number that would be returned with a call to previous().
305: */
306: public int previousIndex() {
307: return currentIndex - 1;
308: }
309: }
|