Jump to: navigation, search

Stardust/Knowledge Base/API/ExamplesofAPIUsage/CompleteAnActivity

Complete an Activity

Purpose

Completing an activity consists of four steps:

  • Get the activity from the process instance
  • Get the in data from the activity instance
  • Calculate the out data for the activity instance
  • Call the API with the out data to complete the activity instance.

Requirements

You need a Process Instance, Initialized services and the id of the in data mappings:

Stardust Complete Activity In Data Mapping

And out data mappings of the activity you want to complete:

Stardust Complete Activity Out Data Mapping

Implementation

Exception handling
To keep the example code clear the exception handling is very simple. If you use this code productive, please add appropriate exception handling

Missing parts
The missing code fragments ([...]) can be found here.

package com.sungard.wiki;
 
import ag.carnot.workflow.query.ActivityInstanceQuery;
import ag.carnot.workflow.query.ActivityStateFilter;
import ag.carnot.workflow.runtime.ActivityInstance;
[...]
 
public class WikiProgram {
	[...]
 
	public static void main(String[] unused) throws Exception {
		try {
			init();
			ProcessInstance lProcessInstance = startProcess();
			completeBeforeLoopActivity(lProcessInstance);
 
		} finally {
			uninit();
		}
	}
 
	static void completeBeforeLoopActivity(ProcessInstance pProcessInstance) throws WorkflowException,RemoteException{
		// Get next activity of the process instance
		ActivityInstance lActivityInstance = getNextActivity(pProcessInstance);
		assertActivity(lActivityInstance, "BeforeLoop");
		// Get the in data of the activity instance
		Object lInData = getInData(lActivityInstance, "DoWhileCondition");
		// No out data calculation needed but we check the default value from the model
 which should be false.
		if (!Boolean.FALSE.equals(lInData)) {
			throw new IllegalStateException("In data has the wrong value: " + lInData);
		}
 
		// Create the out data of the activity instance
		Map<String, Object> lOutputData = new HashMap<String, Object>();
		lOutputData.put("DoWhileCondition", Boolean.TRUE);
		// Finish the activity with the out data
		finishActivity(lActivityInstance, lOutputData);
	}
 
	static void assertActivity(ActivityInstance pActivityInstance, String pExpectedActivityId) 
throws IllegalStateException {
		if (pExpectedActivityId == null) {
			throw new IllegalArgumentException("pActivityId might not be null!");
		}
		if (!pExpectedActivityId.equals(pActivityInstance.getActivity().getId())) {
			throw new IllegalStateException(pActivityInstance.getActivity().getId() 
+ " isn't expected! Expected was " + pExpectedActivityId);
		}
	}
 
	static void finishActivity(ActivityInstance pActivityInstance, Map<String, Object> pOutputMap)
 throws RemoteException, WorkflowException {
		workflowService.activateAndComplete(pActivityInstance.getOID(), null, pOutputMap);
	}
 
	static Object getInData(ActivityInstance pActivityInstance, String key) {
		Object inData = workflowService.getInDataValue(pActivityInstance.getOID(), null, key);
		return inData;
	}
 
	static ActivityInstance getNextActivity(ProcessInstance pProcessInstance) 
throws RemoteException, WorkflowException {
		ActivityInstanceQuery query = 
ActivityInstanceQuery.findForProcessInstance(pProcessInstance.getOID());
		query.where(ActivityStateFilter.ALIVE);
 
		ActivityInstance lActivityInstance = queryService.findFirstActivityInstance(query);
 
		return lActivityInstance;
	}
 
	static ProcessInstance startProcess() {[...]}
 
	static void init() {[...]}
 
	static void uninit() {[...]}
}
  • The ID is WhileDoCondition because it's called the same in both mappings (see the screenshots above)
  • null as second argument while working with the data mappings always referes to the context. null means default which should be sufficent for most use-cases.
  • If you haven't started the process instance by your program you have to get it via a query.
  • The in and out data is provided via a java.util.Map<String, Object> whereas the key (the string) is the id of the mapping. The value (the object) is the data itself.
  • A process is completed if there are no further accessible activities left. This might also be the case somewhere in the middle of the model if you have created an XOR split and no condition is matched.