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: import com.Yasna.forum.*;
057:
058: /**
059: * Iterates through all the groups in the system.
060: */
061: public class DbGroupIterator implements Iterator, ListIterator {
062:
063: /** DATABASE QUERIES **/
064: private static final String ALL_GROUPS = "SELECT groupID from yazdGroup";
065:
066: private int currentIndex = -1;
067: private int[] groups;
068:
069: private ProfileManager profileManager;
070:
071: protected DbGroupIterator(ProfileManager profileManager) {
072: this .profileManager = profileManager;
073: //We don't know how many results will be returned, so store them
074: //in an ArrayList.
075: ArrayList tempGroups = new ArrayList();
076: Connection con = null;
077: PreparedStatement pstmt = null;
078: try {
079: con = DbConnectionManager.getConnection();
080: pstmt = con.prepareStatement(ALL_GROUPS);
081: ResultSet rs = pstmt.executeQuery();
082: while (rs.next()) {
083: tempGroups.add(new Integer(rs.getInt("groupID")));
084: }
085: } catch (SQLException sqle) {
086: System.err
087: .println("Error in DbGroupIterator:constructor()-"
088: + sqle);
089: } finally {
090: try {
091: pstmt.close();
092: } catch (Exception e) {
093: e.printStackTrace();
094: }
095: try {
096: con.close();
097: } catch (Exception e) {
098: e.printStackTrace();
099: }
100: }
101: groups = new int[tempGroups.size()];
102: for (int i = 0; i < groups.length; i++) {
103: groups[i] = ((Integer) tempGroups.get(i)).intValue();
104: }
105: }
106:
107: protected DbGroupIterator(ProfileManager profileManager,
108: int startIndex, int numResults) {
109: this .profileManager = profileManager;
110:
111: int[] tempResults = new int[numResults];
112: //It's very possible that there might not be as many results to get
113: //as we requested. Therefore, we keep track of how many results we
114: //get by keeping a count.
115: int resultCount = 0;
116:
117: Connection con = null;
118: PreparedStatement pstmt = null;
119:
120: try {
121: con = DbConnectionManager.getConnection();
122: pstmt = con.prepareStatement(ALL_GROUPS);
123: ResultSet rs = pstmt.executeQuery();
124: //Move to start of index
125: for (int i = 0; i < startIndex; i++) {
126: rs.next();
127: }
128: //Now read in desired number of results
129: for (int i = 0; i < numResults; i++) {
130: if (rs.next()) {
131: tempResults[resultCount] = rs.getInt("groupID");
132: resultCount++;
133: } else {
134: break;
135: }
136: }
137: } catch (SQLException sqle) {
138: System.err
139: .println("Error in DbGroupIterator:constructor()-"
140: + sqle);
141: } finally {
142: try {
143: pstmt.close();
144: } catch (Exception e) {
145: e.printStackTrace();
146: }
147: try {
148: con.close();
149: } catch (Exception e) {
150: e.printStackTrace();
151: }
152: }
153: groups = new int[resultCount];
154: for (int i = 0; i < resultCount; i++) {
155: groups[i] = tempResults[i];
156: }
157: }
158:
159: /**
160: * Returns true if there are more groups left to iteratate through.
161: */
162: public boolean hasNext() {
163: return (currentIndex + 1 < groups.length);
164: }
165:
166: /**
167: * Returns the next Group.
168: */
169: public Object next() throws java.util.NoSuchElementException {
170: Group group = null;
171: currentIndex++;
172: if (currentIndex >= groups.length) {
173: throw new java.util.NoSuchElementException();
174: }
175: try {
176: group = profileManager.getGroup(groups[currentIndex]);
177: } catch (GroupNotFoundException gnfe) {
178: System.err.println(gnfe);
179: }
180: return group;
181: }
182:
183: /**
184: * For security reasons, the remove operation is not supported. Use
185: * ProfileManager.deleteGroup() instead.
186: *
187: * @see ProfileManager
188: */
189: public void remove() {
190: throw new UnsupportedOperationException();
191: }
192:
193: /**
194: * Returns true if there are more groups left to iterate through backwards.
195: */
196: public boolean hasPrevious() {
197: return (currentIndex > 0);
198: }
199:
200: /**
201: * For security reasons, the add operation is not supported. Use
202: * ProfileManager instead.
203: *
204: * @see ProfileManager
205: */
206: public void add(Object o) throws UnsupportedOperationException {
207: throw new UnsupportedOperationException();
208: }
209:
210: /**
211: * For security reasons, the set operation is not supported. Use
212: * ProfileManager instead.
213: *
214: * @see ProfileManager
215: */
216: public void set(Object o) throws UnsupportedOperationException {
217: throw new UnsupportedOperationException();
218: }
219:
220: /**
221: * Returns the index number that would be returned with a call to next().
222: */
223: public int nextIndex() {
224: return currentIndex + 1;
225: }
226:
227: /**
228: * Returns the previous group.
229: */
230: public Object previous() throws java.util.NoSuchElementException {
231: Group group = null;
232: currentIndex--;
233: if (currentIndex < 0) {
234: currentIndex++;
235: throw new java.util.NoSuchElementException();
236: }
237: try {
238: group = profileManager.getGroup(groups[currentIndex]);
239: } catch (GroupNotFoundException gnfe) {
240: System.err.println(gnfe);
241: }
242: return group;
243: }
244:
245: /**
246: * Returns the index number that would be returned with a call to previous().
247: */
248: public int previousIndex() {
249: return currentIndex - 1;
250: }
251: }
|