KickJava   Java API By Example, From Geeks To Geeks.

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


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.Forum;
39 import org.nemesis.forum.Message;
40 import org.nemesis.forum.User;
41 import org.nemesis.forum.exception.NotFoundException;
42 import org.nemesis.forum.util.jdbc.DbConnectionManager;
43 /**
44  * An Iterator for all the user's Messages in a forum.
45  */

46 public class DbUserMessagesIterator implements Iterator JavaDoc, ListIterator JavaDoc {
47     static protected Log log = LogFactory.getLog(DbUserMessagesIterator.class);
48     /** DATABASE QUERIES **/
49     private static final String JavaDoc USER_MESSAGES =
50         "SELECT messageID, yazdMessage.threadID FROM yazdMessage,yazdForum,yazdThread WHERE "
51             + "yazdMessage.userID=? AND yazdForum.forumID=? AND "
52             + "yazdThread.forumID=yazdForum.forumID AND "
53             + "yazdMessage.threadID=yazdThread.threadID";
54
55     private int currentIndex = -1;
56     private int[] messages;
57     //The threads that correspond to each message.
58
private int[] threads;
59     private Forum forum;
60
61     private DbForumFactory factory;
62
63     protected DbUserMessagesIterator(DbForumFactory factory, User user, Forum forum) {
64         this.factory = factory;
65         this.forum = forum;
66         //We don't know how many results will be returned, so store them
67
//in an ArrayList.
68
ArrayList JavaDoc tempMessages = new ArrayList JavaDoc();
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(USER_MESSAGES);
75             pstmt.setInt(1, user.getID());
76             pstmt.setInt(2, forum.getID());
77             ResultSet JavaDoc rs = pstmt.executeQuery();
78             while (rs.next()) {
79                 tempMessages.add(new Integer JavaDoc(rs.getInt(1)));
80                 tempThreads.add(new Integer JavaDoc(rs.getInt(2)));
81             }
82         } catch (SQLException JavaDoc sqle) {
83             log.error("Error in DbUserMessagesIterator:constructor()-" , sqle);
84             
85         } finally {
86             try {
87                 pstmt.close();
88             } catch (Exception JavaDoc e) {
89                 log.error("" , e);
90             }
91             try {
92                 con.close();
93             } catch (Exception JavaDoc e) {
94                 log.error("" , e);
95             }
96         }
97         //Now copy into an array.
98
messages = new int[tempMessages.size()];
99         for (int i = 0; i < messages.length; i++) {
100             messages[i] = ((Integer JavaDoc) tempMessages.get(i)).intValue();
101         }
102         //Now copy into an array.
103
threads = new int[tempThreads.size()];
104         for (int i = 0; i < threads.length; i++) {
105             threads[i] = ((Integer JavaDoc) tempThreads.get(i)).intValue();
106         }
107     }
108
109     /**
110      * Returns true if there are more users left to iteratate through forwards.
111      */

112     public boolean hasNext() {
113         return (currentIndex + 1 < messages.length);
114     }
115
116     /**
117      * Returns true if there are more users left to iteratore through backwards.
118      */

119     public boolean hasPrevious() {
120         return (currentIndex > 0);
121     }
122
123     /**
124      * Returns the next User.
125      */

126     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
127         Message message = null;
128         currentIndex++;
129         if (currentIndex >= messages.length) {
130             throw new java.util.NoSuchElementException JavaDoc();
131         }
132         try {
133             message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]);
134         } catch (NotFoundException fmnfe) {
135             log.error("" , fmnfe);
136         }
137         return message;
138     }
139
140     /**
141      * For security reasons, the add operation is not supported. Use
142      * ProfileManager instead.
143      *
144      * @see ProfileManager
145      */

146     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
147         throw new UnsupportedOperationException JavaDoc();
148     }
149
150     /**
151      * For security reasons, the remove operation is not supported. Use
152      * ProfileManager instead.
153      *
154      * @see ProfileManager
155      */

156     public void remove() {
157         throw new UnsupportedOperationException JavaDoc();
158     }
159
160     /**
161      * For security reasons, the set operation is not supported. Use
162      * ProfileManager instead.
163      *
164      * @see ProfileManager
165      */

166     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
167         throw new UnsupportedOperationException JavaDoc();
168     }
169
170     /**
171      * Returns the index number that would be returned with a call to next().
172      */

173     public int nextIndex() {
174         return currentIndex + 1;
175     }
176
177     /**
178      * Returns the previous user.
179      */

180     public Object JavaDoc previous() throws NoSuchElementException JavaDoc {
181         Message message = null;
182         currentIndex--;
183         if (currentIndex < 0) {
184             currentIndex++;
185             throw new java.util.NoSuchElementException JavaDoc();
186         }
187         try {
188             message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]);
189         } catch (NotFoundException fmnfe) {
190             log.error("" , fmnfe);
191         }
192         return message;
193     }
194
195     /**
196      * Returns the index number that would be returned with a call to previous().
197      */

198     public int previousIndex() {
199         return currentIndex - 1;
200     }
201 }
202
Popular Tags