KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.util.NoSuchElementException JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.nemesis.forum.Group;
39 import org.nemesis.forum.ProfileManager;
40 import org.nemesis.forum.exception.GroupNotFoundException;
41 import org.nemesis.forum.util.jdbc.DbConnectionManager;
42 /**
43  * Iterates through all the groups in the system.
44  */

45 public class DbGroupIterator implements Iterator JavaDoc, ListIterator JavaDoc {
46     static protected Log log = LogFactory.getLog(DbGroupIterator.class);
47     /** DATABASE QUERIES **/
48     private static final String JavaDoc ALL_GROUPS = "SELECT groupID from yazdGroup";
49
50     private int currentIndex = -1;
51     private int[] groups;
52
53     private ProfileManager profileManager;
54
55     protected DbGroupIterator(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 tempGroups = new ArrayList JavaDoc();
60         Connection JavaDoc con = null;
61         PreparedStatement JavaDoc pstmt = null;
62         try {
63             con = DbConnectionManager.getConnection();
64             pstmt = con.prepareStatement(ALL_GROUPS);
65             ResultSet JavaDoc rs = pstmt.executeQuery();
66             while (rs.next()) {
67                 tempGroups.add(new Integer JavaDoc(rs.getInt("groupID")));
68             }
69         } catch (SQLException JavaDoc sqle) {
70             log.error("Error in DbGroupIterator: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         groups = new int[tempGroups.size()];
84         for (int i = 0; i < groups.length; i++) {
85             groups[i] = ((Integer JavaDoc) tempGroups.get(i)).intValue();
86         }
87     }
88
89     protected DbGroupIterator(ProfileManager profileManager, int startIndex, int numResults) {
90         this.profileManager = profileManager;
91
92         int[] tempResults = new int[numResults];
93         //It's very possible that there might not be as many results to get
94
//as we requested. Therefore, we keep track of how many results we
95
//get by keeping a count.
96
int resultCount = 0;
97
98         Connection JavaDoc con = null;
99         PreparedStatement JavaDoc pstmt = null;
100
101         try {
102             con = DbConnectionManager.getConnection();
103             pstmt = con.prepareStatement(ALL_GROUPS);
104             ResultSet JavaDoc rs = pstmt.executeQuery();
105             //Move to start of index
106
for (int i = 0; i < startIndex; i++) {
107                 rs.next();
108             }
109             //Now read in desired number of results
110
for (int i = 0; i < numResults; i++) {
111                 if (rs.next()) {
112                     tempResults[resultCount] = rs.getInt("groupID");
113                     resultCount++;
114                 } else {
115                     break;
116                 }
117             }
118         } catch (SQLException JavaDoc sqle) {
119             log.error("Error in DbGroupIterator:constructor()-" ,sqle);
120         } finally {
121             try {
122                 pstmt.close();
123             } catch (Exception JavaDoc e) {
124                 log.error("",e);
125             }
126             try {
127                 con.close();
128             } catch (Exception JavaDoc e) {
129                 log.error("",e);
130             }
131         }
132         groups = new int[resultCount];
133         for (int i = 0; i < resultCount; i++) {
134             groups[i] = tempResults[i];
135         }
136     }
137
138     /**
139      * Returns true if there are more groups left to iteratate through.
140      */

141     public boolean hasNext() {
142         return (currentIndex + 1 < groups.length);
143     }
144
145     /**
146      * Returns the next Group.
147      */

148     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
149         Group group = null;
150         currentIndex++;
151         if (currentIndex >= groups.length) {
152             throw new java.util.NoSuchElementException JavaDoc();
153         }
154         try {
155             group = profileManager.getGroup(groups[currentIndex]);
156         } catch (GroupNotFoundException gnfe) {
157             log.error("",gnfe);
158         }
159         return group;
160     }
161
162     /**
163      * For security reasons, the remove operation is not supported. Use
164      * ProfileManager.deleteGroup() instead.
165      *
166      * @see ProfileManager
167      */

168     public void remove() {
169         throw new UnsupportedOperationException JavaDoc();
170     }
171
172     /**
173      * Returns true if there are more groups left to iterate through backwards.
174      */

175     public boolean hasPrevious() {
176         return (currentIndex > 0);
177     }
178
179     /**
180      * For security reasons, the add operation is not supported. Use
181      * ProfileManager instead.
182      *
183      * @see ProfileManager
184      */

185     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
186         throw new UnsupportedOperationException JavaDoc();
187     }
188
189     /**
190      * For security reasons, the set operation is not supported. Use
191      * ProfileManager instead.
192      *
193      * @see ProfileManager
194      */

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

202     public int nextIndex() {
203         return currentIndex + 1;
204     }
205
206     /**
207      * Returns the previous group.
208      */

209     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
210         Group group = null;
211         currentIndex--;
212         if (currentIndex < 0) {
213             currentIndex++;
214             throw new java.util.NoSuchElementException JavaDoc();
215         }
216         try {
217             group = profileManager.getGroup(groups[currentIndex]);
218         } catch (GroupNotFoundException gnfe) {
219             log.error("",gnfe);
220         }
221         return group;
222     }
223
224     /**
225      * Returns the index number that would be returned with a call to previous().
226      */

227     public int previousIndex() {
228         return currentIndex - 1;
229     }
230 }
231
Popular Tags