001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004:
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008:
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * This file creation date: Mar 3, 2003 / 11:28:25 AM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.util;
044:
045: import java.util.ArrayList;
046: import java.util.Iterator;
047: import java.util.List;
048:
049: import net.jforum.dao.DataAccessDriver;
050: import net.jforum.dao.TreeGroupDAO;
051:
052: /**
053: * Implements a tree hierarchy of groups.
054: * This class process all group hierarchy, and each group may have unlimited sub groups.
055: * Each group is called <code>node</code> ( <code>net.jforum.model.GroupNode</code> object ), and
056: * each node may have sub-nodes. For example, given a table like the folowing:
057: *
058: * <pre>
059: * <code>
060: * +----+----------------+--------+
061: * | id | name | parent |
062: * +----+---------------+--------+
063: * | 6 | Parent 1 | 0 |
064: * | 7 | Sub 1.1 | 6 |
065: * | 8 | Sub 1.2 | 6 |
066: * | 9 | SubSub 1.2.1 | 8 |
067: * | 10 | SubSub 1.2.2 | 8 |
068: * | 11 | Parent 2 | 0 |
069: * | 12 | Parent 3 | 0 |
070: * | 13 | Sub 3.1 | 12 |
071: * | 14 | SubSub 3.1.1 | 13 |
072: * | 15 | Sub 3.2 | 12 |
073: * | 16 | Parent 4 | 0 |
074: * +----+---------------+--------+
075: * </code>
076: * </pre>
077: *
078: * results on the folowing hierarchy
079: * <pre>
080: * <code>
081: * Parent 1
082: * ------
083: * |
084: * Sub 1.1
085: * ----------
086: * |
087: * Sub 1.2
088: * ----------
089: * |
090: * SubSub 1.2.1
091: * ------------
092: * |
093: * SubSub 1.2.2
094: * Parent 2
095: * -----
096: * Parent 3
097: * -----
098: * |
099: * 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.10 2006/08/20 22:47:42 rafaelsteil Exp $
129: */
130: public class TreeGroup {
131: /**
132: * Default Constructor
133: */
134: public TreeGroup() {
135: }
136:
137: /**
138: * Process the group hierarchy.
139: *
140: * @return <code>List</code> containing the complete group hierarchy. Each element
141: * from the list represents a single <code>GroupNode<code> object.
142: * */
143: public List getNodes() {
144: List nodes = new ArrayList();
145:
146: TreeGroupDAO tgm = DataAccessDriver.getInstance()
147: .newTreeGroupDAO();
148:
149: List rootGroups = tgm.selectGroups(0);
150:
151: for (Iterator iter = rootGroups.iterator(); iter.hasNext();) {
152: GroupNode n = (GroupNode) iter.next();
153:
154: this .checkExtraNodes(n);
155:
156: nodes.add(n);
157: }
158:
159: return nodes;
160: }
161:
162: /**
163: * Searchs for subgroups of a determined group
164: *
165: * @param n GroupNode
166: */
167: private void checkExtraNodes(GroupNode n) {
168: TreeGroupDAO tgm = DataAccessDriver.getInstance()
169: .newTreeGroupDAO();
170:
171: List childGroups = tgm.selectGroups(n.getId());
172:
173: for (Iterator iter = childGroups.iterator(); iter.hasNext();) {
174: GroupNode f = (GroupNode) iter.next();
175:
176: this.checkExtraNodes(f);
177:
178: n.addNode(f);
179: }
180: }
181: }
|