| 1 53 54 106 107 package com.Yasna.forum.database; 108 109 import java.sql.*; 110 111 import com.Yasna.forum.*; 112 import com.Yasna.util.*; 113 114 120 public class DbTreeWalker implements TreeWalker { 121 122 123 private static final String GET_CHILD = 124 "SELECT yazdMessageTree.childID, yazdMessage.creationDate FROM " + 125 "yazdMessageTree, yazdMessage WHERE " + 126 "yazdMessageTree.childID=yazdMessage.messageID AND " + 127 "yazdMessageTree.parentID=? ORDER BY yazdMessage.creationDate"; 128 private static final String CHILD_COUNT = 129 "SELECT count(*) FROM yazdMessageTree WHERE parentID=?"; 130 private static final String INDEX_OF_CHILD = 131 "SELECT yazdMessageTree.childID, yazdMessage.creationDate " + 132 "FROM yazdMessageTree, yazdMessage WHERE yazdMessageTree.childID=yazdMessage.messageID " + 133 "AND yazdMessageTree.parentID=? ORDER BY yazdMessage.creationDate"; 134 135 private DbForumThread thread; 136 private DbForumFactory factory; 137 138 public DbTreeWalker(DbForumThread thread, DbForumFactory factory) { 139 this.thread = thread; 140 this.factory = factory; 141 } 142 143 148 public ForumMessage getRoot() { 149 return thread.getRootMessage(); 150 } 151 152 161 public ForumMessage getChild(ForumMessage parent, int index) { 162 ForumMessage message = null; 163 Connection con = null; 164 PreparedStatement pstmt = null; 165 try { 166 con = DbConnectionManager.getConnection(); 167 pstmt = con.prepareStatement(GET_CHILD); 168 pstmt.setInt(1, parent.getID()); 169 ResultSet rs = pstmt.executeQuery(); 170 while (rs.next() && index > 0) { 171 index--; 172 } 173 if (index == 0) { 174 int messageID = rs.getInt(1); 175 message = thread.getMessage(messageID); 176 } 177 } 178 catch (Exception e) { 179 System.err.println("Error in DbMessageTreeWalker:getChild(" 180 + index + ")-" + e); 181 e.printStackTrace(); 182 } 183 finally { 184 try { pstmt.close(); } 185 catch (Exception e) { e.printStackTrace(); } 186 try { con.close(); } 187 catch (Exception e) { e.printStackTrace(); } 188 } 189 return message; 190 } 191 192 199 public int getChildCount(ForumMessage parent) { 200 int childCount = 0; 201 Connection con = null; 202 PreparedStatement pstmt = null; 203 try { 204 con = DbConnectionManager.getConnection(); 205 pstmt = con.prepareStatement(CHILD_COUNT); 206 pstmt.setInt(1, parent.getID()); 207 ResultSet rs = pstmt.executeQuery(); 208 rs.next(); 209 childCount = rs.getInt(1); 210 } 211 catch (Exception e) { 212 System.err.println("Error in DbTreeWalker:getChildCount()-" 213 + e); 214 e.printStackTrace(); 215 } 216 finally { 217 try { pstmt.close(); } 218 catch (Exception e) { e.printStackTrace(); } 219 try { con.close(); } 220 catch (Exception e) { e.printStackTrace(); } 221 } 222 return childCount; 223 } 224 225 230 public int getRecursiveChildCount(ForumMessage parent) { 231 int numChildren = 0; 232 int num = getChildCount(parent); 233 numChildren += num; 234 for (int i=0; i<num; i++) { 235 ForumMessage child = getChild(parent,i); 236 if (child != null) { 237 numChildren += getRecursiveChildCount(child); 238 } 239 } 240 return numChildren; 241 } 242 243 246 public int getIndexOfChild(ForumMessage parent, ForumMessage child) { 247 int index = 0; 248 Connection con = null; 249 PreparedStatement pstmt = null; 250 ResultSet rs = null; 251 try { 252 con = DbConnectionManager.getConnection(); 253 pstmt = con.prepareStatement(INDEX_OF_CHILD); 254 pstmt.setInt(1, parent.getID()); 255 rs = pstmt.executeQuery(); 256 while (rs.next()) { 257 if (rs.getInt(1) == child.getID()) { 258 break; 259 } 260 index++; 261 } 262 } 263 catch (Exception e) { 264 System.err.println("Error in DbTreeWalker:getIndexOfChild()-" + e); 265 } 266 finally { 267 try { pstmt.close(); } 268 catch (Exception e) { e.printStackTrace(); } 269 try { con.close(); } 270 catch (Exception e) { e.printStackTrace(); } 271 } 272 return index; 273 } 274 275 282 public boolean isLeaf(ForumMessage node) { 283 return (getChildCount(node) == 0); 284 } 285 } 286 | Popular Tags |