//===========================================================================
// package
//===========================================================================
package jp.gr.java_conf.bugslife.bug;

//===========================================================================
// import
//===========================================================================
// Java core API
import java.util.*;
import java.text.*;
import org.apache.commons.logging.*;

// DB API
import jp.gr.java_conf.bugslife.db.*;

// Util API
import jp.gr.java_conf.bugslife.util.*;
//import jp.gr.java_conf.bugslife.system.*;


/**
 *<PRE>
 * 
 * BugDependsクラス
 * 
 *</PRE>
 * @version	0.0, 2002/09/24
 * @author	末廣 琢磨
 */
//===========================================================================
public class BugDependsTree implements Comparator
{
//===========================================================================
// attributes
//===========================================================================
	/**
	 *	バグ番号
	 */
	private int m_bno = 0;
	/**
	 *	子バグTree
	 */
	private TreeSet m_childTree = null;
	/**
	 *	親バグTree
	 */
	private TreeSet m_parentTree = null;
	/**
	 *	HTMLテキスト
	 */
	private StringBuffer m_sb = null;
	/**
	 * Log
	 */
	private Log log = LogFactory.getLog(this.getClass().getName());
//===========================================================================
// methods
//===========================================================================
	/**
	 *	コンストラクタ
	 *
	 * @parem	int	バグ番号
	 */
	public BugDependsTree(int bno)
	{
		m_bno = bno;
	}

