alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

jforum example source code file (TreeGroup.java)

This example jforum source code file (TreeGroup.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - jforum tags/keywords

arraylist, groupnode, groupnode, iterator, iterator, list, list, treegroup, treegroupdao, treegroupdao, util

The jforum TreeGroup.java source code

/*
 * Copyright (c) JForum Team
 * All rights reserved.

 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided 
 * that the following conditions are met:

 * 1) Redistributions of source code must retain the above 
 * copyright notice, this list of conditions and the 
 * following  disclaimer.
 * 2)  Redistributions in binary form must reproduce the 
 * above copyright notice, this list of conditions and 
 * the following disclaimer in the documentation and/or 
 * other materials provided with the distribution.
 * 3) Neither the name of "Rafael Steil" nor 
 * the names of its contributors may be used to endorse 
 * or promote products derived from this software without 
 * specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT 
 * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, 
 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 
 * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE 
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT 
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
 * 
 * This file creation date: Mar 3, 2003 / 11:28:25 AM
 * The JForum Project
 * http://www.jforum.net
 */
package net.jforum.util;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import net.jforum.dao.DataAccessDriver;
import net.jforum.dao.TreeGroupDAO;

/** 
 * Implements a tree hierarchy of groups.
 * This class process all group hierarchy, and each group may have unlimited sub groups.
 * Each group is called <code>node ( net.jforum.model.GroupNode object ), and
 * each node may have sub-nodes. For example, given a table like the folowing:  
 * 
 * <pre>
 * <code>
* +----+----------------+--------+
 * | id | name          | parent |
 * +----+---------------+--------+
 * |  6 | Parent 1      |      0 |
 * |  7 | Sub 1.1       |      6 |
 * |  8 | Sub 1.2       |      6 |
 * |  9 | SubSub 1.2.1  |      8 |
 * | 10 | SubSub 1.2.2  |      8 |
 * | 11 | Parent 2      |      0 |
 * | 12 | Parent 3      |      0 |
 * | 13 | Sub 3.1       |     12 |
 * | 14 | SubSub 3.1.1  |     13 |
 * | 15 | Sub 3.2       |     12 |
 * | 16 | Parent 4      |      0 |
 * +----+---------------+--------+
 * </code>
 * </pre>
 * 
 * results on the folowing hierarchy 
 * <pre>
 * <code>
 * Parent 1
 * ------
 * 	|
 *     Sub 1.1
 * 	----------
 * 	|
 * 	Sub 1.2
 * 	----------
 * 		|
 * 		SubSub 1.2.1
 * 		------------
 * 		|
 * 		SubSub 1.2.2
 * Parent 2
 * -----
 * Parent 3
 * -----
 * 	|
 * 	Sub 3.1
 * 	---------
 * 		|
 * 		SubSub 3.1.1
 * 		------------
 * 	|
 * 	Sub 3.2
 * 	---------
 * Parent 4
 * ------
 * </code>
 * </pre>
 *  
 * As is possible to see, we have 4 parent groups, called <code>Parent 1, Parent 2, 
 * <code>Parent 3 and Parent 4. Parent 1 has 2 sub groups: Sub 1.1
 * and <code>Sub 1.2. Sub 1.2 contains 2 subgroups, SubSub 1.2.1 and 
 * <code>SubSub 1.2.2. As every group is a node, ( GroupNode object ), and as each node
 * may have sub-nodes, the processing would be as:
 * <p>
 * <li> When the method size() of the Parent 1 object is called,  the number 2 will
 * be retorned, because <code>Parent 1 has 2 sub groups;
 * <li> when the size() method is called on the object of Sub 1.1, will be returned 0, because
 * <code>Sub 1.1 does not have any sub groups;
 * <li> On the other hand, then we call the size() method of the object represented by Sub 1.2 object,
 * we wil have a return value of 2, because <code>Sub 1.2 has 2 sub groups.
 * <br>
 * The same operation is done to all other groups and its sub groups. 
 * 
 * @author Rafael Steil
 * @version $Id: TreeGroup.java,v 1.10 2006/08/20 22:47:42 rafaelsteil Exp $
 */
public class TreeGroup 
{
	/**
	 * Default Constructor
	 */
	public TreeGroup() { }

	
	/**
	 * Process the group hierarchy.
	 * 
	 * @return <code>List containing the complete group hierarchy. Each element
	 * from the list represents a single <code>GroupNode object.	 
	 * */
	public List getNodes()
	{
		List nodes = new ArrayList();
		
		TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();

		List rootGroups = tgm.selectGroups(0);	
				
		for (Iterator iter = rootGroups.iterator(); iter.hasNext();) {
			GroupNode n = (GroupNode)iter.next();
						
			this.checkExtraNodes(n);
			
			nodes.add(n);
		}
		
		return nodes;
	}
	
	/**
	 * Searchs for subgroups of a determined group
	 *
     * @param n  GroupNode
     */
	private void checkExtraNodes(GroupNode n)
	{
		TreeGroupDAO tgm = DataAccessDriver.getInstance().newTreeGroupDAO();

		List childGroups = tgm.selectGroups(n.getId());	
				
		for (Iterator iter = childGroups.iterator(); iter.hasNext();) {
			GroupNode f = (GroupNode)iter.next();
			
			this.checkExtraNodes(f);
			
			n.addNode(f);
		}
	}
}

Other jforum examples (source code examples)

Here is a short list of links related to this jforum TreeGroup.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.