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



public class WorkflowRoleImpl extends WorkflowRole
{
	public WorkflowRoleImpl(WorkflowSession session,WorkflowUserDatabase database, String uuid_, String name)
	{
		this(session,database, uuid_, name, null);
	}

	public WorkflowRoleImpl(WorkflowSession session,
	WorkflowUserDatabase database,
		String uuid_,
		String name,
		String description)
	{
		super(uuid_, name, description);
		this.database = database;
	}

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

	public WorkflowUserDatabase getUserDatabase()
	{
		return 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());
	}
	// --------------------------------------------------------- Public Methods

	public void addPaticipant(WorkflowParticipant user)
	{
	}

	public void removeParticipant(WorkflowParticipant user)
	{
	}

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

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