You are here

Completing a workflow task

This tutorial shows you how to complete a workflow task.

This tutorial assumes you are familiar with the content of previous tutorials. It also assumes that you are using Eclipse.

Once a workflow process has been started, you may want to complete all or some of the tasks within it using the API. This tutorial shows you how that can be achieved. The code presented here builds on that developed in the tutorial Starting a new workflow. Once a process has been created, it is saved in a variable adhocProcess for future use. A list of processes is then displayed for debugging purposes. All tasks associated with a specific process are then retrieved, and one by one these are completed with a suitable closing comment. There are many ways to approach this problem, and this tutorial shows one of the simpler methods, in order to get you started quickly.
  1. Replace the code you developed in the tutorial Starting a new workflow with that shown here:

    						
    package com.alfresco.tutorials.workflowtest;
    
    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.GregorianCalendar;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    import org.alfresco.mobile.android.api.constants.WorkflowModel;
    import org.alfresco.mobile.android.api.exceptions.AlfrescoSessionException;
    import org.alfresco.mobile.android.api.model.Document;
    import org.alfresco.mobile.android.api.model.Person;
    import org.alfresco.mobile.android.api.model.ProcessDefinition;
    import org.alfresco.mobile.android.api.model.Task;
    import org.alfresco.mobile.android.api.services.WorkflowService;
    import org.alfresco.mobile.android.api.session.RepositorySession;
    import org.alfresco.mobile.android.api.utils.DateUtils;
    import org.alfresco.mobile.android.api.model.Process;
    
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
    
    	@Override
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.activity_main);
    
    		// Modify to suit your SkyVault server installation
    		String url = "http://localhost:8080/alfresco";
    		String username = "admin";
    		String password = "admin";
    
    		new ConnectToRepo().execute(url, username, password);
    
    	}
    
    	class ConnectToRepo extends AsyncTask<String, Integer, String> {
    
    		private static final String TAG = "ConnectToRepo";
    		protected static final String DESCRIPTION = "Tutorial Adhoc Process";
    		private WorkflowService workflowService;
    
    		@Override
    		protected String doInBackground(String... params) {
    
    			Log.d(TAG, "doInBackground");
    			Log.d(TAG, params[0] + ":" + params[1] + ":" + params[2]);
    
    			String url = params[0];
    			String username = params[1];
    			String password = params[2];
    
    			try {
    				// connect to on-premise repo
    				RepositorySession session = RepositorySession.connect(url,
    						username, password);
    
    				if (session != null) {
    
    					// Get WorkflowService
    					workflowService = session.getServiceRegistry()
    							.getWorkflowService();
    					
    					// start an adhoc workflow
    
    					Map<String, Serializable> variables = new HashMap<String, Serializable>();
    
    					// Process Definition
    					String processDefinitionIdentifier = "activitiAdhoc:1:4";
    					ProcessDefinition adhoc = workflowService
    							.getProcessDefinition(processDefinitionIdentifier);
    
    					// Assignee
    					Person user = session.getServiceRegistry()
    							.getPersonService()
    							.getPerson(session.getPersonIdentifier());
    					List<Person> users = new ArrayList<Person>();
    					users.add(user);
    
    					// Items - Attachments
    					String sampleDocumentName = "android.pdf";
    					Document doc = (Document) session.getServiceRegistry()
    							.getDocumentFolderService()
    							.getChildByPath(sampleDocumentName);
    					Log.d(TAG, "sample document id: " + doc.getIdentifier());
    					List<Document> docs = new ArrayList<Document>();
    					docs.add(doc);
    
    					// Due date
    					GregorianCalendar calendar = new GregorianCalendar();
    					calendar.set(Calendar.YEAR, 2013);
    					variables.put(WorkflowModel.PROP_WORKFLOW_DUE_DATE,
    							DateUtils.format(calendar));
    
    					// Priority
    					variables.put(WorkflowModel.PROP_WORKFLOW_PRIORITY,
    							WorkflowModel.PRIORITY_HIGH);
    
    					// Description
    					variables.put(WorkflowModel.PROP_WORKFLOW_DESCRIPTION,
    							DESCRIPTION);
    
    					// Notification
    					variables.put(WorkflowModel.PROP_SEND_EMAIL_NOTIFICATIONS,
    							"true");
    
    					// START THE PROCESS
    					Process adhocProcess = workflowService.startProcess(adhoc,
    							users, variables, docs);
    
    					Log.d(TAG, "process identifier for newly started process: "+adhocProcess.getIdentifier());
    					
    					// show a list of all processes
    					List<Process> processes = workflowService.getProcesses();
    					
    					for (Process process : processes){
    						Log.d(TAG, "process identifier: "+process.getIdentifier());
    					}
    				
    					// set up closing comment
    					String comment = "Completing task ";
    					Map<String, Serializable> vars = new HashMap<String, Serializable>();
    
    					// find task(s) to complete
    					List<Task> tasks = workflowService.getTasks(adhocProcess);
    
    					// for each task
    					for (Task task : tasks) {
    
    						// set up comment and/or other variables as required
    						comment = comment + task.getIdentifier();
    						vars.put(WorkflowModel.PROP_COMMENT, comment);
    
    						Log.d(TAG, comment);	
    						// Close Active Task
    						workflowService.completeTask(task, vars);
    
    					}
    
    				} else {
    
    					Log.d(TAG, "No Session available!");
    
    				}
    
    			} catch (AlfrescoSessionException e) {
    				Log.e(TAG, "Failed to connect: " + e.toString());
    			}
    
    			Log.d(TAG, "doInBackground Complete");
    			return "doInBackground Complete";
    		}
    
    		@Override
    		protected void onPostExecute(String result) {
    			super.onPostExecute(result);
    			Log.d(TAG, "onPostExecute");
    			Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
    		}
    
    		@Override
    		protected void onProgressUpdate(Integer... values) {
    			super.onProgressUpdate(values);
    			Log.d(TAG, "onProgressUpdate");
    
    		}
    
    	}
    
    }						
    						
    					
  2. Run the project and observe the output in LogCat. Take particular note of the ID of any task completed.
  3. In SkyVault Share, click on Tasks My Tasks in the main menubar.
  4. In the tasks filter on the left hand side, click Completed to display only completed tasks.
  5. Click on the last item in the list (if there is more than one task showing) - this is the task you just completed. You can also check that the ID corresponds to the ID of a task the code just completed.

    You will see something similar to the following:

  6. Check that the task status is Completed. Also check the comment you added programmatically is as expected according to the code.
In this tutorial you have seen how to use the new Workflow API added in version 1.3 of the SDK to complete a workflow task. Subsequent tutorials will investigate further parts of the Workflow API.