001: /**
002: * Copyright (C) 2001 Yasna.com. All rights reserved.
003: *
004: * ===================================================================
005: * The Apache Software License, Version 1.1
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * 1. Redistributions of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * 2. Redistributions in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * 3. The end-user documentation included with the redistribution,
020: * if any, must include the following acknowledgment:
021: * "This product includes software developed by
022: * Yasna.com (http://www.yasna.com)."
023: * Alternately, this acknowledgment may appear in the software itself,
024: * if and wherever such third-party acknowledgments normally appear.
025: *
026: * 4. The names "Yazd" and "Yasna.com" must not be used to
027: * endorse or promote products derived from this software without
028: * prior written permission. For written permission, please
029: * contact yazd@yasna.com.
030: *
031: * 5. Products derived from this software may not be called "Yazd",
032: * nor may "Yazd" appear in their name, without prior written
033: * permission of Yasna.com.
034: *
035: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
036: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
037: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
038: * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
039: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
040: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
041: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
042: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
043: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
044: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
045: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
046: * SUCH DAMAGE.
047: * ====================================================================
048: *
049: * This software consists of voluntary contributions made by many
050: * individuals on behalf of Yasna.com. For more information
051: * on Yasna.com, please see <http://www.yasna.com>.
052: */
053:
054: /**
055: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
056: *
057: * ===================================================================
058: * The Apache Software License, Version 1.1
059: *
060: * Redistribution and use in source and binary forms, with or without
061: * modification, are permitted provided that the following conditions
062: * are met:
063: *
064: * 1. Redistributions of source code must retain the above copyright
065: * notice, this list of conditions and the following disclaimer.
066: *
067: * 2. Redistributions in binary form must reproduce the above copyright
068: * notice, this list of conditions and the following disclaimer in
069: * the documentation and/or other materials provided with the
070: * distribution.
071: *
072: * 3. The end-user documentation included with the redistribution,
073: * if any, must include the following acknowledgment:
074: * "This product includes software developed by
075: * CoolServlets.com (http://www.coolservlets.com)."
076: * Alternately, this acknowledgment may appear in the software itself,
077: * if and wherever such third-party acknowledgments normally appear.
078: *
079: * 4. The names "Jive" and "CoolServlets.com" must not be used to
080: * endorse or promote products derived from this software without
081: * prior written permission. For written permission, please
082: * contact webmaster@coolservlets.com.
083: *
084: * 5. Products derived from this software may not be called "Jive",
085: * nor may "Jive" appear in their name, without prior written
086: * permission of CoolServlets.com.
087: *
088: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
089: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
090: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
091: * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
092: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
093: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
094: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
095: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
096: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
097: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
098: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
099: * 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: */package com.Yasna.forum.database;
106:
107: import java.util.*; //JDK1.1// import com.sun.java.util.collections.*;
108: import java.sql.*;
109: import com.Yasna.forum.*;
110:
111: /**
112: * An Iterator for all the user's Messages in a forum.
113: */
114: public class DbUserMessagesIterator implements Iterator, ListIterator {
115:
116: /** DATABASE QUERIES **/
117: private static final String USER_MESSAGES = "SELECT messageID, yazdMessage.threadID FROM yazdMessage,yazdForum,yazdThread WHERE "
118: + "yazdMessage.userID=? AND yazdForum.forumID=? AND "
119: + "yazdThread.forumID=yazdForum.forumID AND "
120: + "yazdMessage.threadID=yazdThread.threadID order by yazdMessage.creationDate desc";
121:
122: private int currentIndex = -1;
123: private int[] messages;
124: //The threads that correspond to each message.
125: private int[] threads;
126: private Forum forum;
127:
128: private DbForumFactory factory;
129:
130: protected DbUserMessagesIterator(DbForumFactory factory, User user,
131: Forum forum) {
132: this .factory = factory;
133: this .forum = forum;
134: //We don't know how many results will be returned, so store them
135: //in an ArrayList.
136: ArrayList tempMessages = new ArrayList();
137: ArrayList tempThreads = new ArrayList();
138: Connection con = null;
139: PreparedStatement pstmt = null;
140: try {
141: con = DbConnectionManager.getConnection();
142: pstmt = con.prepareStatement(USER_MESSAGES);
143: pstmt.setInt(1, user.getID());
144: pstmt.setInt(2, forum.getID());
145: ResultSet rs = pstmt.executeQuery();
146: while (rs.next()) {
147: tempMessages.add(new Integer(rs.getInt(1)));
148: tempThreads.add(new Integer(rs.getInt(2)));
149: }
150: } catch (SQLException sqle) {
151: System.err
152: .println("Error in DbUserMessagesIterator:constructor()-"
153: + sqle);
154: sqle.printStackTrace();
155: } finally {
156: try {
157: pstmt.close();
158: } catch (Exception e) {
159: e.printStackTrace();
160: }
161: try {
162: con.close();
163: } catch (Exception e) {
164: e.printStackTrace();
165: }
166: }
167: //Now copy into an array.
168: messages = new int[tempMessages.size()];
169: for (int i = 0; i < messages.length; i++) {
170: messages[i] = ((Integer) tempMessages.get(i)).intValue();
171: }
172: //Now copy into an array.
173: threads = new int[tempThreads.size()];
174: for (int i = 0; i < threads.length; i++) {
175: threads[i] = ((Integer) tempThreads.get(i)).intValue();
176: }
177: }
178:
179: protected DbUserMessagesIterator(DbForumFactory factory, User user,
180: Forum forum, int startIndex, int numResults) {
181: this .factory = factory;
182: this .forum = forum;
183: //We don't know how many results will be returned, so store them
184: //in an ArrayList.
185: int[] tempMessages = new int[numResults];
186: int[] tempThreads = new int[numResults];
187:
188: Connection con = null;
189: PreparedStatement pstmt = null;
190: int messageCount = 0;
191:
192: try {
193: con = DbConnectionManager.getConnection();
194: pstmt = con.prepareStatement(USER_MESSAGES);
195: pstmt.setInt(1, user.getID());
196: pstmt.setInt(2, forum.getID());
197: ResultSet rs = pstmt.executeQuery();
198: //Move to start of index
199: for (int i = 0; i < startIndex; i++) {
200: rs.next();
201: }
202: //Now read in desired number of results
203: for (int i = 0; i < numResults; i++) {
204: if (rs.next()) {
205: tempMessages[messageCount] = rs.getInt(1);
206: tempThreads[messageCount] = rs.getInt(2);
207: messageCount++;
208: } else {
209: break;
210: }
211: }
212: } catch (SQLException sqle) {
213: System.err
214: .println("Error in DbUserMessagesIterator (2): constructor()-"
215: + sqle);
216: } finally {
217: try {
218: pstmt.close();
219: } catch (Exception e) {
220: e.printStackTrace();
221: }
222: try {
223: con.close();
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: }
228: messages = new int[messageCount];
229: for (int i = 0; i < messageCount; i++) {
230: messages[i] = tempMessages[i];
231: }
232: threads = new int[messageCount];
233: for (int i = 0; i < messageCount; i++) {
234: threads[i] = tempThreads[i];
235: }
236: }
237:
238: /**
239: * Returns true if there are more users left to iteratate through forwards.
240: */
241: public boolean hasNext() {
242: return (currentIndex + 1 < messages.length);
243: }
244:
245: /**
246: * Returns true if there are more users left to iteratore through backwards.
247: */
248: public boolean hasPrevious() {
249: return (currentIndex > 0);
250: }
251:
252: /**
253: * Returns the next User.
254: */
255: public Object next() throws java.util.NoSuchElementException {
256: ForumMessage message = null;
257: currentIndex++;
258: if (currentIndex >= messages.length) {
259: throw new java.util.NoSuchElementException();
260: }
261: try {
262: message = forum.getThread(threads[currentIndex])
263: .getMessage(messages[currentIndex]);
264: } catch (ForumThreadNotFoundException ftnfe) {
265: ftnfe.printStackTrace();
266: } catch (ForumMessageNotFoundException fmnfe) {
267: fmnfe.printStackTrace();
268: }
269: return message;
270: }
271:
272: /**
273: * For security reasons, the add operation is not supported. Use
274: * ProfileManager instead.
275: *
276: * @see ProfileManager
277: */
278: public void add(Object o) throws UnsupportedOperationException {
279: throw new UnsupportedOperationException();
280: }
281:
282: /**
283: * For security reasons, the remove operation is not supported. Use
284: * ProfileManager instead.
285: *
286: * @see ProfileManager
287: */
288: public void remove() {
289: throw new UnsupportedOperationException();
290: }
291:
292: /**
293: * For security reasons, the set operation is not supported. Use
294: * ProfileManager instead.
295: *
296: * @see ProfileManager
297: */
298: public void set(Object o) throws UnsupportedOperationException {
299: throw new UnsupportedOperationException();
300: }
301:
302: /**
303: * Returns the index number that would be returned with a call to next().
304: */
305: public int nextIndex() {
306: return currentIndex + 1;
307: }
308:
309: /**
310: * Returns the previous user.
311: */
312: public Object previous() throws java.util.NoSuchElementException {
313: ForumMessage message = null;
314: currentIndex--;
315: if (currentIndex < 0) {
316: currentIndex++;
317: throw new java.util.NoSuchElementException();
318: }
319: try {
320: message = forum.getThread(threads[currentIndex])
321: .getMessage(messages[currentIndex]);
322: } catch (ForumThreadNotFoundException ftnfe) {
323: ftnfe.printStackTrace();
324: } catch (ForumMessageNotFoundException fmnfe) {
325: fmnfe.printStackTrace();
326: }
327: return message;
328: }
329:
330: /**
331: * Returns the index number that would be returned with a call to previous().
332: */
333: public int previousIndex() {
334: return currentIndex - 1;
335: }
336: }
|