cancel
Showing results for 
Search instead for 
Did you mean: 

TaskQuery when using custom Group/User Manager

stbill79
Champ in-the-making
Champ in-the-making
Hi

I've overridden the User/Group Manager and Factories similar to the LDAP example. This means that the DB tables ACT_ID_GROUP,USER, and MEMBERSHIP are empty.

Essentially, I'd like to use the TaskQuery to grab a list of tasks for a given user; this would include all tasks where one of the following apply:
  • User is assigned
  • User's id is contained in the list of the task's CandidateUsers
  • One or more of User's groups are contained in the list of CandidateGroups (Groups need to be determined by GroupManager, not the DB)

  • The method TaskCandidateOrAssigned in TaskQuery seems to have some comments alluring to the fact that there are issues with candidate groups. Using the standard query.candidateOrAssigned() does not work correctly as it uses a direct join on the ACT_ID_MEMEBERSHIP table.

    What is the correct way to obtain the tasks as required above? Or maybe I need to do multiple queries and create a union myself.

    Thanks,
    Bill
7 REPLIES 7

jbarrez
Star Contributor
Star Contributor
You are correct, that does seem to be tightly (too tightly!) coupled with the membership table.
I would say a custom query is the only solution to solve it (or do multiple queries, but that's not nice)

errorcv
Champ in-the-making
Champ in-the-making
Hi all,

I have the same situation with user/groups/membership.

How to write such custom query using Activiti API?

Thanks in advance

xvm03
Champ in-the-making
Champ in-the-making
how to write the custom query?my users and groups not in the same db

stbill79
Champ in-the-making
Champ in-the-making
I ended up doing something like this (obtain your groupIds for the user however you need to do it):

<java>
    @Transactional(readOnly = true)
    public List<Task> findCandidateTasks(User context) {
        Assert.notNull(context);
        String userId = context.getId();
        LOG.debug("Getting assigned, candidate user, and candidate group tasks for user: {}", userId);

        List<Task> candidateUserTasks = taskService.createTaskQuery().
                includeProcessVariables().
                taskCandidateUser(userId).
                orderByTaskCreateTime().asc().list();
        LOG.debug("Found: {} candidate user tasks for user: {}", candidateUserTasks.size(), userId);

        List<Task> assignedTasks = taskService.createTaskQuery().includeProcessVariables().
                taskAssignee(userId).orderByTaskCreateTime().asc().list();
        LOG.debug("Found: {} assigned user tasks for user: {}", assignedTasks.size(), userId);


        //Get your groupIds however you do it (identityService, custom method, etc.)
        List<String> groupIds = getGroupIdsByUserId(userId);

        List<Task> groupCandidateTasks = taskService.createTaskQuery().includeProcessVariables().
            taskCandidateGroupIn(groupIds).
            orderByTaskCreateTime().asc().list();
        LOG.debug("Found: {} group tasks for user: {}", groupTasks.size(), userId);

        Set<Task> candidateTasks = Sets.newHashSet();
        candidateTasks.add(assigneeTasks);
        candidateTasks.add(candidateUserTasks);
        candidateTasks.add(candidateGroupTasks);
        LOG.debug("got {} total group/user candidate tasks for user {}", candidateTasks.size(), userId);

        List<Task> candidateTaskList = Lists.newArrayList(candidateTasks);
        Collections.sort(candidateTaskList);
        return candidateTaskList;
    }

</java>

In my code, I actually created a TaskWrapper class to make sure both sorting works by creation date and equals uses the task id. YMMV.

sendreams
Champ in-the-making
Champ in-the-making
I have the same situation with user/groups/membership. and i use or condition, but nothing task return. why?
List<Task> tasks = taskService.createTaskQuery()
    .processDefinitionKey("leaveProcess")  // 后期要改
    .taskAssignee(user.getUsername())
    .or()
    .taskCandidateUser(user.getUsername())
//    .taskCandidateOrAssigned(user.getUsername())
    .list();

sendreams
Champ in-the-making
Champ in-the-making
if i change the code to below, it can return a task list.
List tasks = taskService.createTaskQuery()
.processDefinitionKey("leaveProcess") // 后期要改
.taskAssignee(user.getUsername())
// .taskCandidateOrAssigned(user.getUsername())
.list();

i use 5.19.0.2

jbarrez
Star Contributor
Star Contributor
the or() method is supposed to be used in a block with endOr(). Everything in the middle of that block will be or-ed.