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.Forum;
039: import org.nemesis.forum.Message;
040: import org.nemesis.forum.User;
041: import org.nemesis.forum.exception.NotFoundException;
042: import org.nemesis.forum.util.jdbc.DbConnectionManager;
043:
044: /**
045: * An Iterator for all the user's Messages in a forum.
046: */
047: public class DbUserMessagesIterator implements Iterator, ListIterator {
048: static protected Log log = LogFactory
049: .getLog(DbUserMessagesIterator.class);
050: /** DATABASE QUERIES **/
051: private static final String USER_MESSAGES = "SELECT messageID, yazdMessage.threadID FROM yazdMessage,yazdForum,yazdThread WHERE "
052: + "yazdMessage.userID=? AND yazdForum.forumID=? AND "
053: + "yazdThread.forumID=yazdForum.forumID AND "
054: + "yazdMessage.threadID=yazdThread.threadID";
055:
056: private int currentIndex = -1;
057: private int[] messages;
058: //The threads that correspond to each message.
059: private int[] threads;
060: private Forum forum;
061:
062: private DbForumFactory factory;
063:
064: protected DbUserMessagesIterator(DbForumFactory factory, User user,
065: Forum forum) {
066: this .factory = factory;
067: this .forum = forum;
068: //We don't know how many results will be returned, so store them
069: //in an ArrayList.
070: ArrayList tempMessages = new ArrayList();
071: ArrayList tempThreads = new ArrayList();
072: Connection con = null;
073: PreparedStatement pstmt = null;
074: try {
075: con = DbConnectionManager.getConnection();
076: pstmt = con.prepareStatement(USER_MESSAGES);
077: pstmt.setInt(1, user.getID());
078: pstmt.setInt(2, forum.getID());
079: ResultSet rs = pstmt.executeQuery();
080: while (rs.next()) {
081: tempMessages.add(new Integer(rs.getInt(1)));
082: tempThreads.add(new Integer(rs.getInt(2)));
083: }
084: } catch (SQLException sqle) {
085: log.error("Error in DbUserMessagesIterator:constructor()-",
086: sqle);
087:
088: } finally {
089: try {
090: pstmt.close();
091: } catch (Exception e) {
092: log.error("", e);
093: }
094: try {
095: con.close();
096: } catch (Exception e) {
097: log.error("", e);
098: }
099: }
100: //Now copy into an array.
101: messages = new int[tempMessages.size()];
102: for (int i = 0; i < messages.length; i++) {
103: messages[i] = ((Integer) tempMessages.get(i)).intValue();
104: }
105: //Now copy into an array.
106: threads = new int[tempThreads.size()];
107: for (int i = 0; i < threads.length; i++) {
108: threads[i] = ((Integer) tempThreads.get(i)).intValue();
109: }
110: }
111:
112: /**
113: * Returns true if there are more users left to iteratate through forwards.
114: */
115: public boolean hasNext() {
116: return (currentIndex + 1 < messages.length);
117: }
118:
119: /**
120: * Returns true if there are more users left to iteratore through backwards.
121: */
122: public boolean hasPrevious() {
123: return (currentIndex > 0);
124: }
125:
126: /**
127: * Returns the next User.
128: */
129: public Object next() throws java.util.NoSuchElementException {
130: Message message = null;
131: currentIndex++;
132: if (currentIndex >= messages.length) {
133: throw new java.util.NoSuchElementException();
134: }
135: try {
136: message = forum.getThread(threads[currentIndex])
137: .getMessage(messages[currentIndex]);
138: } catch (NotFoundException fmnfe) {
139: log.error("", fmnfe);
140: }
141: return message;
142: }
143:
144: /**
145: * For security reasons, the add operation is not supported. Use
146: * ProfileManager instead.
147: *
148: * @see ProfileManager
149: */
150: public void add(Object o) throws UnsupportedOperationException {
151: throw new UnsupportedOperationException();
152: }
153:
154: /**
155: * For security reasons, the remove operation is not supported. Use
156: * ProfileManager instead.
157: *
158: * @see ProfileManager
159: */
160: public void remove() {
161: throw new UnsupportedOperationException();
162: }
163:
164: /**
165: * For security reasons, the set operation is not supported. Use
166: * ProfileManager instead.
167: *
168: * @see ProfileManager
169: */
170: public void set(Object o) throws UnsupportedOperationException {
171: throw new UnsupportedOperationException();
172: }
173:
174: /**
175: * Returns the index number that would be returned with a call to next().
176: */
177: public int nextIndex() {
178: return currentIndex + 1;
179: }
180:
181: /**
182: * Returns the previous user.
183: */
184: public Object previous() throws NoSuchElementException {
185: Message message = null;
186: currentIndex--;
187: if (currentIndex < 0) {
188: currentIndex++;
189: throw new java.util.NoSuchElementException();
190: }
191: try {
192: message = forum.getThread(threads[currentIndex])
193: .getMessage(messages[currentIndex]);
194: } catch (NotFoundException fmnfe) {
195: log.error("", fmnfe);
196: }
197: return message;
198: }
199:
200: /**
201: * Returns the index number that would be returned with a call to previous().
202: */
203: public int previousIndex() {
204: return currentIndex - 1;
205: }
206: }
|