//----------------------------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.WorkflowDepartment;
import org.huihoo.workflow.usermodel.WorkflowGroup;
import org.huihoo.workflow.usermodel.WorkflowParticipant;

public class WorkflowGroupImpl extends  WorkflowGroup
{

	//----------------------------------------------------------- Constructors
	/**
		* @param database The {@link WorkflowUserDatabase} that owns this group
		* @param groupid   Group id of this group
		* @param groupname Group name of this group
		* @param description Description of this group
		*/
	public WorkflowGroupImpl(
		WorkflowSession session,
		WorkflowUserDatabase database,
		String groupid,
		String groupname,
		String description)
	{
		super(groupid, groupname, description);
		this.database = database;
		this.session = session;
	}
	public WorkflowGroupImpl(
		WorkflowSession session,
		WorkflowUserDatabase database,
		String groupid,
		String groupname,
		String description,
		WorkflowGroup parentGroup)
	{
		super(groupid, groupname, description);
		this.database = database;
		this.session = session;
		this.parentGroup = parentGroup;
	}
	// ----------------------------------------------------- Instance Variables
	protected WorkflowGroup parentGroup;

	protected WorkflowParticipant president;

	protected WorkflowDepartment department;

	protected ArrayList participants = null;
	/**
	 * The {@link WorkflowUserDatabase} that owns this group.
	 */
	protected transient WorkflowUserDatabase database = null;
	protected transient WorkflowSession session = null;

	// ------------------------------------------------------------- Properties
	public WorkflowGroup getParentGroup()
	{
		return parentGroup;
	}
	/**
		* 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.
		*/
	public Iterator getParticipants()
	{
		if (participants == null)
		{
			participants = new ArrayList();
			genParticipantTree(participants);
		}

		return (participants.iterator());
	}

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

	public void addPaticipant(WorkflowParticipant user)
	{
	}

	public void removeParticipant(WorkflowParticipant user)
	{
	}

	public void removeParticipants()
	{
	}

	public void setDepartment(WorkflowDepartment department)
	{
		this.department = department;
	}

	public WorkflowDepartment getDepartment()
	{
		return department;
	}

	public boolean isInDepartment(WorkflowDepartment depart)
	{
		if (department != null && depart != null)
		{
			return department.getUUID().equals(depart.getUUID());
		}

		return false;
	}

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

	public WorkflowParticipant getPresident()
	{
		return president;
	}

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

		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;
		}
	}
}
