/*
 * Created on 2004-6-29
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package org.huihoo.willow.client;

import java.rmi.RemoteException;

import workflow.WorkflowException;
import workflow.live.rtm.LiveWork;
import workflow.live.store.LiveDispatcher;
import workflow.services.WorkflowService;
import workflow.wfmc.rtm.WorkflowCase;
import workflow.wfmc.rtm.WorkflowWork;
import workflow.wfmc.xpdl.WorkflowProcess;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class LiveDispatcherImpl extends LiveRemoteObject implements LiveDispatcher
{
	private WorkflowService workflowService;
	
	public LiveDispatcherImpl(WorkflowService workflowService)throws RemoteException
	{
		super();
		this.workflowService=workflowService;
	}
	
	public boolean    accept(String sessionID,LiveWork liveWork)   throws WorkflowException,RemoteException
	{	
		WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveWork.getPackageID()).getMainProcess();
		WorkflowCase workflowCase=workflowService.getCaseDatabase().findWorkflowCase(sessionID,workflowProcess,liveWork.getCaseID());
		String packageID=workflowCase.getWorkflowProcess().getWorkflowPackage().getUUID();
		if(!liveWork.getPackageID().equals(packageID))
		{
			throw new WorkflowException("crossWorkflow not supported");
		}
		
		WorkflowWork workflowWork=workflowService.getCaseDatabase().findWorkflowWork(sessionID,workflowProcess,workflowCase,liveWork.getUUID());		
		return workflowService.getWorkflowDispatcher().accept(sessionID,workflowWork);
	}
  
	public boolean    dispatch(String sessionID,LiveWork liveWork) throws WorkflowException,RemoteException
	{	
		WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveWork.getPackageID()).getMainProcess();
		WorkflowCase workflowCase=workflowService.getCaseDatabase().findWorkflowCase(sessionID,workflowProcess,liveWork.getCaseID());
		
		String packageID=workflowCase.getWorkflowProcess().getWorkflowPackage().getUUID();
		if(!liveWork.getPackageID().equals(packageID))
		{
			throw new WorkflowException("crossWorkflow not supported");
		}

		WorkflowWork workflowWork=workflowService.getCaseDatabase().findWorkflowWork(sessionID,workflowProcess,workflowCase,liveWork.getUUID());		

		return workflowService.getWorkflowDispatcher().dispatch(sessionID,workflowWork);
	}	
  
	public boolean    revert(String sessionID,LiveWork liveWork)   throws WorkflowException,RemoteException
	{	
		WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveWork.getPackageID()).getMainProcess();
		WorkflowCase workflowCase=workflowService.getCaseDatabase().findWorkflowCase(sessionID,workflowProcess,liveWork.getCaseID());
		String packageID=workflowCase.getWorkflowProcess().getWorkflowPackage().getUUID();
		if(!liveWork.getPackageID().equals(packageID))
		{
			throw new WorkflowException("crossWorkflow not supported");
		}
		
		WorkflowWork workflowWork=workflowService.getCaseDatabase().findWorkflowWork(sessionID,workflowProcess,workflowCase,liveWork.getUUID());		
		return workflowService.getWorkflowDispatcher().revert(sessionID,workflowWork);
	}	
  
	public boolean    fetch(String sessionID,LiveWork liveWork)    throws WorkflowException,RemoteException
	{	
		WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveWork.getPackageID()).getMainProcess();
		WorkflowCase workflowCase=workflowService.getCaseDatabase().findWorkflowCase(sessionID,workflowProcess,liveWork.getCaseID());
		String packageID=workflowCase.getWorkflowProcess().getWorkflowPackage().getUUID();
		if(!liveWork.getPackageID().equals(packageID))
		{
			throw new WorkflowException("crossWorkflow not supported");
		}
		
		WorkflowWork workflowWork=workflowService.getCaseDatabase().findWorkflowWork(sessionID,workflowProcess,workflowCase,liveWork.getUUID());		
		return workflowService.getWorkflowDispatcher().fetch(sessionID,workflowWork);
	}	

}
