ALF-3896: F69 REST API to get the history (completed tasks) of a workflow instance (GET /api/workflow-instances/{workflow_instance_id}/task-instances)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@22111 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Gavin Cornwell
2010-09-01 05:52:38 +00:00
parent 092dbd87a2
commit 915f85a6ab
3 changed files with 74 additions and 37 deletions

View File

@@ -4,7 +4,8 @@
Lists all Workflow Task Instances associated with an authority and of a given State. Lists all Workflow Task Instances associated with an authority and of a given State.
The list of returned tasks also includes pooled tasks which the specified authority is eligible to claim. The list of returned tasks also includes pooled tasks which the specified authority is eligible to claim.
</description> </description>
<url>/api/task-instances?authority={authority?}&amp;state={state?}&amp;priority={priority?}&amp;pooledTasks={pooledTasks?}&amp;dueBefore={dueBefore?}&amp;dueAfter={dueAfter?}&amp;properties={properties?}&amp;detailed={detailed?}&amp;maxItems={maxItems?}&amp;skipCount={skipCount?}&amp;exclude={exclude?}</url> <url>/api/task-instances?authority={authority?}&amp;state={state?}&amp;priority={priority?}&amp;pooledTasks={pooledTasks?}&amp;dueBefore={dueBefore?}&amp;dueAfter={dueAfter?}&amp;properties={properties?}&amp;maxItems={maxItems?}&amp;skipCount={skipCount?}&amp;exclude={exclude?}</url>
<url>/api/workflow-instances/{workflow_instance_id}/task-instances?authority={authority?}&amp;state={state?}&amp;priority={priority?}&amp;dueBefore={isoDate?}&amp;dueAfter={isoDate?}&amp;properties={prop1, prop2, prop3...?}&amp;maxItems={maxItems?}&amp;skipCount={skipCount?}&amp;exclude={exclude?}</url>
<format default="json"/> <format default="json"/>
<authentication>user</authentication> <authentication>user</authentication>
<transaction allow="readonly">required</transaction> <transaction allow="readonly">required</transaction>

View File

