AnsweredAssumed Answered

Problems adding TaskListener from ParseHandler

Question asked by felipe1 on Nov 27, 2013
Latest reply on Dec 13, 2013 by frederikheremans1
I'm trying to add custom a TaskListener to every UserTask declared in my processes.

I have looked at this bit of the documentation (http://www.activiti.org/userguide/#advanced_parseHandlers) to get me started.

I have also found this example of Handler that insert a custom handler to UserTasks - https://github.com/jbarrez/activiti-advanced-scripting/blob/master/src/test/java/org/activiti/ExecuteScriptOnTaskCompleteBpmnParseHandler.java.

So I created the class shown below. It seems that I have did everything according to the documentation, however it is not working.  The problem is that I cannot get a reference to a TaskDefinition object.

I assumed that The ProcessDefinitionEntity#getTaskDefinitions() method would return all the tasks defined in the current process, then it would be easy to get the task with the taskDefinitionKey, but that is not the case.

When I open an external start form, the parse() method is called 3-4 times, and the return from the getTaskDefinitions() method is different on every call.



public class CustomParse implements BpmnParseHandler {

   @Override
   public Collection<Class<? extends BaseElement>> getHandledTypes() {
      List<Class<? extends BaseElement>> elements = new ArrayList<>();
      elements.add(UserTask.class);
      return elements;
   }

   @Override
   public void parse(BpmnParse bpmnParse, BaseElement element) {
      String taskDefinitionKey = element.getId(); // returns the correct task key
      Map<String, TaskDefinition> tarefas = ((ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition()).getTaskDefinitions();
      
      for(Entry<String, TaskDefinition> e : tarefas.entrySet()) {
         System.out.println("****************** " + e.getKey() + " :: " + e.getValue());
      }
      
      TaskDefinition taskDefinition = ((ProcessDefinitionEntity) bpmnParse.getCurrentScope().getProcessDefinition()).getTaskDefinitions().get(
            taskDefinitionKey);

      if (taskDefinition != null) { // taskDefinition is always null at this point
         taskDefinition.addTaskListener(TaskListener.EVENTNAME_CREATE, new CustomTaskListener());
      }
   }
}

Outcomes