001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.impl;
026:
027: import java.sql.Connection;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.ListIterator;
034:
035: import org.apache.commons.logging.Log;
036: import org.apache.commons.logging.LogFactory;
037: import org.nemesis.forum.ProfileManager;
038: import org.nemesis.forum.User;
039: import org.nemesis.forum.exception.UserNotFoundException;
040: import org.nemesis.forum.util.jdbc.DbConnectionManager;
041:
042: /**
043: * An Iterator for all the users in the system. Those wishing to integrate
044: * into their own User system should also modify this class.
045: */
046: public class DbUserIterator implements Iterator, ListIterator {
047: static protected Log log = LogFactory.getLog(DbUserIterator.class);
048: /** DATABASE QUERIES **/
049: private static final String ALL_USERS = "SELECT userID from yazdUser";
050:
051: private int currentIndex = -1;
052: private int[] users;
053:
054: private ProfileManager profileManager;
055:
056: protected DbUserIterator(ProfileManager profileManager) {
057: this .profileManager = profileManager;
058: //We don't know how many results will be returned, so store them
059: //in an ArrayList.
060: ArrayList tempUsers = new ArrayList();
061: Connection con = null;
062: PreparedStatement pstmt = null;
063: try {
064: con = DbConnectionManager.getConnection();
065: pstmt = con.prepareStatement(ALL_USERS);
066: ResultSet rs = pstmt.executeQuery();
067: while (rs.next()) {
068: tempUsers.add(new Integer(rs.getInt("userID")));
069: }
070: } catch (SQLException sqle) {
071: log.error("Error in DbUserIterator:constructor()-", sqle);
072: } finally {
073: try {
074: pstmt.close();
075: } catch (Exception e) {
076: log.error("", e);
077: }
078: try {
079: con.close();
080: } catch (Exception e) {
081: log.error("", e);
082: }
083: }
084: //Now copy into an array.
085: users = new int[tempUsers.size()];
086: for (int i = 0; i < users.length; i++) {
087: users[i] = ((Integer) tempUsers.get(i)).intValue();
088: }
089: }
090:
091: protected DbUserIterator(ProfileManager profileManager,
092: int startIndex, int numResults) {
093: this .profileManager = profileManager;
094:
095: int[] tempUsers = new int[numResults];
096: //It's very possible that there might not be as many results to get
097: //as we requested. Therefore, we keep track of how many results we
098: //get by keeping a count.
099: int userCount = 0;
100:
101: Connection con = null;
102: PreparedStatement pstmt = null;
103:
104: try {
105: con = DbConnectionManager.getConnection();
106: pstmt = con.prepareStatement(ALL_USERS);
107: ResultSet rs = pstmt.executeQuery();
108: //Move to start of index
109: for (int i = 0; i < startIndex; i++) {
110: rs.next();
111: }
112: //Now read in desired number of results
113: for (int i = 0; i < numResults; i++) {
114: if (rs.next()) {
115: tempUsers[userCount] = rs.getInt("userID");
116: userCount++;
117: } else {
118: break;
119: }
120: }
121: } catch (SQLException sqle) {
122: log.error("Error in DbUserIterator:constructor()-", sqle);
123: } finally {
124: try {
125: pstmt.close();
126: } catch (Exception e) {
127: log.error("", e);
128: }
129: try {
130: con.close();
131: } catch (Exception e) {
132: log.error("", e);
133: }
134: }
135: users = new int[userCount];
136: for (int i = 0; i < userCount; i++) {
137: users[i] = tempUsers[i];
138: }
139: }
140:
141: /**
142: * Returns true if there are more users left to iteratate through forwards.
143: */
144: public boolean hasNext() {
145: return (currentIndex + 1 < users.length);
146: }
147:
148: /**
149: * Returns true if there are more users left to iteratore through backwards.
150: */
151: public boolean hasPrevious() {
152: return (currentIndex > 0);
153: }
154:
155: /**
156: * Returns the next User.
157: *
158: * @return the next User.
159: * @throws NoSuchElementException if there are no more elements to return.
160: */
161: public Object next() throws java.util.NoSuchElementException {
162: User user = null;
163: currentIndex++;
164: if (currentIndex >= users.length) {
165: throw new java.util.NoSuchElementException();
166: }
167: try {
168: user = profileManager.getUser(users[currentIndex]);
169: } catch (UserNotFoundException gnfe) {
170: log.error("", gnfe);
171: }
172: return user;
173: }
174:
175: /**
176: * For security reasons, the add operation is not supported. Use
177: * ProfileManager instead.
178: *
179: * @see ProfileManager
180: */
181: public void add(Object o) throws UnsupportedOperationException {
182: throw new UnsupportedOperationException();
183: }
184:
185: /**
186: * For security reasons, the remove operation is not supported. Use
187: * ProfileManager instead.
188: *
189: * @see ProfileManager
190: */
191: public void remove() {
192: throw new UnsupportedOperationException();
193: }
194:
195: /**
196: * For security reasons, the set operation is not supported. Use
197: * ProfileManager instead.
198: *
199: * @see ProfileManager
200: */
201: public void set(Object o) throws UnsupportedOperationException {
202: throw new UnsupportedOperationException();
203: }
204:
205: /**
206: * Returns the index number that would be returned with a call to next().
207: */
208: public int nextIndex() {
209: return currentIndex + 1;
210: }
211:
212: /**
213: * Returns the previous user.
214: */
215: public Object previous() throws java.util.NoSuchElementException {
216: User user = null;
217: currentIndex--;
218: if (currentIndex < 0) {
219: currentIndex++;
220: throw new java.util.NoSuchElementException();
221: }
222: try {
223: user = profileManager.getUser(users[currentIndex]);
224: } catch (UserNotFoundException gnfe) {
225: log.error("", gnfe);
226: }
227: return user;
228: }
229:
230: /**
231: * Returns the index number that would be returned with a call to previous().
232: */
233: public int previousIndex() {
234: return currentIndex - 1;
235: }
236: }
|