//----------------------------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.event;

import java.util.Map;

import org.huihoo.workflow.WorkflowTransaction;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.server.WorkflowXPDL;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.xpdl.WorkflowProcess;
import org.huihoo.workflow.xpdl.WorkflowTransition;

public class JoinEvent extends WorkflowEvent
{
	/**
	 * The first number in the range of ids used for join events.
	 */
	public static final int JOIN_FIRST = 400;

	/**
	 * The last number in the range of ids used for join events.
	 */
	public static final int JOIN_LAST = 407;

	public final static int JOIN_AND = JOIN_FIRST;
	public final static int JOIN_XOR = 1 + JOIN_FIRST;
	public final static int JOIN_OR = 2 + JOIN_FIRST;
	public final static int JOIN_WEIGHT = 3 + JOIN_FIRST;

	//for shared route (just used to predigest process)
	public final static int JOIN_ROUTE = 4 + JOIN_FIRST;

	//used for activity multi-instance (i.e meeting signature)
	public final static int JOIN_INSTANCE_AND = 5 + JOIN_FIRST;
	public final static int JOIN_INSTANCE_XOR = 6 + JOIN_FIRST;
	public final static int JOIN_INSTANCE_WEIGHT = 7 + JOIN_FIRST;

	//--------------------------------------------------
	protected boolean enventCompleted = false;
	protected JoinEventGuard eventGuard;

	//--------------------------------------------------
	protected WorkflowSession workflowSession;
	protected WorkflowXPDL workflowRuntime;
	protected WorkflowTransaction userTransaction;

	protected WorkflowProcess workflowProcess;
	protected WorkflowTransition workflowTransition;

	protected Map userObject;
	//----------------------------------------------------

	public JoinEvent(
		WorkflowWork source,
		int id,
		WorkflowSession workflowSession,
		WorkflowXPDL workflowRuntime,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowTransition workflowTransition,
		Map userObject)
	{
		super(source, id);
		this.workflowRuntime = workflowRuntime;
		this.workflowProcess = workflowProcess;
		this.workflowTransition = workflowTransition;

		this.userTransaction = userTransaction;

		this.userObject = userObject;
	}

	public synchronized JoinEventGuard getJoinEventGuard() throws InterruptedException
	{
		while (!enventCompleted)
		{
			wait();
		}

		return eventGuard;
	}

	//-----------------------------------------------------------------
	// Dispatch Interface
	//-----------------------------------------------------------------	
	public synchronized void dispatch()
	{
		eventGuard =
			JoinEventDispatcher.eventDispatch(
				workflowSession,
				workflowRuntime,
				userTransaction,
				workflowTransition,
				workflowProcess,
				(WorkflowWork) getSource(),
				userObject);
		enventCompleted = true;
		notifyAll();
	}
	
	/**
	 * Returns a string representing the state of this <code>Event</code>.
	 * This method is intended to be used only for debugging purposes, and the 
	 * content and format of the returned string may vary between 
	 * implementations. The returned string may be empty but may not be 
	 * <code>null</code>.
	 * 
	 * @return  a string representation of this event
	 */
	protected String paramString()
	{
		StringBuffer sb = new StringBuffer();
		sb.append(workflowTransition);
		return sb.toString();
	}
}
