KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbUserIterator


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.ListIterator JavaDoc;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.nemesis.forum.ProfileManager;
38 import org.nemesis.forum.User;
39 import org.nemesis.forum.exception.UserNotFoundException;
40 import org.nemesis.forum.util.jdbc.DbConnectionManager;
41 /**
42  * An Iterator for all the users in the system. Those wishing to integrate
43  * into their own User system should also modify this class.
44  */

45 public class DbUserIterator implements Iterator JavaDoc, ListIterator JavaDoc {
46     static protected Log log = LogFactory.getLog(DbUserIterator.class);
47     /** DATABASE QUERIES **/
48     private static final String JavaDoc ALL_USERS = "SELECT userID from yazdUser";
49
50     private int currentIndex = -1;
51     private int[] users;
52
53     private ProfileManager profileManager;
54
55     protected DbUserIterator(ProfileManager profileManager) {
56         this.profileManager = profileManager;
57         //We don't know how many results will be returned, so store them
58
//in an ArrayList.
59
ArrayList JavaDoc tempUsers = new ArrayList JavaDoc();
60         Connection JavaDoc con = null;
61         PreparedStatement JavaDoc pstmt = null;
62         try {
63             con = DbConnectionManager.getConnection();
64             pstmt = con.prepareStatement(ALL_USERS);
65             ResultSet JavaDoc rs = pstmt.executeQuery();
66             while (rs.next()) {
67                 tempUsers.add(new Integer JavaDoc(rs.getInt("userID")));
68             }
69         } catch (SQLException JavaDoc sqle) {
70             log.error("Error in DbUserIterator:constructor()-" , sqle);
71         } finally {
72             try {
73                 pstmt.close();
74             } catch (Exception JavaDoc e) {
75                 log.error("" , e);
76             }
77             try {
78                 con.close();
79             } catch (Exception JavaDoc e) {
80                 log.error("" , e);
81             }
82         }
83         //Now copy into an array.
84
users = new int[tempUsers.size()];
85         for (int i = 0; i < users.length; i++) {
86             users[i] = ((Integer JavaDoc) tempUsers.get(i)).intValue();
87         }
88     }
89
90     protected DbUserIterator(ProfileManager profileManager, int startIndex, int numResults) {
91         this.profileManager = profileManager;
92
93         int[] tempUsers = new int[numResults];
94         //It's very possible that there might not be as many results to get
95
//as we requested. Therefore, we keep track of how many results we
96
//get by keeping a count.
97
int userCount = 0;
98
99         Connection JavaDoc con = null;
100         PreparedStatement JavaDoc pstmt = null;
101
102         try {
103             con = DbConnectionManager.getConnection();
104             pstmt = con.prepareStatement(ALL_USERS);
105             ResultSet JavaDoc rs = pstmt.executeQuery();
106             //Move to start of index
107
for (int i = 0; i < startIndex; i++) {
108                 rs.next();
109             }
110             //Now read in desired number of results
111
for (int i = 0; i < numResults; i++) {
112                 if (rs.next()) {
113                     tempUsers[userCount] = rs.getInt("userID");
114                     userCount++;
115                 } else {
116                     break;
117                 }
118             }
119         } catch (SQLException JavaDoc sqle) {
120             log.error("Error in DbUserIterator:constructor()-" , sqle);
121         } finally {
122             try {
123                 pstmt.close();
124             } catch (Exception JavaDoc e) {
125                 log.error("" , e);
126             }
127             try {
128                 con.close();
129             } catch (Exception JavaDoc e) {
130                 log.error("" , e);
131             }
132         }
133         users = new int[userCount];
134         for (int i = 0; i < userCount; i++) {
135             users[i] = tempUsers[i];
136         }
137     }
138
139     /**
140      * Returns true if there are more users left to iteratate through forwards.
141      */

142     public boolean hasNext() {
143         return (currentIndex + 1 < users.length);
144     }
145
146     /**
147      * Returns true if there are more users left to iteratore through backwards.
148      */

149     public boolean hasPrevious() {
150         return (currentIndex > 0);
151     }
152
153     /**
154      * Returns the next User.
155      *
156      * @return the next User.
157      * @throws NoSuchElementException if there are no more elements to return.
158      */

159     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
160         User user = null;
161         currentIndex++;
162         if (currentIndex >= users.length) {
163             throw new java.util.NoSuchElementException JavaDoc();
164         }
165         try {
166             user = profileManager.getUser(users[currentIndex]);
167         } catch (UserNotFoundException gnfe) {
168             log.error("" , gnfe);
169         }
170         return user;
171     }
172
173     /**
174      * For security reasons, the add operation is not supported. Use
175      * ProfileManager instead.
176      *
177      * @see ProfileManager
178      */

179     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
180         throw new UnsupportedOperationException JavaDoc();
181     }
182
183     /**
184      * For security reasons, the remove operation is not supported. Use
185      * ProfileManager instead.
186      *
187      * @see ProfileManager
188      */

189     public void remove() {
190         throw new UnsupportedOperationException JavaDoc();
191     }
192
193     /**
194      * For security reasons, the set operation is not supported. Use
195      * ProfileManager instead.
196      *
197      * @see ProfileManager
198      */

199     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
200         throw new UnsupportedOperationException JavaDoc();
201     }
202
203     /**
204      * Returns the index number that would be returned with a call to next().
205      */

206     public int nextIndex() {
207         return currentIndex + 1;
208     }
209
210     /**
211      * Returns the previous user.
212      */

213     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
214         User user = null;
215         currentIndex--;
216         if (currentIndex < 0) {
217             currentIndex++;
218             throw new java.util.NoSuchElementException JavaDoc();
219         }
220         try {
221             user = profileManager.getUser(users[currentIndex]);
222         } catch (UserNotFoundException gnfe) {
223             log.error("" , gnfe);
224         }
225         return user;
226     }
227
228     /**
229      * Returns the index number that would be returned with a call to previous().
230      */

231     public int previousIndex() {
232         return currentIndex - 1;
233     }
234 }
235
Popular Tags