//----------------------------BEGIN LICENSE----------------------------
/* 
 * Willow : the Open Source WorkFlow Project
 * Distributable under GNU LGPL license by gun.org
 *
 * Copyright (C) 2004-2010 huihoo.org 
 * Copyright (C) 2004-2010  ZosaTapo <dertyang@hotmail.com> 
 * 
 * ====================================================================
 * Project Homepage : http://www.huihoo.org/willow
 * Source Forge     : http://sourceforge.net/projects/huihoo
 * Mailing list     : willow@lists.sourceforge.net 
 */
//----------------------------END  LICENSE-----------------------------
package org.huihoo.workflow.coreImpl.usermodel;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;

import org.huihoo.commons.jdbc.JDBCFactory;
import org.huihoo.commons.jdbc.JDBCUtilities;
import org.huihoo.workflow.SchemaContext;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.store.WorkflowUserDatabase;
import org.huihoo.workflow.usermodel.WorkflowCategory;
import org.huihoo.workflow.usermodel.WorkflowDepartment;
import org.huihoo.workflow.usermodel.WorkflowGroup;
import org.huihoo.workflow.usermodel.WorkflowParticipant;



public class WorkflowDepartmentImpl extends WorkflowDepartment
{
	protected WorkflowCategory category;
	
	
	public WorkflowCategory getCategory()
	{
		return category;
	}

	public void setCategory(WorkflowCategory departCategory)
	{
		this.category = departCategory;
	}
	//----------------------------------------------------------- Constructors
	/**
		* @param database The {@link WorkflowUserDatabase} that owns this department
		* @param departmentid   Group id of this department
		* @param departmentname Group name of this department
		* @param description Description of this department
		*/
	public WorkflowDepartmentImpl(WorkflowSession session,
	WorkflowUserDatabase database,
		String departmentid,
		String departmentname,
		String description)
	{
		super(departmentid, departmentname, description);
		this.database = database;
		this.session = session;
	}
	public WorkflowDepartmentImpl(WorkflowSession session,
	WorkflowUserDatabase database,
		String departmentid,
		String departmentname,
		String description,
		WorkflowDepartment parentDepartment)
	{
		super(departmentid, departmentname, description);
		this.database = database;
		this.session = session;
		this.parentDepartment = parentDepartment;
	}
	// ----------------------------------------------------- Instance Variables
	protected WorkflowDepartment parentDepartment;
	protected WorkflowParticipant president;

	/**
		* The {@link WorkflowUserDatabase} that owns this group.
		*/
	protected transient WorkflowUserDatabase database = null;
	protected transient WorkflowSession session;
	
	// ------------------------------------------------------------- Properties
	public WorkflowDepartment getParentDepartment()
	{
		return parentDepartment;
	}
	/**
		* Return the {@link WorkflowUserDatabase} within which this Group is defined.
		*/
	public WorkflowUserDatabase getUserDatabase()
	{
		return (this.database);
	}
	/**
	 * Return the set of {@link WorkflowParticipant}s that are members of this group.
	 */
	protected ArrayList participants = null;
	public Iterator getParticipants()
	{
		if (participants == null)
		{
			participants = new ArrayList();
			genParticipantTree(participants);
		}

		return (participants.iterator());
	}

	/**
	 * Return the set of {@link WorkflowParticipant}s that are members of this group.
	 */
	protected ArrayList groups = null;
	public Iterator getGroups()
	{
		if (groups == null)
		{
			groups = new ArrayList();
			genGroupTree(groups);
		}

		return (groups.iterator());
	}

	// --------------------------------------------------------- Public Methods

	public void addPaticipant(WorkflowParticipant user)
	{
	}

	public void removeParticipant(WorkflowParticipant user)
	{
	}

	public void removeParticipants()
	{
	}

	public void addGroup(WorkflowGroup group)
	{
	}

	public void removeGroup(WorkflowGroup group)
	{
	}

	public void removeGroups()
	{
	}

	public void setPresident(WorkflowParticipant president)
	{
		this.president = president;
	}

	public WorkflowParticipant getPresident()
	{
		return president;
	}

	//---------------------------------------------------------------------------------
	// generate group tree
	//---------------------------------------------------------------------------------
	private void genGroupTree(ArrayList groups)
	{
		String strSQL =
			"SELECT vc_uuid FROM "
				+ database.getSchemaContext().getTableName(SchemaContext.SCHEMA_GROUP)
				+ " "
				+ "WHERE vc_departmentid = ?";

		Connection conn = null;
		PreparedStatement pstmt = null;

		try
		{
			conn = JDBCFactory.getConnection(database.getStore());
			pstmt = conn.prepareStatement(strSQL);

			pstmt.setString(1, getUUID());

			ResultSet rs = pstmt.executeQuery();

			while (rs.next())
			{
				groups.add(database.findGroup(session,rs.getString("vc_uuid")));
			}
		}
		catch (SQLException ex)
		{
			;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(conn, pstmt);
		}
	}

	//---------------------------------------------------------------------------------
	// generate WorkflowParticipant tree
	//---------------------------------------------------------------------------------	
	public void genParticipantTree(ArrayList participants)
	{
		String strSQL =
			"SELECT vc_userid FROM "
				+ database.getSchemaContext().getTableName(SchemaContext.SCHEMA_USER_DEPARTMENT)
				+ " "
				+ "WHERE vc_departmenetid = ?";

		Connection conn = null;
		PreparedStatement pstmt = null;

		try
		{
			conn = JDBCFactory.getConnection(database.getStore());
			pstmt = conn.prepareStatement(strSQL);

			pstmt.setString(1, getUUID());

			ResultSet rs = pstmt.executeQuery();

			while (rs.next())
			{
				participants.add(database.findParticipant(session,rs.getString("vc_userid")));
			}
		}
		catch (SQLException ex)
		{
			;
		}
		finally
		{
			JDBCUtilities.cleanupNoThrow(conn, pstmt);
		}
	}
	
	public String toString()
	{
		if (getName() != null)
		{
			return "name=" + getName();
		}
		else
		{
			return "uuid=" + uuid;
		}
	}
}
