KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbForumFactoryIterator


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
30 import java.sql.SQLException JavaDoc;
31 import java.util.ArrayList JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import java.util.ListIterator JavaDoc;
34 import java.util.NoSuchElementException JavaDoc;
35
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.nemesis.forum.Forum;
39 import org.nemesis.forum.ForumFactory;
40 import org.nemesis.forum.util.jdbc.DbConnectionManager;
41 /**
42  * Iterator for all forums defined for a ForumFactory instance.
43  */

44 public class DbForumFactoryIterator implements Iterator JavaDoc, ListIterator JavaDoc {
45 static protected Log log = LogFactory.getLog(DbForumFactoryIterator.class);
46     /** DATABASE QUERIES **/
47     private static final String JavaDoc GET_FORUMS = "SELECT forumID FROM yazdForum";
48
49     private ForumFactory factory;
50     private int[] forums;
51     //The current index points to
52
int currentIndex = -1;
53
54     protected DbForumFactoryIterator(ForumFactory factory) {
55         this.factory = factory;
56         ArrayList JavaDoc allForums = new ArrayList JavaDoc();
57         Connection JavaDoc con = null;
58         PreparedStatement JavaDoc pstmt = null;
59         try {
60             con = DbConnectionManager.getConnection();
61             pstmt = con.prepareStatement(GET_FORUMS);
62             ResultSet JavaDoc rs = pstmt.executeQuery();
63             while (rs.next()) {
64                 allForums.add(new Integer JavaDoc(rs.getInt("forumID")));
65             }
66         } catch (SQLException JavaDoc sqle) {
67             log.error("Error in DbForumFactoryIterator:constructor()-" , sqle);
68         } finally {
69             try {
70                 pstmt.close();
71             } catch (Exception JavaDoc e) {
72                 log.error("" , e);
73             }
74             try {
75                 con.close();
76             } catch (Exception JavaDoc e) {
77                 log.error("" , e);
78             }
79         }
80         //Now, elimiante all forums the user doesn't have read access to.
81
/* for (int i=0; i<allForums.size(); i++) {
82                    int tempID = ((Integer)allForums.get(i)).intValue();
83                    try {
84                        Forum tempForum = factory.getForum(tempID);
85                    }
86                    catch (Exception ee) {
87                        log(ee);
88                        allForums.remove(i);
89                    }
90                 } */

91
92         //Now, put in array
93
forums = new int[allForums.size()];
94         for (int i = 0; i < forums.length; i++) {
95             forums[i] = ((Integer JavaDoc) allForums.get(i)).intValue();
96         }
97     }
98
99     /**
100      * Returns true if there are more forums left to iteratate through.
101      */

102     public boolean hasNext() {
103         return (currentIndex + 1 < forums.length);
104     }
105
106     /**
107      * Returns the next Forum the user has READ access for.
108      */

109     public Object JavaDoc next() throws NoSuchElementException JavaDoc {
110         Forum forum = null;
111         currentIndex++;
112         if (currentIndex >= forums.length) {
113             throw new java.util.NoSuchElementException JavaDoc();
114         }
115         try {
116             forum = factory.getForum(forums[currentIndex]);
117         } catch (Exception JavaDoc e) {
118             log.error("" , e);
119         }
120         return forum;
121     }
122
123     /**
124      * For security reasons, the remove operation is not supported. Use
125      * ForumFactory.deleteForum() instead.
126      *
127      * @see ForumFactory
128      */

129     public void remove() {
130         throw new UnsupportedOperationException JavaDoc();
131     }
132
133     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
134         throw new UnsupportedOperationException JavaDoc();
135     }
136
137     public boolean hasPrevious() {
138         return (currentIndex > 0);
139     }
140
141     public int nextIndex() {
142         return currentIndex + 1;
143     }
144
145     public Object JavaDoc previous() throws NoSuchElementException JavaDoc {
146         Forum forum = null;
147         currentIndex--;
148         if (currentIndex < 0) {
149             currentIndex++;
150             throw new NoSuchElementException JavaDoc();
151         }
152         try {
153             forum = factory.getForum(forums[currentIndex]);
154         } catch (Exception JavaDoc e) {
155             log.error("" , e);
156         }
157         return forum;
158     }
159
160     public int previousIndex() {
161         return currentIndex - 1;
162     }
163
164     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
165         throw new UnsupportedOperationException JavaDoc();
166     }
167 }
168
Popular Tags