KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > util > TreeGroup


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * This file creation date: Mar 3, 2003 / 11:28:25 AM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.util;
44
45 import java.util.ArrayList JavaDoc;
46 import java.util.Iterator JavaDoc;
47 import java.util.List JavaDoc;
48
49 import net.jforum.dao.DataAccessDriver;
50 import net.jforum.dao.TreeGroupDAO;
51
52 /**
53  * Implements a tree hierarchy of groups.
54  * This class process all group hierarchy, and each group may have unlimited sub groups.
55  * Each group is called <code>node</code> ( <code>net.jforum.model.GroupNode</code> object ), and
56  * each node may have sub-nodes. For example, given a table like the folowing:
57  *
58  * <pre>
59  * <code>
60 * +----+----------------+--------+
61  * | id | name | parent |
62  * +----+---------------+--------+
63  * | 6 | Parent 1 | 0 |
64  * | 7 | Sub 1.1 | 6 |
65  * | 8 | Sub 1.2 | 6 |
66  * | 9 | SubSub 1.2.1 | 8 |
67  * | 10 | SubSub 1.2.2 | 8 |
68  * | 11 | Parent 2 | 0 |
69  * | 12 | Parent 3 | 0 |
70  * | 13 | Sub 3.1 | 12 |
71  * | 14 | SubSub 3.1.1 | 13 |
72  * | 15 | Sub 3.2 | 12 |
73  * | 16 | Parent 4 | 0 |
74  * +----+---------------+--------+
75  * </code>
76  * </pre>
77  *
78  * results on the folowing hierarchy
79  * <pre>
80  * <code>
81  * Parent 1
82  * ------
83  * |
84  * Sub 1.1
85  * ----------
86  * |
87  * Sub 1.2
88  * ----------
89  * |
90  * SubSub 1.2.1
91  * ------------
92  * |
93  * SubSub 1.2.2
94  * Parent 2
95  * -----
96  * Parent 3
97  * -----
98  * |
99  * Sub 3.1
100  * ---------
101  * |
102  * SubSub 3.1.1
103  * ------------
104  * |
105  * Sub 3.2
106  * ---------
107  * Parent 4
108  * ------
109  * </code>
110  * </pre>
111  *
112  * As is possible to see, we have 4 parent groups, called <code>Parent 1</code>, <code>Parent 2</code>,
113  * <code>Parent 3</code> and <code>Parent 4</code>. <code>Parent 1</code> has 2 sub groups: <code>Sub 1.1</code>
114  * and <code>Sub 1.2</code>. <code>Sub 1.2</code> contains 2 subgroups, <code>SubSub 1.2.1</code> and
115  * <code>SubSub 1.2.2</code>. As every group is a node, ( <code>GroupNode</code> object ), and as each node
116  * may have sub-nodes, the processing would be as:
117  * <p>
118  * <li> When the method <code>size()</code> of the <code>Parent 1</code> object is called, the number 2 will
119  * be retorned, because <code>Parent 1</code> has 2 sub groups;
120  * <li> when the <code>size()</code> method is called on the object of <code>Sub 1.1</code>, will be returned 0, because
121  * <code>Sub 1.1</code> does not have any sub groups;
122  * <li> On the other hand, then we call the <code>size()</code> method of the object represented by <code>Sub 1.2</code> object,
123  * we wil have a return value of 2, because <code>Sub 1.2</code> has 2 sub groups.
124  * <br>
125  * The same operation is done to all other groups and its sub groups.
126  *
127  * @author Rafael Steil
128  * @version $Id: TreeGroup.java,v 1.8 2005/11/30 13:17:10 rafaelsteil Exp $
129  */

130 public class TreeGroup
131 {
132     /**
133      * Default Constructor
134      */

135     public TreeGroup() { }
136
137     
138     /**
139      * Process the group hierarchy.
140      *
141      * @return <code>List</code> containing the complete group hierarchy. Each element
142      * from the list represents a single <code>GroupNode<code> object.
143      * */

144     public List JavaDoc getNodes() throws Exception JavaDoc
145     {
146         List JavaDoc nodes = new ArrayList JavaDoc();
147         
148         TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();
149
150         List JavaDoc rootGroups = tgm.selectGroups(0);
151                 
152         for (Iterator JavaDoc iter = rootGroups.iterator(); iter.hasNext();) {
153             GroupNode n = (GroupNode)iter.next();
154                         
155             this.checkExtraNodes(n);
156             
157             nodes.add(n);
158         }
159         
160         return nodes;
161     }
162     
163     /**
164      * Searchs for subgroups of a determined group
165      * */

166     private void checkExtraNodes(GroupNode n) throws Exception JavaDoc
167     {
168         TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();
169
170         List JavaDoc childGroups = tgm.selectGroups(n.getId());
171                 
172         for (Iterator JavaDoc iter = childGroups.iterator(); iter.hasNext();) {
173             GroupNode f = (GroupNode)iter.next();
174             
175             this.checkExtraNodes(f);
176             
177             n.addNode(f);
178         }
179     }
180 }
181
Popular Tags