Hi,
We had the same problem in our project. We solved it in a bit different way. We use Activiti as a process orchestrator. And we use org.activiti.engine.delegate.event.ActivitiEventListener set up as a bean in Spring application context. In its onEvent(ActivitiEvent event) method we analyse event type and when a process creates a user task it issues a TASK_CREATED event. What we found out is that if you are looking into an event which was created in a sub process launched via an activity call, its execution entity will have a super execution entity which we can iterate through up to the top to get the parent process instance id. Then we set task entity this process instance id as an assignee:
<java>
@Override
public void onEvent(ActivitiEvent event) {
switch (event.getType()) {
case TASK_CREATED:
ActivitiEntityEvent activityEntityEvent = (ActivitiEntityEvent) event;
TaskEntity taskEntity = (TaskEntity) activityEntityEvent.getEntity();
ExecutionEntity exEntity = taskEntity.getExecution();
String superExId = getSuperProcessInstanceId(exEntity != null ? exEntity.getSuperExecution() : null);
if (superExId != null) {
taskEntity.setAssignee(superExId);
}
break;
….
}
private String getSuperProcessInstanceId(ExecutionEntity exEntity) {
if (exEntity != null && exEntity.getSuperExecution() != null) {
return getSuperProcessInstanceId(exEntity.getSuperExecution());
} else if (exEntity != null) {
return exEntity.getProcessInstanceId();
}
return null;
}
</java>
And then, at the place where we need to get all user tasks created within process and all its sub processes, we just query by process instance id and by assignee using the same process instance id as the assignee id:
<java>
import org.activiti.engine.TaskService;
…
List<Task> userTasksByProcessInstanceId = taskService.createTaskQuery().processInstanceId(processId).list();
List<Task> tasksByAssignee = taskService.createTaskQuery().taskAssignee(processId).list();
//now merge both lists into a single one
</java>
So, this way we are sure we'll get only those sub processes' user tasks which are created by our main process since the process instance id is unique. Also, we do not have to add any event listeners to any of the elements inside the workflow as the entire mechanism is transparent to the developers who create diagrams.