1 53 54 package com.Yasna.forum.database; 55 56 import java.util.*; 57 import java.sql.*; 59 60 import com.Yasna.util.*; 61 import com.Yasna.forum.*; 62 63 66 public class DbForumGroupIterator implements Iterator, ListIterator { 67 68 69 private static final String GET_FORUM_GROUPS = 70 "SELECT forumGroupID, creationDate FROM yazdForumGroup WHERE categoryID=? " + 71 "ORDER BY grporder DESC"; 72 73 private DbCategory category; 76 private int [] forumGroups; 78 private int currentIndex = -1; 80 81 DbForumFactory factory; 82 83 public DbForumGroupIterator(DbCategory category, DbForumFactory factory) 84 { 85 this.category = category; 86 this.factory = factory; 87 ArrayList tempForumGroups = new ArrayList(); 90 Connection con = null; 91 PreparedStatement pstmt = null; 92 try { 93 con = DbConnectionManager.getConnection(); 94 pstmt = con.prepareStatement(GET_FORUM_GROUPS); 95 pstmt.setInt(1,category.getID()); 96 ResultSet rs = pstmt.executeQuery(); 97 98 while (rs.next()) { 99 tempForumGroups.add(new Integer (rs.getInt("forumGroupID"))); 100 } 101 } 102 catch( SQLException sqle ) { 103 System.err.println("Error in DbForumGroupIterator:constructor()-" + sqle); 104 } 105 finally { 106 try { pstmt.close(); } 107 catch (Exception e) { e.printStackTrace(); } 108 try { con.close(); } 109 catch (Exception e) { e.printStackTrace(); } 110 } 111 forumGroups = new int[tempForumGroups.size()]; 112 for (int i=0; i<forumGroups.length; i++) { 113 forumGroups[i] = ((Integer )tempForumGroups.get(i)).intValue(); 114 } 115 } 116 117 public void add(Object o) throws UnsupportedOperationException { 118 throw new UnsupportedOperationException (); 119 } 120 121 public boolean hasNext() { 122 return (currentIndex+1 < forumGroups.length); 123 } 124 125 public boolean hasPrevious() { 126 return (currentIndex > 0); 127 } 128 129 public Object next() throws java.util.NoSuchElementException { 130 ForumGroup forumGroup = null; 131 currentIndex++; 132 if (currentIndex >= forumGroups.length) { 133 currentIndex--; 134 throw new java.util.NoSuchElementException (); 135 } 136 try { 137 forumGroup = category.getForumGroup(forumGroups[currentIndex]); 138 } 139 catch (ForumGroupNotFoundException tnfe) { 140 System.err.println(tnfe); 141 } 142 return forumGroup; 143 } 144 145 public int nextIndex() { 146 return currentIndex+1; 147 } 148 149 public Object previous() throws java.util.NoSuchElementException { 150 ForumGroup forumGroup = null; 151 currentIndex--; 152 if (currentIndex < 0) { 153 currentIndex++; 154 throw new java.util.NoSuchElementException (); 155 } 156 try { 157 forumGroup = category.getForumGroup(forumGroups[currentIndex]); 158 } 159 catch (ForumGroupNotFoundException tnfe) { 160 System.err.println(tnfe); 161 } 162 return forumGroup; 163 } 164 165 public int previousIndex() { 166 return currentIndex-1; 167 } 168 169 public void remove() throws UnsupportedOperationException { 170 throw new UnsupportedOperationException (); 171 } 172 173 public void set(Object o) throws UnsupportedOperationException { 174 throw new UnsupportedOperationException (); 175 } 176 } 177 | Popular Tags |