Search in sources :

Example 16 with TaskPersistenceContext

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

the class JPATaskLifeCycleEventListener method afterTaskStartedEvent.

@Override
public void afterTaskStartedEvent(TaskEvent event) {
    String userId = event.getTaskContext().getUserId();
    Task ti = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    try {
        persistenceContext.persist(new TaskEventImpl(ti.getId(), org.kie.internal.task.api.model.TaskEvent.TaskEventType.STARTED, ti.getTaskData().getProcessInstanceId(), ti.getTaskData().getWorkItemId(), userId));
        AuditTaskImpl auditTaskImpl = getAuditTask(event, persistenceContext, ti);
        if (auditTaskImpl == null) {
            logger.warn("Unable find audit task entry for task id {} '{}', skipping audit task update", ti.getId(), ti.getName());
            return;
        }
        auditTaskImpl.setStatus(ti.getTaskData().getStatus().name());
        auditTaskImpl.setActualOwner(getActualOwner(ti));
        auditTaskImpl.setLastModificationDate(event.getEventDate());
        persistenceContext.merge(auditTaskImpl);
    } finally {
        cleanup(persistenceContext);
    }
}
Also used : Task(org.kie.api.task.model.Task) TaskEventImpl(org.jbpm.services.task.audit.impl.model.TaskEventImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) AuditTaskImpl(org.jbpm.services.task.audit.impl.model.AuditTaskImpl)

Example 17 with TaskPersistenceContext

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

the class JPATaskLifeCycleEventListener method afterTaskAddedEvent.

@Override
public void afterTaskAddedEvent(TaskEvent event) {
    String userId = event.getTaskContext().getUserId();
    Task ti = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    try {
        if (ti.getTaskData().getProcessId() != null) {
            userId = ti.getTaskData().getProcessId();
        }
        AuditTaskImpl auditTaskImpl = new AuditTaskImpl(ti.getId(), ti.getName(), ti.getTaskData().getStatus().name(), ti.getTaskData().getActivationTime(), (ti.getTaskData().getActualOwner() != null) ? ti.getTaskData().getActualOwner().getId() : "", ti.getDescription(), ti.getPriority(), (ti.getTaskData().getCreatedBy() != null) ? ti.getTaskData().getCreatedBy().getId() : "", ti.getTaskData().getCreatedOn(), ti.getTaskData().getExpirationTime(), ti.getTaskData().getProcessInstanceId(), ti.getTaskData().getProcessId(), ti.getTaskData().getProcessSessionId(), ti.getTaskData().getDeploymentId(), ti.getTaskData().getParentId(), ti.getTaskData().getWorkItemId());
        persistenceContext.persist(auditTaskImpl);
        persistenceContext.persist(new TaskEventImpl(ti.getId(), org.kie.internal.task.api.model.TaskEvent.TaskEventType.ADDED, ti.getTaskData().getProcessInstanceId(), ti.getTaskData().getWorkItemId(), userId));
    } finally {
        cleanup(persistenceContext);
    }
}
Also used : Task(org.kie.api.task.model.Task) TaskEventImpl(org.jbpm.services.task.audit.impl.model.TaskEventImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) AuditTaskImpl(org.jbpm.services.task.audit.impl.model.AuditTaskImpl)

Example 18 with TaskPersistenceContext

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

the class JPATaskLifeCycleEventListener method afterTaskClaimedEvent.

