0 Replies Latest reply on Dec 3, 2014 5:06 AM by akoskm

    Retrieving the number of tasks assigned to an owner in TaskLifeCycleEventListener

    akoskm Newbie

      I already asked this on #jbpm but so far I have no idea how to implement this.

       

      I want to retrieve the number of tasks assigned to an owner, _after_ the task data is persisted.

      For this purpose I implemented the TaskLifeCycleEventListener interface and the afterTaskAddedEvent method like:

       

      @Override
      public void afterTaskAddedEvent(TaskEvent event) {
          PeopleAssignments ppa = event.getTask().getPeopleAssignments();
          if (ppa != null) {
              for (OrganizationalEntity oe : ppa.getPotentialOwners()) {
                  Long taskCount = kbManager.getTaskCountByPotentialOwner(oe.getId());
                  // do something with taskCount
              }
          }
      }
      

       

      I guess the problem is that I'm executing my query in a new transaction, because the tasks list is always empty . Here is the method I'm calling in from the listener:

       

      public long getTaskCountByPotentialOwner(String owner) {
          Query q = this.em.createNamedQuery("NewTasksAssignedAsPotentialOwner");
      
      
          List<Status> newStatus = new ArrayList();
          newStatus.add(Status.Ready);
          newStatus.add(Status.InProgress);
          newStatus.add(Status.Reserved);
      
      
          q.setParameter("userId", owner);
          q.setParameter("status", newStatus);
          q.setParameter("groupIds", GROUP_ID);
      
      
          Collection<AuditTaskImpl> tasks = q.getResultList();
          if (tasks == null || tasks.size() == 0) {
              return 0;
          }
          return tasks.size();
      }
      

       

      Note that the this.em I'm using here is created from the same EntityManagerFactory which is used to create my RuntimeEnvironment:

      RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault()
                                                                .persistence(true)
                                                                .entityManagerFactory(emf)
                                                                .userGroupCallback(this.getUsersAndGroups())
                                                                .knowledgeBase(kbase)
                                                                .registerableItemsFactory(new DefaultRegisterableItemsFactory() {
                                                                  @Override
                                                                  public List<TaskLifeCycleEventListener> getTaskListeners() {
                                                                      List<TaskLifeCycleEventListener> defaultListeners = new ArrayList<>();
                                                                      defaultListeners.add(new JPATaskLifeCycleEventListener());
                                                                      // add any custom listeners
                                                                      defaultListeners.addAll(super.getTaskListeners());
                                                                      // add listeners from deployment descriptor
                                                                      defaultListeners.addAll(getTaskListenersFromDescriptor());
      
      
                                                                      defaultListeners.add(new MyTaskLifeCycleEventListener());
      
      
                                                                      return defaultListeners;
                                                                  }
                                                                })
                                                                .get();