KickJava   Java API By Example, From Geeks To Geeks.

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


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.ForumThread;
38 import org.nemesis.forum.exception.ForumThreadNotFoundException;
39 import org.nemesis.forum.util.jdbc.DbConnectionManager;
40 /**
41  * Database implementation to iterate through threads in a forum.
42  * At the moment, threads are always ordered by creationDate. Obviously,
43  * various skins will probably want different iterators. The solution will
44  * probably be to pass in various flags to forum.iterator() in order to get
45  * different types of iterators. That will require small changes to this
46  * class.
47  */

48 public class DbForumIterator implements Iterator JavaDoc, ListIterator JavaDoc {
49 static protected Log log = LogFactory.getLog(DbForumIterator.class);
50     /** DATABASE QUERIES **/
51     private static final String JavaDoc GET_THREADS ="SELECT threadID, creationDate FROM yazdThread WHERE forumID=? ORDER BY creationDate DESC";
52     private static final String JavaDoc GET_THREADS_APPROVED ="SELECT threadID, creationDate FROM yazdThread WHERE forumID=? AND approved=? ORDER BY creationDate DESC ";
53
54     //A reference to the forum object that the iterator was created from.
55
//This is used to load thread objects.
56
private DbForum forum;
57     //maintain an array of thread ids to iterator through.
58
private int[] threads;
59     //points to the current thread id that the user has iterated to.
60
private int currentIndex = -1;
61
62     DbForumFactory factory;
63
64     public DbForumIterator(DbForum forum, DbForumFactory factory) {
65         this.forum = forum;
66         this.factory = factory;
67         //We don't know how many results will be returned, so store them
68
//in an ArrayList.
69
ArrayList JavaDoc tempThreads = new ArrayList JavaDoc();
70         Connection JavaDoc con = null;
71         PreparedStatement JavaDoc pstmt = null;
72         try {
73             con = DbConnectionManager.getConnection();
74             pstmt = con.prepareStatement(GET_THREADS);
75             pstmt.setInt(1, forum.getID());
76             ResultSet JavaDoc rs = pstmt.executeQuery();
77
78             while (rs.next()) {
79                 tempThreads.add(new Integer JavaDoc(rs.getInt("threadID")));
80             }
81         } catch (SQLException JavaDoc sqle) {
82             log.error("Error in DbThreadIterator:constructor()-" ,sqle);
83         } finally {
84             try {
85                 pstmt.close();
86             } catch (Exception JavaDoc e) {
87                 log.error("" ,e);
88             }
89             try {
90                 con.close();
91             } catch (Exception JavaDoc e) {
92                 log.error("" ,e);
93             }
94         }
95         threads = new int[tempThreads.size()];
96         for (int i = 0; i < threads.length; i++) {
97             threads[i] = ((Integer JavaDoc) tempThreads.get(i)).intValue();
98         }
99     }
100
101     public DbForumIterator(DbForum forum, DbForumFactory factory, int startIndex, int numResults) {
102         this.forum = forum;
103         this.factory = factory;
104
105         int[] tempThreads = new int[numResults];
106         //It's very possible that there might not be as many threads to get
107
//as we requested. Therefore, we keep track of how many threads we
108
//get by keeping a threadCount.
109
int threadCount = 0;
110
111         Connection JavaDoc con = null;
112         PreparedStatement JavaDoc pstmt = null;
113         try {
114             con = DbConnectionManager.getConnection();
115             pstmt = con.prepareStatement(GET_THREADS);
116             pstmt.setInt(1, forum.getID());
117             ResultSet JavaDoc rs = pstmt.executeQuery();
118
119             //Move to start of index
120
for (int i = 0; i < startIndex; i++) {
121                 rs.next();
122             }
123             //Now read in desired number of results
124
for (int i = 0; i < numResults; i++) {
125                 if (rs.next()) {
126                     tempThreads[threadCount] = rs.getInt("threadID");
127                     threadCount++;
128                 } else {
129                     break;
130                 }
131             }
132         } catch (SQLException JavaDoc sqle) {
133             log.error("Error in DbThreadIterator:constructor()-", sqle);
134         } finally {
135             try {
136                 pstmt.close();
137             } catch (Exception JavaDoc e) {
138                 log.error("" ,e);
139             }
140             try {
141                 con.close();
142             } catch (Exception JavaDoc e) {
143                 log.error("" ,e);
144             }
145         }
146         threads = new int[threadCount];
147         for (int i = 0; i < threadCount; i++) {
148             threads[i] = tempThreads[i];
149         }
150     }
151     
152     public DbForumIterator(DbForum forum, DbForumFactory factory,boolean approved) {
153             this.forum = forum;
154             this.factory = factory;
155             //We don't know how many results will be returned, so store them
156
//in an ArrayList.
157
ArrayList JavaDoc tempThreads = new ArrayList JavaDoc();
158             Connection JavaDoc con = null;
159             PreparedStatement JavaDoc pstmt = null;
160             try {
161                 con = DbConnectionManager.getConnection();
162                 pstmt = con.prepareStatement(GET_THREADS_APPROVED);
163                 pstmt.setInt(1, forum.getID());
164                 pstmt.setInt(2, approved ? 1:0);
165                 ResultSet JavaDoc rs = pstmt.executeQuery();
166
167                 while (rs.next()) {
168                     tempThreads.add(new Integer JavaDoc(rs.getInt("threadID")));
169                 }
170             } catch (SQLException JavaDoc sqle) {
171                 log.error("Error in DbThreadIterator:constructor()-" ,sqle);
172             } finally {
173                 try {
174                     pstmt.close();
175                 } catch (Exception JavaDoc e) {
176                     log.error("" ,e);
177                 }
178                 try {
179                     con.close();
180                 } catch (Exception JavaDoc e) {
181                     log.error("" ,e);
182                 }
183             }
184             threads = new int[tempThreads.size()];
185             for (int i = 0; i < threads.length; i++) {
186                 threads[i] = ((Integer JavaDoc) tempThreads.get(i)).intValue();
187             }
188         }
189
190         public DbForumIterator(DbForum forum, DbForumFactory factory, int startIndex, int numResults,boolean approved) {
191             this.forum = forum;
192             this.factory = factory;
193
194             int[] tempThreads = new int[numResults];
195             //It's very possible that there might not be as many threads to get
196
//as we requested. Therefore, we keep track of how many threads we
197
//get by keeping a threadCount.
198
int threadCount = 0;
199
200             Connection JavaDoc con = null;
201             PreparedStatement JavaDoc pstmt = null;
202             try {
203                 con = DbConnectionManager.getConnection();
204                 pstmt = con.prepareStatement(GET_THREADS_APPROVED);
205                 pstmt.setInt(1, forum.getID());
206                 pstmt.setInt(2, approved ? 1:0);
207                 ResultSet JavaDoc rs = pstmt.executeQuery();
208
209                 //Move to start of index
210
for (int i = 0; i < startIndex; i++) {
211                     rs.next();
212                 }
213                 //Now read in desired number of results
214
for (int i = 0; i < numResults; i++) {
215                     if (rs.next()) {
216                         tempThreads[threadCount] = rs.getInt("threadID");
217                         threadCount++;
218                     } else {
219                         break;
220                     }
221                 }
222             } catch (SQLException JavaDoc sqle) {
223                 log.error("Error in DbThreadIterator:constructor()-", sqle);
224             } finally {
225                 try {
226                     pstmt.close();
227                 } catch (Exception JavaDoc e) {
228                     log.error("" ,e);
229                 }
230                 try {
231                     con.close();
232                 } catch (Exception JavaDoc e) {
233                     log.error("" ,e);
234                 }
235             }
236             threads = new int[threadCount];
237             for (int i = 0; i < threadCount; i++) {
238                 threads[i] = tempThreads[i];
239             }
240         }
241
242     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
243         throw new UnsupportedOperationException JavaDoc();
244     }
245
246     public boolean hasNext() {
247         return (currentIndex + 1 < threads.length);
248     }
249
250     public boolean hasPrevious() {
251         return (currentIndex > 0);
252     }
253
254     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
255         ForumThread thread = null;
256         currentIndex++;
257         if (currentIndex >= threads.length) {
258             currentIndex--;
259             throw new java.util.NoSuchElementException JavaDoc();
260         }
261         try {
262             thread = forum.getThread(threads[currentIndex]);
263         } catch (ForumThreadNotFoundException tnfe) {
264             log.error("" ,tnfe);
265         }
266         return thread;
267     }
268
269     public int nextIndex() {
270         return currentIndex + 1;
271     }
272
273     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
274         ForumThread thread = null;
275         currentIndex--;
276         if (currentIndex < 0) {
277             currentIndex++;
278             throw new java.util.NoSuchElementException JavaDoc();
279         }
280         try {
281             thread = forum.getThread(threads[currentIndex]);
282         } catch (ForumThreadNotFoundException tnfe) {
283             log.error("" ,tnfe);
284         }
285         return thread;
286     }
287
288     public int previousIndex() {
289         return currentIndex - 1;
290     }
291
292     public void remove() throws UnsupportedOperationException JavaDoc {
293         throw new UnsupportedOperationException JavaDoc();
294     }
295
296     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
297         throw new UnsupportedOperationException JavaDoc();
298     }
299 }
300
Popular Tags