KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > database > DbUserIterator


1 /**
2  * Copyright (C) 2001 Yasna.com. All rights reserved.
3  *
4  * ===================================================================
5  * The Apache Software License, Version 1.1
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  * notice, this list of conditions and the following disclaimer.
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in
16  * the documentation and/or other materials provided with the
17  * distribution.
18  *
19  * 3. The end-user documentation included with the redistribution,
20  * if any, must include the following acknowledgment:
21  * "This product includes software developed by
22  * Yasna.com (http://www.yasna.com)."
23  * Alternately, this acknowledgment may appear in the software itself,
24  * if and wherever such third-party acknowledgments normally appear.
25  *
26  * 4. The names "Yazd" and "Yasna.com" must not be used to
27  * endorse or promote products derived from this software without
28  * prior written permission. For written permission, please
29  * contact yazd@yasna.com.
30  *
31  * 5. Products derived from this software may not be called "Yazd",
32  * nor may "Yazd" appear in their name, without prior written
33  * permission of Yasna.com.
34  *
35  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
39  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
46  * SUCH DAMAGE.
47  * ====================================================================
48  *
49  * This software consists of voluntary contributions made by many
50  * individuals on behalf of Yasna.com. For more information
51  * on Yasna.com, please see <http://www.yasna.com>.
52  */

53
54 /**
55  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
56  *
57  * ===================================================================
58  * The Apache Software License, Version 1.1
59  *
60  * Redistribution and use in source and binary forms, with or without
61  * modification, are permitted provided that the following conditions
62  * are met:
63  *
64  * 1. Redistributions of source code must retain the above copyright
65  * notice, this list of conditions and the following disclaimer.
66  *
67  * 2. Redistributions in binary form must reproduce the above copyright
68  * notice, this list of conditions and the following disclaimer in
69  * the documentation and/or other materials provided with the
70  * distribution.
71  *
72  * 3. The end-user documentation included with the redistribution,
73  * if any, must include the following acknowledgment:
74  * "This product includes software developed by
75  * CoolServlets.com (http://www.coolservlets.com)."
76  * Alternately, this acknowledgment may appear in the software itself,
77  * if and wherever such third-party acknowledgments normally appear.
78  *
79  * 4. The names "Jive" and "CoolServlets.com" must not be used to
80  * endorse or promote products derived from this software without
81  * prior written permission. For written permission, please
82  * contact webmaster@coolservlets.com.
83  *
84  * 5. Products derived from this software may not be called "Jive",
85  * nor may "Jive" appear in their name, without prior written
86  * permission of CoolServlets.com.
87  *
88  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
89  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
90  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
91  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
92  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
93  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
94  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
95  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
96  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
97  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
98  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
99  * 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  */

106
107 package com.Yasna.forum.database;
108
109 import java.util.Iterator JavaDoc;
110 import java.util.ListIterator JavaDoc;
111 import java.util.ArrayList JavaDoc;
112 import java.sql.*;
113 import com.Yasna.forum.*;
114
115 /**
116  * An Iterator for all the users in the system. Those wishing to integrate
117  * Yazd into their own User system should also modify this class.
118  */

