001: /*
002: * NEMESIS-FORUM.
003: * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
004: *
005: * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
006: *
007: * Copyright (C) 2001 Yasna.com. All rights reserved.
008: *
009: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
010: *
011: * NEMESIS-FORUM. is free software; you can redistribute it and/or
012: * modify it under the terms of the Apache Software License, Version 1.1,
013: * or (at your option) any later version.
014: *
015: * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
016: * application are parts of NEMESIS-FORUM and are distributed under
017: * same terms of licence.
018: *
019: *
020: * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
021: * and software developed by CoolServlets.com (http://www.coolservlets.com).
022: * and software developed by Yasna.com (http://www.yasna.com).
023: *
024: */
025: package org.nemesis.forum.impl;
026:
027: import java.sql.Connection;
028: import java.sql.PreparedStatement;
029: import java.sql.ResultSet;
030: import java.sql.SQLException;
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.ListIterator;
034: import java.util.NoSuchElementException;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038: import org.nemesis.forum.Group;
039: import org.nemesis.forum.ProfileManager;
040: import org.nemesis.forum.exception.GroupNotFoundException;
041: import org.nemesis.forum.util.jdbc.DbConnectionManager;
042:
043: /**
044: * Iterates through all the groups in the system.
045: */
046: public class DbGroupIterator implements Iterator, ListIterator {
047: static protected Log log = LogFactory.getLog(DbGroupIterator.class);
048: /** DATABASE QUERIES **/
049: private static final String ALL_GROUPS = "SELECT groupID from yazdGroup";
050:
051: private int currentIndex = -1;
052: private int[] groups;
053:
054: private ProfileManager profileManager;
055:
056: protected DbGroupIterator(ProfileManager profileManager) {
057: this .profileManager = profileManager;
058: //We don't know how many results will be returned, so store them
059: //in an ArrayList.
060: ArrayList tempGroups = new ArrayList();
061: Connection con = null;
062: PreparedStatement pstmt = null;
063: try {
064: con = DbConnectionManager.getConnection();
065: pstmt = con.prepareStatement(ALL_GROUPS);
066: ResultSet rs = pstmt.executeQuery();
067: while (rs.next()) {
068: tempGroups.add(new Integer(rs.getInt("groupID")));
069: }
070: } catch (SQLException sqle) {
071: log.error("Error in DbGroupIterator:constructor()-", sqle);
072: } finally {
073: try {
074: pstmt.close();
075: } catch (Exception e) {
076: log.error("", e);
077: }
078: try {
079: con.close();
080: } catch (Exception e) {
081: log.error("", e);
082: }
083: }
084: groups = new int[tempGroups.size()];
085: for (int i = 0; i < groups.length; i++) {
086: groups[i] = ((Integer) tempGroups.get(i)).intValue();
087: }
088: }
089:
090: protected DbGroupIterator(ProfileManager profileManager,
091: int startIndex, int numResults) {
092: this .profileManager = profileManager;
093:
094: int[] tempResults = new int[numResults];
095: //It's very possible that there might not be as many results to get
096: //as we requested. Therefore, we keep track of how many results we
097: //get by keeping a count.
098: int resultCount = 0;
099:
100: Connection con = null;
101: PreparedStatement pstmt = null;
102:
103: try {
104: con = DbConnectionManager.getConnection();
105: pstmt = con.prepareStatement(ALL_GROUPS);
106: ResultSet rs = pstmt.executeQuery();
107: //Move to start of index
108: for (int i = 0; i < startIndex; i++) {
109: rs.next();
110: }
111: //Now read in desired number of results
112: for (int i = 0; i < numResults; i++) {
113: if (rs.next()) {
114: tempResults[resultCount] = rs.getInt("groupID");
115: resultCount++;
116: } else {
117: break;
118: }
119: }
120: } catch (SQLException sqle) {
121: log.error("Error in DbGroupIterator:constructor()-", sqle);
122: } finally {
123: try {
124: pstmt.close();
125: } catch (Exception e) {
126: log.error("", e);
127: }
128: try {
129: con.close();
130: } catch (Exception e) {
131: log.error("", e);
132: }
133: }
134: groups = new int[resultCount];
135: for (int i = 0; i < resultCount; i++) {
136: groups[i] = tempResults[i];
137: }
138: }
139:
140: /**
141: * Returns true if there are more groups left to iteratate through.
142: */
143: public boolean hasNext() {
144: return (currentIndex + 1 < groups.length);
145: }
146:
147: /**
148: * Returns the next Group.
149: */
150: public Object next() throws NoSuchElementException {
151: Group group = null;
152: currentIndex++;
153: if (currentIndex >= groups.length) {
154: throw new java.util.NoSuchElementException();
155: }
156: try {
157: group = profileManager.getGroup(groups[currentIndex]);
158: } catch (GroupNotFoundException gnfe) {
159: log.error("", gnfe);
160: }
161: return group;
162: }
163:
164: /**
165: * For security reasons, the remove operation is not supported. Use
166: * ProfileManager.deleteGroup() instead.
167: *
168: * @see ProfileManager
169: */
170: public void remove() {
171: throw new UnsupportedOperationException();
172: }
173:
174: /**
175: * Returns true if there are more groups left to iterate through backwards.
176: */
177: public boolean hasPrevious() {
178: return (currentIndex > 0);
179: }
180:
181: /**
182: * For security reasons, the add operation is not supported. Use
183: * ProfileManager instead.
184: *
185: * @see ProfileManager
186: */
187: public void add(Object o) throws UnsupportedOperationException {
188: throw new UnsupportedOperationException();
189: }
190:
191: /**
192: * For security reasons, the set operation is not supported. Use
193: * ProfileManager instead.
194: *
195: * @see ProfileManager
196: */
197: public void set(Object o) throws UnsupportedOperationException {
198: throw new UnsupportedOperationException();
199: }
200:
201: /**
202: * Returns the index number that would be returned with a call to next().
203: */
204: public int nextIndex() {
205: return currentIndex + 1;
206: }
207:
208: /**
209: * Returns the previous group.
210: */
211: public Object previous() throws java.util.NoSuchElementException {
212: Group group = null;
213: currentIndex--;
214: if (currentIndex < 0) {
215: currentIndex++;
216: throw new java.util.NoSuchElementException();
217: }
218: try {
219: group = profileManager.getGroup(groups[currentIndex]);
220: } catch (GroupNotFoundException gnfe) {
221: log.error("", gnfe);
222: }
223: return group;
224: }
225:
226: /**
227: * Returns the index number that would be returned with a call to previous().
228: */
229: public int previousIndex() {
230: return currentIndex - 1;
231: }
232: }
|