@Override public Assignment apply(Task task, TaskContext taskContext, String excludedUser) { UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) taskContext).get(EnvironmentName.TASK_USER_INFO); List<OrganizationalEntity> excluded = getExcludedEntities(task, userInfo); // Get the the users from the task's the potential owners List<OrganizationalEntity> potentialOwners = task.getPeopleAssignments().getPotentialOwners().stream() .filter(oe -> oe instanceof User && !excluded.contains(oe)) .collect(Collectors.toList()); // Get the users belonging to groups that are potential owners task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof Group) .forEach(oe -> { Iterator<OrganizationalEntity> groupUsers = userInfo.getMembersForGroup((Group) oe); if (groupUsers != null) { groupUsers.forEachRemaining(user -> { if (user != null && !excluded.contains(user) && !potentialOwners.contains(user)) { potentialOwners.add(user); } }); } }); if (excludedUser != null) { logger.debug("Removing excluded user {} from the list of eligible users", excludedUser); potentialOwners.removeIf(entity -> entity.getId().equals(excludedUser)); } String queueName = getQueueName(task); CircularQueue<OrganizationalEntity> mappedQueue = synchronizedQueue(queueName, potentialOwners); OrganizationalEntity owner = mappedQueue.take(); return new Assignment(owner.getId()); }
/** * Synchronizes the {@code OrganizationalEntity} objects contained in the {@code CircularQueue} and the list of * potential owners * @param queueName The name of the queue to be synchronized. * @param potentialOwners This list of potential owners of the task * @return The CircularQueue that contains all potential owners */ private synchronized CircularQueue<OrganizationalEntity> synchronizedQueue(String queueName, List<OrganizationalEntity> potentialOwners) { CircularQueue<OrganizationalEntity> existingQueue = (queueName == null || queueName.trim().length() == 0) ? null : circularQueueMap.get(queueName); // If the queue does not exist then a new CircularQueue should be created final CircularQueue<OrganizationalEntity> workingQueue = existingQueue != null ? existingQueue : new CircularQueue(); potentialOwners.forEach(po -> { if (!queueContainsUser(workingQueue, po)) { workingQueue.add(po); } }); workingQueue.removeIf(oe -> !potentialOwners.contains(oe)); circularQueueMap.put(queueName, workingQueue); return workingQueue; }
@Before public void setup() { System.setProperty("org.jbpm.task.assignment.enabled", "true"); pds = setupPoolingDataSource(); emf = Persistence.createEntityManagerFactory("org.jbpm.services.task"); AssignmentServiceProvider.override(new RoundRobinAssignmentStrategy()); this.taskService = (InternalTaskService) HumanTaskServiceFactory.newTaskServiceConfigurator() .entityManagerFactory(emf) .getTaskService(); }