@Override
public void afterTaskClaimedEvent(TaskEvent event) {
    String userId = event.getTaskContext().getUserId();
    Task ti = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    try {
        persistenceContext.persist(new TaskEventImpl(ti.getId(), org.kie.internal.task.api.model.TaskEvent.TaskEventType.CLAIMED, ti.getTaskData().getProcessInstanceId(), ti.getTaskData().getWorkItemId(), userId));
        AuditTaskImpl auditTaskImpl = getAuditTask(event, persistenceContext, ti);
        if (auditTaskImpl == null) {
            logger.warn("Unable find audit task entry for task id {} '{}', skipping audit task update", ti.getId(), ti.getName());
            return;
        }
        auditTaskImpl.setStatus(ti.getTaskData().getStatus().name());
        auditTaskImpl.setActualOwner(getActualOwner(ti));
        auditTaskImpl.setDescription(ti.getDescription());
        auditTaskImpl.setLastModificationDate(event.getEventDate());
        persistenceContext.merge(auditTaskImpl);
    } finally {
        cleanup(persistenceContext);
    }
}
Also used : Task(org.kie.api.task.model.Task) TaskEventImpl(org.jbpm.services.task.audit.impl.model.TaskEventImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) AuditTaskImpl(org.jbpm.services.task.audit.impl.model.AuditTaskImpl)

Example 19 with TaskPersistenceContext

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

the class TaskCountLoadCalculator method getUserTaskLoads.

@Override
public Collection<UserTaskLoad> getUserTaskLoads(List<User> users, TaskContext context) {
    Collection<UserTaskLoad> userTaskLoads = new ArrayList<>();
    List<String> userIds = users.stream().map(user -> {
        return user.getId();
    }).collect(Collectors.toList());
    TaskPersistenceContext persistenceContext = ((org.jbpm.services.task.commands.TaskContext) context).getPersistenceContext();
    Map<String, Object> params = new HashMap<>();
    params.put("owners", userIds);
    logger.debug("DB query to be used for finding assignments :: '{}'", getMultiUserQuery());
    List<AssignmentImpl> assignments = persistenceContext.queryStringWithParametersInTransaction(getMultiUserQuery(), params, ClassUtil.<List<AssignmentImpl>>castClass(List.class));
    Map<String, AssignmentImpl> assignmentMap = assignments.stream().collect(Collectors.toMap(assignKey, (assign) -> assign));
    if (assignments != null && !assignments.isEmpty()) {
        users.forEach(usr -> {
            String uid = usr.getId();
            if (assignmentMap.containsKey(uid)) {
                Long loadValue = assignmentMap.get(uid).getCurrentlyAssigned();
                userTaskLoads.add(new UserTaskLoad(getIdentifier(), usr, new Double(loadValue != null ? loadValue : 0)));
            } else {
                userTaskLoads.add(new UserTaskLoad(getIdentifier(), usr, new Double(0)));
            }
        });
    } else {
        users.forEach(u -> {
            userTaskLoads.add(new UserTaskLoad(getIdentifier(), u, new Double(0)));
        });
    }
    return userTaskLoads;
}
Also used : TaskContext(org.kie.api.task.TaskContext) Logger(org.slf4j.Logger) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) User(org.kie.api.task.model.User) TaskContext(org.kie.api.task.TaskContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) UserTaskLoad(org.jbpm.services.task.assignment.UserTaskLoad) ArrayList(java.util.ArrayList) List(java.util.List)

Example 20 with TaskPersistenceContext

use of org.kie.internal.task.api.TaskPersistenceContext 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)

Aggregations

TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)48 Task (org.kie.api.task.model.Task)31 AuditTaskImpl (org.jbpm.services.task.audit.impl.model.AuditTaskImpl)19 TaskEventImpl (org.jbpm.services.task.audit.impl.model.TaskEventImpl)19 List (java.util.List)14 HashMap (java.util.HashMap)13 TaskContext (org.kie.internal.task.api.TaskContext)9 ArrayList (java.util.ArrayList)8 Map (java.util.Map)8 TaskContext (org.jbpm.services.task.commands.TaskContext)7 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)5 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)5 TaskContext (org.kie.api.task.TaskContext)5 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)5 Logger (org.slf4j.Logger)5 LoggerFactory (org.slf4j.LoggerFactory)5 Date (java.util.Date)4 ClassUtil (org.jbpm.services.task.utils.ClassUtil)4 Content (org.kie.api.task.model.Content)4 Status (org.kie.api.task.model.Status)4