/*
 * Created on 2004-6-27
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
package workflow.test;

import java.rmi.RemoteException;
import java.util.Date;
import java.util.Hashtable;

import javax.naming.NamingException;

import workflow.WorkflowException;
import workflow.client.InitialLiveClient;
import workflow.client.WorkflowClient;
import workflow.live.LiveService;
import workflow.live.pm.LiveParticipant;
import workflow.live.rtm.LiveCase;
import workflow.live.rtm.LiveWork;
import workflow.live.store.LiveCaseDatabase;
import workflow.live.store.LiveUserDatabase;
import workflow.live.xpdl.LivePackage;
import workflow.live.xpdl.LiveProcess;
import workflow.wfmc.rtm.Constants;

/**
 * @author reic
 *
 * To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Generation - Code and Comments
 */
public class WillowClientTest
{

	public static void main(String[] args) throws NamingException, ClassNotFoundException, RemoteException, WorkflowException
	{
		Hashtable env=new Hashtable();
		env.put(WorkflowClient.INITIAL_CLIENT_FACTORY,"org.huihoo.willow.client.LiveClientFactoryImpl");
		env.put(WorkflowClient.PROVIDER_URL,"workflow://localhost:2126");
		env.put(WorkflowClient.SECURITY_PRINCIPAL,"zosatapo");
		env.put(WorkflowClient.SECURITY_CREDENTIALS,"zosatapo");
		
		InitialLiveClient liveClient=new InitialLiveClient(env);
		
		LiveService liveService=liveClient.findService("examples");
		System.err.println("workflow="+liveService.getName());
			
		LiveUserDatabase userDatabase=liveService.getUserDatabase();	
		LiveParticipant zosatapo=userDatabase.findParticipant("zosatapo");
		LiveCaseDatabase caseDatabase=liveService.getCaseDatabase();
		String sessionID=liveClient.getSessionID();
		
		LivePackage livePackage=liveService.findLivePackage("or-join");
		LiveProcess liveProcess=livePackage.findLiveProcess("wfp-1");
		
		System.err.println("liveProcess : "+liveProcess+",PackageID : "+liveProcess.getPackageID());
		System.err.println("caseDatabase : "+caseDatabase);
		System.err.println("maxInactiveInterval : "+liveClient.getMaxInactiveInterval()+" [s] ");
		String caseId=caseDatabase.createLiveCase(sessionID,liveProcess,zosatapo,new Date(),"first case"," "," ");
		
		LiveCase liveCase=caseDatabase.findLiveCase(sessionID,liveProcess,caseId);		
		System.err.println("PackageID="+liveCase.getPackageID());
		
		LiveWork liveWork=(LiveWork)caseDatabase.getLiveWorkList(sessionID,liveProcess,liveCase,zosatapo,Constants.WORK_STATUS_ACCEPTED).get(0);
		liveService.getLiveDispatcher().dispatch(sessionID,liveWork);
		liveClient.disconnect();
		
		System.err.println("-------Game over--------------");
	}
}
