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.Iterator;
108: import java.sql.*;
109:
110: import com.Yasna.forum.*;
111:
112: /**
113: * Database implementation of Iterator for ForumMesages in a ForumThread.
114: */
115: public class DbThreadIterator implements Iterator {
116:
117: /** DATABASE QUERIES **/
118: private static final String MESSAGE_COUNT = "SELECT count(messageID) FROM yazdMessage WHERE threadID=?";
119: private static final String GET_MESSAGES = "SELECT messageID, creationDate FROM yazdMessage WHERE threadID=? "
120: + "ORDER BY creationDate ASC";
121:
122: private ForumThread thread;
123: private int[] messages;
124: private int currentIndex = -1;
125:
126: private ForumMessage nextMessage = null;
127:
128: protected DbThreadIterator(ForumThread thread) {
129: this .thread = thread;
130: Connection con = null;
131: PreparedStatement pstmt = null;
132: try {
133: con = DbConnectionManager.getConnection();
134: pstmt = con.prepareStatement(MESSAGE_COUNT);
135: pstmt.setInt(1, thread.getID());
136: ResultSet rs = pstmt.executeQuery();
137: rs.next();
138: messages = new int[rs.getInt(1)];
139: pstmt.close();
140:
141: pstmt = con.prepareStatement(GET_MESSAGES);
142: pstmt.setInt(1, thread.getID());
143: rs = pstmt.executeQuery();
144: for (int i = 0; i < messages.length; i++) {
145: rs.next();
146: messages[i] = rs.getInt(1);
147: }
148: } catch (SQLException sqle) {
149: System.err
150: .println("Error in DbThreadIterator:constructor()-"
151: + sqle);
152: } finally {
153: try {
154: pstmt.close();
155: } catch (Exception e) {
156: e.printStackTrace();
157: }
158: try {
159: con.close();
160: } catch (Exception e) {
161: e.printStackTrace();
162: }
163: }
164: }
165:
166: protected DbThreadIterator(ForumThread thread, int startIndex,
167: int numResults) {
168: this .thread = thread;
169:
170: int[] tempMessages = new int[numResults];
171: //It's very possible that there might not be as many messages to get
172: //as we requested. Therefore, we keep track of how many messages we
173: //get by keeping a messageCount.
174: int messageCount = 0;
175:
176: Connection con = null;
177: PreparedStatement pstmt = null;
178:
179: try {
180: con = DbConnectionManager.getConnection();
181: pstmt = con.prepareStatement(GET_MESSAGES);
182: pstmt.setInt(1, thread.getID());
183: ResultSet rs = pstmt.executeQuery();
184: //Move to start of index
185: for (int i = 0; i < startIndex; i++) {
186: rs.next();
187: }
188: //Now read in desired number of results
189: for (int i = 0; i < numResults; i++) {
190: if (rs.next()) {
191: tempMessages[messageCount] = rs.getInt("messageID");
192: messageCount++;
193: } else {
194: break;
195: }
196: }
197: } catch (SQLException sqle) {
198: System.err
199: .println("Error in DbThreadIterator:constructor()-"
200: + sqle);
201: } finally {
202: try {
203: pstmt.close();
204: } catch (Exception e) {
205: e.printStackTrace();
206: }
207: try {
208: con.close();
209: } catch (Exception e) {
210: e.printStackTrace();
211: }
212: }
213: messages = new int[messageCount];
214: for (int i = 0; i < messageCount; i++) {
215: messages[i] = tempMessages[i];
216: }
217: }
218:
219: /**
220: * Returns true if there are more messages in the list.
221: *
222: * @return true if the iterator has more elements.
223: */
224: public boolean hasNext() {
225: //If we are at the end of the list, there can't be any more messages
226: //to iterate through.
227: if (currentIndex + 1 >= messages.length) {
228: return false;
229: }
230: return true;
231:
232: /*
233: BUG: this code calls getNextMessage() which will increment the
234: currentIndex variable as a result of calling this message!
235:
236: //Otherwise, see if nextMessage is null. If so, try to load the next
237: //message to make sure it exists.
238:
239: // hmm, do we really need to do this here? i think it should be left up
240: // to the next() method -- BL
241:
242: if (nextMessage == null) {
243: nextMessage = getNextMessage();
244: if (nextMessage == null) {
245: return false;
246: }
247: }
248: return true;
249: */
250: }
251:
252: /**
253: * Returns the next message in the interation.
254: *
255: * @return the next message in the interation.
256: * @throws NoSuchElementException if the iteration has no more elements.
257: */
258: public Object next() throws java.util.NoSuchElementException {
259: ForumMessage message = null;
260: if (nextMessage != null) {
261: message = nextMessage;
262: nextMessage = null;
263: } else {
264: message = getNextMessage();
265: if (message == null) {
266: throw new java.util.NoSuchElementException();
267: }
268: }
269: return message;
270: }
271:
272: /**
273: * Not supported for security reasons. Use the deleteMessage method in the
274: * ForumThread class instead.
275: *
276: * @see ForumThread#deleteMessage(ForumMessage)
277: */
278: public void remove() throws UnsupportedOperationException {
279: throw new UnsupportedOperationException();
280: }
281:
282: /**
283: * Returns the next available message, or null if there are no more
284: * messages to return.
285: */
286: private ForumMessage getNextMessage() {
287: while (currentIndex + 1 < messages.length) {
288: currentIndex++;
289: try {
290: ForumMessage message = thread
291: .getMessage(messages[currentIndex]);
292: return message;
293: } catch (ForumMessageNotFoundException mnfe) {
294: }
295: }
296: return null;
297: }
298: }
|