Search in sources :

Example 1 with Reassignment

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

the class ExecuteDeadlinesCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Void execute(Context context) {
    TaskContext ctx = (TaskContext) context;
    UserInfo userInfo = (UserInfo) context.get(EnvironmentName.TASK_USER_INFO);
    TaskPersistenceContext persistenceContext = ctx.getPersistenceContext();
    try {
        Task task = persistenceContext.findTask(taskId);
        Deadline deadline = persistenceContext.findDeadline(deadlineId);
        if (task == null || deadline == null) {
            return null;
        }
        TaskData taskData = task.getTaskData();
        if (taskData != null) {
            // check if task is still in valid status
            if (type.isValidStatus(taskData.getStatus())) {
                Map<String, Object> variables = null;
                Content content = persistenceContext.findContent(taskData.getDocumentContentId());
                if (content != null) {
                    ContentMarshallerContext mContext = ctx.getTaskContentService().getMarshallerContext(task);
                    Object objectFromBytes = ContentMarshallerHelper.unmarshall(content.getContent(), mContext.getEnvironment(), mContext.getClassloader());
                    if (objectFromBytes instanceof Map) {
                        variables = (Map<String, Object>) objectFromBytes;
                    } else {
                        variables = new HashMap<String, Object>();
                        variables.put("content", objectFromBytes);
                    }
                } else {
                    variables = Collections.emptyMap();
                }
                if (deadline == null || deadline.getEscalations() == null) {
                    return null;
                }
                TaskEventSupport taskEventSupport = ctx.getTaskEventSupport();
                for (Escalation escalation : deadline.getEscalations()) {
                    // run reassignment first to allow notification to be send to new potential owners
                    if (!escalation.getReassignments().isEmpty()) {
                        taskEventSupport.fireBeforeTaskReassigned(task, ctx);
                        // get first and ignore the rest.
                        Reassignment reassignment = escalation.getReassignments().get(0);
                        logger.debug("Reassigning to {}", reassignment.getPotentialOwners());
                        ((InternalTaskData) task.getTaskData()).setStatus(Status.Ready);
                        List<OrganizationalEntity> potentialOwners = new ArrayList<OrganizationalEntity>(reassignment.getPotentialOwners());
                        ((InternalPeopleAssignments) task.getPeopleAssignments()).setPotentialOwners(potentialOwners);
                        ((InternalTaskData) task.getTaskData()).setActualOwner(null);
                        // use assignment service to directly assign actual owner if enabled
                        AssignmentService assignmentService = AssignmentServiceProvider.get();
                        if (assignmentService.isEnabled()) {
                            assignmentService.assignTask(task, ctx);
                        }
                        taskEventSupport.fireAfterTaskReassigned(task, ctx);
                    }
                    for (Notification notification : escalation.getNotifications()) {
                        if (notification.getNotificationType() == NotificationType.Email) {
                            taskEventSupport.fireBeforeTaskNotified(task, ctx);
                            logger.debug("Sending an Email");
                            NotificationListenerManager.get().broadcast(new NotificationEvent(notification, task, variables), userInfo);
                            taskEventSupport.fireAfterTaskNotified(task, ctx);
                        }
                    }
                }
            }
        }
        deadline.setEscalated(true);
        persistenceContext.updateDeadline(deadline);
        persistenceContext.updateTask(task);
    } catch (Exception e) {
        logger.error("Error when executing deadlines", e);
    }
    return null;
}
Also used : Task(org.kie.api.task.model.Task) Escalation(org.kie.internal.task.api.model.Escalation) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) ArrayList(java.util.ArrayList) UserInfo(org.kie.internal.task.api.UserInfo) AssignmentService(org.jbpm.services.task.assignment.AssignmentService) Notification(org.kie.internal.task.api.model.Notification) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) TaskData(org.kie.api.task.model.TaskData) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) Deadline(org.kie.internal.task.api.model.Deadline) NotificationEvent(org.kie.internal.task.api.model.NotificationEvent) Reassignment(org.kie.internal.task.api.model.Reassignment) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) Content(org.kie.api.task.model.Content) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with Reassignment

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

the class ListTaskReassignmentsCommand method execute.

