1 53 54 106 107 package com.Yasna.forum.database; 108 109 import com.Yasna.forum.*; 110 import com.Yasna.util.*; 111 import java.util.*; 112 import java.sql.*; 114 115 118 public class DbForumFactoryIterator implements Iterator, ListIterator { 119 120 121 private static final String GET_FORUMS = "SELECT forumID FROM yazdForum"; 122 private static final String GET_FORUMS_BY_FORUM_GROUP_ID = 123 "SELECT forumID FROM yazdForum WHERE forumGroupID = ? order by forumorder desc"; 124 125 private ForumFactory factory; 126 private int [] forums; 127 int currentIndex = -1; 129 130 protected DbForumFactoryIterator(ForumFactory factory) { 131 this.factory = factory; 132 ArrayList allForums = new ArrayList(); 133 Connection con = null; 134 PreparedStatement pstmt = null; 135 try { 136 con = DbConnectionManager.getConnection(); 137 pstmt = con.prepareStatement(GET_FORUMS); 138 ResultSet rs = pstmt.executeQuery(); 139 while (rs.next()) { 140 allForums.add(new Integer (rs.getInt("forumID"))); 141 } 142 } 143 catch( SQLException sqle ) { 144 System.err.println("Error in DbForumFactoryIterator:constructor()-" + sqle); 145 } 146 finally { 147 try { pstmt.close(); } 148 catch (Exception e) { e.printStackTrace(); } 149 try { con.close(); } 150 catch (Exception e) { e.printStackTrace(); } 151 } 152 163 164 forums = new int[allForums.size()]; 166 for (int i=0; i<forums.length; i++) { 167 forums[i] = ((Integer )allForums.get(i)).intValue(); 168 } 169 } 170 171 protected DbForumFactoryIterator(DbForumGroup forumGroup, ForumFactory factory) { 172 this.factory = factory; 173 ArrayList allForums = new ArrayList(); 174 Connection con = null; 175 PreparedStatement pstmt = null; 176 try { 177 con = DbConnectionManager.getConnection(); 178 pstmt = con.prepareStatement(GET_FORUMS_BY_FORUM_GROUP_ID); 179 pstmt.setInt(1,forumGroup.getID()); 180 ResultSet rs = pstmt.executeQuery(); 181 while (rs.next()) { 182 allForums.add(new Integer (rs.getInt("forumID"))); 183 } 184 } 185 catch( SQLException sqle ) { 186 System.err.println("Error in DbForumFactoryIterator:constructor()-" + sqle); 187 } 188 finally { 189 try { pstmt.close(); } 190 catch (Exception e) { e.printStackTrace(); } 191 try { con.close(); } 192 catch (Exception e) { e.printStackTrace(); } 193 } 194 195 forums = new int[allForums.size()]; 197 for (int i=0; i<forums.length; i++) { 198 forums[i] = ((Integer )allForums.get(i)).intValue(); 199 } 200 } 201 202 205 public boolean hasNext() { 206 return (currentIndex+1 < forums.length); 207 } 208 209 212 public Object next() throws java.util.NoSuchElementException { 213 Forum forum = null; 214 currentIndex++; 215 if (currentIndex >= forums.length) { 216 throw new java.util.NoSuchElementException (); 217 } 218 try { 219 forum = factory.getForum(forums[currentIndex]); 220 } 221 catch (Exception e) { 222 e.printStackTrace(); 223 } 224 return forum; 225 } 226 227 233 public void remove() { 234 throw new UnsupportedOperationException (); 235 } 236 237 public void add(Object o) throws UnsupportedOperationException { 238 throw new UnsupportedOperationException (); 239 } 240 241 public boolean hasPrevious() { 242 return (currentIndex > 0); 243 } 244 245 public int nextIndex() { 246 return currentIndex+1; 247 } 248 249 public Object previous() throws java.util.NoSuchElementException { 250 Forum forum = null; 251 currentIndex--; 252 if (currentIndex < 0) { 253 currentIndex++; 254 throw new java.util.NoSuchElementException (); 255 } 256 try { 257 forum = factory.getForum(forums[currentIndex]); 258 } 259 catch (Exception e) { 260 e.printStackTrace(); 261 } 262 return forum; 263 } 264 265 public int previousIndex() { 266 return currentIndex-1; 267 } 268 269 public void set(Object o) throws UnsupportedOperationException { 270 throw new UnsupportedOperationException (); 271 } 272 } 273 | Popular Tags |