1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.util.*; 110 import java.sql.*; 112 import com.Yasna.forum.*; 113 114 117 public class DbUserMessagesIterator implements Iterator, ListIterator { 118 119 120 private static final String USER_MESSAGES = 121 "SELECT messageID, yazdMessage.threadID FROM yazdMessage,yazdForum,yazdThread WHERE " + 122 "yazdMessage.userID=? AND yazdForum.forumID=? AND " + 123 "yazdThread.forumID=yazdForum.forumID AND " + 124 "yazdMessage.threadID=yazdThread.threadID order by yazdMessage.creationDate desc"; 125 126 private int currentIndex = -1; 127 private int [] messages; 128 private int [] threads; 130 private Forum forum; 131 132 private DbForumFactory factory; 133 134 protected DbUserMessagesIterator(DbForumFactory factory, User user, Forum forum) 135 { 136 this.factory = factory; 137 this.forum = forum; 138 ArrayList tempMessages = new ArrayList(); 141 ArrayList tempThreads = new ArrayList(); 142 Connection con = null; 143 PreparedStatement pstmt = null; 144 try { 145 con = DbConnectionManager.getConnection(); 146 pstmt = con.prepareStatement(USER_MESSAGES); 147 pstmt.setInt(1, user.getID()); 148 pstmt.setInt(2, forum.getID()); 149 ResultSet rs = pstmt.executeQuery(); 150 while (rs.next()) { 151 tempMessages.add(new Integer (rs.getInt(1))); 152 tempThreads.add(new Integer (rs.getInt(2))); 153 } 154 } 155 catch( SQLException sqle ) { 156 System.err.println("Error in DbUserMessagesIterator:constructor()-" + sqle); 157 sqle.printStackTrace(); 158 } 159 finally { 160 try { pstmt.close(); } 161 catch (Exception e) { e.printStackTrace(); } 162 try { con.close(); } 163 catch (Exception e) { e.printStackTrace(); } 164 } 165 messages = new int[tempMessages.size()]; 167 for (int i=0; i<messages.length; i++) { 168 messages[i] = ((Integer )tempMessages.get(i)).intValue(); 169 } 170 threads = new int[tempThreads.size()]; 172 for (int i=0; i<threads.length; i++) { 173 threads[i] = ((Integer )tempThreads.get(i)).intValue(); 174 } 175 } 176 protected DbUserMessagesIterator(DbForumFactory factory, User user, Forum forum, int startIndex, int numResults) 177 { 178 this.factory = factory; 179 this.forum = forum; 180 int[] tempMessages = new int[numResults]; 183 int[] tempThreads = new int[numResults]; 184 185 Connection con = null; 186 PreparedStatement pstmt = null; 187 int messageCount = 0; 188 189 try { 190 con = DbConnectionManager.getConnection(); 191 pstmt = con.prepareStatement(USER_MESSAGES); 192 pstmt.setInt(1, user.getID()); 193 pstmt.setInt(2, forum.getID()); 194 ResultSet rs = pstmt.executeQuery(); 195 for (int i=0; i<startIndex; i++) { 197 rs.next(); 198 } 199 for (int i=0; i<numResults; i++) { 201 if (rs.next()) { 202 tempMessages[messageCount] = rs.getInt(1); 203 tempThreads[messageCount] = rs.getInt(2); 204 messageCount++; 205 } 206 else { 207 break; 208 } 209 } 210 } 211 catch( SQLException sqle ) { 212 System.err.println("Error in DbUserMessagesIterator (2): constructor()-" + sqle); 213 } 214 finally { 215 try { pstmt.close(); } 216 catch (Exception e) { e.printStackTrace(); } 217 try { con.close(); } 218 catch (Exception e) { e.printStackTrace(); } 219 } 220 messages = new int[messageCount]; 221 for (int i=0; i<messageCount; i++) { 222 messages[i] = tempMessages[i]; 223 } 224 threads = new int[messageCount]; 225 for (int i=0; i<messageCount; i++) { 226 threads[i] = tempThreads[i]; 227 } 228 } 229 230 233 public boolean hasNext() { 234 return (currentIndex+1 < messages.length); 235 } 236 237 240 public boolean hasPrevious() { 241 return (currentIndex > 0); 242 } 243 244 247 public Object next() throws java.util.NoSuchElementException { 248 ForumMessage message = null; 249 currentIndex++; 250 if (currentIndex >= messages.length) { 251 throw new java.util.NoSuchElementException (); 252 } 253 try { 254 message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]); 255 } 256 catch (ForumThreadNotFoundException ftnfe) { 257 ftnfe.printStackTrace(); 258 } 259 catch (ForumMessageNotFoundException fmnfe) { 260 fmnfe.printStackTrace(); 261 } 262 return message; 263 } 264 265 271 public void add(Object o) throws UnsupportedOperationException { 272 throw new UnsupportedOperationException (); 273 } 274 275 281 public void remove() { 282 throw new UnsupportedOperationException (); 283 } 284 285 291 public void set(Object o) throws UnsupportedOperationException { 292 throw new UnsupportedOperationException (); 293 } 294 295 298 public int nextIndex() { 299 return currentIndex+1; 300 } 301 302 305 public Object previous() throws java.util.NoSuchElementException { 306 ForumMessage message = null; 307 currentIndex--; 308 if (currentIndex < 0) { 309 currentIndex++; 310 throw new java.util.NoSuchElementException (); 311 } 312 try { 313 message = forum.getThread(threads[currentIndex]).getMessage(messages[currentIndex]); 314 } 315 catch (ForumThreadNotFoundException ftnfe) { 316 ftnfe.printStackTrace(); 317 } 318 catch (ForumMessageNotFoundException fmnfe) { 319 fmnfe.printStackTrace(); 320 } 321 return message; 322 } 323 324 327 public int previousIndex() { 328 return currentIndex-1; 329 } 330 } 331 | Popular Tags |