Search in sources :

Example 6 with Deadline

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

Example 7 with Deadline

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

the class ExecuteReminderCommand method executedeadLine.

private Void executedeadLine(TaskContext ctx, TaskPersistenceContext persistenceContext, Task task, DeadlineSummary deadlineSummary, TaskData taskData) {
    Deadline deadline = persistenceContext.findDeadline(deadlineSummary.getDeadlineId());
    if (task == null || deadline == null) {
        return null;
    }
    if (taskData != null) {
        UserInfo userInfo = (UserInfo) ctx.get(EnvironmentName.TASK_USER_INFO);
        // check if task is still in valid status
        if (DeadlineType.START.isValidStatus(taskData.getStatus()) || DeadlineType.END.isValidStatus(taskData.getStatus())) {
            Map<String, Object> variables = getVariables(ctx, persistenceContext, task, taskData);
            if (deadline == null || deadline.getEscalations() == null) {
                return null;
            }
            TaskEventSupport taskEventSupport = ctx.getTaskEventSupport();
            taskEventSupport.fireBeforeTaskNotified(task, ctx);
            for (Escalation escalation : deadline.getEscalations()) {
                for (Notification notification : escalation.getNotifications()) {
                    if (notification.getNotificationType() == NotificationType.Email) {
                        logger.debug("Sending an Email");
                        NotificationListenerManager.get().broadcast(new NotificationEvent(notification, task, variables), userInfo);
                    }
                }
            }
            taskEventSupport.fireAfterTaskNotified(task, ctx);
        }
    }
    return null;
}
Also used : Deadline(org.kie.internal.task.api.model.Deadline) Escalation(org.kie.internal.task.api.model.Escalation) UserInfo(org.kie.internal.task.api.UserInfo) NotificationEvent(org.kie.internal.task.api.model.NotificationEvent) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport) Notification(org.kie.internal.task.api.model.Notification) EmailNotification(org.kie.internal.task.api.model.EmailNotification)

Example 8 with Deadline

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

the class TaskDeadlinesServiceImpl method unschedule.

public void unschedule(long taskId, DeadlineType type) {
    Task task = persistenceContext.findTask(taskId);
    String deploymentId = task.getTaskData().getDeploymentId();
    Deadlines deadlines = ((InternalTask) task).getDeadlines();
    TimerService timerService = TimerServiceRegistry.getInstance().get(deploymentId + TimerServiceRegistry.TIMER_SERVICE_SUFFIX);
    if (timerService != null && timerService instanceof GlobalTimerService) {
        if (type == DeadlineType.START) {
            List<Deadline> startDeadlines = deadlines.getStartDeadlines();
            List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedStartDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
            for (DeadlineSummary summary : resultList) {
                TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.START, deploymentId, task.getTaskData().getProcessInstanceId());
                logger.debug("unscheduling timer job for deadline {} {} and task {}  using timer service {}", deadlineJob.getId(), summary.getDeadlineId(), taskId, timerService);
                JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
                if (jobHandle == null) {
                    jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
                }
                timerService.removeJob(jobHandle);
                // mark the deadlines so they won't be rescheduled again
                for (Deadline deadline : startDeadlines) {
                    if (deadline.getId() == summary.getDeadlineId()) {
                        deadline.setEscalated(true);
                    }
                }
            }
        } else if (type == DeadlineType.END) {
            List<Deadline> endDeadlines = deadlines.getStartDeadlines();
            List<DeadlineSummary> resultList = (List<DeadlineSummary>) persistenceContext.queryWithParametersInTransaction("UnescalatedEndDeadlinesByTaskId", persistenceContext.addParametersToMap("taskId", taskId), ClassUtil.<List<DeadlineSummary>>castClass(List.class));
            for (DeadlineSummary summary : resultList) {
                TaskDeadlineJob deadlineJob = new TaskDeadlineJob(summary.getTaskId(), summary.getDeadlineId(), DeadlineType.END, deploymentId, task.getTaskData().getProcessInstanceId());
                logger.debug("unscheduling timer job for deadline {} and task {}  using timer service {}", deadlineJob.getId(), taskId, timerService);
                JobHandle jobHandle = jobHandles.remove(deadlineJob.getId());
                if (jobHandle == null) {
                    jobHandle = ((GlobalTimerService) timerService).buildJobHandleForContext(new TaskDeadlineJobContext(deadlineJob.getId(), task.getTaskData().getProcessInstanceId(), deploymentId));
                }
                timerService.removeJob(jobHandle);
                // mark the deadlines so they won't be rescheduled again
                for (Deadline deadline : endDeadlines) {
                    if (deadline.getId() == summary.getDeadlineId()) {
                        deadline.setEscalated(true);
                    }
                }
            }
        }
    } else {
        List<ScheduledFuture<ScheduledTaskDeadline>> knownFutures = null;
        if (type == DeadlineType.START) {
            knownFutures = startScheduledTaskDeadlines.get(taskId);
        } else if (type == DeadlineType.END) {
            knownFutures = endScheduledTaskDeadlines.get(taskId);
        }
        if (knownFutures == null) {
            return;
        }
        Iterator<ScheduledFuture<ScheduledTaskDeadline>> it = knownFutures.iterator();
        while (it.hasNext()) {
            ScheduledFuture<ScheduledTaskDeadline> scheduled = it.next();
            try {
                if (!scheduled.isDone() && !scheduled.isCancelled()) {
                    scheduled.cancel(true);
                }
            } catch (Exception e) {
                logger.error("Error while cancelling scheduled deadline task for Task with id {} -> {}", taskId, e);
            }
        }
    }
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalTask(org.kie.internal.task.api.model.InternalTask) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) Deadline(org.kie.internal.task.api.model.Deadline) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService) TimerService(org.drools.core.time.TimerService) ScheduledFuture(java.util.concurrent.ScheduledFuture) JobHandle(org.drools.core.time.JobHandle) Deadlines(org.kie.internal.task.api.model.Deadlines) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) GlobalTimerService(org.jbpm.process.core.timer.impl.GlobalTimerService)

