1 25 package org.nemesis.forum.impl; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 import java.sql.SQLException ; 31 import java.util.ArrayList ; 32 import java.util.Iterator ; 33 import java.util.ListIterator ; 34 35 import org.apache.commons.logging.Log; 36 import org.apache.commons.logging.LogFactory; 37 import org.nemesis.forum.ForumThread; 38 import org.nemesis.forum.exception.ForumThreadNotFoundException; 39 import org.nemesis.forum.util.jdbc.DbConnectionManager; 40 48 public class DbForumIterator implements Iterator , ListIterator { 49 static protected Log log = LogFactory.getLog(DbForumIterator.class); 50 51 private static final String GET_THREADS ="SELECT threadID, creationDate FROM yazdThread WHERE forumID=? ORDER BY creationDate DESC"; 52 private static final String GET_THREADS_APPROVED ="SELECT threadID, creationDate FROM yazdThread WHERE forumID=? AND approved=? ORDER BY creationDate DESC "; 53 54 private DbForum forum; 57 private int[] threads; 59 private int currentIndex = -1; 61 62 DbForumFactory factory; 63 64 public DbForumIterator(DbForum forum, DbForumFactory factory) { 65 this.forum = forum; 66 this.factory = factory; 67 ArrayList tempThreads = new ArrayList (); 70 Connection con = null; 71 PreparedStatement pstmt = null; 72 try { 73 con = DbConnectionManager.getConnection(); 74 pstmt = con.prepareStatement(GET_THREADS); 75 pstmt.setInt(1, forum.getID()); 76 ResultSet rs = pstmt.executeQuery(); 77 78 while (rs.next()) { 79 tempThreads.add(new Integer (rs.getInt("threadID"))); 80 } 81 } catch (SQLException sqle) { 82 log.error("Error in DbThreadIterator:constructor()-" ,sqle); 83 } finally { 84 try { 85 pstmt.close(); 86 } catch (Exception e) { 87 log.error("" ,e); 88 } 89 try { 90 con.close(); 91 } catch (Exception e) { 92 log.error("" ,e); 93 } 94 } 95 threads = new int[tempThreads.size()]; 96 for (int i = 0; i < threads.length; i++) { 97 threads[i] = ((Integer ) tempThreads.get(i)).intValue(); 98 } 99 } 100 101 public DbForumIterator(DbForum forum, DbForumFactory factory, int startIndex, int numResults) { 102 this.forum = forum; 103 this.factory = factory; 104 105 int[] tempThreads = new int[numResults]; 106 int threadCount = 0; 110 111 Connection con = null; 112 PreparedStatement pstmt = null; 113 try { 114 con = DbConnectionManager.getConnection(); 115 pstmt = con.prepareStatement(GET_THREADS); 116 pstmt.setInt(1, forum.getID()); 117 ResultSet rs = pstmt.executeQuery(); 118 119 for (int i = 0; i < startIndex; i++) { 121 rs.next(); 122 } 123 for (int i = 0; i < numResults; i++) { 125 if (rs.next()) { 126 tempThreads[threadCount] = rs.getInt("threadID"); 127 threadCount++; 128 } else { 129 break; 130 } 131 } 132 } catch (SQLException sqle) { 133 log.error("Error in DbThreadIterator:constructor()-", sqle); 134 } finally { 135 try { 136 pstmt.close(); 137 } catch (Exception e) { 138 log.error("" ,e); 139 } 140 try { 141 con.close(); 142 } catch (Exception e) { 143 log.error("" ,e); 144 } 145 } 146 threads = new int[threadCount]; 147 for (int i = 0; i < threadCount; i++) { 148 threads[i] = tempThreads[i]; 149 } 150 } 151 152 public DbForumIterator(DbForum forum, DbForumFactory factory,boolean approved) { 153 this.forum = forum; 154 this.factory = factory; 155 ArrayList tempThreads = new ArrayList (); 158 Connection con = null; 159 PreparedStatement pstmt = null; 160 try { 161 con = DbConnectionManager.getConnection(); 162 pstmt = con.prepareStatement(GET_THREADS_APPROVED); 163 pstmt.setInt(1, forum.getID()); 164 pstmt.setInt(2, approved ? 1:0); 165 ResultSet rs = pstmt.executeQuery(); 166 167 while (rs.next()) { 168 tempThreads.add(new Integer (rs.getInt("threadID"))); 169 } 170 } catch (SQLException sqle) { 171 log.error("Error in DbThreadIterator:constructor()-" ,sqle); 172 } finally { 173 try { 174 pstmt.close(); 175 } catch (Exception e) { 176 log.error("" ,e); 177 } 178 try { 179 con.close(); 180 } catch (Exception e) { 181 log.error("" ,e); 182 } 183 } 184 threads = new int[tempThreads.size()]; 185 for (int i = 0; i < threads.length; i++) { 186 threads[i] = ((Integer ) tempThreads.get(i)).intValue(); 187 } 188 } 189 190 public DbForumIterator(DbForum forum, DbForumFactory factory, int startIndex, int numResults,boolean approved) { 191 this.forum = forum; 192 this.factory = factory; 193 194 int[] tempThreads = new int[numResults]; 195 int threadCount = 0; 199 200 Connection con = null; 201 PreparedStatement pstmt = null; 202 try { 203 con = DbConnectionManager.getConnection(); 204 pstmt = con.prepareStatement(GET_THREADS_APPROVED); 205 pstmt.setInt(1, forum.getID()); 206 pstmt.setInt(2, approved ? 1:0); 207 ResultSet rs = pstmt.executeQuery(); 208 209 for (int i = 0; i < startIndex; i++) { 211 rs.next(); 212 } 213 for (int i = 0; i < numResults; i++) { 215 if (rs.next()) { 216 tempThreads[threadCount] = rs.getInt("threadID"); 217 threadCount++; 218 } else { 219 break; 220 } 221 } 222 } catch (SQLException sqle) { 223 log.error("Error in DbThreadIterator:constructor()-", sqle); 224 } finally { 225 try { 226 pstmt.close(); 227 } catch (Exception e) { 228 log.error("" ,e); 229 } 230 try { 231 con.close(); 232 } catch (Exception e) { 233 log.error("" ,e); 234 } 235 } 236 threads = new int[threadCount]; 237 for (int i = 0; i < threadCount; i++) { 238 threads[i] = tempThreads[i]; 239 } 240 } 241 242 public void add(Object o) throws UnsupportedOperationException { 243 throw new UnsupportedOperationException (); 244 } 245 246 public boolean hasNext() { 247 return (currentIndex + 1 < threads.length); 248 } 249 250 public boolean hasPrevious() { 251 return (currentIndex > 0); 252 } 253 254 public Object next() throws java.util.NoSuchElementException { 255 ForumThread thread = null; 256 currentIndex++; 257 if (currentIndex >= threads.length) { 258 currentIndex--; 259 throw new java.util.NoSuchElementException (); 260 } 261 try { 262 thread = forum.getThread(threads[currentIndex]); 263 } catch (ForumThreadNotFoundException tnfe) { 264 log.error("" ,tnfe); 265 } 266 return thread; 267 } 268 269 public int nextIndex() { 270 return currentIndex + 1; 271 } 272 273 public Object previous() throws java.util.NoSuchElementException { 274 ForumThread thread = null; 275 currentIndex--; 276 if (currentIndex < 0) { 277 currentIndex++; 278 throw new java.util.NoSuchElementException (); 279 } 280 try { 281 thread = forum.getThread(threads[currentIndex]); 282 } catch (ForumThreadNotFoundException tnfe) { 283 log.error("" ,tnfe); 284 } 285 return thread; 286 } 287 288 public int previousIndex() { 289 return currentIndex - 1; 290 } 291 292 public void remove() throws UnsupportedOperationException { 293 throw new UnsupportedOperationException (); 294 } 295 296 public void set(Object o) throws UnsupportedOperationException { 297 throw new UnsupportedOperationException (); 298 } 299 } 300 | Popular Tags |