KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > impl > DbTreeWalker


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25 package org.nemesis.forum.impl;
26
27 import java.sql.Connection JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.ResultSet JavaDoc;
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 /**
37  * Database implementation of the TreeWalker interface. This class is relatively
38  * inefficient compared to how it will eventually be implemented. However,
39  * a schema change is needed before that optimization is needed (represent
40  * tree structure directly in schema).
41  */

42 public class DbTreeWalker implements TreeWalker {
43     static protected Log log = LogFactory.getLog(DbTreeWalker.class);
44     /** DATABASE QUERIES **/
45     private static final String JavaDoc 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 JavaDoc CHILD_COUNT = "SELECT count(*) FROM yazdMessageTree WHERE parentID=?";
51     private static final String JavaDoc 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 JavaDoc 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 JavaDoc CHILD_COUNT_APPROVED = "SELECT count(*) FROM yazdMessageTree, yazdMessage WHERE yazdMessageTree.childID=yazdMessage.messageID AND yazdMessageTree.parentID=? AND yazdMessage.approved=? ";
62     private static final String JavaDoc 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     /**
86      * Returns the root of the tree. Returns null only if the tree has no nodes.
87      *
88      * @returns the root of the tree
89      */

90     public Message getRoot() {
91         return thread.getRootMessage();
92     }
93
94     /**
95      * Returns the child of parent at index index in the parent's child array.
96      * This should not return null if index is a valid index for parent (that
97      * is index >= 0 && index < getChildCount(parent)).
98      *
99      * @param parent the parent message.
100      * @param index the index of the child.
101      * @returns the child of parent at index.
102      */

103     public Message getChild(Message parent, int index) {
104         Message message = null;
105         Connection JavaDoc con = null;
106         PreparedStatement JavaDoc 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 JavaDoc 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 JavaDoc e) {
129             log.error("Error in DbMessageTreeWalker:getChild(" + index + ")-" , e);
130             
131         } finally {
132             try {
133                 pstmt.close();
134             } catch (Exception JavaDoc e) {
135                 log.error("" , e);
136             }
137             try {
138                 con.close();
139             } catch (Exception JavaDoc e) {
140                 log.error("" , e);
141             }
142         }
143         return message;
144     }
145     
146
147
148
149     /**
150      * Returns the number of children of parent. Returns 0 if the node is a
151      * leaf or if it has no children.
152      *
153      * @param parent a node in the tree, obtained from this data source.
154      * @returns the number of children of the node parent.
155      */

156     public int getChildCount(Message parent) {
157         int childCount = 0;
158         Connection JavaDoc con = null;
159         PreparedStatement JavaDoc 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 JavaDoc rs = pstmt.executeQuery();
174             rs.next();
175             childCount = rs.getInt(1);
176         } catch (Exception JavaDoc e) {
177             log.error("Error in DbTreeWalker:getChildCount()-" ,e);
178             
179         } finally {
180             try {
181                 pstmt.close();
182             } catch (Exception JavaDoc e) {
183                 log.error("" , e);
184             }
185             try {
186                 con.close();
187             } catch (Exception JavaDoc e) {
188                 log.error("" , e);
189             }
190         }
191         return childCount;
192     }
193
194     /**
195      * Returns the total number of recursive children of a parent. Returns 0
196      * if there are no children. This method is not intended to aid in
197      * navigation of a Thread but is included as an added utility.
198      */

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     /**
213      * Returns the index of child in parent.
214      */

215     public int getIndexOfChild(Message parent, Message child) {
216         int index = 0;
217         Connection JavaDoc con = null;
218         PreparedStatement JavaDoc pstmt = null;
219         ResultSet JavaDoc 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 JavaDoc e) {
242             log.error("Error in DbTreeWalker:getIndexOfChild()-" , e);
243         } finally {
244             try {
245                 pstmt.close();
246             } catch (Exception JavaDoc e) {
247                 log.error("" , e);
248             }
249             try {
250                 con.close();
251             } catch (Exception JavaDoc e) {
252                 log.error("" , e);
253             }
254         }
255         return index;
256     }
257
258     /**
259      * Returns true if node is a leaf. A node is a leaf when it has no children
260      * messages.
261      *
262      * @param node a node in the tree, obtained from this data source
263      * @returns true if node is a leaf
264      */

265     public boolean isLeaf(Message node) {
266         return (getChildCount(node) == 0);
267     }
268 }
269
Popular Tags