@Override
public List<TaskReassignment> execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    Deadlines deadlines = ((InternalTask) task).getDeadlines();
    List<TaskReassignment> reassignmantsNotStarted = deadlines.getStartDeadlines().stream().filter(d -> !d.getEscalations().isEmpty() && !d.getEscalations().get(0).getReassignments().isEmpty()).map(d -> {
        Reassignment r = d.getEscalations().get(0).getReassignments().get(0);
        return new TaskReassignmentImpl(d.getId(), get(r.getDocumentation()), d.getDate(), r.getPotentialOwners(), !d.isEscalated());
    }).collect(Collectors.toList());
    List<TaskReassignment> reassignmantsNotCompleted = deadlines.getEndDeadlines().stream().filter(d -> !d.getEscalations().isEmpty() && !d.getEscalations().get(0).getReassignments().isEmpty()).map(d -> {
        Reassignment r = d.getEscalations().get(0).getReassignments().get(0);
        return new TaskReassignmentImpl(d.getId(), get(r.getDocumentation()), d.getDate(), r.getPotentialOwners(), !d.isEscalated());
    }).collect(Collectors.toList());
    List<TaskReassignment> result = new ArrayList<>();
    result.addAll(reassignmantsNotStarted);
    result.addAll(reassignmantsNotCompleted);
    if (activeOnly) {
        logger.debug("Removing already completed deadlines from the result");
        result = result.stream().filter(t -> t.isActive()).collect(Collectors.toList());
    }
    return result;
}
Also used : Logger(org.slf4j.Logger) Task(org.kie.api.task.model.Task) LoggerFactory(org.slf4j.LoggerFactory) I18NText(org.kie.api.task.model.I18NText) TaskReassignmentImpl(org.jbpm.kie.services.impl.admin.TaskReassignmentImpl) Collectors(java.util.stream.Collectors) TaskReassignment(org.jbpm.services.api.admin.TaskReassignment) ArrayList(java.util.ArrayList) UserGroupCallbackTaskCommand(org.jbpm.services.task.commands.UserGroupCallbackTaskCommand) List(java.util.List) Context(org.kie.api.runtime.Context) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) InternalTask(org.kie.internal.task.api.model.InternalTask) Reassignment(org.kie.internal.task.api.model.Reassignment) TaskContext(org.jbpm.services.task.commands.TaskContext) Deadlines(org.kie.internal.task.api.model.Deadlines) Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) TaskContext(org.jbpm.services.task.commands.TaskContext) Deadlines(org.kie.internal.task.api.model.Deadlines) InternalTask(org.kie.internal.task.api.model.InternalTask) TaskReassignment(org.jbpm.services.api.admin.TaskReassignment) ArrayList(java.util.ArrayList) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskReassignmentImpl(org.jbpm.kie.services.impl.admin.TaskReassignmentImpl) TaskReassignment(org.jbpm.services.api.admin.TaskReassignment) Reassignment(org.kie.internal.task.api.model.Reassignment)

Example 3 with Reassignment

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

the class HumanTaskHandlerHelperTest method testSetDeadlinesNotStartedReassignTwoTimes.

