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.Forum; 39 import org.nemesis.forum.ForumFactory; 40 import org.nemesis.forum.util.jdbc.DbConnectionManager; 41 44 public class DbForumFactoryIterator implements Iterator , ListIterator { 45 static protected Log log = LogFactory.getLog(DbForumFactoryIterator.class); 46 47 private static final String GET_FORUMS = "SELECT forumID FROM yazdForum"; 48 49 private ForumFactory factory; 50 private int[] forums; 51 int currentIndex = -1; 53 54 protected DbForumFactoryIterator(ForumFactory factory) { 55 this.factory = factory; 56 ArrayList allForums = new ArrayList (); 57 Connection con = null; 58 PreparedStatement pstmt = null; 59 try { 60 con = DbConnectionManager.getConnection(); 61 pstmt = con.prepareStatement(GET_FORUMS); 62 ResultSet rs = pstmt.executeQuery(); 63 while (rs.next()) { 64 allForums.add(new Integer (rs.getInt("forumID"))); 65 } 66 } catch (SQLException sqle) { 67 log.error("Error in DbForumFactoryIterator:constructor()-" , sqle); 68 } finally { 69 try { 70 pstmt.close(); 71 } catch (Exception e) { 72 log.error("" , e); 73 } 74 try { 75 con.close(); 76 } catch (Exception e) { 77 log.error("" , e); 78 } 79 } 80 91 92 forums = new int[allForums.size()]; 94 for (int i = 0; i < forums.length; i++) { 95 forums[i] = ((Integer ) allForums.get(i)).intValue(); 96 } 97 } 98 99 102 public boolean hasNext() { 103 return (currentIndex + 1 < forums.length); 104 } 105 106 109 public Object next() throws NoSuchElementException { 110 Forum forum = null; 111 currentIndex++; 112 if (currentIndex >= forums.length) { 113 throw new java.util.NoSuchElementException (); 114 } 115 try { 116 forum = factory.getForum(forums[currentIndex]); 117 } catch (Exception e) { 118 log.error("" , e); 119 } 120 return forum; 121 } 122 123 129 public void remove() { 130 throw new UnsupportedOperationException (); 131 } 132 133 public void add(Object o) throws UnsupportedOperationException { 134 throw new UnsupportedOperationException (); 135 } 136 137 public boolean hasPrevious() { 138 return (currentIndex > 0); 139 } 140 141 public int nextIndex() { 142 return currentIndex + 1; 143 } 144 145 public Object previous() throws NoSuchElementException { 146 Forum forum = null; 147 currentIndex--; 148 if (currentIndex < 0) { 149 currentIndex++; 150 throw new NoSuchElementException (); 151 } 152 try { 153 forum = factory.getForum(forums[currentIndex]); 154 } catch (Exception e) { 155 log.error("" , e); 156 } 157 return forum; 158 } 159 160 public int previousIndex() { 161 return currentIndex - 1; 162 } 163 164 public void set(Object o) throws UnsupportedOperationException { 165 throw new UnsupportedOperationException (); 166 } 167 } 168 | Popular Tags |