KickJava   Java API By Example, From Geeks To Geeks.

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


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 package com.Yasna.forum.database;
55
56 import java.util.*;
57 //JDK1.1// import com.sun.java.util.collections.*;
58
import java.sql.*;
59 import com.Yasna.forum.*;
60
61 /**
62  * Iterates through all the groups in the system.
63  */

64 public class DbGroupIterator implements Iterator, ListIterator {
65
66     /** DATABASE QUERIES **/
67     private static final String JavaDoc ALL_GROUPS = "SELECT groupID from yazdGroup";
68
69     private int currentIndex = -1;
70     private int [] groups;
71
72     private ProfileManager profileManager;
73
74     protected DbGroupIterator(ProfileManager profileManager) {
75         this.profileManager = profileManager;
76         //We don't know how many results will be returned, so store them
77
//in an ArrayList.
78
ArrayList tempGroups = new ArrayList();
79         Connection con = null;
80         PreparedStatement pstmt = null;
81         try {
82             con = DbConnectionManager.getConnection();
83             pstmt = con.prepareStatement(ALL_GROUPS);
84             ResultSet rs = pstmt.executeQuery();
85             while (rs.next()) {
86                 tempGroups.add(new Integer JavaDoc(rs.getInt("groupID")));
87             }
88         }
89         catch( SQLException sqle ) {
90             System.err.println("Error in DbGroupIterator:constructor()-" + sqle);
91         }
92         finally {
93             try { pstmt.close(); }
94             catch (Exception JavaDoc e) { e.printStackTrace(); }
95             try { con.close(); }
96             catch (Exception JavaDoc e) { e.printStackTrace(); }
97         }
98         groups = new int[tempGroups.size()];
99         for (int i=0; i<groups.length; i++) {
100             groups[i] = ((Integer JavaDoc)tempGroups.get(i)).intValue();
101         }
102     }
103
104     protected DbGroupIterator(ProfileManager profileManager, int startIndex,
105             int numResults)
106     {
107         this.profileManager = profileManager;
108
109         int[] tempResults = new int[numResults];
110         //It's very possible that there might not be as many results to get
111
//as we requested. Therefore, we keep track of how many results we
112
//get by keeping a count.
113
int resultCount = 0;
114
115         Connection con = null;
116         PreparedStatement pstmt = null;
117
118         try {
119             con = DbConnectionManager.getConnection();
120             pstmt = con.prepareStatement(ALL_GROUPS);
121             ResultSet rs = pstmt.executeQuery();
122             //Move to start of index
123
for (int i=0; i<startIndex; i++) {
124                 rs.next();
125             }
126             //Now read in desired number of results
127
for (int i=0; i<numResults; i++) {
128                 if (rs.next()) {
129                     tempResults[resultCount] = rs.getInt("groupID");
130                     resultCount++;
131                 }
132                 else {
133                     break;
134                 }
135             }
136         }
137         catch( SQLException sqle ) {
138             System.err.println("Error in DbGroupIterator:constructor()-" + sqle);
139         }
140         finally {
141             try { pstmt.close(); }
142             catch (Exception JavaDoc e) { e.printStackTrace(); }
143             try { con.close(); }
144             catch (Exception JavaDoc e) { e.printStackTrace(); }
145         }
146         groups = new int[resultCount];
147         for (int i=0; i<resultCount; i++) {
148             groups[i] = tempResults[i];
149         }
150     }
151
152     /**
153      * Returns true if there are more groups left to iteratate through.
154      */

155     public boolean hasNext() {
156         return (currentIndex+1 < groups.length);
157     }
158
159     /**
160      * Returns the next Group.
161      */

162     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
163         Group group = null;
164         currentIndex++;
165         if (currentIndex >= groups.length) {
166             throw new java.util.NoSuchElementException JavaDoc();
167         }
168         try {
169             group = profileManager.getGroup(groups[currentIndex]);
170         }
171         catch (GroupNotFoundException gnfe) {
172             System.err.println(gnfe);
173         }
174         return group;
175     }
176
177     /**
178      * For security reasons, the remove operation is not supported. Use
179      * ProfileManager.deleteGroup() instead.
180      *
181      * @see ProfileManager
182      */

183     public void remove() {
184         throw new UnsupportedOperationException JavaDoc();
185     }
186
187     /**
188      * Returns true if there are more groups left to iterate through backwards.
189      */

190     public boolean hasPrevious() {
191         return (currentIndex > 0);
192     }
193
194     /**
195      * For security reasons, the add operation is not supported. Use
196      * ProfileManager instead.
197      *
198      * @see ProfileManager
199      */

200     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     /**
205      * For security reasons, the set operation is not supported. Use
206      * ProfileManager instead.
207      *
208      * @see ProfileManager
209      */

210     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
211         throw new UnsupportedOperationException JavaDoc();
212     }
213
214     /**
215      * Returns the index number that would be returned with a call to next().
216      */

217     public int nextIndex() {
218         return currentIndex+1;
219     }
220
221     /**
222      * Returns the previous group.
223      */

224     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
225         Group group = null;
226         currentIndex--;
227         if (currentIndex < 0) {
228             currentIndex++;
229             throw new java.util.NoSuchElementException JavaDoc();
230         }
231         try {
232             group = profileManager.getGroup(groups[currentIndex]);
233         }
234         catch (GroupNotFoundException gnfe) {
235             System.err.println(gnfe);
236         }
237         return group;
238     }
239
240     /**
241      * Returns the index number that would be returned with a call to previous().
242      */

243     public int previousIndex() {
244         return currentIndex-1;
245     }
246 }
247
248
Popular Tags