
package cust.cm_report;

import wt.query.*;
import wt.workflow.engine.*;
import wt.workflow.definer.*;

import wt.fc.*;
import wt.fc.WTObject;
import wt.method.RemoteMethodServer;
import wt.httpgw.GatewayAuthenticator;

import java.util.*;

public class wf_demo_test_02 {

	public static void main(String[] args) throws Exception{
		RemoteMethodServer rms = RemoteMethodServer.getDefault();
		GatewayAuthenticator auth = new GatewayAuthenticator();
		auth.setRemoteUser("wcadmin");
		rms.setAuthenticator(auth);

		QueryResult resWFPT=PersistenceHelper.manager.find(
			new QuerySpec(WfProcessTemplate.class)
		);

		while(resWFPT.hasMoreElements()) {
			WfProcessTemplate wtwfpt = (WfProcessTemplate)resWFPT.nextElement();
			System.out.println( "Name: " + wtwfpt.getName() + " & Desc: " + wtwfpt.getDescription()  );
		}
		System.out.println("\nNumber of WfProcessTemplate: " + resWFPT.size() + div);

		try {
			WfProcessTemplate processTemplateName = new WfProcessTemplate();
			Enumeration templateEnum = null;
			WTObject anObject = null;

			// -- Step 1. Retrieve the workflow template on which the process will be based.

			QuerySpec qs = new QuerySpec(WfProcessTemplate.class);

			qs.appendWhere (new SearchCondition (WfProcessTemplate.class,
					WfTemplateObject.NAME, SearchCondition.EQUAL, processTemplateName));

			templateEnum = PersistenceHelper.manager.find (qs);

			if (templateEnum.hasMoreElements()) {
				processTemplateName = (wt.workflow.definer.WfProcessTemplate)templateEnum.nextElement();
			}

			// -- Step 2. Create a Process for the ProcessTemplate

			WfProcess process = WfEngineHelper.service.createProcess(processTemplateName, null);

			// -- Step 3.1 Set basic values

			process.setName("Programatically Instantiated Process");
			process.setDescription("SomeDescriptionText");
			process.setPriority(1);

			// -- Step 3.2 Save the process

			process = (WfProcess)PersistenceHelper.manager.save(process);

			// -- Step 4  Get process context and populate variables in table

			ProcessDataInfo signature = processTemplate.getContextSignature();
			ProcessData input = ProcessData.newProcessData(signature);
			input.setValue("primaryBusinessObject", anObject);

			// -- Step 5. Start the process.

			process = WfEngineHelper.service.startProcess(process, input, 1);
		}
		catch (wt.util.WTException wex) {
			wex.printStackTrace();
		}
	}
}