119 public class DbUserIterator implements Iterator JavaDoc, ListIterator JavaDoc {
120
121     /** DATABASE QUERIES **/
122     private static final String JavaDoc ALL_USERS =
123         "SELECT userID from yazdUser";
124
125     private int currentIndex = -1;
126     private int [] users;
127
128     private ProfileManager profileManager;
129
130     protected DbUserIterator(ProfileManager profileManager) {
131         this.profileManager = profileManager;
132         //We don't know how many results will be returned, so store them
133
//in an ArrayList.
134
ArrayList JavaDoc tempUsers = new ArrayList JavaDoc();
135         Connection con = null;
136         PreparedStatement pstmt = null;
137         try {
138             con = DbConnectionManager.getConnection();
139             pstmt = con.prepareStatement(ALL_USERS);
140             ResultSet rs = pstmt.executeQuery();
141             while (rs.next()) {
142                 tempUsers.add(new Integer JavaDoc(rs.getInt("userID")));
143             }
144         }
145         catch( SQLException sqle ) {
146             System.err.println("Error in DbUserIterator:constructor()-" + sqle);
147         }
148         finally {
149             try { pstmt.close(); }
150             catch (Exception JavaDoc e) { e.printStackTrace(); }
151             try { con.close(); }
152             catch (Exception JavaDoc e) { e.printStackTrace(); }
153         }
154         //Now copy into an array.
155
users = new int[tempUsers.size()];
156         for (int i=0; i<users.length; i++) {
157             users[i] = ((Integer JavaDoc)tempUsers.get(i)).intValue();
158         }
159     }
160
161     protected DbUserIterator(ProfileManager profileManager, int startIndex,
162             int numResults)
163     {
164         this.profileManager = profileManager;
165
166         int[] tempUsers = new int[numResults];
167         //It's very possible that there might not be as many results to get
168
//as we requested. Therefore, we keep track of how many results we
169
//get by keeping a count.
170
int userCount = 0;
171
172         Connection con = null;
173         PreparedStatement pstmt = null;
174
175         try {
176             con = DbConnectionManager.getConnection();
177             pstmt = con.prepareStatement(ALL_USERS);
178             ResultSet rs = pstmt.executeQuery();
179             //Move to start of index
180
for (int i=0; i<startIndex; i++) {
181                 rs.next();
182             }
183             //Now read in desired number of results
184
for (int i=0; i<numResults; i++) {
185                 if (rs.next()) {
186                     tempUsers[userCount] = rs.getInt("userID");
187                     userCount++;
188                 }
189                 else {
190                     break;
191                 }
192             }
193         }
194         catch( SQLException sqle ) {
195             System.err.println("Error in DbUserIterator:constructor()-" + sqle);
196         }
197         finally {
198             try { pstmt.close(); }
199             catch (Exception JavaDoc e) { e.printStackTrace(); }
200             try { con.close(); }
201             catch (Exception JavaDoc e) { e.printStackTrace(); }
202         }
203         users = new int[userCount];
204         for (int i=0; i<userCount; i++) {
205             users[i] = tempUsers[i];
206         }
207     }
208
209     /**
210      * Returns true if there are more users left to iteratate through forwards.
211      */

212     public boolean hasNext() {
213         return (currentIndex+1 < users.length);
214     }
215
216     /**
217      * Returns true if there are more users left to iteratore through backwards.
218      */

219     public boolean hasPrevious() {
220         return (currentIndex > 0);
221     }
222
223     /**
224      * Returns the next User.
225      *
226      * @return the next User.
227      * @throws NoSuchElementException if there are no more elements to return.
228      */

229     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
230         User user = null;
231         currentIndex++;
232         if (currentIndex >= users.length) {
233             throw new java.util.NoSuchElementException JavaDoc();
234         }
235         try {
236             user = profileManager.getUser(users[currentIndex]);
237         }
238         catch (UserNotFoundException gnfe) {
239             gnfe.printStackTrace();
240         }
241         return user;
242     }
243
244     /**
245      * For security reasons, the add operation is not supported. Use
246      * ProfileManager instead.
247      *
248      * @see ProfileManager
249      */

250     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
251         throw new UnsupportedOperationException JavaDoc();
252     }
253
254     /**
255      * For security reasons, the remove operation is not supported. Use
256      * ProfileManager instead.
257      *
258      * @see ProfileManager
259      */

260     public void remove() {
261         throw new UnsupportedOperationException JavaDoc();
262     }
263
264     /**
265      * For security reasons, the set operation is not supported. Use
266      * ProfileManager instead.
267      *
268      * @see ProfileManager
269      */

270     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
271         throw new UnsupportedOperationException JavaDoc();
272     }
273
274     /**
275      * Returns the index number that would be returned with a call to next().
276      */

277     public int nextIndex() {
278         return currentIndex+1;
279     }
280
281     /**
282      * Returns the previous user.
283      */

284     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
285         User user = null;
286         currentIndex--;
287         if (currentIndex < 0) {
288             currentIndex++;
289             throw new java.util.NoSuchElementException JavaDoc();
290         }
291         try {
292             user = profileManager.getUser(users[currentIndex]);
293         }
294         catch (UserNotFoundException gnfe) {
295             System.err.println(gnfe);
296         }
297         return user;
298     }
299
300     /**
301      * Returns the index number that would be returned with a call to previous().
302      */

303     public int previousIndex() {
304         return currentIndex-1;
305     }
306 }
307
Popular Tags