Search code examples
alfrescoactivitialfresco-webscripts

How to get the ID of the process instance in the controller?


My environment:

Alfresco Share v5.2.d (r134641-b15, Aikau 1.0.101.3, Spring Surf 5.2.d, Spring WebScripts 6.13, Freemarker 2.3.20-alfresco-patched, Rhino 1.7R4-alfresco-patched, Yui 2.9.0-alfresco-20141223)

Alfresco Community v5.2.0 (r134428-b13) schema 10005

When I start the workflow, I can assign executors - the list of users who will participate in the business process. I need to get a list of all those users and display this list in the reviewTask screen.

To display this data I have the custom FreeMarker template. From this template I'll call my Web Script.

To interact with the repository through REST I use the Web Script Framework MVC - the controller that executes the logic of receiving the list of users and FreeMarker template for the JSON response.

I've been offered the great idea, how it can be done. But I faced with some problem.

Let's say, I launched multiple instances of the business process. In the Workflow Console I can see the following:

id: activiti$1801 , desc: DESCRIPTION_1 , start date: Sun Mar 12 19:19:09 GST 2017 , def: activiti$activitiParallelReview v1
id: activiti$2005 , desc: DESCRIPTION_2 , start date: Sun Mar 12 20:11:57 GST 2017 , def: activiti$activitiParallelReview v1
id: activiti$2138 , desc: DESCRIPTION_3 , start date: Sun Mar 12 20:12:55 GST 2017 , def: activiti$activitiParallelReview v1

Thus I have three IDs:

activiti$1801
activiti$2005
activiti$2138

These IDs are available for me only in the Workflow Console.

How can I get these identifiers in the controller?

Can I, for example, write a class that will be called by Activiti? Let's say,

public class SomeListener implements TaskListener {
...
    @Override
    public void notify(DelegateTask task) {
        ...
        String taskId = task.getId();
        ...
    }
}   

Then attach it to the complete event of the some task and pass ID to the controller.

Is this the right way?..

I would be very grateful for the information. Thanks to all.


I wrote a class that implements the ExecutionListener interface. Then added it as a listener on the transition from mswf:submitParallelReviewTask to mswf:activitiReviewTask.

All the properties that I need, I can get here (Thank to Gagravarr for his answer):

public class PropertiesReceiver implements ExecutionListener {
    private static final long serialVersionUID = 1L;
    private static Log logger = LogFactory.getLog(PropertiesReceiver.class);

    @Override
    public void notify(DelegateExecution delegateExecution) throws Exception {
        // TODO Auto-generated method stub
        String processInstanceId = delegateExecution.getProcessInstanceId();
        logger.debug("processInstanceId == " + processInstanceId);

        ActivitiScriptNodeList assignees = 
                (ActivitiScriptNodeList) delegateExecution.getVariable("bpm_assignees"); 
        for (ActivitiScriptNode personNode : assignees) {
            String username = (String) personNode.getProperties().get("cm:userName");
            logger.debug("username == " + username);
        }
    }
}

alfrescotomcat-stdout.2017-03-13.log:

...
2017-03-13 11:03:12,244  DEBUG [mswf.bpm.PropertiesReceiver] [http-apr-8080-exec-3] processInstanceId == 26205
2017-03-13 11:03:12,248  DEBUG [mswf.bpm.PropertiesReceiver] [http-apr-8080-exec-3] username == first
2017-03-13 11:03:12,250  DEBUG [mswf.bpm.PropertiesReceiver] [http-apr-8080-exec-3] username == second
...

But this is a separate project, packaged in an AMP file.


Ok, another way. I can get the id of the process instance in ScriptExecutionListener:

execution.getProcessInstanceId();

And call the Alfresco REST API from the Share side to retrieve all the information that I need (Thanks to Martin Ehe).

For example, this call allows to get all the executors:

http://localhost:8080/alfresco/api/-default-/public/workflow/versions/1/processes/26205/variables

,where 26205 == id of the process instance.

But where should I save this ID to access it from Share? Can I add aspect to the workflow model and whether it's right?..


Solution

  • please try this.

    public JSONObject test(String userName) {
    
    
            JSONObject allTasks = new JSONObject();
    
            companyHome = repository.getCompanyHome();
            try {
                List<WorkflowTask> wft=serviceRegistry.getWorkflowService().getAssignedTasks(userName,WorkflowTaskState.IN_PROGRESS );
    
                    JSONArray ja = new JSONArray();
                    System.out.println("WF sizes = "+wft.size());
                    for (WorkflowTask  temp : wft) {
                        JSONObject userWFDetails = new JSONObject();
    
                        userWFDetails.put("taskId", temp.getId());
                        userWFDetails.put("taskDesc", temp.getDescription());
                        userWFDetails.put("wfInstanceId", temp.getPath().getInstance().getId());
    
                        System.out.println("wf tasks-"+temp);
                        System.out.println("task id-"+temp.getId());
                        System.out.println("wf instance id-"+temp.getPath().getInstance().getId());
                        System.out.println("wf path id-"+temp.getPath().getId());
                        ja.put(userWFDetails);
                    }           
                    allTasks.put("userTasksDetails", ja);
                } catch (Exception e) {
                e.printStackTrace();
            }
            return allTasks;
        }
    

    pass your userName. and you will get all the user task details. workflow instance details in json format.