KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
110 //JDK1.1// import com.sun.java.util.collections.*;
111
import java.sql.*;
112 import com.Yasna.forum.*;
113
114 /**
115  * An Iterator for all the user's Messages in a forum.
116  */

117 public class DbUserMessagesIterator implements Iterator, ListIterator {
118
119     /** DATABASE QUERIES **/
120     private static final String JavaDoc USER_MESSAGES =
121         "SELECT messageID, yazdMessage.threadID FROM yazdMessage,yazdForum,yazdThread WHERE " +
122         "yazdMessage.userID=? AND yazdForum.forumID=? AND " +
123         "yazdThread.forumID=yazdForum.forumID AND " +
124         "yazdMessage.threadID=yazdThread.threadID order by yazdMessage.creationDate desc";
125
126     private int currentIndex = -1;
127     private int [] messages;
128     //The threads that correspond to each message.
129
private int [] threads;
130     private Forum forum;
131
132     private DbForumFactory factory;
133
134     protected DbUserMessagesIterator(DbForumFactory factory, User user, Forum forum)
135     {
136         this.factory = factory;
137         this.forum = forum;
138         //We don't know how many results will be returned, so store them
139
//in an ArrayList.
140
ArrayList tempMessages = new ArrayList();
141         ArrayList tempThreads = new ArrayList();
142         Connection con = null;
143         PreparedStatement pstmt = null;
144         try {
145             con = DbConnectionManager.getConnection();
146             pstmt = con.prepareStatement(USER_MESSAGES);
147             pstmt.setInt(1, user.getID());
148             pstmt.setInt(2, forum.getID());
149             ResultSet rs = pstmt.executeQuery();
150             while (rs.next()) {
151                 tempMessages.add(new Integer JavaDoc(rs.getInt(1)));
152                 tempThreads.add(new Integer JavaDoc(rs.getInt(2)));
153             }
154         }
155         catch( SQLException sqle ) {
156             System.err.println("Error in DbUserMessagesIterator:constructor()-" + sqle);
157             sqle.printStackTrace();
158         }
159         finally {
160             try { pstmt.close(); }
161             catch (Exception JavaDoc e) { e.printStackTrace(); }
162             try { con.close(); }
163             catch (Exception JavaDoc e) { e.printStackTrace(); }
164         }
165         //Now copy into an array.
166
messages = new int[tempMessages.size()];
167         for (int i=0; i<messages.length; i++) {
168             messages[i] = ((Integer JavaDoc)tempMessages.get(i)).intValue();
169         }
170         //Now copy into an array.
171
threads = new int[tempThreads.size()];
172         for (int i=0; i<threads.length; i++) {
173             threads[i] = ((Integer JavaDoc)tempThreads.get(i)).intValue();
174         }
175     }
176     protected DbUserMessagesIterator(DbForumFactory factory, User user, Forum forum, int startIndex, int numResults)
177      {
178         this.factory = factory;
179         this.forum = forum;
180         //We don't know how many results will be returned, so store them
181
//in an ArrayList.
182
int[] tempMessages = new int[numResults];
183         int[] tempThreads = new int[numResults];
184
185         Connection con = null;
186         PreparedStatement pstmt = null;
187         int messageCount = 0;
188
189          try {
190              con = DbConnectionManager.getConnection();
191              pstmt = con.prepareStatement(USER_MESSAGES);
192              pstmt.setInt(1, user.getID());
193              pstmt.setInt(2, forum.getID());
194              ResultSet rs = pstmt.executeQuery();
195              //Move to start of index
196
for (int i=0; i<startIndex; i++) {
197                  rs.next();
198              }
199              //Now read in desired number of results
200
for (int i=0; i<numResults; i++) {
201                  if (rs.next()) {
202                      tempMessages[messageCount] = rs.getInt(1);
203                      tempThreads[messageCount] = rs.getInt(2);
204                      messageCount++;
205                  }
206                  else {
207                      break;
208                  }
209              }
210          }
211          catch( SQLException sqle ) {
212              System.err.println("Error in DbUserMessagesIterator (2): constructor()-" + sqle);
213          }
214          finally {
215              try { pstmt.close(); }
216              catch (Exception JavaDoc e) { e.printStackTrace(); }
217              try { con.close(); }
218              catch (Exception JavaDoc e) { e.printStackTrace(); }
219          }
220          messages = new int[messageCount];
221          for (int i=0; i<messageCount; i++) {
222              messages[i] = tempMessages[i];
223          }
224         threads = new int[messageCount];
225         for (int i=0; i<messageCount; i++) {
226             threads[i] = tempThreads[i];
227         }
228      }
229
230     /**
231      * Returns true if there are more users left to iteratate through forwards.
232      */

233     public boolean hasNext() {
234         return (currentIndex+1 < messages.length);
235     }
236
237     /**
238      * Returns true if there are more users left to iteratore through backwards.
239      */

240     public boolean hasPrevious() {
241         return (currentIndex > 0);
242     }
243
244     /**
245      * Returns the next User.
246      */

247     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
248         ForumMessage message = null;
249         currentIndex++;
250         if (currentIndex >= messages.length) {
251             throw new java.util.NoSuchElementException JavaDoc();
252         }
253         try {
254             message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]);
255         }
256         catch (ForumThreadNotFoundException ftnfe) {
257             ftnfe.printStackTrace();
258         }
259         catch (ForumMessageNotFoundException fmnfe) {
260             fmnfe.printStackTrace();
261         }
262         return message;
263     }
264
265     /**
266      * For security reasons, the add operation is not supported. Use
267      * ProfileManager instead.
268      *
269      * @see ProfileManager
270      */

271     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
272         throw new UnsupportedOperationException JavaDoc();
273     }
274
275     /**
276      * For security reasons, the remove operation is not supported. Use
277      * ProfileManager instead.
278      *
279      * @see ProfileManager
280      */

281     public void remove() {
282         throw new UnsupportedOperationException JavaDoc();
283     }
284
285     /**
286      * For security reasons, the set operation is not supported. Use
287      * ProfileManager instead.
288      *
289      * @see ProfileManager
290      */

291     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
292         throw new UnsupportedOperationException JavaDoc();
293     }
294
295     /**
296      * Returns the index number that would be returned with a call to next().
297      */

298     public int nextIndex() {
299         return currentIndex+1;
300     }
301
302     /**
303      * Returns the previous user.
304      */

305     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
306         ForumMessage message = null;
307         currentIndex--;
308         if (currentIndex < 0) {
309             currentIndex++;
310             throw new java.util.NoSuchElementException JavaDoc();
311         }
312         try {
313             message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]);
314         }
315         catch (ForumThreadNotFoundException ftnfe) {
316             ftnfe.printStackTrace();
317         }
318         catch (ForumMessageNotFoundException fmnfe) {
319             fmnfe.printStackTrace();
320         }
321         return message;
322     }
323
324     /**
325      * Returns the index number that would be returned with a call to previous().
326      */

327     public int previousIndex() {
328         return currentIndex-1;
329     }
330 }
331
Popular Tags