Friday, March 2, 2012

End old tasks in Alfresco from Admin

I have been working with Alfresco for a while now. It is a very comprehensive tool, but today I need delete old tasks in archive task node. 

The purpose of the Workflow JavaScript API is to allow access to Alfresco advanced workflows from within JavaScript. The API supports EMCA Script 1.6 compatible JavaScript. Through this API workflow definitions, instances, paths, tasks and transitions can be accessed and managed.

The Workflow JavaScript API may be used to do any of the the following:
  • Create a workflow package.
  • Start a new workflow instance with a given definition and assign a package to it.
  • Cancel a workflow instance.
  • Delete a workflow instance.
  • Progress a workflow path to the next node, with a specified transition.
  • End a workflow task and signal the associated workflow path to progress to the next node with a specified transition.
But I want end tasks which came to archive 10 days ago. And I want write this function for admin. In this situation JavaScript API can't help me. So I decide write Java function and call it from my WebScript.
/**
 * End old tasks from archive
 */
public void endOldTasks(int daysAgo, String actorId, String transitionId){
     long DAY_IN_MS = 1000 * 60 * 60 * 24;
     
     Date currentDate = new Date();
     currentDate.setTime(currentDate.getTime() - daysAgo*DAY_IN_MS);
     
     long currentDateInMillis = currentDate.getTime();
     
  WorkflowTaskQuery taskQuery = new WorkflowTaskQuery();
  taskQuery.setActorId(actorId);
  taskQuery.setTaskState(WorkflowTaskState.IN_PROGRESS);
  
  List<WorkflowTask> tasksInArchive = workflowService.queryTasks(taskQuery);
  
  for (WorkflowTask task : tasksInArchive)
  {
   Date createdDate = (Date)task.getProperties().get(ContentModel.PROP_CREATED);
   if ( createdDate.getTime() <= currentDateInMillis ){
    workflowService.endTask(task.getId(), transitionId);
   }
  }
}

No comments:

Post a Comment