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.Iterator ; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.nemesis.forum.ForumThread; 36 import org.nemesis.forum.Message; 37 import org.nemesis.forum.exception.ForumMessageNotFoundException; 38 import org.nemesis.forum.util.jdbc.DbConnectionManager; 39 42 public class DbThreadIterator implements Iterator { 43 static protected Log log = LogFactory.getLog(DbThreadIterator.class); 44 45 private static final String MESSAGE_COUNT = "SELECT count(messageID) FROM yazdMessage WHERE threadID=?"; 46 private static final String MESSAGE_COUNT_APPROVED = "SELECT count(messageID) FROM yazdMessage WHERE threadID=? AND approved=?"; 47 private static final String GET_MESSAGES = "SELECT messageID, creationDate FROM yazdMessage WHERE threadID=? " + "ORDER BY creationDate ASC"; 48 private static final String GET_MESSAGES_APPROVED = "SELECT messageID, creationDate FROM yazdMessage WHERE threadID=? AND approved=? ORDER BY creationDate ASC"; 49 50 private ForumThread thread; 51 private int[] messages; 52 private int currentIndex = -1; 53 54 private Message nextMessage = null; 55 56 protected DbThreadIterator(ForumThread thread) { 57 this.thread = thread; 58 Connection con = null; 59 PreparedStatement pstmt = null; 60 try { 61 con = DbConnectionManager.getConnection(); 62 pstmt = con.prepareStatement(MESSAGE_COUNT); 63 pstmt.setInt(1, thread.getID()); 64 ResultSet rs = pstmt.executeQuery(); 65 rs.next(); 66 messages = new int[rs.getInt(1)]; 67 pstmt.close(); 68 69 pstmt = con.prepareStatement(GET_MESSAGES); 70 pstmt.setInt(1, thread.getID()); 71 rs = pstmt.executeQuery(); 72 for (int i = 0; i < messages.length; i++) { 73 rs.next(); 74 messages[i] = rs.getInt(1); 75 } 76 } catch (SQLException sqle) { 77 log.error("Error in DbThreadIterator:constructor()-" , sqle); 78 } finally { 79 try { 80 pstmt.close(); 81 } catch (Exception e) { 82 log.error("" , e); 83 } 84 try { 85 con.close(); 86 } catch (Exception e) { 87 log.error("" , e); 88 } 89 } 90 } 91 92 protected DbThreadIterator(ForumThread thread, int startIndex, int numResults) { 93 this.thread = thread; 94 95 int[] tempMessages = new int[numResults]; 96 int messageCount = 0; 100 101 Connection con = null; 102 PreparedStatement pstmt = null; 103 104 try { 105 con = DbConnectionManager.getConnection(); 106 pstmt = con.prepareStatement(GET_MESSAGES); 107 pstmt.setInt(1, thread.getID()); 108 109 ResultSet rs = pstmt.executeQuery(); 110 for (int i = 0; i < startIndex; i++) { 112 rs.next(); 113 } 114 for (int i = 0; i < numResults; i++) { 116 if (rs.next()) { 117 tempMessages[messageCount] = rs.getInt("messageID"); 118 messageCount++; 119 } else { 120 break; 121 } 122 } 123 } catch (SQLException sqle) { 124 log.error("Error in DbThreadIterator:constructor()-" , sqle); 125 } finally { 126 try { 127 pstmt.close(); 128 } catch (Exception e) { 129 log.error("" , e); 130 } 131 try { 132 con.close(); 133 } catch (Exception e) { 134 log.error("" , e); 135 } 136 } 137 messages = new int[messageCount]; 138 for (int i = 0; i < messageCount; i++) { 139 messages[i] = tempMessages[i]; 140 } 141 } 142 143 protected DbThreadIterator(boolean approved,ForumThread thread) { 144 this.thread = thread; 145 Connection con = null; 146 PreparedStatement pstmt = null; 147 try { 148 con = DbConnectionManager.getConnection(); 149 pstmt = con.prepareStatement(MESSAGE_COUNT_APPROVED); 150 pstmt.setInt(1, thread.getID()); 151 pstmt.setInt(2, approved?1:0); 152 153 ResultSet rs = pstmt.executeQuery(); 154 rs.next(); 155 messages = new int[rs.getInt(1)]; 156 pstmt.close(); 157 158 pstmt = con.prepareStatement(GET_MESSAGES_APPROVED); 159 pstmt.setInt(1, thread.getID()); 160 pstmt.setInt(2, approved?1:0); 161 rs = pstmt.executeQuery(); 162 for (int i = 0; i < messages.length; i++) { 163 rs.next(); 164 messages[i] = rs.getInt(1); 165 } 166 } catch (SQLException sqle) { 167 log.error("Error in DbThreadIterator:constructor()-" , sqle); 168 } finally { 169 try { 170 pstmt.close(); 171 } catch (Exception e) { 172 log.error("" , e); 173 } 174 try { 175 con.close(); 176 } catch (Exception e) { 177 log.error("" , e); 178 } 179 } 180 } 181 182 protected DbThreadIterator(boolean approved,ForumThread thread, int startIndex, int numResults) { 183 this.thread = thread; 184 185 int[] tempMessages = new int[numResults]; 186 int messageCount = 0; 190 191 Connection con = null; 192 PreparedStatement pstmt = null; 193 194 try { 195 con = DbConnectionManager.getConnection(); 196 pstmt = con.prepareStatement(GET_MESSAGES_APPROVED); 197 pstmt.setInt(1, thread.getID()); 198 pstmt.setInt(2, approved?1:0); 199 ResultSet rs = pstmt.executeQuery(); 200 for (int i = 0; i < startIndex; i++) { 202 rs.next(); 203 } 204 for (int i = 0; i < numResults; i++) { 206 if (rs.next()) { 207 tempMessages[messageCount] = rs.getInt("messageID"); 208 messageCount++; 209 } else { 210 break; 211 } 212 } 213 } catch (SQLException sqle) { 214 log.error("Error in DbThreadIterator:constructor()-" , sqle); 215 } finally { 216 try { 217 pstmt.close(); 218 } catch (Exception e) { 219 log.error("" , e); 220 } 221 try { 222 con.close(); 223 } catch (Exception e) { 224 log.error("" , e); 225 } 226 } 227 messages = new int[messageCount]; 228 for (int i = 0; i < messageCount; i++) { 229 messages[i] = tempMessages[i]; 230 } 231 } 232 233 238 public boolean hasNext() { 239 if (currentIndex + 1 >= messages.length) { 242 return false; 243 } 244 return true; 245 246 264 } 265 266 272 public Object next() throws java.util.NoSuchElementException { 273 Message message = null; 274 if (nextMessage != null) { 275 message = nextMessage; 276 nextMessage = null; 277 } else { 278 message = getNextMessage(); 279 if (message == null) { 280 throw new java.util.NoSuchElementException (); 281 } 282 } 283 return message; 284 } 285 286 292 public void remove() throws UnsupportedOperationException { 293 throw new UnsupportedOperationException (); 294 } 295 296 300 private Message getNextMessage() { 301 while (currentIndex + 1 < messages.length) { 302 currentIndex++; 303 try { 304 Message message = thread.getMessage(messages[currentIndex]); 305 return message; 306 } catch (ForumMessageNotFoundException mnfe) { 307 } 308 } 309 return null; 310 } 311 } 312 | Popular Tags |