1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.util.*; 110 import java.sql.*; 112 113 import com.Yasna.util.*; 114 import com.Yasna.forum.*; 115 116 124 public class DbForumIterator implements Iterator, ListIterator { 125 126 127 private static final String GET_THREADS_SORT_BY_CREATION_DATE = 128 "SELECT threadID, creationDate FROM yazdThread WHERE forumID=? " + 129 "ORDER BY sticky desc,creationDate DESC"; 130 private static final String GET_THREADS_SORT_BY_MODIFIED_DATE = 131 "SELECT threadID, modifiedDate FROM yazdThread WHERE forumID=? " + 132 "ORDER BY sticky desc,modifiedDate DESC"; 133 134 private DbForum forum; 137 private int [] threads; 139 private int currentIndex = -1; 141 142 DbForumFactory factory; 143 144 public DbForumIterator(DbForum forum, DbForumFactory factory) 145 { 146 this.forum = forum; 147 this.factory = factory; 148 ArrayList tempThreads = new ArrayList(); 151 Connection con = null; 152 PreparedStatement pstmt = null; 153 try { 154 con = DbConnectionManager.getConnection(); 155 pstmt = con.prepareStatement(GET_THREADS_SORT_BY_CREATION_DATE); 156 pstmt.setInt(1,forum.getID()); 157 ResultSet rs = pstmt.executeQuery(); 158 159 while (rs.next()) { 160 tempThreads.add(new Integer (rs.getInt("threadID"))); 161 } 162 } 163 catch( SQLException sqle ) { 164 System.err.println("Error in DbThreadIterator:constructor()-" + sqle); 165 } 166 finally { 167 try { pstmt.close(); } 168 catch (Exception e) { e.printStackTrace(); } 169 try { con.close(); } 170 catch (Exception e) { e.printStackTrace(); } 171 } 172 threads = new int[tempThreads.size()]; 173 for (int i=0; i<threads.length; i++) { 174 threads[i] = ((Integer )tempThreads.get(i)).intValue(); 175 } 176 } 177 178 public DbForumIterator(DbForum forum, DbForumFactory factory, 179 int startIndex, int numResults, int sortBy) 180 { 181 this.forum = forum; 182 this.factory = factory; 183 184 int[] tempThreads = new int[numResults]; 185 int threadCount = 0; 189 190 Connection con = null; 191 PreparedStatement pstmt = null; 192 try { 193 con = DbConnectionManager.getConnection(); 194 if (Forum.SORT_BY_CREATE_DATE==sortBy) { 195 pstmt = con.prepareStatement(GET_THREADS_SORT_BY_CREATION_DATE); 196 } else { 197 pstmt = con.prepareStatement(GET_THREADS_SORT_BY_MODIFIED_DATE); 198 } 199 pstmt.setInt(1,forum.getID()); 200 ResultSet rs = pstmt.executeQuery(); 201 202 for (int i=0; i<startIndex; i++) { 204 rs.next(); 205 } 206 for (int i=0; i<numResults; i++) { 208 if (rs.next()) { 209 tempThreads[threadCount] = rs.getInt("threadID"); 210 threadCount++; 211 } 212 else { 213 break; 214 } 215 } 216 } 217 catch( SQLException sqle ) { 218 System.err.println("Error in DbThreadIterator:constructor()-" + sqle); 219 } 220 finally { 221 try { pstmt.close(); } 222 catch (Exception e) { e.printStackTrace(); } 223 try { con.close(); } 224 catch (Exception e) { e.printStackTrace(); } 225 } 226 threads = new int[threadCount]; 227 for (int i=0; i<threadCount; i++) { 228 threads[i] = tempThreads[i]; 229 } 230 } 231 232 public void add(Object o) throws UnsupportedOperationException { 233 throw new UnsupportedOperationException (); 234 } 235 236 public boolean hasNext() { 237 return (currentIndex+1 < threads.length); 238 } 239 240 public boolean hasPrevious() { 241 return (currentIndex > 0); 242 } 243 244 public Object next() throws java.util.NoSuchElementException { 245 ForumThread thread = null; 246 currentIndex++; 247 if (currentIndex >= threads.length) { 248 currentIndex--; 249 throw new java.util.NoSuchElementException (); 250 } 251 try { 252 thread = forum.getThread(threads[currentIndex]); 253 } 254 catch (ForumThreadNotFoundException tnfe) { 255 System.err.println(tnfe); 256 } 257 return thread; 258 } 259 260 public int nextIndex() { 261 return currentIndex+1; 262 } 263 264 public Object previous() throws java.util.NoSuchElementException { 265 ForumThread thread = null; 266 currentIndex--; 267 if (currentIndex < 0) { 268 currentIndex++; 269 throw new java.util.NoSuchElementException (); 270 } 271 try { 272 thread = forum.getThread(threads[currentIndex]); 273 } 274 catch (ForumThreadNotFoundException tnfe) { 275 System.err.println(tnfe); 276 } 277 return thread; 278 } 279 280 public int previousIndex() { 281 return currentIndex-1; 282 } 283 284 public void remove() throws UnsupportedOperationException { 285 throw new UnsupportedOperationException (); 286 } 287 288 public void set(Object o) throws UnsupportedOperationException { 289 throw new UnsupportedOperationException (); 290 } 291 } 292 | Popular Tags |