	/**
	 *	バグ番号の取得
	 */
	public int getBno()
	{
		return m_bno;
	}
	/**
	 *	子Treeの生成
	 */
	public TreeSet getChilds()
	{
		m_childTree = new TreeSet(this);
		Collection collection = BugDepends.getBugDepends(m_bno);
		if (collection != null) {
			Iterator ite = collection.iterator();
			while (ite.hasNext()) {
				m_childTree.add(new BugDependsTree(((BugDepends)ite.next()).getDependBno()));
			}
		}
		return m_childTree;
	}
	/**
	 *	全ての子Treeの生成
	 */
	public TreeSet getAllChilds()
	{
		TreeSet set = new TreeSet(this);
		getAllChilds(this, set);
		return set;
	}
	/**
	 *	全ての子Treeの生成(内部用)
	 */
	private TreeSet getAllChilds(BugDependsTree tree, TreeSet set)
	{
		BugDependsTree childTree = null;
		Iterator ite = tree.getChilds().iterator();
		while (ite.hasNext()) {
			childTree = (BugDependsTree)ite.next();
			if (set.add(childTree)) {
				getAllChilds(childTree, set);
			}
		}
		return set;
	}
	/**
	 *	親Treeの生成
	 */
	public TreeSet getParents()
	{
		m_parentTree = new TreeSet(this);
		Collection collection = BugDepends.getChildBugDepends(m_bno);
		if (collection != null) {
			Iterator ite = collection.iterator();
			while (ite.hasNext()) {
				m_parentTree.add(new BugDependsTree(((BugDepends)ite.next()).getBno()));
			}
		}
		return m_parentTree;
	}
	/**
	 *	全ての親Treeの生成
	 */
	public TreeSet getAllParents()
	{
		TreeSet set = new TreeSet(this);
		getAllParents(this, set);
		return set;
	}
	/**
	 *	全ての親Treeの生成(内部用)
	 */
	private TreeSet getAllParents(BugDependsTree tree, TreeSet set)
	{
		BugDependsTree parentTree = null;
		Iterator ite = tree.getParents().iterator();
		while (ite.hasNext()) {
			parentTree = (BugDependsTree)ite.next();
			if (set.add(parentTree)) {
				getAllParents(parentTree, set);
			}
		}
		return set;
	}
	/**
	 *	HTMLの生成
	 */
	public String getHtml()
	{
		if (m_sb == null) {
			m_sb = new StringBuffer();
			//バグインスタンスの生成
			Bug bug = new Bug(m_bno);
			if (bug != null) {
				m_sb.append("<li>");
				m_sb.append("<a href=\"../showAll.do?bno=" + m_bno + "\">");
				m_sb.append(bug.getBnoString());					//バグ番号
				m_sb.append(" ");
				m_sb.append(bug.getStatusString());				//ステータス
				m_sb.append(" ");
				m_sb.append("[" + bug.getDispatchName() + "]");	//ディスパッチ先名
				m_sb.append(" - ");
				m_sb.append(bug.getTitle());						//タイトル
				m_sb.append("</a>\n");
			} else {
				//あり得ないはずだが
				m_sb.append("<li><a href=\"#\">障害番号 = " + m_bno + "は存在しない</a>\n");
			}
		}
		return m_sb.toString();
	}
	/**
	 *	dispTreeの生成
	 */
	public String getDispChildTree()
	{
		StringBuffer sb = new StringBuffer();
		sb = dispChildTree(this, new StringBuffer());
		
		return sb.toString();
	}
	/**
	 *	dispChildTreeの生成
	 */
	private StringBuffer dispChildTree(BugDependsTree tree, StringBuffer sb)
	{
		BugDependsTree childTree = null;
		Iterator ite = tree.getChilds().iterator();
		while (ite.hasNext()) {
			childTree = (BugDependsTree)ite.next();
			sb.append("<ul>");
			sb.append(childTree.getHtml());
			dispChildTree(childTree, sb);
		}
		sb.append("</ul>");
		
		return sb;
	}
	/**
	 *	dispTreeの生成
	 */
	public String getDispParentTree()
	{
		StringBuffer sb = new StringBuffer();
		sb = dispParentTree(this, new StringBuffer());
		
		return sb.toString();
	}
	/**
	 *	dispParentTreeの生成
	 */
	private StringBuffer dispParentTree(BugDependsTree tree, StringBuffer sb)
	{
		BugDependsTree parentTree = null;
		Iterator ite = tree.getParents().iterator();
		while (ite.hasNext()) {
			parentTree = (BugDependsTree)ite.next();
			sb.append("<ul>");
			sb.append(parentTree.getHtml());
			dispParentTree(parentTree, sb);
		}
		sb.append("</ul>");
		
		return sb;
	}
	/**
	 *	comparaTo
	 */
	public int compare(Object o1, Object o2)
	{
		int ret = 0;
		
		if (((BugDependsTree)o1).getBno() < ((BugDependsTree)o2).getBno()) {
			ret = -1;
		} else if (((BugDependsTree)o1).getBno() == ((BugDependsTree)o2).getBno()) {
			ret = 0;
		} else if (((BugDependsTree)o1).getBno() > ((BugDependsTree)o2).getBno()) {
			ret = 1;
		}
		return ret;
	}

//===========================================================================
// methods
//===========================================================================
	/**
	 *	クラス情報
	 */
	public String toString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append("--- [BugDependsTree] --- \n");
		sb.append("bno      : " + m_bno + "\n");
		sb.append("html     : " + getHtml() + "\n");
		sb.append("--- [BugDependsTree] --- \n");
		return sb.toString();
	}
	/**
	 *	テストメイン
	 */
	public static void main(String[] args)
	{
		System.out.println("------- << BugDependsTree Test Start >> -----------");
		unitTest1();	//dispTreeテスト
		
		System.out.println("------- << BugDependsTree Test End >> -----------");
	}
	
	/**
	 *	unitTest1
	 *
	 * 新規作成テスト
	 *
	 */
	public static void unitTest1()
	{
		System.out.println("------- unitTest1 -----------");
		
		int bno = 6;	//バグ番号
		//値のセット
		BugDependsTree tree = new BugDependsTree(bno);
		
		System.out.println("[クラス情報]");
		System.out.println(tree.toString());
		
		System.out.println("[Tree表示HTML]");
		System.out.println(tree.getDispChildTree());
		
		System.out.println("[全ての子Tree情報]");
		Iterator ite = tree.getAllChilds().iterator();
		while (ite.hasNext()) {
			System.out.println("bno = " + ((BugDependsTree)ite.next()).getBno());
		}
		
		System.out.println("[全ての親Tree情報]");
		Iterator parentIte = tree.getAllParents().iterator();
		while (parentIte.hasNext()) {
			System.out.println("bno = " + ((BugDependsTree)parentIte.next()).getBno());
		}
		
	}

}