@Test
public void testSetDeadlinesNotStartedReassignTwoTimes() {
    WorkItem workItem = new WorkItemImpl();
    workItem.setParameter("NotStartedReassign", "[users:john]@[4h,6h]");
    @SuppressWarnings("unchecked") Deadlines deadlines = HumanTaskHandlerHelper.setDeadlines(workItem, Collections.EMPTY_LIST, null);
    assertNotNull(deadlines);
    assertEquals(2, deadlines.getStartDeadlines().size());
    assertEquals(0, deadlines.getEndDeadlines().size());
    assertEquals(1, deadlines.getStartDeadlines().get(0).getEscalations().size());
    assertEquals(1, deadlines.getStartDeadlines().get(0).getEscalations().get(0).getReassignments().size());
    assertEquals(0, deadlines.getStartDeadlines().get(0).getEscalations().get(0).getNotifications().size());
    assertEquals(1, deadlines.getStartDeadlines().get(1).getEscalations().size());
    assertEquals(1, deadlines.getStartDeadlines().get(1).getEscalations().get(0).getReassignments().size());
    assertEquals(0, deadlines.getStartDeadlines().get(1).getEscalations().get(0).getNotifications().size());
    // verify reassignment
    Reassignment reassignment = deadlines.getStartDeadlines().get(0).getEscalations().get(0).getReassignments().get(0);
    assertEquals(1, reassignment.getPotentialOwners().size());
    assertEquals("john", reassignment.getPotentialOwners().get(0).getId());
    // check deadline expiration time
    assertNotNull(deadlines.getStartDeadlines().get(0).getDate());
    long expirationTime = deadlines.getStartDeadlines().get(0).getDate().getTime() - System.currentTimeMillis();
    assertEquals(4, roundExpirationTime(expirationTime));
    // verify reassignment
    reassignment = deadlines.getStartDeadlines().get(1).getEscalations().get(0).getReassignments().get(0);
    assertEquals(1, reassignment.getPotentialOwners().size());
    assertEquals("john", reassignment.getPotentialOwners().get(0).getId());
    // check deadline expiration time
    assertNotNull(deadlines.getStartDeadlines().get(1).getDate());
    expirationTime = deadlines.getStartDeadlines().get(1).getDate().getTime() - System.currentTimeMillis();
    assertEquals(6, roundExpirationTime(expirationTime));
}
Also used : Deadlines(org.kie.internal.task.api.model.Deadlines) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkItem(org.drools.core.process.instance.WorkItem) Reassignment(org.kie.internal.task.api.model.Reassignment) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 4 with Reassignment

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

the class HumanTaskHandlerHelperTest method testSetDeadlinesNotStartedReassignWithGroups.

@Test
public void testSetDeadlinesNotStartedReassignWithGroups() {
    WorkItem workItem = new WorkItemImpl();
    workItem.setParameter("NotStartedReassign", "[users:john|groups:sales]@[4h]");
    @SuppressWarnings("unchecked") Deadlines deadlines = HumanTaskHandlerHelper.setDeadlines(workItem, Collections.EMPTY_LIST, null);
    assertNotNull(deadlines);
    assertEquals(1, deadlines.getStartDeadlines().size());
    assertEquals(0, deadlines.getEndDeadlines().size());
    assertEquals(1, deadlines.getStartDeadlines().get(0).getEscalations().size());
    assertEquals(1, deadlines.getStartDeadlines().get(0).getEscalations().get(0).getReassignments().size());
    assertEquals(0, deadlines.getStartDeadlines().get(0).getEscalations().get(0).getNotifications().size());
    // verify reassignment
    Reassignment reassignment = deadlines.getStartDeadlines().get(0).getEscalations().get(0).getReassignments().get(0);
    assertEquals(2, reassignment.getPotentialOwners().size());
    assertEquals("john", reassignment.getPotentialOwners().get(0).getId());
    assertEquals("sales", reassignment.getPotentialOwners().get(1).getId());
    // check deadline expiration time
    assertNotNull(deadlines.getStartDeadlines().get(0).getDate());
    long expirationTime = deadlines.getStartDeadlines().get(0).getDate().getTime() - System.currentTimeMillis();
    assertEquals(4, roundExpirationTime(expirationTime));
}
Also used : Deadlines(org.kie.internal.task.api.model.Deadlines) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkItem(org.drools.core.process.instance.WorkItem) Reassignment(org.kie.internal.task.api.model.Reassignment) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Example 5 with Reassignment

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

the class UserGroupCallbackTaskCommand method doCallbackOperationForTaskDeadlines.

