TreeGroup.java
Upload User: gdxydsw
Upload Date: 2019-01-29
Package Size: 16721k
Code Size: 5k
Category:

Java Develop

Development Platform:

Java

  1. /*
  2.  * Copyright (c) JForum Team
  3.  * All rights reserved.
  4.  * Redistribution and use in source and binary forms, 
  5.  * with or without modification, are permitted provided 
  6.  * that the following conditions are met:
  7.  * 1) Redistributions of source code must retain the above 
  8.  * copyright notice, this list of conditions and the 
  9.  * following  disclaimer.
  10.  * 2)  Redistributions in binary form must reproduce the 
  11.  * above copyright notice, this list of conditions and 
  12.  * the following disclaimer in the documentation and/or 
  13.  * other materials provided with the distribution.
  14.  * 3) Neither the name of "Rafael Steil" nor 
  15.  * the names of its contributors may be used to endorse 
  16.  * or promote products derived from this software without 
  17.  * specific prior written permission.
  18.  * 
  19.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
  20.  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
  21.  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
  22.  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
  23.  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  24.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
  25.  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
  26.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
  27.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
  28.  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  29.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  30.  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
  31.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
  32.  * IN CONTRACT, STRICT LIABILITY, OR TORT 
  33.  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
  34.  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
  35.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  36.  * 
  37.  * This file creation date: Mar 3, 2003 / 11:28:25 AM
  38.  * The JForum Project
  39.  * http://www.jforum.net
  40.  */
  41. package net.jforum.util;
  42. import java.util.ArrayList;
  43. import java.util.Iterator;
  44. import java.util.List;
  45. import net.jforum.dao.DataAccessDriver;
  46. import net.jforum.dao.TreeGroupDAO;
  47. /** 
  48.  * Implements a tree hierarchy of groups.
  49.  * This class process all group hierarchy, and each group may have unlimited sub groups.
  50.  * Each group is called <code>node</code> ( <code>net.jforum.model.GroupNode</code> object ), and
  51.  * each node may have sub-nodes. For example, given a table like the folowing:  
  52.  * 
  53.  * <pre>
  54.  * <code>
  55. * +----+----------------+--------+
  56.  * | id | name          | parent |
  57.  * +----+---------------+--------+
  58.  * |  6 | Parent 1      |      0 |
  59.  * |  7 | Sub 1.1       |      6 |
  60.  * |  8 | Sub 1.2       |      6 |
  61.  * |  9 | SubSub 1.2.1  |      8 |
  62.  * | 10 | SubSub 1.2.2  |      8 |
  63.  * | 11 | Parent 2      |      0 |
  64.  * | 12 | Parent 3      |      0 |
  65.  * | 13 | Sub 3.1       |     12 |
  66.  * | 14 | SubSub 3.1.1  |     13 |
  67.  * | 15 | Sub 3.2       |     12 |
  68.  * | 16 | Parent 4      |      0 |
  69.  * +----+---------------+--------+
  70.  * </code>
  71.  * </pre>
  72.  * 
  73.  * results on the folowing hierarchy 
  74.  * <pre>
  75.  * <code>
  76.  * Parent 1
  77.  * ------
  78.  *  |
  79.  *     Sub 1.1
  80.  *  ----------
  81.  *  |
  82.  *  Sub 1.2
  83.  *  ----------
  84.  *  |
  85.  *  SubSub 1.2.1
  86.  *  ------------
  87.  *  |
  88.  *  SubSub 1.2.2
  89.  * Parent 2
  90.  * -----
  91.  * Parent 3
  92.  * -----
  93.  *  |
  94.  *  Sub 3.1
  95.  *  ---------
  96.  *  |
  97.  *  SubSub 3.1.1
  98.  *  ------------
  99.  *  |
  100.  *  Sub 3.2
  101.  *  ---------
  102.  * Parent 4
  103.  * ------
  104.  * </code>
  105.  * </pre>
  106.  *  
  107.  * As is possible to see, we have 4 parent groups, called <code>Parent 1</code>, <code>Parent 2</code>, 
  108.  * <code>Parent 3</code> and <code>Parent 4</code>. <code>Parent 1</code> has 2 sub groups: <code>Sub 1.1</code>
  109.  * and <code>Sub 1.2</code>. <code>Sub 1.2</code> contains 2 subgroups, <code>SubSub 1.2.1</code> and 
  110.  * <code>SubSub 1.2.2</code>. As every group is a node, ( <code>GroupNode</code> object ), and as each node
  111.  * may have sub-nodes, the processing would be as:
  112.  * <p>
  113.  * <li> When the method <code>size()</code> of the <code>Parent 1</code> object is called,  the number 2 will
  114.  * be retorned, because <code>Parent 1</code> has 2 sub groups;
  115.  * <li> when the <code>size()</code> method is called on the object of <code>Sub 1.1</code>, will be returned 0, because
  116.  * <code>Sub 1.1</code> does not have any sub groups;
  117.  * <li> On the other hand, then we call the <code>size()</code> method of the object represented by <code>Sub 1.2</code> object,
  118.  * we wil have a return value of 2, because <code>Sub 1.2</code> has 2 sub groups.
  119.  * <br>
  120.  * The same operation is done to all other groups and its sub groups. 
  121.  * 
  122.  * @author Rafael Steil
  123.  * @version $Id: TreeGroup.java,v 1.10 2006/08/20 22:47:42 rafaelsteil Exp $
  124.  */
  125. public class TreeGroup 
  126. {
  127. /**
  128.  * Default Constructor
  129.  */
  130. public TreeGroup() { }
  131. /**
  132.  * Process the group hierarchy.
  133.  * 
  134.  * @return <code>List</code> containing the complete group hierarchy. Each element
  135.  * from the list represents a single <code>GroupNode<code> object.  
  136.  * */
  137. public List getNodes()
  138. {
  139. List nodes = new ArrayList();
  140. TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();
  141. List rootGroups = tgm.selectGroups(0);
  142. for (Iterator iter = rootGroups.iterator(); iter.hasNext();) {
  143. GroupNode n = (GroupNode)iter.next();
  144. this.checkExtraNodes(n);
  145. nodes.add(n);
  146. }
  147. return nodes;
  148. }
  149. /**
  150.  * Searchs for subgroups of a determined group
  151.  *
  152.      * @param n  GroupNode
  153.      */
  154. private void checkExtraNodes(GroupNode n)
  155. {
  156. TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();
  157. List childGroups = tgm.selectGroups(n.getId());
  158. for (Iterator iter = childGroups.iterator(); iter.hasNext();) {
  159. GroupNode f = (GroupNode)iter.next();
  160. this.checkExtraNodes(f);
  161. n.addNode(f);
  162. }
  163. }
  164. }