//----------------------------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.store;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.huihoo.workflow.WorkflowException;
import org.huihoo.workflow.WorkflowTransaction;
import org.huihoo.workflow.server.WorkflowSession;
import org.huihoo.workflow.server.WorkflowWork;
import org.huihoo.workflow.store.WorkflowDispatcher;
import org.huihoo.workflow.store.WorkflowTemplate;
import org.huihoo.workflow.xpdl.WorkflowActivity;
import org.huihoo.workflow.xpdl.WorkflowPackage;
import org.huihoo.workflow.xpdl.WorkflowProcess;

public class WorkflowDistributeImpl implements WorkflowDispatcher
{
	private static Log log = LogFactory.getLog("willow.WorkflowDistribute");

	private WorkflowTemplate workflowemplate;

	public WorkflowDistributeImpl(WorkflowTemplate workflowemplate)
	{
		setWorkflowTemplate(workflowemplate);
	}

	//------------------------------------------------------------------
	public WorkflowTemplate getWorkflowTemplate()
	{
		return this.workflowemplate;
	}

	public void setWorkflowTemplate(WorkflowTemplate workflowemplate)
	{
		this.workflowemplate = workflowemplate;
	}

	public boolean accept(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work)
		throws WorkflowException
	{
		log.debug("workitem: " + work);

		WorkflowActivity workflowActivity = work.getActivity();
		WorkflowPackage workflowPackage = workflowProcess.getWorkflowPackage();

		return ControllerDispatcher.accept_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work);
	}
	public boolean resubmit(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work)
		throws WorkflowException
	{
		Map infoMap = org.huihoo.workflow.store.Constants.createMap();
		org.huihoo.workflow.store.Constants.setDispatchStyle(infoMap, org.huihoo.workflow.store.Constants.RESUBMIT);

		return ControllerDispatcher.dispatch_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work,
			infoMap);
	}

	public boolean submit(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work,
		Map infoMap)
		throws WorkflowException
	{
		log.debug("workitem: " + work);

		return ControllerDispatcher.dispatch_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work,
			infoMap);
	}

	public boolean reverse(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work)
		throws WorkflowException
	{
		Map infoMap = org.huihoo.workflow.store.Constants.createMap();
		org.huihoo.workflow.store.Constants.setDispatchStyle(infoMap, org.huihoo.workflow.store.Constants.REVERSE);

		return ControllerDispatcher.dispatch_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work,infoMap);
	}

	public boolean backtrack(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work)
		throws WorkflowException
	{
		log.debug("workitem: " + work);
		Map infoMap = org.huihoo.workflow.store.Constants.createMap();
		org.huihoo.workflow.store.Constants.setDispatchStyle(infoMap, org.huihoo.workflow.store.Constants.BACKTRACK);

		return ControllerDispatcher.dispatch_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work,infoMap);
	}

	public boolean fetch(
		WorkflowSession session,
		WorkflowTransaction userTransaction,
		WorkflowProcess workflowProcess,
		WorkflowWork work)
		throws WorkflowException
	{
		log.debug("workitem: " + work);

		return ControllerDispatcher.fetch_core(
			session,
			workflowemplate.getOwnerWorkflowXPDL(),
			userTransaction,
			workflowProcess,
			work);
	}
}
