You are here

Updating a workflow task

This tutorial shows you how to update a workflow task.

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

In the tutorial you will see how to update a task once it has been started. In this tutorial you will simply update the comment associated with the task.
  1. Now in the Eclipse Package Explorer, locate the WorkflowTest project and expand it to locate MainActivity.java.
  2. Replace the existing code with the following:

    
    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 (update task)";
    		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();
    
    					
    					// test process definitions
    					
    					List<ProcessDefinition> definitions = workflowService.getProcessDefinitions();
    					
    					for (ProcessDefinition def : definitions){
    						
    						Log.d(TAG, "process definition id: "+def.getIdentifier());
    						
    					}
    					
    								
    					// 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());
    					
    					// find task(s) to update
    					List<Task> tasks = workflowService.getTasks(adhocProcess);
    
    					// for each task
    					Log.d(TAG, "Get tasks...");
    					
    					for (Task task : tasks) {
    
    						// print task details
    						Log.d(TAG, "task identifier: "+task.getIdentifier());	
    						Log.d(TAG, "task name: "+task.getName());	
    						Log.d(TAG, "task description: "+task.getDescription());	
    						Log.d(TAG, "task priority: "+task.getPriority());	
    						Log.d(TAG, "task due date: "+task.getDueAt().getTime().toGMTString());	
    						Log.d(TAG, "comment: "+task.getVariableValue(WorkflowModel.PROP_COMMENT));	
    						
    						// add a comment
    						String comment = "I'm adding a new comment!";
    						variables.put(WorkflowModel.PROP_COMMENT, comment);
    						
    						// update task variables
    						Task updatedTask = workflowService.updateVariables(task, variables);
    						
    						// refresh object
    						updatedTask = workflowService.refresh(updatedTask);
    
    						Log.d(TAG, "Updated comment: "+updatedTask.getVariableValue(WorkflowModel.PROP_COMMENT));	
    						
    					}
    
    				} 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");
    
    		}
    
    	}
    
    }
    
    
    					
  3. Find the code comment // Modify to suit your SkyVault server installation and modify the settings to suit your SkyVault installation.
  4. Rebuild and run your project.
  5. Check the messages in LogCat to make sure the comment has been updated as expected.
  6. In the Share main menu bar, click on Tasks > My Tasks.
  7. Locate the task that was just updated. You can do this by noting the identifier of the task you updated in LogCat, and then matching this ID to a task in your task list in Share.
  8. Verify that the comment has indeed been updated.
In this tutorial you have seen how to use the new Workflow API added in version 1.3 of the SDK to update a workflow task. Subsequent tutorials will investigate further parts of the Workflow API.