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:
110: import com.Yasna.util.*;
111: import com.Yasna.forum.*;
112:
113: /**
114: * Database implementation to iterate through threads in a forum.
115: * At the moment, threads are always ordered by creationDate. Obviously,
116: * various skins will probably want different iterators. The solution will
117: * probably be to pass in various flags to forum.iterator() in order to get
118: * different types of iterators. That will require small changes to this
119: * class.
120: */
121: public class DbForumIterator implements Iterator, ListIterator {
122:
123: /** DATABASE QUERIES **/
124: private static final String GET_THREADS_SORT_BY_CREATION_DATE = "SELECT threadID, creationDate FROM yazdThread WHERE forumID=? "
125: + "ORDER BY sticky desc,creationDate DESC";
126: private static final String GET_THREADS_SORT_BY_MODIFIED_DATE = "SELECT threadID, modifiedDate FROM yazdThread WHERE forumID=? "
127: + "ORDER BY sticky desc,modifiedDate DESC";
128:
129: //A reference to the forum object that the iterator was created from.
130: //This is used to load thread objects.
131: private DbForum forum;
132: //maintain an array of thread ids to iterator through.
133: private int[] threads;
134: //points to the current thread id that the user has iterated to.
135: private int currentIndex = -1;
136:
137: DbForumFactory factory;
138:
139: public DbForumIterator(DbForum forum, DbForumFactory factory) {
140: this .forum = forum;
141: this .factory = factory;
142: //We don't know how many results will be returned, so store them
143: //in an ArrayList.
144: ArrayList tempThreads = new ArrayList();
145: Connection con = null;
146: PreparedStatement pstmt = null;
147: try {
148: con = DbConnectionManager.getConnection();
149: pstmt = con
150: .prepareStatement(GET_THREADS_SORT_BY_CREATION_DATE);
151: pstmt.setInt(1, forum.getID());
152: ResultSet rs = pstmt.executeQuery();
153:
154: while (rs.next()) {
155: tempThreads.add(new Integer(rs.getInt("threadID")));
156: }
157: } catch (SQLException sqle) {
158: System.err
159: .println("Error in DbThreadIterator:constructor()-"
160: + sqle);
161: } finally {
162: try {
163: pstmt.close();
164: } catch (Exception e) {
165: e.printStackTrace();
166: }
167: try {
168: con.close();
169: } catch (Exception e) {
170: e.printStackTrace();
171: }
172: }
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: public DbForumIterator(DbForum forum, DbForumFactory factory,
180: int startIndex, int numResults, int sortBy) {
181: this .forum = forum;
182: this .factory = factory;
183:
184: int[] tempThreads = new int[numResults];
185: //It's very possible that there might not be as many threads to get
186: //as we requested. Therefore, we keep track of how many threads we
187: //get by keeping a threadCount.
188: int threadCount = 0;
189:
190: Connection con = null;
191: PreparedStatement pstmt = null;
192: try {
193: con = DbConnectionManager.getConnection();
194: if (Forum.SORT_BY_CREATE_DATE == sortBy) {
195: pstmt = con
196: .prepareStatement(GET_THREADS_SORT_BY_CREATION_DATE);
197: } else {
198: pstmt = con
199: .prepareStatement(GET_THREADS_SORT_BY_MODIFIED_DATE);
200: }
201: pstmt.setInt(1, forum.getID());
202: ResultSet rs = pstmt.executeQuery();
203:
204: //Move to start of index
205: for (int i = 0; i < startIndex; i++) {
206: rs.next();
207: }
208: //Now read in desired number of results
209: for (int i = 0; i < numResults; i++) {
210: if (rs.next()) {
211: tempThreads[threadCount] = rs.getInt("threadID");
212: threadCount++;
213: } else {
214: break;
215: }
216: }
217: } catch (SQLException sqle) {
218: System.err
219: .println("Error in DbThreadIterator:constructor()-"
220: + sqle);
221: } finally {
222: try {
223: pstmt.close();
224: } catch (Exception e) {
225: e.printStackTrace();
226: }
227: try {
228: con.close();
229: } catch (Exception e) {
230: e.printStackTrace();
231: }
232: }
233: threads = new int[threadCount];
234: for (int i = 0; i < threadCount; i++) {
235: threads[i] = tempThreads[i];
236: }
237: }
238:
239: public void add(Object o) throws UnsupportedOperationException {
240: throw new UnsupportedOperationException();
241: }
242:
243: public boolean hasNext() {
244: return (currentIndex + 1 < threads.length);
245: }
246:
247: public boolean hasPrevious() {
248: return (currentIndex > 0);
249: }
250:
251: public Object next() throws java.util.NoSuchElementException {
252: ForumThread thread = null;
253: currentIndex++;
254: if (currentIndex >= threads.length) {
255: currentIndex--;
256: throw new java.util.NoSuchElementException();
257: }
258: try {
259: thread = forum.getThread(threads[currentIndex]);
260: } catch (ForumThreadNotFoundException tnfe) {
261: System.err.println(tnfe);
262: }
263: return thread;
264: }
265:
266: public int nextIndex() {
267: return currentIndex + 1;
268: }
269:
270: public Object previous() throws java.util.NoSuchElementException {
271: ForumThread thread = null;
272: currentIndex--;
273: if (currentIndex < 0) {
274: currentIndex++;
275: throw new java.util.NoSuchElementException();
276: }
277: try {
278: thread = forum.getThread(threads[currentIndex]);
279: } catch (ForumThreadNotFoundException tnfe) {
280: System.err.println(tnfe);
281: }
282: return thread;
283: }
284:
285: public int previousIndex() {
286: return currentIndex - 1;
287: }
288:
289: public void remove() throws UnsupportedOperationException {
290: throw new UnsupportedOperationException();
291: }
292:
293: public void set(Object o) throws UnsupportedOperationException {
294: throw new UnsupportedOperationException();
295: }
296: }
|