use of org.kie.api.task.model.OrganizationalEntity in project drools by kiegroup.
the class ProcessContextTest method testProcessContextGetAssignment.
@Test
public void testProcessContextGetAssignment() {
KieBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
KieSession ksession = kbase.newKieSession();
assertNotNull(ksession);
CaseInformation caseInfo = new CaseInformation();
caseInfo.assign("owner", new OrganizationalEntity() {
@Override
public String getId() {
return "testUser";
}
@Override
public void writeExternal(ObjectOutput out) throws IOException {
}
@Override
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
});
ksession.insert(caseInfo);
ProcessContext processContext = new ProcessContext(ksession);
CaseAssignment caseAssignment = processContext.getCaseAssignment();
assertNotNull(caseAssignment);
Collection<OrganizationalEntity> forRole = caseAssignment.getAssignments("owner");
assertNotNull(forRole);
assertEquals(1, forRole.size());
}
use of org.kie.api.task.model.OrganizationalEntity in project jbpm by kiegroup.
the class DefaultChecklistManager method addTask.
public ChecklistItem addTask(String userId, String[] actorIds, String[] groupIds, String name, String orderingId, long processInstanceId) {
RuntimeEngine runtime = getRuntime();
InternalTask task = (InternalTask) TaskModelProvider.getFactory().newTask();
;
setTaskName(task, name);
setTaskDescription(task, orderingId);
// task.setPriority(priority);
InternalTaskData taskData = (InternalTaskData) TaskModelProvider.getFactory().newTaskData();
taskData.setProcessInstanceId(processInstanceId);
// taskData.setProcessSessionId(sessionId);
taskData.setSkipable(false);
taskData.setDeploymentId("default-singleton");
User cuser = TaskModelProvider.getFactory().newUser();
((InternalOrganizationalEntity) cuser).setId(userId);
taskData.setCreatedBy(cuser);
task.setTaskData(taskData);
InternalPeopleAssignments peopleAssignments = (InternalPeopleAssignments) task.getPeopleAssignments();
if (peopleAssignments == null) {
peopleAssignments = (InternalPeopleAssignments) TaskModelProvider.getFactory().newPeopleAssignments();
peopleAssignments.setPotentialOwners(new ArrayList<OrganizationalEntity>());
peopleAssignments.setBusinessAdministrators(new ArrayList<OrganizationalEntity>());
peopleAssignments.setExcludedOwners(new ArrayList<OrganizationalEntity>());
peopleAssignments.setRecipients(new ArrayList<OrganizationalEntity>());
peopleAssignments.setTaskStakeholders(new ArrayList<OrganizationalEntity>());
task.setPeopleAssignments(peopleAssignments);
}
List<OrganizationalEntity> potentialOwners = new ArrayList<OrganizationalEntity>();
for (String actorId : actorIds) {
User user = TaskModelProvider.getFactory().newUser();
((InternalOrganizationalEntity) user).setId(actorId);
potentialOwners.add(user);
}
for (String groupId : groupIds) {
Group group = TaskModelProvider.getFactory().newGroup();
((InternalOrganizationalEntity) group).setId(groupId);
potentialOwners.add(group);
}
setTaskPotentialOwners(task, potentialOwners);
List<OrganizationalEntity> businessAdministrators = peopleAssignments.getBusinessAdministrators();
User administrator = TaskModelProvider.getFactory().newUser();
((InternalOrganizationalEntity) administrator).setId("Administrator");
businessAdministrators.add(administrator);
TaskService taskService = runtime.getTaskService();
long taskId = taskService.addTask(task, (Map<String, Object>) null);
manager.disposeRuntimeEngine(runtime);
return ChecklistItemFactory.createChecklistItem(taskService.getTaskById(taskId));
}
use of org.kie.api.task.model.OrganizationalEntity in project jbpm by kiegroup.
the class LoadBalanceAssignmentStrategy method getExcludedEntities.
private static List<OrganizationalEntity> getExcludedEntities(Task task, UserInfo userInfo) {
List<OrganizationalEntity> excluded = ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners();
List<OrganizationalEntity> excludedUsers = new ArrayList<>();
for (OrganizationalEntity entity : excluded) {
if (entity instanceof Group) {
userInfo.getMembersForGroup((Group) entity).forEachRemaining(excludedUsers::add);
}
}
excluded.addAll(excludedUsers);
return excluded;
}
use of org.kie.api.task.model.OrganizationalEntity in project jbpm by kiegroup.
the class LoadBalanceAssignmentStrategy method apply.
@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, making sure that excluded users are not included
List<OrganizationalEntity> potentialOwners = task.getPeopleAssignments().getPotentialOwners().stream().filter(oe -> oe instanceof User && !excluded.contains(oe) && !oe.getId().equals(excludedUser)).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) && !user.getId().equals(excludedUser)) {
potentialOwners.add(user);
}
});
}
});
logger.debug("Asking the load calculator [{}] for task loads for the users {}", calculator.getIdentifier(), potentialOwners);
List<User> users = potentialOwners.stream().map(entityToUser).collect(Collectors.toList());
Collection<UserTaskLoad> loads = calculator.getUserTaskLoads(users, taskContext);
UserTaskLoad lightestLoad = loads.stream().min(UserTaskLoad::compareTo).orElse(null);
return lightestLoad != null ? new Assignment(lightestLoad.getUser().getId()) : null;
}
use of org.kie.api.task.model.OrganizationalEntity in project jbpm by kiegroup.
the class PotentialOwnerBusynessAssignmentStrategy method apply.
@Override
public Assignment apply(Task task, TaskContext context, String excludedUser) {
if (task.getPeopleAssignments().getPotentialOwners().isEmpty()) {
logger.debug("No potential owners in the task {} can't auto assign", task);
return null;
}
List<OrganizationalEntity> potentialOwners = new ArrayList<>(task.getPeopleAssignments().getPotentialOwners());
Set<String> resolvedUsers = new TreeSet<>(Collections.reverseOrder());
List<OrganizationalEntity> excludedOwners = ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners();
potentialOwners.stream().filter(po -> po instanceof User && !excludedOwners.contains(po)).forEach(po -> resolvedUsers.add(po.getId()));
UserInfo userInfo = (UserInfo) ((org.jbpm.services.task.commands.TaskContext) context).get(EnvironmentName.TASK_USER_INFO);
if (userInfo != null) {
logger.debug("Groups going to be resolved by {}", userInfo);
potentialOwners.stream().filter(po -> po instanceof Group && !excludedOwners.contains(po)).forEach(po -> {
Iterator<OrganizationalEntity> usersOfGroup = userInfo.getMembersForGroup((Group) po);
if (usersOfGroup != null) {
while (usersOfGroup.hasNext()) {
OrganizationalEntity entity = usersOfGroup.next();
if (!excludedOwners.contains(entity)) {
resolvedUsers.add(entity.getId());
}
}
}
});
}
logger.debug("Resolved users eligible for task {} assignments are {}", task, resolvedUsers);
if (excludedUser != null) {
logger.debug("Removing excluded user {} from the list of eligible users", excludedUser);
resolvedUsers.remove(excludedUser);
}
TaskPersistenceContext persistenceContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
Map<String, Object> params = new HashMap<>();
params.put("owners", resolvedUsers);
logger.debug("DB query to be used for finding assignments :: '{}'", getQuery());
List<Assignment> assignments = persistenceContext.queryStringWithParametersInTransaction(getQuery(), params, ClassUtil.<List<Assignment>>castClass(List.class));
if (assignments.size() < resolvedUsers.size()) {
logger.debug("Not all eligible users found in db, adding missing bits (eligible {}, found in db {})", resolvedUsers, assignments);
// in case not all users have already assigned tasks added them to the list so can get the tasks
resolvedUsers.forEach(user -> {
Assignment assignment = new AssignmentImpl(user);
if (!assignments.contains(assignment)) {
// always add missing users to the top of the list so they get assigned first
assignments.add(0, assignment);
}
});
}
if (assignments.isEmpty()) {
logger.debug("No assignments found for task {}", task);
return null;
}
logger.debug("Following assignments {} were found for task {}", assignments, task);
// select first from the top of the list as it has the least assigned tasks
Assignment selected = assignments.get(0);
logger.debug("Retruning first of found assignments {}", selected);
return new Assignment(selected.getUser());
}
Aggregations