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.ProfileManager; 38 import org.nemesis.forum.User; 39 import org.nemesis.forum.exception.UserNotFoundException; 40 import org.nemesis.forum.util.jdbc.DbConnectionManager; 41 45 public class DbUserIterator implements Iterator , ListIterator { 46 static protected Log log = LogFactory.getLog(DbUserIterator.class); 47 48 private static final String ALL_USERS = "SELECT userID from yazdUser"; 49 50 private int currentIndex = -1; 51 private int[] users; 52 53 private ProfileManager profileManager; 54 55 protected DbUserIterator(ProfileManager profileManager) { 56 this.profileManager = profileManager; 57 ArrayList tempUsers = new ArrayList (); 60 Connection con = null; 61 PreparedStatement pstmt = null; 62 try { 63 con = DbConnectionManager.getConnection(); 64 pstmt = con.prepareStatement(ALL_USERS); 65 ResultSet rs = pstmt.executeQuery(); 66 while (rs.next()) { 67 tempUsers.add(new Integer (rs.getInt("userID"))); 68 } 69 } catch (SQLException sqle) { 70 log.error("Error in DbUserIterator:constructor()-" , sqle); 71 } finally { 72 try { 73 pstmt.close(); 74 } catch (Exception e) { 75 log.error("" , e); 76 } 77 try { 78 con.close(); 79 } catch (Exception e) { 80 log.error("" , e); 81 } 82 } 83 users = new int[tempUsers.size()]; 85 for (int i = 0; i < users.length; i++) { 86 users[i] = ((Integer ) tempUsers.get(i)).intValue(); 87 } 88 } 89 90 protected DbUserIterator(ProfileManager profileManager, int startIndex, int numResults) { 91 this.profileManager = profileManager; 92 93 int[] tempUsers = new int[numResults]; 94 int userCount = 0; 98 99 Connection con = null; 100 PreparedStatement pstmt = null; 101 102 try { 103 con = DbConnectionManager.getConnection(); 104 pstmt = con.prepareStatement(ALL_USERS); 105 ResultSet rs = pstmt.executeQuery(); 106 for (int i = 0; i < startIndex; i++) { 108 rs.next(); 109 } 110 for (int i = 0; i < numResults; i++) { 112 if (rs.next()) { 113 tempUsers[userCount] = rs.getInt("userID"); 114 userCount++; 115 } else { 116 break; 117 } 118 } 119 } catch (SQLException sqle) { 120 log.error("Error in DbUserIterator:constructor()-" , sqle); 121 } finally { 122 try { 123 pstmt.close(); 124 } catch (Exception e) { 125 log.error("" , e); 126 } 127 try { 128 con.close(); 129 } catch (Exception e) { 130 log.error("" , e); 131 } 132 } 133 users = new int[userCount]; 134 for (int i = 0; i < userCount; i++) { 135 users[i] = tempUsers[i]; 136 } 137 } 138 139 142 public boolean hasNext() { 143 return (currentIndex + 1 < users.length); 144 } 145 146 149 public boolean hasPrevious() { 150 return (currentIndex > 0); 151 } 152 153 159 public Object next() throws java.util.NoSuchElementException { 160 User user = null; 161 currentIndex++; 162 if (currentIndex >= users.length) { 163 throw new java.util.NoSuchElementException (); 164 } 165 try { 166 user = profileManager.getUser(users[currentIndex]); 167 } catch (UserNotFoundException gnfe) { 168 log.error("" , gnfe); 169 } 170 return user; 171 } 172 173 179 public void add(Object o) throws UnsupportedOperationException { 180 throw new UnsupportedOperationException (); 181 } 182 183 189 public void remove() { 190 throw new UnsupportedOperationException (); 191 } 192 193 199 public void set(Object o) throws UnsupportedOperationException { 200 throw new UnsupportedOperationException (); 201 } 202 203 206 public int nextIndex() { 207 return currentIndex + 1; 208 } 209 210 213 public Object previous() throws java.util.NoSuchElementException { 214 User user = null; 215 currentIndex--; 216 if (currentIndex < 0) { 217 currentIndex++; 218 throw new java.util.NoSuchElementException (); 219 } 220 try { 221 user = profileManager.getUser(users[currentIndex]); 222 } catch (UserNotFoundException gnfe) { 223 log.error("" , gnfe); 224 } 225 return user; 226 } 227 228 231 public int previousIndex() { 232 return currentIndex - 1; 233 } 234 } 235 | Popular Tags |