KickJava   Java API By Example, From Geeks To Geeks.

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


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.Iterator JavaDoc;
32
33 import org.apache.commons.logging.Log;
34 import org.apache.commons.logging.LogFactory;
35 import org.nemesis.forum.ForumThread;
36 import org.nemesis.forum.Message;
37 import org.nemesis.forum.exception.ForumMessageNotFoundException;
38 import org.nemesis.forum.util.jdbc.DbConnectionManager;
39 /**
40  * Database implementation of Iterator for ForumMesages in a ForumThread.
41  */

42 public class DbThreadIterator implements Iterator JavaDoc {
43     static protected Log log = LogFactory.getLog(DbThreadIterator.class);
44     /** DATABASE QUERIES **/
45     private static final String JavaDoc MESSAGE_COUNT = "SELECT count(messageID) FROM yazdMessage WHERE threadID=?";
46     private static final String JavaDoc MESSAGE_COUNT_APPROVED = "SELECT count(messageID) FROM yazdMessage WHERE threadID=? AND approved=?";
47     private static final String JavaDoc GET_MESSAGES = "SELECT messageID, creationDate FROM yazdMessage WHERE threadID=? " + "ORDER BY creationDate ASC";
48     private static final String JavaDoc GET_MESSAGES_APPROVED = "SELECT messageID, creationDate FROM yazdMessage WHERE threadID=? AND approved=? ORDER BY creationDate ASC";
49
50     private ForumThread thread;
51     private int[] messages;
52     private int currentIndex = -1;
53
54     private Message nextMessage = null;
55
56     protected DbThreadIterator(ForumThread thread) {
57         this.thread = thread;
58         Connection JavaDoc con = null;
59         PreparedStatement JavaDoc pstmt = null;
60         try {
61             con = DbConnectionManager.getConnection();
62             pstmt = con.prepareStatement(MESSAGE_COUNT);
63             pstmt.setInt(1, thread.getID());
64             ResultSet JavaDoc rs = pstmt.executeQuery();
65             rs.next();
66             messages = new int[rs.getInt(1)];
67             pstmt.close();
68
69             pstmt = con.prepareStatement(GET_MESSAGES);
70             pstmt.setInt(1, thread.getID());
71             rs = pstmt.executeQuery();
72             for (int i = 0; i < messages.length; i++) {
73                 rs.next();
74                 messages[i] = rs.getInt(1);
75             }
76         } catch (SQLException JavaDoc sqle) {
77             log.error("Error in DbThreadIterator:constructor()-" , sqle);
78         } finally {
79             try {
80                 pstmt.close();
81             } catch (Exception JavaDoc e) {
82                 log.error("" , e);
83             }
84             try {
85                 con.close();
86             } catch (Exception JavaDoc e) {
87                 log.error("" , e);
88             }
89         }
90     }
91
92     protected DbThreadIterator(ForumThread thread, int startIndex, int numResults) {
93         this.thread = thread;
94
95         int[] tempMessages = new int[numResults];
96         //It's very possible that there might not be as many messages to get
97
//as we requested. Therefore, we keep track of how many messages we
98
//get by keeping a messageCount.
99
int messageCount = 0;
100
101         Connection JavaDoc con = null;
102         PreparedStatement JavaDoc pstmt = null;
103
104         try {
105             con = DbConnectionManager.getConnection();
106             pstmt = con.prepareStatement(GET_MESSAGES);
107             pstmt.setInt(1, thread.getID());
108             
109             ResultSet JavaDoc rs = pstmt.executeQuery();
110             //Move to start of index
111
for (int i = 0; i < startIndex; i++) {
112                 rs.next();
113             }
114             //Now read in desired number of results
115
for (int i = 0; i < numResults; i++) {
116                 if (rs.next()) {
117                     tempMessages[messageCount] = rs.getInt("messageID");
118                     messageCount++;
119                 } else {
120                     break;
121                 }
122             }
123         } catch (SQLException JavaDoc sqle) {
124             log.error("Error in DbThreadIterator:constructor()-" , sqle);
125         } finally {
126             try {
127                 pstmt.close();
128             } catch (Exception JavaDoc e) {
129                 log.error("" , e);
130             }
131             try {
132                 con.close();
133             } catch (Exception JavaDoc e) {
134                 log.error("" , e);
135             }
136         }
137         messages = new int[messageCount];
138         for (int i = 0; i < messageCount; i++) {
139             messages[i] = tempMessages[i];
140         }
141     }
142     
143     protected DbThreadIterator(boolean approved,ForumThread thread) {
144             this.thread = thread;
145             Connection JavaDoc con = null;
146             PreparedStatement JavaDoc pstmt = null;
147             try {
148                 con = DbConnectionManager.getConnection();
149                 pstmt = con.prepareStatement(MESSAGE_COUNT_APPROVED);
150                 pstmt.setInt(1, thread.getID());
151                 pstmt.setInt(2, approved?1:0);
152                 
153                 ResultSet JavaDoc rs = pstmt.executeQuery();
154                 rs.next();
155                 messages = new int[rs.getInt(1)];
156                 pstmt.close();
157
158                 pstmt = con.prepareStatement(GET_MESSAGES_APPROVED);
159                 pstmt.setInt(1, thread.getID());
160                 pstmt.setInt(2, approved?1:0);
161                 rs = pstmt.executeQuery();
162                 for (int i = 0; i < messages.length; i++) {
163                     rs.next();
164                     messages[i] = rs.getInt(1);
165                 }
166             } catch (SQLException JavaDoc sqle) {
167                 log.error("Error in DbThreadIterator:constructor()-" , sqle);
168             } finally {
169                 try {
170                     pstmt.close();
171                 } catch (Exception JavaDoc e) {
172                     log.error("" , e);
173                 }
174                 try {
175                     con.close();
176                 } catch (Exception JavaDoc e) {
177                     log.error("" , e);
178                 }
179             }
180         }
181
182         protected DbThreadIterator(boolean approved,ForumThread thread, int startIndex, int numResults) {
183             this.thread = thread;
184
185             int[] tempMessages = new int[numResults];
186             //It's very possible that there might not be as many messages to get
187
//as we requested. Therefore, we keep track of how many messages we
188
//get by keeping a messageCount.
189
int messageCount = 0;
190
191             Connection JavaDoc con = null;
192             PreparedStatement JavaDoc pstmt = null;
193
194             try {
195                 con = DbConnectionManager.getConnection();
196                 pstmt = con.prepareStatement(GET_MESSAGES_APPROVED);
197                 pstmt.setInt(1, thread.getID());
198                 pstmt.setInt(2, approved?1:0);
199                 ResultSet JavaDoc rs = pstmt.executeQuery();
200                 //Move to start of index
201
for (int i = 0; i < startIndex; i++) {
202                     rs.next();
203                 }
204                 //Now read in desired number of results
205
for (int i = 0; i < numResults; i++) {
206                     if (rs.next()) {
207                         tempMessages[messageCount] = rs.getInt("messageID");
208                         messageCount++;
209                     } else {
210                         break;
211                     }
212                 }
213             } catch (SQLException JavaDoc sqle) {
214                 log.error("Error in DbThreadIterator:constructor()-" , sqle);
215             } finally {
216                 try {
217                     pstmt.close();
218                 } catch (Exception JavaDoc e) {
219                     log.error("" , e);
220                 }
221                 try {
222                     con.close();
223                 } catch (Exception JavaDoc e) {
224                     log.error("" , e);
225                 }
226             }
227             messages = new int[messageCount];
228             for (int i = 0; i < messageCount; i++) {
229                 messages[i] = tempMessages[i];
230             }
231         }
232
233     /**
234      * Returns true if there are more messages in the list.
235      *
236      * @return true if the iterator has more elements.
237      */

238     public boolean hasNext() {
239         //If we are at the end of the list, there can't be any more messages
240
//to iterate through.
241
if (currentIndex + 1 >= messages.length) {
242             return false;
243         }
244         return true;
245
246         /*
247         BUG: this code calls getNextMessage() which will increment the
248         currentIndex variable as a result of calling this message!
249         
250         //Otherwise, see if nextMessage is null. If so, try to load the next
251         //message to make sure it exists.
252         
253         // hmm, do we really need to do this here? i think it should be left up
254         // to the next() method -- BL
255         
256         if (nextMessage == null) {
257             nextMessage = getNextMessage();
258             if (nextMessage == null) {
259                 return false;
260             }
261         }
262         return true;
263         */

264     }
265
266     /**
267      * Returns the next message in the interation.
268      *
269      * @return the next message in the interation.
270      * @throws NoSuchElementException if the iteration has no more elements.
271      */

272     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
273         Message message = null;
274         if (nextMessage != null) {
275             message = nextMessage;
276             nextMessage = null;
277         } else {
278             message = getNextMessage();
279             if (message == null) {
280                 throw new java.util.NoSuchElementException JavaDoc();
281             }
282         }
283         return message;
284     }
285
286     /**
287      * Not supported for security reasons. Use the deleteMessage method in the
288      * ForumThread class instead.
289      *
290      * @see ForumThread#deleteMessage(ForumMessage)
291      */

292     public void remove() throws UnsupportedOperationException JavaDoc {
293         throw new UnsupportedOperationException JavaDoc();
294     }
295
296     /**
297      * Returns the next available message, or null if there are no more
298      * messages to return.
299      */

300     private Message getNextMessage() {
301         while (currentIndex + 1 < messages.length) {
302             currentIndex++;
303             try {
304                 Message message = thread.getMessage(messages[currentIndex]);
305                 return message;
306             } catch (ForumMessageNotFoundException mnfe) {
307             }
308         }
309         return null;
310     }
311 }
312
Popular Tags