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 import java.util.NoSuchElementException ; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 import org.nemesis.forum.Group; 39 import org.nemesis.forum.ProfileManager; 40 import org.nemesis.forum.exception.GroupNotFoundException; 41 import org.nemesis.forum.util.jdbc.DbConnectionManager; 42 45 public class DbGroupIterator implements Iterator , ListIterator { 46 static protected Log log = LogFactory.getLog(DbGroupIterator.class); 47 48 private static final String ALL_GROUPS = "SELECT groupID from yazdGroup"; 49 50 private int currentIndex = -1; 51 private int[] groups; 52 53 private ProfileManager profileManager; 54 55 protected DbGroupIterator(ProfileManager profileManager) { 56 this.profileManager = profileManager; 57 ArrayList tempGroups = new ArrayList (); 60 Connection con = null; 61 PreparedStatement pstmt = null; 62 try { 63 con = DbConnectionManager.getConnection(); 64 pstmt = con.prepareStatement(ALL_GROUPS); 65 ResultSet rs = pstmt.executeQuery(); 66 while (rs.next()) { 67 tempGroups.add(new Integer (rs.getInt("groupID"))); 68 } 69 } catch (SQLException sqle) { 70 log.error("Error in DbGroupIterator: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 groups = new int[tempGroups.size()]; 84 for (int i = 0; i < groups.length; i++) { 85 groups[i] = ((Integer ) tempGroups.get(i)).intValue(); 86 } 87 } 88 89 protected DbGroupIterator(ProfileManager profileManager, int startIndex, int numResults) { 90 this.profileManager = profileManager; 91 92 int[] tempResults = new int[numResults]; 93 int resultCount = 0; 97 98 Connection con = null; 99 PreparedStatement pstmt = null; 100 101 try { 102 con = DbConnectionManager.getConnection(); 103 pstmt = con.prepareStatement(ALL_GROUPS); 104 ResultSet rs = pstmt.executeQuery(); 105 for (int i = 0; i < startIndex; i++) { 107 rs.next(); 108 } 109 for (int i = 0; i < numResults; i++) { 111 if (rs.next()) { 112 tempResults[resultCount] = rs.getInt("groupID"); 113 resultCount++; 114 } else { 115 break; 116 } 117 } 118 } catch (SQLException sqle) { 119 log.error("Error in DbGroupIterator:constructor()-" ,sqle); 120 } finally { 121 try { 122 pstmt.close(); 123 } catch (Exception e) { 124 log.error("",e); 125 } 126 try { 127 con.close(); 128 } catch (Exception e) { 129 log.error("",e); 130 } 131 } 132 groups = new int[resultCount]; 133 for (int i = 0; i < resultCount; i++) { 134 groups[i] = tempResults[i]; 135 } 136 } 137 138 141 public boolean hasNext() { 142 return (currentIndex + 1 < groups.length); 143 } 144 145 148 public Object next() throws NoSuchElementException { 149 Group group = null; 150 currentIndex++; 151 if (currentIndex >= groups.length) { 152 throw new java.util.NoSuchElementException (); 153 } 154 try { 155 group = profileManager.getGroup(groups[currentIndex]); 156 } catch (GroupNotFoundException gnfe) { 157 log.error("",gnfe); 158 } 159 return group; 160 } 161 162 168 public void remove() { 169 throw new UnsupportedOperationException (); 170 } 171 172 175 public boolean hasPrevious() { 176 return (currentIndex > 0); 177 } 178 179 185 public void add(Object o) throws UnsupportedOperationException { 186 throw new UnsupportedOperationException (); 187 } 188 189 195 public void set(Object o) throws UnsupportedOperationException { 196 throw new UnsupportedOperationException (); 197 } 198 199 202 public int nextIndex() { 203 return currentIndex + 1; 204 } 205 206 209 public Object previous() throws java.util.NoSuchElementException { 210 Group group = null; 211 currentIndex--; 212 if (currentIndex < 0) { 213 currentIndex++; 214 throw new java.util.NoSuchElementException (); 215 } 216 try { 217 group = profileManager.getGroup(groups[currentIndex]); 218 } catch (GroupNotFoundException gnfe) { 219 log.error("",gnfe); 220 } 221 return group; 222 } 223 224 227 public int previousIndex() { 228 return currentIndex - 1; 229 } 230 } 231 | Popular Tags |