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 com.Yasna.forum.*;
108: import com.Yasna.util.*;
109: import java.util.*; //JDK1.1// import com.sun.java.util.collections.*;
110: import java.sql.*;
111:
112: /**
113: * Iterator for all forums defined for a ForumFactory instance.
114: */
115: public class DbForumFactoryIterator implements Iterator, ListIterator {
116:
117: /** DATABASE QUERIES **/
118: private static final String GET_FORUMS = "SELECT forumID FROM yazdForum";
119: private static final String GET_FORUMS_BY_FORUM_GROUP_ID = "SELECT forumID FROM yazdForum WHERE forumGroupID = ? order by forumorder desc";
120:
121: private ForumFactory factory;
122: private int[] forums;
123: //The current index points to
124: int currentIndex = -1;
125:
126: protected DbForumFactoryIterator(ForumFactory factory) {
127: this .factory = factory;
128: ArrayList allForums = new ArrayList();
129: Connection con = null;
130: PreparedStatement pstmt = null;
131: try {
132: con = DbConnectionManager.getConnection();
133: pstmt = con.prepareStatement(GET_FORUMS);
134: ResultSet rs = pstmt.executeQuery();
135: while (rs.next()) {
136: allForums.add(new Integer(rs.getInt("forumID")));
137: }
138: } catch (SQLException sqle) {
139: System.err
140: .println("Error in DbForumFactoryIterator:constructor()-"
141: + sqle);
142: } finally {
143: try {
144: pstmt.close();
145: } catch (Exception e) {
146: e.printStackTrace();
147: }
148: try {
149: con.close();
150: } catch (Exception e) {
151: e.printStackTrace();
152: }
153: }
154: //Now, elimiante all forums the user doesn't have read access to.
155: /* for (int i=0; i<allForums.size(); i++) {
156: int tempID = ((Integer)allForums.get(i)).intValue();
157: try {
158: Forum tempForum = factory.getForum(tempID);
159: }
160: catch (Exception ee) {
161: System.err.println(ee);
162: allForums.remove(i);
163: }
164: } */
165:
166: //Now, put in array
167: forums = new int[allForums.size()];
168: for (int i = 0; i < forums.length; i++) {
169: forums[i] = ((Integer) allForums.get(i)).intValue();
170: }
171: }
172:
173: protected DbForumFactoryIterator(DbForumGroup forumGroup,
174: ForumFactory factory) {
175: this .factory = factory;
176: ArrayList allForums = new ArrayList();
177: Connection con = null;
178: PreparedStatement pstmt = null;
179: try {
180: con = DbConnectionManager.getConnection();
181: pstmt = con.prepareStatement(GET_FORUMS_BY_FORUM_GROUP_ID);
182: pstmt.setInt(1, forumGroup.getID());
183: ResultSet rs = pstmt.executeQuery();
184: while (rs.next()) {
185: allForums.add(new Integer(rs.getInt("forumID")));
186: }
187: } catch (SQLException sqle) {
188: System.err
189: .println("Error in DbForumFactoryIterator:constructor()-"
190: + sqle);
191: } finally {
192: try {
193: pstmt.close();
194: } catch (Exception e) {
195: e.printStackTrace();
196: }
197: try {
198: con.close();
199: } catch (Exception e) {
200: e.printStackTrace();
201: }
202: }
203:
204: //Now, put in array
205: forums = new int[allForums.size()];
206: for (int i = 0; i < forums.length; i++) {
207: forums[i] = ((Integer) allForums.get(i)).intValue();
208: }
209: }
210:
211: /**
212: * Returns true if there are more forums left to iteratate through.
213: */
214: public boolean hasNext() {
215: return (currentIndex + 1 < forums.length);
216: }
217:
218: /**
219: * Returns the next Forum the user has READ access for.
220: */
221: public Object next() throws java.util.NoSuchElementException {
222: Forum forum = null;
223: currentIndex++;
224: if (currentIndex >= forums.length) {
225: throw new java.util.NoSuchElementException();
226: }
227: try {
228: forum = factory.getForum(forums[currentIndex]);
229: } catch (Exception e) {
230: e.printStackTrace();
231: }
232: return forum;
233: }
234:
235: /**
236: * For security reasons, the remove operation is not supported. Use
237: * ForumFactory.deleteForum() instead.
238: *
239: * @see ForumFactory
240: */
241: public void remove() {
242: throw new UnsupportedOperationException();
243: }
244:
245: public void add(Object o) throws UnsupportedOperationException {
246: throw new UnsupportedOperationException();
247: }
248:
249: public boolean hasPrevious() {
250: return (currentIndex > 0);
251: }
252:
253: public int nextIndex() {
254: return currentIndex + 1;
255: }
256:
257: public Object previous() throws java.util.NoSuchElementException {
258: Forum forum = null;
259: currentIndex--;
260: if (currentIndex < 0) {
261: currentIndex++;
262: throw new java.util.NoSuchElementException();
263: }
264: try {
265: forum = factory.getForum(forums[currentIndex]);
266: } catch (Exception e) {
267: e.printStackTrace();
268: }
269: return forum;
270: }
271:
272: public int previousIndex() {
273: return currentIndex - 1;
274: }
275:
276: public void set(Object o) throws UnsupportedOperationException {
277: throw new UnsupportedOperationException();
278: }
279: }
|