Example 9 with Deadline

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

the class HumanTaskHandlerHelper method setDeadlines.

public static Deadlines setDeadlines(WorkItem workItem, List<OrganizationalEntity> businessAdministrators, Environment environment) {
    String notStartedReassign = (String) workItem.getParameter("NotStartedReassign");
    String notStartedNotify = (String) workItem.getParameter("NotStartedNotify");
    String notCompletedReassign = (String) workItem.getParameter("NotCompletedReassign");
    String notCompletedNotify = (String) workItem.getParameter("NotCompletedNotify");
    Deadlines deadlinesTotal = TaskModelProvider.getFactory().newDeadlines();
    List<Deadline> startDeadlines = new ArrayList<Deadline>();
    startDeadlines.addAll(parseDeadlineString(notStartedNotify, businessAdministrators, environment));
    startDeadlines.addAll(parseDeadlineString(notStartedReassign, businessAdministrators, environment));
    List<Deadline> endDeadlines = new ArrayList<Deadline>();
    endDeadlines.addAll(parseDeadlineString(notCompletedNotify, businessAdministrators, environment));
    endDeadlines.addAll(parseDeadlineString(notCompletedReassign, businessAdministrators, environment));
    if (!startDeadlines.isEmpty()) {
        deadlinesTotal.setStartDeadlines(startDeadlines);
    }
    if (!endDeadlines.isEmpty()) {
        deadlinesTotal.setEndDeadlines(endDeadlines);
    }
    return deadlinesTotal;
}
Also used : Deadlines(org.kie.internal.task.api.model.Deadlines) Deadline(org.kie.internal.task.api.model.Deadline) ArrayList(java.util.ArrayList)

Example 10 with Deadline

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

the class HumanTaskHandlerHelper method parseDeadlineString.

protected static List<Deadline> parseDeadlineString(String deadlineInfo, List<OrganizationalEntity> businessAdministrators, Environment environment) {
    if (deadlineInfo == null || deadlineInfo.length() == 0) {
        return new ArrayList<Deadline>();
    }
    List<Deadline> deadlines = new ArrayList<Deadline>();
    String[] allComponents = deadlineInfo.split(COMPONENT_SEPARATOR);
    BusinessCalendar businessCalendar = null;
    if (environment != null && environment.get("jbpm.business.calendar") != null) {
        businessCalendar = (BusinessCalendar) environment.get("jbpm.business.calendar");
    }
    for (String component : allComponents) {
        String[] mainComponents = component.split(ELEMENT_SEPARATOR);
        if (mainComponents != null && mainComponents.length == 2) {
            String actionComponent = mainComponents[0].substring(1, mainComponents[0].length() - 1);
            String expireComponents = mainComponents[1].substring(1, mainComponents[1].length() - 1);
            String[] expireElements = expireComponents.split(ATTRIBUTES_ELEMENTS_SEPARATOR);
            Deadline taskDeadline = null;
            for (String expiresAt : expireElements) {
                logger.debug("Expires at is {}", expiresAt);
                taskDeadline = TaskModelProvider.getFactory().newDeadline();
                if (businessCalendar != null) {
                    taskDeadline.setDate(businessCalendar.calculateBusinessTimeAsDate(expiresAt));
                } else {
                    taskDeadline.setDate(new Date(System.currentTimeMillis() + TimeUtils.parseTimeString(expiresAt)));
                }
                logger.debug("Calculated date of execution is {} and current date {}", taskDeadline.getDate(), new Date());
                List<Escalation> escalations = new ArrayList<Escalation>();
                Escalation escalation = TaskModelProvider.getFactory().newEscalation();
                escalations.add(escalation);
                escalation.setName("Default escalation");
                taskDeadline.setEscalations(escalations);
                escalation.setReassignments(parseReassignment(actionComponent));
                escalation.setNotifications(parseNotifications(actionComponent, businessAdministrators));
                deadlines.add(taskDeadline);
            }
        } else {
            logger.warn("Incorrect syntax of deadline property {}", deadlineInfo);
        }
    }
    return deadlines;
}
Also used : Deadline(org.kie.internal.task.api.model.Deadline) Escalation(org.kie.internal.task.api.model.Escalation) ArrayList(java.util.ArrayList) BusinessCalendar(org.jbpm.process.core.timer.BusinessCalendar) Date(java.util.Date)

Aggregations

Deadline (org.kie.internal.task.api.model.Deadline)11 ArrayList (java.util.ArrayList)6 Escalation (org.kie.internal.task.api.model.Escalation)6 Notification (org.kie.internal.task.api.model.Notification)4 Task (org.kie.api.task.model.Task)3 Deadlines (org.kie.internal.task.api.model.Deadlines)3 Reassignment (org.kie.internal.task.api.model.Reassignment)3 Date (java.util.Date)2 ScheduleTaskDeadlineCommand (org.jbpm.kie.services.impl.admin.commands.ScheduleTaskDeadlineCommand)2 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)2 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)2 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)2 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)2 UserInfo (org.kie.internal.task.api.UserInfo)2 EmailNotification (org.kie.internal.task.api.model.EmailNotification)2 InternalTask (org.kie.internal.task.api.model.InternalTask)2 NotificationEvent (org.kie.internal.task.api.model.NotificationEvent)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1