1 25 package org.nemesis.forum.impl; 26 27 import java.sql.Connection ; 28 import java.sql.PreparedStatement ; 29 import java.sql.ResultSet ; 30 31 import org.apache.commons.logging.Log; 32 import org.apache.commons.logging.LogFactory; 33 import org.nemesis.forum.Message; 34 import org.nemesis.forum.TreeWalker; 35 import org.nemesis.forum.util.jdbc.DbConnectionManager; 36 42 public class DbTreeWalker implements TreeWalker { 43 static protected Log log = LogFactory.getLog(DbTreeWalker.class); 44 45 private static final String GET_CHILD = 46 "SELECT yazdMessageTree.childID, yazdMessage.creationDate FROM " 47 + "yazdMessageTree, yazdMessage WHERE " 48 + "yazdMessageTree.childID=yazdMessage.messageID AND " 49 + "yazdMessageTree.parentID=? ORDER BY yazdMessage.creationDate"; 50 private static final String CHILD_COUNT = "SELECT count(*) FROM yazdMessageTree WHERE parentID=?"; 51 private static final String INDEX_OF_CHILD = 52 "SELECT yazdMessageTree.childID, yazdMessage.creationDate " 53 + "FROM yazdMessageTree, yazdMessage WHERE yazdMessageTree.childID=yazdMessage.messageID " 54 + "AND yazdMessageTree.parentID=? ORDER BY yazdMessage.creationDate"; 55 56 private static final String GET_CHILD_APPROVED = 57 "SELECT yazdMessageTree.childID, yazdMessage.creationDate FROM " 58 + "yazdMessageTree, yazdMessage WHERE " 59 + "yazdMessageTree.childID=yazdMessage.messageID AND " 60 + "yazdMessageTree.parentID=? AND yazdMessage.approved=? ORDER BY yazdMessage.creationDate"; 61 private static final String CHILD_COUNT_APPROVED = "SELECT count(*) FROM yazdMessageTree, yazdMessage WHERE yazdMessageTree.childID=yazdMessage.messageID AND yazdMessageTree.parentID=? AND yazdMessage.approved=? "; 62 private static final String INDEX_OF_CHILD_APPROVED = 63 "SELECT yazdMessageTree.childID, yazdMessage.creationDate " 64 + "FROM yazdMessageTree, yazdMessage WHERE yazdMessageTree.childID=yazdMessage.messageID " 65 + "AND yazdMessageTree.parentID=? AND yazdMessage.approved=? ORDER BY yazdMessage.creationDate"; 66 67 68 private DbForumThread thread; 69 private DbForumFactory factory; 70 private boolean approved; 71 private boolean filter=false; 72 73 public DbTreeWalker(DbForumThread thread, DbForumFactory factory) { 74 this.thread = thread; 75 this.factory = factory; 76 } 77 78 public DbTreeWalker(boolean approved, DbForumThread thread, DbForumFactory factory) { 79 this.thread = thread; 80 this.factory = factory; 81 this.approved=approved; 82 this.filter=true; 83 } 84 85 90 public Message getRoot() { 91 return thread.getRootMessage(); 92 } 93 94 103 public Message getChild(Message parent, int index) { 104 Message message = null; 105 Connection con = null; 106 PreparedStatement pstmt = null; 107 try { 108 con = DbConnectionManager.getConnection(); 109 if(filter){ 110 pstmt = con.prepareStatement(GET_CHILD_APPROVED); 111 pstmt.setInt(1, parent.getID()); 112 pstmt.setInt(2, approved ? 1:0); 113 } 114 else{ 115 pstmt = con.prepareStatement(GET_CHILD); 116 pstmt.setInt(1, parent.getID()); 117 } 118 119 120 ResultSet rs = pstmt.executeQuery(); 121 while (rs.next() && index > 0) { 122 index--; 123 } 124 if (index == 0) { 125 int messageID = rs.getInt(1); 126 message = thread.getMessage(messageID); 127 } 128 } catch (Exception e) { 129 log.error("Error in DbMessageTreeWalker:getChild(" + index + ")-" , e); 130 131 } finally { 132 try { 133 pstmt.close(); 134 } catch (Exception e) { 135 log.error("" , e); 136 } 137 try { 138 con.close(); 139 } catch (Exception e) { 140 log.error("" , e); 141 } 142 } 143 return message; 144 } 145 146 147 148 149 156 public int getChildCount(Message parent) { 157 int childCount = 0; 158 Connection con = null; 159 PreparedStatement pstmt = null; 160 try { 161 con = DbConnectionManager.getConnection(); 162 163 if(filter){ 164 pstmt = con.prepareStatement(CHILD_COUNT_APPROVED); 165 pstmt.setInt(1, parent.getID()); 166 pstmt.setInt(2, approved ? 1:0); 167 } 168 else{ 169 pstmt = con.prepareStatement(CHILD_COUNT); 170 pstmt.setInt(1, parent.getID()); 171 } 172 173 ResultSet rs = pstmt.executeQuery(); 174 rs.next(); 175 childCount = rs.getInt(1); 176 } catch (Exception e) { 177 log.error("Error in DbTreeWalker:getChildCount()-" ,e); 178 179 } finally { 180 try { 181 pstmt.close(); 182 } catch (Exception e) { 183 log.error("" , e); 184 } 185 try { 186 con.close(); 187 } catch (Exception e) { 188 log.error("" , e); 189 } 190 } 191 return childCount; 192 } 193 194 199 public int getRecursiveChildCount(Message parent) { 200 int numChildren = 0; 201 int num = getChildCount(parent); 202 numChildren += num; 203 for (int i = 0; i < num; i++) { 204 Message child = getChild(parent, i); 205 if (child != null) { 206 numChildren += getRecursiveChildCount(child); 207 } 208 } 209 return numChildren; 210 } 211 212 215 public int getIndexOfChild(Message parent, Message child) { 216 int index = 0; 217 Connection con = null; 218 PreparedStatement pstmt = null; 219 ResultSet rs = null; 220 try { 221 con = DbConnectionManager.getConnection(); 222 223 if(filter){ 224 pstmt = con.prepareStatement(INDEX_OF_CHILD_APPROVED); 225 pstmt.setInt(1, parent.getID()); 226 pstmt.setInt(2, approved ? 1:0); 227 } 228 else{ 229 pstmt = con.prepareStatement(INDEX_OF_CHILD); 230 pstmt.setInt(1, parent.getID()); 231 } 232 233 234 rs = pstmt.executeQuery(); 235 while (rs.next()) { 236 if (rs.getInt(1) == child.getID()) { 237 break; 238 } 239 index++; 240 } 241 } catch (Exception e) { 242 log.error("Error in DbTreeWalker:getIndexOfChild()-" , e); 243 } finally { 244 try { 245 pstmt.close(); 246 } catch (Exception e) { 247 log.error("" , e); 248 } 249 try { 250 con.close(); 251 } catch (Exception e) { 252 log.error("" , e); 253 } 254 } 255 return index; 256 } 257 258 265 public boolean isLeaf(Message node) { 266 return (getChildCount(node) == 0); 267 } 268 } 269 | Popular Tags |