/*
 * 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 java.util.ArrayList;
import java.util.List;

import workflow.WorkflowException;
import workflow.live.pm.LiveParticipant;
import workflow.live.rtm.LiveCase;
import workflow.live.rtm.LiveWork;
import workflow.live.store.LiveCaseDatabase;
import workflow.live.xpdl.LiveProcess;
import workflow.services.WorkflowService;
import workflow.store.CaseDatabase;
import workflow.store.UserDatabase;
import workflow.store.persistence.PrimaryKey;
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 LiveCaseDatabaseImpl extends LiveRemoteObject implements LiveCaseDatabase
{
	private transient WorkflowService workflowService;
	
	public LiveCaseDatabaseImpl(WorkflowService workflowService) throws RemoteException
	{
		super();
		this.workflowService = workflowService;
	}

	public CaseDatabase getWrappedObject()
	{
		return workflowService.getCaseDatabase();
	}
	
	//------------------------------------------------------------------------------------
	// LiveCase utilities   
	//------------------------------------------------------------------------------------                                        
	public String createLiveCase(String sessionID,
		LiveProcess liveProcess,
		LiveParticipant creator,
		java.util.Date creationTime,
		String primaryKey)
		throws WorkflowException, RemoteException
		{
			return createLiveCase(sessionID,liveProcess,creator,creationTime,primaryKey,"","");
		}
		
	public String createLiveCase(String sessionID,
	LiveProcess liveProcess,
		LiveParticipant creator,
		java.util.Date creationTime,
		String primaryKey,
		String name)
		throws WorkflowException, RemoteException
		{
			return createLiveCase(sessionID,liveProcess,creator,creationTime,primaryKey,name,"");
		}
	public String createLiveCase(String sessionID,
	LiveProcess liveProcess,
		LiveParticipant creator,
		java.util.Date creationTime,
		String primaryKey,
		String name,
		String description)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			UserDatabase userDatabase=workflowService.getUserDatabase();
			return caseDatabase.createWorkflowCase(sessionID,workflowProcess,userDatabase.findParticipant(creator.getUUID()),creationTime,primaryKey,name,description);
		}

	public LiveCase findLiveCase(String sessionID,LiveProcess liveProcess,String caseId)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			WorkflowCase liveCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,caseId);
			if(liveCase!=null)
			{
				return new LiveCase(liveCase);
			}
			return null;
		}
		
	public LiveCase findLiveCase(String sessionID,LiveProcess liveProcess,PrimaryKey primaryKey)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			WorkflowCase liveCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,primaryKey);
			if(liveCase!=null)
			{
				return new LiveCase(liveCase);
			}
			return null;
		}
		
	public List getLiveCaseList(String sessionID,LiveProcess liveProcess,LiveParticipant creator) throws WorkflowException, RemoteException
	{
		WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
		CaseDatabase caseDatabase=workflowService.getCaseDatabase();
		UserDatabase userDatabase=workflowService.getUserDatabase();
		List caseList=caseDatabase.getWorkflowCaseList(sessionID,workflowProcess,userDatabase.findParticipant(creator.getUUID()));	
		List result=new ArrayList();
		
		for(int i=0;i<caseList.size();++i)
		{
			result.add(new LiveCase((WorkflowCase)caseList.get(i)));
		}
		return result;
	}

	public void deleteLiveCase(String sessionID,LiveProcess liveProcess,LiveCase liveCase)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
			
			caseDatabase.deleteWorkflowCase(sessionID,workflowProcess,localCase);
		}
	public void cancelLiveCase(String sessionID,LiveProcess liveProcess,LiveCase liveCase)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
			
			caseDatabase.cancelWorkflowCase(sessionID,workflowProcess,localCase);			
		}
	public void suspendLiveCase(String sessionID,LiveProcess liveProcess,LiveCase liveCase)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();

			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
			
			caseDatabase.suspendWorkflowCase(sessionID,workflowProcess,localCase);		
		}
	public void resumeLiveCase(String sessionID,LiveProcess liveProcess,LiveCase liveCase)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();

			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
			
			caseDatabase.resumeWorkflowCase(sessionID,workflowProcess,localCase);		
		}

	//------------------------------------------------------------------------------------
	// LiveWork utilities
	//------------------------------------------------------------------------------------      

	public LiveWork findLiveWork(String sessionID,LiveProcess liveProcess,LiveCase liveCase, String workId)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();

			
			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
			
			WorkflowWork workflowWork=caseDatabase.findWorkflowWork(sessionID,workflowProcess,localCase,workId);
			if(workflowWork!=null)
			{
				return new LiveWork(workflowWork);
			}
			return null;
		}
		
	public List getLiveWorkList(String sessionID,LiveProcess liveProcess,LiveParticipant performer, int itemStatus)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			UserDatabase userDatabase=workflowService.getUserDatabase();
			
			List workList=caseDatabase.getWorkflowWorkList(sessionID,workflowProcess,userDatabase.findParticipant(performer.getUUID()),itemStatus);
		
			List result=new ArrayList();
		
			for(int i=0;i<workList.size();++i)
			{
				result.add(new LiveWork((WorkflowWork)workList.get(i)));
			}
			return result;
		}
		
	public List getLiveWorkList(String sessionID,LiveProcess liveProcess,
		LiveCase liveCase,
		LiveParticipant performer,
		int itemStatus)
		throws WorkflowException, RemoteException
		{
			WorkflowProcess workflowProcess=workflowService.findWorkflowPackage(liveProcess.getPackageID()).findWorkflowProcess(liveProcess.getUUID());
			CaseDatabase caseDatabase=workflowService.getCaseDatabase();
			UserDatabase userDatabase=workflowService.getUserDatabase();
			WorkflowCase localCase=caseDatabase.findWorkflowCase(sessionID,workflowProcess,liveCase.getUUID());
			String packageID=localCase.getWorkflowProcess().getWorkflowPackage().getUUID();
			if(!liveCase.getPackageID().equals(packageID))
			{
				throw new WorkflowException("crossWorkflow not supported");
			}
						
			List workList=caseDatabase.getWorkflowWorkList(sessionID,workflowProcess,localCase,userDatabase.findParticipant(performer.getUUID()),itemStatus);	
			List result=new ArrayList();
		
			for(int i=0;i<workList.size();++i)
			{
				result.add(new LiveWork((WorkflowWork)workList.get(i)));
			}
			return result;			
		}

}
