Search in sources :

Example 1 with InternalPeopleAssignments

use of org.kie.internal.task.api.model.InternalPeopleAssignments 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));
}
Also used : Group(org.kie.api.task.model.Group) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalTaskService(org.kie.internal.task.api.InternalTaskService) TaskService(org.kie.api.task.TaskService) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) ArrayList(java.util.ArrayList) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments)

Example 2 with InternalPeopleAssignments

use of org.kie.internal.task.api.model.InternalPeopleAssignments 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;
}
Also used : Group(org.kie.api.task.model.Group) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) ArrayList(java.util.ArrayList)

Example 3 with InternalPeopleAssignments

use of org.kie.internal.task.api.model.InternalPeopleAssignments 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());
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Task(org.kie.api.task.model.Task) Assignment(org.kie.internal.task.api.assignment.Assignment) EnvironmentName(org.kie.api.runtime.EnvironmentName) UserInfo(org.kie.internal.task.api.UserInfo) AssignmentImpl(org.jbpm.services.task.assignment.impl.AssignmentImpl) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) List(java.util.List) Group(org.kie.api.task.model.Group) AssignmentStrategy(org.kie.internal.task.api.assignment.AssignmentStrategy) Map(java.util.Map) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) Collections(java.util.Collections) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) User(org.kie.api.task.model.User) Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) HashMap(java.util.HashMap) AssignmentImpl(org.jbpm.services.task.assignment.impl.AssignmentImpl) ArrayList(java.util.ArrayList) UserInfo(org.kie.internal.task.api.UserInfo) Assignment(org.kie.internal.task.api.assignment.Assignment) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) List(java.util.List) TaskContext(org.kie.api.task.TaskContext) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 4 with InternalPeopleAssignments

use of org.kie.internal.task.api.model.InternalPeopleAssignments in project jbpm by kiegroup.

the class LifeCycleBaseTest method testActivateFromIncorrectStatus.

@Test
public void testActivateFromIncorrectStatus() {
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { status = Status.Ready } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [ new User('Darth Vader'), new User('Bobba Fet') ], ";
    str += "businessAdministrators = [ new User('Jabba Hutt') ] } ),";
    str += "name = 'This is my task name' })";
    Task task = TaskFactory.evalTask(new StringReader(str), null);
    // We need to add the Admin if we don't initialize the task
    if (task.getPeopleAssignments() != null && task.getPeopleAssignments().getBusinessAdministrators() != null) {
        List<OrganizationalEntity> businessAdmins = new ArrayList<OrganizationalEntity>();
        User user = createUser("Administrator");
        businessAdmins.add(user);
        businessAdmins.addAll(task.getPeopleAssignments().getBusinessAdministrators());
        ((InternalPeopleAssignments) task.getPeopleAssignments()).setBusinessAdministrators(businessAdmins);
    }
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    try {
        taskService.activate(taskId, "Darth Vader");
        fail("Shouldn't have succeded");
    } catch (RuntimeException e) {
    // assertNotNull(activateResponseHandler.getError());
    // assertNotNull(activateResponseHandler.getError().getMessage());
    // assertTrue(activateResponseHandler.getError().getMessage().contains("Darth Vader"));
    }
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 5 with InternalPeopleAssignments

use of org.kie.internal.task.api.model.InternalPeopleAssignments in project jbpm by kiegroup.

the class DeadlinesBaseTest method testDelayedEmailNotificationOnDeadlineTaskCompleted.

@Test(timeout = 10000)
public void testDelayedEmailNotificationOnDeadlineTaskCompleted() throws Exception {
    CountDownTaskEventListener countDownListener = new CountDownTaskEventListener(1, false, true);
    addCountDownListner(countDownListener);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("now", new Date());
    Reader reader = new InputStreamReader(getClass().getResourceAsStream(MvelFilePath.DeadlineWithNotification));
    InternalTask task = (InternalTask) TaskFactory.evalTask(reader, vars);
    ((InternalTaskData) task.getTaskData()).setSkipable(true);
    InternalPeopleAssignments assignments = (InternalPeopleAssignments) TaskModelProvider.getFactory().newPeopleAssignments();
    List<OrganizationalEntity> ba = new ArrayList<OrganizationalEntity>();
    User user = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) user).setId("Administrator");
    ba.add(user);
    assignments.setBusinessAdministrators(ba);
    List<OrganizationalEntity> po = new ArrayList<OrganizationalEntity>();
    User user2 = TaskModelProvider.getFactory().newUser();
    ((InternalOrganizationalEntity) user2).setId("Administrator");
    po.add(user2);
    assignments.setPotentialOwners(po);
    task.setPeopleAssignments(assignments);
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    InternalContent content = (InternalContent) TaskModelProvider.getFactory().newContent();
    Map<String, String> params = fillMarshalSubjectAndBodyParams();
    ContentData marshalledObject = ContentMarshallerHelper.marshal(task, params, null);
    content.setContent(marshalledObject.getContent());
    taskService.addContent(taskId, content);
    long contentId = content.getId();
    content = (InternalContent) taskService.getContentById(contentId);
    Object unmarshallObject = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    checkContentSubjectAndBody(unmarshallObject);
    taskService.start(taskId, "Administrator");
    taskService.complete(taskId, "Administrator", null);
    // emails should not be set yet
    assertThat(((MockNotificationListener) notificationListener).getEventsRecieved().size()).isEqualTo(0);
    countDownListener.waitTillCompleted();
    // no email should ne sent as task was completed before deadline was triggered
    assertThat(((MockNotificationListener) notificationListener).getEventsRecieved().size()).isEqualTo(0);
    task = (InternalTask) taskService.getTaskById(taskId);
    assertThat(task.getTaskData().getStatus()).isEqualTo(Status.Completed);
    assertThat(((InternalTask) task).getDeadlines().getStartDeadlines().size()).isEqualTo(0);
    assertThat(((InternalTask) task).getDeadlines().getEndDeadlines().size()).isEqualTo(0);
}
Also used : User(org.kie.api.task.model.User) CountDownTaskEventListener(org.jbpm.services.task.util.CountDownTaskEventListener) InputStreamReader(java.io.InputStreamReader) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) InternalContent(org.kie.internal.task.api.model.InternalContent) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) ContentData(org.kie.internal.task.api.model.ContentData) Test(org.junit.Test)

Aggregations

InternalPeopleAssignments (org.kie.internal.task.api.model.InternalPeopleAssignments)29 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)27 InternalTask (org.kie.internal.task.api.model.InternalTask)20 User (org.kie.api.task.model.User)19 Test (org.junit.Test)18 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)17 ArrayList (java.util.ArrayList)15 InternalOrganizationalEntity (org.kie.internal.task.api.model.InternalOrganizationalEntity)15 InputStreamReader (java.io.InputStreamReader)10 Reader (java.io.Reader)10 CountDownTaskEventListener (org.jbpm.services.task.util.CountDownTaskEventListener)10 ContentData (org.kie.internal.task.api.model.ContentData)10 InternalContent (org.kie.internal.task.api.model.InternalContent)10 Task (org.kie.api.task.model.Task)9 HashMap (java.util.HashMap)7 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)6 Date (java.util.Date)5 WorkItem (org.drools.core.process.instance.WorkItem)5 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)5 Group (org.kie.api.task.model.Group)4