@@ -51,11 +51,12 @@ public class TaskInstancesGet extends AbstractWorkflowWebscript
public static final String PARAM_DUE_AFTER = "dueAfter"; public static final String PARAM_DUE_AFTER = "dueAfter";
public static final String PARAM_PROPERTIES = "properties"; public static final String PARAM_PROPERTIES = "properties";
public static final String PARAM_POOLED_TASKS = "pooledTasks"; public static final String PARAM_POOLED_TASKS = "pooledTasks";
public static final String PARAM_DETAILED = "detailed"; public static final String VAR_WORKFLOW_INSTANCE_ID = "workflow_instance_id";
@Override @Override
protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache) protected Map<String, Object> buildModel(WorkflowModelBuilder modelBuilder, WebScriptRequest req, Status status, Cache cache)
{ {
Map<String, String> params = req.getServiceMatch().getTemplateVars();
Map<String, Object> filters = new HashMap<String, Object>(4); Map<String, Object> filters = new HashMap<String, Object>(4);
// authority is not included into filters list as it will be taken into account before filtering // authority is not included into filters list as it will be taken into account before filtering
@@ -64,9 +65,14 @@ public class TaskInstancesGet extends AbstractWorkflowWebscript
// state is also not included into filters list, for the same reason // state is also not included into filters list, for the same reason
WorkflowTaskState state = getState(req); WorkflowTaskState state = getState(req);
// look for a workflow instance id
String workflowInstanceId = params.get(VAR_WORKFLOW_INSTANCE_ID);
// determine if pooledTasks should be included, when appropriate i.e. when an authority is supplied
Boolean pooledTasksOnly = getPooledTasks(req); Boolean pooledTasksOnly = getPooledTasks(req);
// get list of properties to include in the response
List<String> properties = getProperties(req); List<String> properties = getProperties(req);
boolean detailed = "true".equals(req.getParameter(PARAM_DETAILED));
// get filter param values // get filter param values
filters.put(PARAM_PRIORITY, req.getParameter(PARAM_PRIORITY)); filters.put(PARAM_PRIORITY, req.getParameter(PARAM_PRIORITY));
@@ -81,56 +87,74 @@ public class TaskInstancesGet extends AbstractWorkflowWebscript
List<WorkflowTask> allTasks; List<WorkflowTask> allTasks;
if (authority != null) if (workflowInstanceId != null)
{ {
List<WorkflowTask> tasks = workflowService.getAssignedTasks(authority, state); // a workflow instance id was provided so query for tasks
List<WorkflowTask> pooledTasks = workflowService.getPooledTasks(authority); WorkflowTaskQuery taskQuery = new WorkflowTaskQuery();
if (pooledTasksOnly != null) taskQuery.setActive(null);
taskQuery.setProcessId(workflowInstanceId);
taskQuery.setTaskState(state);
if (authority != null)
{ {
if (pooledTasksOnly.booleanValue()) taskQuery.setActorId(authority);
}
allTasks = workflowService.queryTasks(taskQuery);
}
else
{
// default task state to IN_PROGRESS if not supplied
if (state == null)
{
state = WorkflowTaskState.IN_PROGRESS;
}
// no workflow instance id is present so get all tasks
if (authority != null)
{
List<WorkflowTask> tasks = workflowService.getAssignedTasks(authority, state);
List<WorkflowTask> pooledTasks = workflowService.getPooledTasks(authority);
if (pooledTasksOnly != null)
{ {
// only return pooled tasks the user can claim if (pooledTasksOnly.booleanValue())
allTasks = new ArrayList<WorkflowTask>(pooledTasks.size()); {
allTasks.addAll(pooledTasks); // only return pooled tasks the user can claim
allTasks = new ArrayList<WorkflowTask>(pooledTasks.size());
allTasks.addAll(pooledTasks);
}
else
{
// only return tasks assigned to the user
allTasks = new ArrayList<WorkflowTask>(tasks.size());
allTasks.addAll(tasks);
}
} }
else else
{ {
// only return tasks assigned to the user // include both assigned and unassigned tasks
allTasks = new ArrayList<WorkflowTask>(tasks.size()); allTasks = new ArrayList<WorkflowTask>(tasks.size() + pooledTasks.size());
allTasks.addAll(tasks); allTasks.addAll(tasks);
allTasks.addAll(pooledTasks);
} }
} }
else else
{ {
// include both assigned and unassigned tasks // authority was not provided -> return all active tasks in the system
allTasks = new ArrayList<WorkflowTask>(tasks.size() + pooledTasks.size()); WorkflowTaskQuery taskQuery = new WorkflowTaskQuery();
allTasks.addAll(tasks); taskQuery.setTaskState(state);
allTasks.addAll(pooledTasks); taskQuery.setActive(null);
allTasks = workflowService.queryTasks(taskQuery);
} }
} }
else
{
// authority was not provided -> return all active tasks in the system
WorkflowTaskQuery taskQuery = new WorkflowTaskQuery();
taskQuery.setTaskState(state);
taskQuery.setActive(null);
allTasks = workflowService.queryTasks(taskQuery);
}
// filter results // filter results
ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); ArrayList<Map<String, Object>> results = new ArrayList<Map<String, Object>>();
for (WorkflowTask task : allTasks) for (WorkflowTask task : allTasks)
{ {
if (matches(task, filters)) if (matches(task, filters))
{ {
if (detailed) results.add(modelBuilder.buildSimple(task, properties));
{
results.add(modelBuilder.buildDetailed(task));
}
else
{
results.add(modelBuilder.buildSimple(task, properties));
}
} }
} }
@@ -174,7 +198,8 @@ public class TaskInstancesGet extends AbstractWorkflowWebscript
} }
/** /**
* Gets the specified {@link WorkflowTaskState}, defaults to IN_PROGRESS. * Gets the specified {@link WorkflowTaskState}, null if not requested
*
* @param req * @param req
* @return * @return
*/ */
@@ -193,8 +218,8 @@ public class TaskInstancesGet extends AbstractWorkflowWebscript
throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, msg); throw new WebScriptException(HttpServletResponse.SC_BAD_REQUEST, msg);
} }
} }
// Defaults to IN_PROGRESS.
return WorkflowTaskState.IN_PROGRESS; return null;
} }
/** /**

View File

@@ -72,6 +72,7 @@ public class WorkflowRestApiTest extends BaseWebScriptTest
private final static String USER3 = "Nick" + GUID.generate(); private final static String USER3 = "Nick" + GUID.generate();
private static final String URL_TASKS = "api/task-instances"; private static final String URL_TASKS = "api/task-instances";
private static final String URL_USER_TASKS = "api/task-instances?authority={0}"; private static final String URL_USER_TASKS = "api/task-instances?authority={0}";
private static final String URL_WORKFLOW_TASKS = "api/workflow-instances/{0}/task-instances";
private static final String URL_WORKFLOW_DEFINITIONS = "api/workflow-definitions"; private static final String URL_WORKFLOW_DEFINITIONS = "api/workflow-definitions";
private static final String URL_WORKFLOW_INSTANCES = "api/workflow-instances"; private static final String URL_WORKFLOW_INSTANCES = "api/workflow-instances";
private static final String URL_WORKFLOW_INSTANCES_FOR_DEFINITION = "api/workflow-definitions/{0}/workflow-instances"; private static final String URL_WORKFLOW_INSTANCES_FOR_DEFINITION = "api/workflow-definitions/{0}/workflow-instances";
@@ -194,6 +195,16 @@ public class WorkflowRestApiTest extends BaseWebScriptTest
} }
assertFalse("Found wf:submitAdhocTask when they were supposed to be excluded", adhocTasksPresent); assertFalse("Found wf:submitAdhocTask when they were supposed to be excluded", adhocTasksPresent);
// retrieve tasks using the workflow instance
String workflowInstanceId = adhocPath.getInstance().getId();
response = sendRequest(new GetRequest(MessageFormat.format(URL_WORKFLOW_TASKS, workflowInstanceId)), 200);
assertEquals(Status.STATUS_OK, response.getStatus());
jsonStr = response.getContentAsString();
json = new JSONObject(jsonStr);
results = json.getJSONArray("data");
assertNotNull(results);
assertTrue(results.length() > 0);
} }
public void testTaskInstanceGet() throws Exception public void testTaskInstanceGet() throws Exception