KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > forum > database > DbThreadTypeIterator


1 package com.Yasna.forum.database;
2
3
4 import com.Yasna.forum.ThreadType;
5 import com.Yasna.forum.Exceptions.ThreadTypeNotFoundException;
6
7 import java.util.Iterator JavaDoc;
8 import java.util.ListIterator JavaDoc;
9 import java.util.ArrayList JavaDoc;
10 import java.sql.Connection JavaDoc;
11 import java.sql.PreparedStatement JavaDoc;
12 import java.sql.ResultSet JavaDoc;
13 import java.sql.SQLException JavaDoc;
14
15 /**
16  * Copyright (C) 2001 Yasna.com. All rights reserved.
17  *
18  * ===================================================================
19  * The Apache Software License, Version 1.1
20  *
21  * Redistribution and use in source and binary forms, with or without
22  * modification, are permitted provided that the following conditions
23  * are met:
24  *
25  * 1. Redistributions of source code must retain the above copyright
26  * notice, this list of conditions and the following disclaimer.
27  *
28  * 2. Redistributions in binary form must reproduce the above copyright
29  * notice, this list of conditions and the following disclaimer in
30  * the documentation and/or other materials provided with the
31  * distribution.
32  *
33  * 3. The end-user documentation included with the redistribution,
34  * if any, must include the following acknowledgment:
35  * "This product includes software developed by
36  * Yasna.com (http://www.yasna.com)."
37  * Alternately, this acknowledgment may appear in the software itself,
38  * if and wherever such third-party acknowledgments normally appear.
39  *
40  * 4. The names "Yazd" and "Yasna.com" must not be used to
41  * endorse or promote products derived from this software without
42  * prior written permission. For written permission, please
43  * contact yazd@yasna.com.
44  *
45  * 5. Products derived from this software may not be called "Yazd",
46  * nor may "Yazd" appear in their name, without prior written
47  * permission of Yasna.com.
48  *
49  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
50  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
51  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
52  * DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
53  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
54  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
55  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
56  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
58  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
59  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60  * SUCH DAMAGE.
61  * ====================================================================
62  *
63  * This software consists of voluntary contributions made by many
64  * individuals on behalf of Yasna.com. For more information
65  * on Yasna.com, please see <http://www.yasna.com>.
66  */

67 public class DbThreadTypeIterator implements Iterator JavaDoc, ListIterator JavaDoc {
68     private static final String JavaDoc ALL_TYPES = "SELECT typeID from yazdThreadType";
69
70     private int currentIndex = -1;
71     private int [] types;
72     private DbForumFactory factory;
73     protected DbThreadTypeIterator(DbForumFactory factory){
74         this.factory=factory;
75         ArrayList JavaDoc tempArr = new ArrayList JavaDoc();
76         Connection JavaDoc con = null;
77         PreparedStatement JavaDoc pstmt = null;
78         try {
79             con = DbConnectionManager.getConnection();
80             pstmt = con.prepareStatement(ALL_TYPES);
81             ResultSet JavaDoc rs = pstmt.executeQuery();
82             while (rs.next()) {
83                 tempArr.add(new Integer JavaDoc(rs.getInt("typeID")));
84             }
85         }
86         catch( SQLException JavaDoc sqle ) {
87             System.err.println("Error in DbThreadTypeIterator:constructor()-" + sqle);
88         }
89         finally {
90             try { pstmt.close(); }
91             catch (Exception JavaDoc e) { e.printStackTrace(); }
92             try { con.close(); }
93             catch (Exception JavaDoc e) { e.printStackTrace(); }
94         }
95         types = new int[tempArr.size()];
96         for (int i=0; i<types.length; i++) {
97             types[i] = ((Integer JavaDoc)tempArr.get(i)).intValue();
98         }
99
100     }
101
102     public boolean hasNext() {
103         return (currentIndex+1 < types.length);
104     }
105
106     /**
107      * Returns the next Type.
108      */

109     public Object JavaDoc next() throws java.util.NoSuchElementException JavaDoc {
110         currentIndex++;
111         if (currentIndex >= types.length) {
112             throw new java.util.NoSuchElementException JavaDoc();
113         }
114         return factory.getThreadType(types[currentIndex]);
115     }
116
117     /**
118      * For security reasons, the remove operation is not supported. Use
119      * ProfileManager.deleteType() instead.
120      *
121      * @see com.Yasna.forum.ProfileManager
122      */

123     public void remove() {
124         throw new UnsupportedOperationException JavaDoc();
125     }
126
127     /**
128      * Returns true if there are more types left to iterate through backwards.
129      */

130     public boolean hasPrevious() {
131         return (currentIndex > 0);
132     }
133
134     /**
135      * For security reasons, the add operation is not supported. Use
136      * ProfileManager instead.
137      *
138      * @see com.Yasna.forum.ProfileManager
139      */

140     public void add(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
141         throw new UnsupportedOperationException JavaDoc();
142     }
143
144     /**
145      * For security reasons, the set operation is not supported. Use
146      * ProfileManager instead.
147      *
148      * @see com.Yasna.forum.ProfileManager
149      */

150     public void set(Object JavaDoc o) throws UnsupportedOperationException JavaDoc {
151         throw new UnsupportedOperationException JavaDoc();
152     }
153
154     /**
155      * Returns the index number that would be returned with a call to next().
156      */

157     public int nextIndex() {
158         return currentIndex+1;
159     }
160
161     /**
162      * Returns the previous group.
163      */

164     public Object JavaDoc previous() throws java.util.NoSuchElementException JavaDoc {
165         currentIndex--;
166         if (currentIndex < 0) {
167             currentIndex++;
168             throw new java.util.NoSuchElementException JavaDoc();
169         }
170         return factory.getThreadType(types[currentIndex]);
171     }
172
173     /**
174      * Returns the index number that would be returned with a call to previous().
175      */

176     public int previousIndex() {
177         return currentIndex-1;
178     }
179
180 }
181
Popular Tags