13 Replies Latest reply on Dec 13, 2009 4:14 PM by Santanu Saraswati

    TaskQuery w/ ProcessInstanceId possible?

    Bob Milstead Newbie

      I've tried everything I can think of to query tasks by ProcessInstanceId but no matter what, if you put a ProcessInstanceId on a TaskQuery, you get back zero results. The test below runs fine until the last line, when the assertion fails. I have tried substituting different ids in the query, including processInstance.getProcessInstance.getId(), processInstance.getExecutions() and iterating over all the executions and using their ids, and various other attempts without success.

      Is it even possible? If anybody can do it, how about pasting a code snippet?


       @Test
       public void testTaskQueryByProcessInstanceId()
       {
      
       TaskQuery taskQueryBefore = getTaskService().createTaskQuery();
       List<Task> taskListBefore = taskQueryBefore.list();
       Assert.assertEquals(0, taskListBefore.size());
      
       ProcessInstance processInstance = createProcessInstance();
       TaskQuery taskQuery = getTaskService().createTaskQuery();
      
       List<Task> taskList = taskQuery.list();
       Assert.assertEquals(2, taskList.size());
      
       TaskQuery taskQuery2 = getTaskService().createTaskQuery();
       taskQuery2.processInstanceId(processInstance.getId());
       List<Task> taskList2 = taskQuery2.list();
      
       Assert.assertEquals(2, taskList2.size());
       }
      


        • 1. Re: TaskQuery w/ ProcessInstanceId possible?
          Joram Barrez Master

          This is certainly possible and is coverd by our QA test suite (see for example TaskQueryProcessTest).

           List<Task> parisTasks = taskService
           .createTaskQuery()
           .processInstanceId(parisProcessInstance.getId())
           .list();
          


          How is the process instance created (ie post your createProcessInstance() method)?

          • 2. Re: TaskQuery w/ ProcessInstanceId possible?
            Ronald van Kuijk Master

            But that is exactly what he does:

            TaskQuery taskQuery2 = getTaskService().createTaskQuery();
             taskQuery2.processInstanceId(processInstance.getId());
             List<Task> taskList2 = taskQuery2.list();
            


            • 3. Re: TaskQuery w/ ProcessInstanceId possible?
              Bob Milstead Newbie

              Yep, that is exactly what I am doing, and I swear it does not work. Possibly there is something strange caused by the fact that we are using HSQLDB under the test suite, but we have been using that for years on our project.

              • 4. Re: TaskQuery w/ ProcessInstanceId possible?
                Joram Barrez Master

                Please post your complete unit test. We are also using HSQLDB in development so the error should pop up with us too.

                • 5. Re: TaskQuery w/ ProcessInstanceId possible?
                  Ronald van Kuijk Master

                  And make sure everything is embedded in the unittest, preferably also the processdefinition as a string. See my post in http://www.jboss.org/index.html?module=bb&op=viewtopic&t=158610

                  • 6. Re: TaskQuery w/ ProcessInstanceId possible?
                    Bob Milstead Newbie

                    OK, first, let me say I have only been working with jbpm for 2 weeks, and I am obviously not quite getting the difference between a ProcessInstance and an Execution, when to use one or when to use the other. And the documentation shows that an execution is a pointer to a task, except there seems to be no easy way to get from an execution to the task it points to, unless you already know the name of that task. Apologies to all for any time that may have been wasted.

                    In trying to put together something to post here as requested, I did the simplest possible example, ran the test, and it worked. So I started going back to figure out what was different, and it turned out to be this: My 2 tasks were each running in subprocesses of the original main process. I still have not figured out how to do what I want, which is to get all the tasks running from the original process, whether they are in the main process or in subprocesses.

                    In our application, there is a very large work list, maybe 100 things that must be completed by worker bees. At various points when some of those things are done, supervisors must do things. When we start the main process it is related to one worker bee. We want the supervisor to be able to look at groups of activities to do, organized by worker bee (the main ProcessInstance and all of its subs) not across all worker bees. That is why I am trying to do this. Perhaps I am not even using the product right.

                    Some attempts to get the tasks for the subprocesses:


                    Collection<? extends Execution> execs = processInstance.getExecutions();
                     for (Execution ex : execs)
                     {
                     taskQuery2 = getTaskService().createTaskQuery();
                     taskQuery2.processInstanceId(ex.getProcessInstance().getId());
                     taskList2 = taskQuery2.list();
                     Assert.assertEquals(1, taskList2.size());
                     }


                    and

                    Collection<? extends Execution> execs = processInstance.getExecutions();
                     for (Execution ex : execs)
                     {
                     taskQuery2 = getTaskService().createTaskQuery();
                     taskQuery2.processInstanceId(ex.getId());
                     taskList2 = taskQuery2.list();
                     Assert.assertEquals(1, taskList2.size());
                     }


                    • 7. Re: TaskQuery w/ ProcessInstanceId possible?
                      Ronald van Kuijk Master

                      hwrox, Please read the two posts before yours. Lots of plain text does not help. Make a full unit test that *we* can run.

                      • 8. Re: TaskQuery w/ ProcessInstanceId possible?
                        Paolo F Newbie

                        hrworx, did you manage to emulate a taskquery that can search for all tasks in all of a master process's subprocesses?

                        I am facing the same issue right now and a pointer would be appreciated!

                        • 9. Re: TaskQuery w/ ProcessInstanceId possible?
                          Bob Milstead Newbie

                          We never really solved this. As best I can tell, trying to get tasks for an execution or track what is going on by execution kind of goes against the paradigm of jbpm. Instead, we created assignment handlers etc. that assigned the tasks at the time they are created, then created methods like getTaskByAssignee or getUnassignedTasks, etc to get hold of the tasks.

                          Even figuring out which execution is the one you want seems somewhat random, so we created this method:

                          public Execution findCurrentExecution(Execution execution)
                           {
                           // TODO consider whether to throw an ExecutionAlreadyFinishedException
                           if (execution.getProcessInstance() == null)
                           {
                           return execution;
                           }
                          
                           return execution.getProcessInstance();
                           }


                          Sorry I can't be of more help. I have not had my jbpm up and running since October because I am stuck in the uncharted waters of trying to get jbpm running with Spring and JTA. :-(

                          • 10. Re: TaskQuery w/ ProcessInstanceId possible?
                            Ronald van Kuijk Master

                             

                            We never really solved this. As best I can tell, trying to get tasks for an execution or track what is going on by execution kind of goes against the paradigm of jbpm. I


                            No specifically. Regarding the tasklists. there just have not been many requests for this (only you two) and noone filed a jira issue for it as an enhancement and tried to gather votes for it.

                            Regarding the tracking, this is (afaik, since I do not use it) an option in the console. And there is a jira issue for adding additional events in the db. So if that is important to you, vote for it .

                            Instead, we created assignment handlers etc. that assigned the tasks at the time they are created, then created methods like getTaskByAssignee or getUnassignedTasks, etc to get hold of the tasks.


                            getTaskByAssignee within the context of a processinstance? Since the global one is there.

                            Even figuring out which execution is the one you want seems somewhat random, so we created this method:


                            This one I do not get... what is the added value of this...

                            • 11. Re: TaskQuery w/ ProcessInstanceId possible?
                              Paolo F Newbie

                              Short story long: if a user starts process SUPER with key = 12, and this forks a subprocess, how can i gather all tasks for key 12, including those that SUPER forked?

                              I see only 2 options:

                              a. the key is transferred to the subprocess (because it has a domain modeling) - HOWEVER, if I understand correctly, jbpm does not allow of searching by process variable, so this is a no-go unless you can loop application-wise through all of the tasks calling getProcessVariables() and comparing them to the needed one.

                              b. tasks for a certain user can be found in subprocesses in some way - and for the sake of me, I am not being able to do this...

                              Anyone else that has faced this? Maybe I'm getting this all wrong...

                              PS - this is triggered because if I understand this correctly, subprocessing is available not just because of domain modeling, but also for convenience (e.g. versioning with a finer granularity or breaking up of large processes for readability). This would not be understandable to the end user...

                              • 12. Re: TaskQuery w/ ProcessInstanceId possible?
                                Sebastian Schneider Master

                                Here are some related issues. You're welcome to vote for them and to also provide feedback and suggestions in the comments:

                                 

                                a lot of things related to the subject history

                                https://jira.jboss.org/jira/browse/JBPM-2568

                                 

                                task audit capabilities:

                                https://jira.jboss.org/jira/browse/JBPM-2416

                                 

                                searching for process instances by process variables

                                https://jira.jboss.org/jira/browse/JBPM-2505

                                 

                                Please make use of JIRA since the jBPM uses JIRA to organize their on jBPM. You can also the comments to point to additional forum posts concerning an issue already in JIRA.

                                 

                                HTH

                                 

                                P.S.: I am missing the button "preview" after the update of the software which runs the community and the forums. Is it still there?

                                • 13. Re: TaskQuery w/ ProcessInstanceId possible?
                                  Santanu Saraswati Novice

                                  Paolo,

                                      You may try using OpenExecution interface. This code below might address your problem

                                   

                                   

                                  /**
                                  * Finds all the tasks for the process instance and all sub processes of the
                                  * passed process instance assigned to the user
                                  *
                                  * @param processInstance
                                  * @param userId
                                  * @return
                                  */
                                  private List<Task> findActiveTasks(Execution processInstance, String userId) {
                                      // the list where we collect all the tasks of the current process instance and all
                                      // sub process instances
                                      List<Task> consolidatedTasks = new ArrayList<Task>();
                                      Collection<? extends Execution> executions = processInstance
                                              .getExecutions();
                                      for (Execution execution : executions) {
                                          // cast it to open execution..we will need it
                                          OpenExecution openExecution = (OpenExecution) execution;
                                          // check if this is a sub-process or just a fork
                                          if (openExecution.getSubProcessInstance() != null) {
                                              OpenExecution subProcessInstance = openExecution
                                                      .getSubProcessInstance();
                                              consolidatedTasks.addAll(findActiveTasks(subProcessInstance, userId));
                                          }
                                      }
                                      // now get all the tasks for the parent process instance
                                      consolidatedTasks.addAll(findActiveTasks(processInstance.getId(), userId));
                                      return consolidatedTasks;
                                  }

                                   

                                  /**
                                  * Finds all the tasks for the process instance assigned to the user passed
                                  *
                                  * @param processInstanceId
                                  * @param userId
                                  * @return
                                  */
                                  private List<Task> findActiveTasks(String processInstanceId, String userId) {
                                      return taskService.createTaskQuery().processInstanceId(processInstanceId)
                                                                          .assignee(userId)
                                                                          .list();
                                  }

                                   

                                  Tried to copy the code im Wiki Markup tag... but that presented a lot of garbled text. So giving the code as plain text. Is there a way by which I can copy code from eclipse and paste it here?