protected void doCallbackOperationForTaskDeadlines(Deadlines deadlines, TaskContext context) {
    if (deadlines != null) {
        if (deadlines.getStartDeadlines() != null) {
            List<? extends Deadline> startDeadlines = deadlines.getStartDeadlines();
            for (Deadline startDeadline : startDeadlines) {
                List<? extends Escalation> escalations = startDeadline.getEscalations();
                if (escalations != null) {
                    for (Escalation escalation : escalations) {
                        List<? extends Notification> notifications = escalation.getNotifications();
                        List<? extends Reassignment> ressignments = escalation.getReassignments();
                        if (notifications != null) {
                            for (Notification notification : notifications) {
                                List<? extends OrganizationalEntity> recipients = notification.getRecipients();
                                if (recipients != null) {
                                    for (OrganizationalEntity recipient : recipients) {
                                        if (recipient instanceof User) {
                                            doCallbackUserOperation(recipient.getId(), context);
                                        }
                                        if (recipient instanceof Group) {
                                            doCallbackGroupOperation(recipient.getId(), context);
                                        }
                                    }
                                }
                                List<? extends OrganizationalEntity> administrators = notification.getBusinessAdministrators();
                                if (administrators != null) {
                                    for (OrganizationalEntity administrator : administrators) {
                                        if (administrator instanceof User) {
                                            doCallbackUserOperation(administrator.getId(), context);
                                        }
                                        if (administrator instanceof Group) {
                                            doCallbackGroupOperation(administrator.getId(), context);
                                        }
                                    }
                                }
                            }
                        }
                        if (ressignments != null) {
                            for (Reassignment reassignment : ressignments) {
                                List<? extends OrganizationalEntity> potentialOwners = reassignment.getPotentialOwners();
                                if (potentialOwners != null) {
                                    for (OrganizationalEntity potentialOwner : potentialOwners) {
                                        if (potentialOwner instanceof User) {
                                            doCallbackUserOperation(potentialOwner.getId(), context);
                                        }
                                        if (potentialOwner instanceof Group) {
                                            doCallbackGroupOperation(potentialOwner.getId(), context);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        if (deadlines.getEndDeadlines() != null) {
            List<? extends Deadline> endDeadlines = deadlines.getEndDeadlines();
            for (Deadline endDeadline : endDeadlines) {
                List<? extends Escalation> escalations = endDeadline.getEscalations();
                if (escalations != null) {
                    for (Escalation escalation : escalations) {
                        List<? extends Notification> notifications = escalation.getNotifications();
                        List<? extends Reassignment> ressignments = escalation.getReassignments();
                        if (notifications != null) {
                            for (Notification notification : notifications) {
                                List<? extends OrganizationalEntity> recipients = notification.getRecipients();
                                if (recipients != null) {
                                    for (OrganizationalEntity recipient : recipients) {
                                        if (recipient instanceof User) {
                                            doCallbackUserOperation(recipient.getId(), context);
                                        }
                                        if (recipient instanceof Group) {
                                            doCallbackGroupOperation(recipient.getId(), context);
                                        }
                                    }
                                }
                                List<? extends OrganizationalEntity> administrators = notification.getBusinessAdministrators();
                                if (administrators != null) {
                                    for (OrganizationalEntity administrator : administrators) {
                                        if (administrator instanceof User) {
                                            doCallbackUserOperation(administrator.getId(), context);
                                        }
                                        if (administrator instanceof Group) {
                                            doCallbackGroupOperation(administrator.getId(), context);
                                        }
                                    }
                                }
                            }
                        }
                        if (ressignments != null) {
                            for (Reassignment reassignment : ressignments) {
                                List<? extends OrganizationalEntity> potentialOwners = reassignment.getPotentialOwners();
                                if (potentialOwners != null) {
                                    for (OrganizationalEntity potentialOwner : potentialOwners) {
                                        if (potentialOwner instanceof User) {
                                            doCallbackUserOperation(potentialOwner.getId(), context);
                                        }
                                        if (potentialOwner instanceof Group) {
                                            doCallbackGroupOperation(potentialOwner.getId(), context);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Group(org.kie.api.task.model.Group) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) InternalOrganizationalEntity(org.kie.internal.task.api.model.InternalOrganizationalEntity) Deadline(org.kie.internal.task.api.model.Deadline) Escalation(org.kie.internal.task.api.model.Escalation) Reassignment(org.kie.internal.task.api.model.Reassignment) Notification(org.kie.internal.task.api.model.Notification)

Aggregations

Reassignment (org.kie.internal.task.api.model.Reassignment)12 Deadlines (org.kie.internal.task.api.model.Deadlines)7 WorkItem (org.drools.core.process.instance.WorkItem)6 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)6 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)6 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)5 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)3 Deadline (org.kie.internal.task.api.model.Deadline)3 Escalation (org.kie.internal.task.api.model.Escalation)3 TaskReassignment (org.jbpm.services.api.admin.TaskReassignment)2 Group (org.kie.api.task.model.Group)2 Task (org.kie.api.task.model.Task)2 User (org.kie.api.task.model.User)2 InternalOrganizationalEntity (org.kie.internal.task.api.model.InternalOrganizationalEntity)2 Notification (org.kie.internal.task.api.model.Notification)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1