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: */package com.Yasna.forum.database;
053:
054: import java.util.*; //JDK1.1// import com.sun.java.util.collections.*;
055: import java.sql.*;
056:
057: import com.Yasna.util.*;
058: import com.Yasna.forum.*;
059:
060: /**
061: * Database implementation to iterate through forumGroups in a Category.
062: */
063: public class DbForumGroupIterator implements Iterator, ListIterator {
064:
065: /** DATABASE QUERIES **/
066: private static final String GET_FORUM_GROUPS = "SELECT forumGroupID, creationDate FROM yazdForumGroup WHERE categoryID=? "
067: + "ORDER BY grporder DESC";
068:
069: //A reference to the Category object that the iterator was created from.
070: //This is used to load forumGroup objects.
071: private DbCategory category;
072: //maintain an array of forumgroup ids to iterator through.
073: private int[] forumGroups;
074: //points to the current forumgroup id that the user has iterated to.
075: private int currentIndex = -1;
076:
077: DbForumFactory factory;
078:
079: public DbForumGroupIterator(DbCategory category,
080: DbForumFactory factory) {
081: this .category = category;
082: this .factory = factory;
083: //We don't know how many results will be returned, so store them
084: //in an ArrayList.
085: ArrayList tempForumGroups = new ArrayList();
086: Connection con = null;
087: PreparedStatement pstmt = null;
088: try {
089: con = DbConnectionManager.getConnection();
090: pstmt = con.prepareStatement(GET_FORUM_GROUPS);
091: pstmt.setInt(1, category.getID());
092: ResultSet rs = pstmt.executeQuery();
093:
094: while (rs.next()) {
095: tempForumGroups.add(new Integer(rs
096: .getInt("forumGroupID")));
097: }
098: } catch (SQLException sqle) {
099: System.err
100: .println("Error in DbForumGroupIterator:constructor()-"
101: + sqle);
102: } finally {
103: try {
104: pstmt.close();
105: } catch (Exception e) {
106: e.printStackTrace();
107: }
108: try {
109: con.close();
110: } catch (Exception e) {
111: e.printStackTrace();
112: }
113: }
114: forumGroups = new int[tempForumGroups.size()];
115: for (int i = 0; i < forumGroups.length; i++) {
116: forumGroups[i] = ((Integer) tempForumGroups.get(i))
117: .intValue();
118: }
119: }
120:
121: public void add(Object o) throws UnsupportedOperationException {
122: throw new UnsupportedOperationException();
123: }
124:
125: public boolean hasNext() {
126: return (currentIndex + 1 < forumGroups.length);
127: }
128:
129: public boolean hasPrevious() {
130: return (currentIndex > 0);
131: }
132:
133: public Object next() throws java.util.NoSuchElementException {
134: ForumGroup forumGroup = null;
135: currentIndex++;
136: if (currentIndex >= forumGroups.length) {
137: currentIndex--;
138: throw new java.util.NoSuchElementException();
139: }
140: try {
141: forumGroup = category
142: .getForumGroup(forumGroups[currentIndex]);
143: } catch (ForumGroupNotFoundException tnfe) {
144: System.err.println(tnfe);
145: }
146: return forumGroup;
147: }
148:
149: public int nextIndex() {
150: return currentIndex + 1;
151: }
152:
153: public Object previous() throws java.util.NoSuchElementException {
154: ForumGroup forumGroup = null;
155: currentIndex--;
156: if (currentIndex < 0) {
157: currentIndex++;
158: throw new java.util.NoSuchElementException();
159: }
160: try {
161: forumGroup = category
162: .getForumGroup(forumGroups[currentIndex]);
163: } catch (ForumGroupNotFoundException tnfe) {
164: System.err.println(tnfe);
165: }
166: return forumGroup;
167: }
168:
169: public int previousIndex() {
170: return currentIndex - 1;
171: }
172:
173: public void remove() throws UnsupportedOperationException {
174: throw new UnsupportedOperationException();
175: }
176:
177: public void set(Object o) throws UnsupportedOperationException {
178: throw new UnsupportedOperationException();
179: }
180: }
|