Search in sources :

Example 26 with TaskPersistenceContext

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

the class ExecuteReminderCommand method execute.

@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);
        TaskData taskData = task.getTaskData();
        List<DeadlineSummary> resultList = null;
        resultList = getAlldeadlines(persistenceContext, taskData);
        TaskEventSupport taskEventSupport = ctx.getTaskEventSupport();
        if (resultList == null || resultList.size() == 0) {
            if (taskData.getActualOwner() == null)
                return null;
            if (taskData != null) {
                // check if task is still in valid status
                if (DeadlineType.START.isValidStatus(taskData.getStatus()) || DeadlineType.END.isValidStatus(taskData.getStatus())) {
                    taskEventSupport.fireBeforeTaskNotified(task, ctx);
                    logger.debug("Sending an Email");
                    Map<String, Object> variables = getVariables(ctx, persistenceContext, task, taskData);
                    Notification notification = buildDefaultNotification(taskData, task);
                    NotificationListenerManager.get().broadcast(new NotificationEvent(notification, task, variables), userInfo);
                    taskEventSupport.fireAfterTaskNotified(task, ctx);
                }
            }
        } else {
            for (DeadlineSummary deadlineSummary : resultList) {
                executedeadLine(ctx, persistenceContext, task, deadlineSummary, taskData);
            }
        }
    } catch (Exception e) {
        logger.error("Error when executing deadlines", e);
    }
    return null;
}
Also used : Task(org.kie.api.task.model.Task) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) UserInfo(org.kie.internal.task.api.UserInfo) NotificationEvent(org.kie.internal.task.api.model.NotificationEvent) Notification(org.kie.internal.task.api.model.Notification) EmailNotification(org.kie.internal.task.api.model.EmailNotification) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) TaskData(org.kie.api.task.model.TaskData) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport)

Example 27 with TaskPersistenceContext

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

the class InitDeadlinesCommand method execute.

@Override
public Void execute(Context context) {
    TaskContext ctx = (TaskContext) context;
    TaskPersistenceContext persistenceContext = ctx.getPersistenceContext();
    TaskDeadlinesService deadlineService = ctx.getTaskDeadlinesService();
    try {
        long now = System.currentTimeMillis();
        List<DeadlineSummary> resultList = persistenceContext.queryInTransaction("UnescalatedStartDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class));
        for (DeadlineSummary summary : resultList) {
            long delay = summary.getDate().getTime() - now;
            deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.START);
        }
        resultList = persistenceContext.queryInTransaction("UnescalatedEndDeadlines", ClassUtil.<List<DeadlineSummary>>castClass(List.class));
        for (DeadlineSummary summary : resultList) {
            long delay = summary.getDate().getTime() - now;
            deadlineService.schedule(summary.getTaskId(), summary.getDeadlineId(), delay, DeadlineType.END);
        }
    } catch (Exception e) {
        logger.error("Error when executing deadlines", e);
    }
    return null;
}
Also used : TaskDeadlinesService(org.kie.internal.task.api.TaskDeadlinesService) DeadlineSummary(org.kie.internal.task.api.model.DeadlineSummary) List(java.util.List) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 28 with TaskPersistenceContext

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

the class RemoveTaskDataCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    TaskEventSupport taskEventSupport = context.getTaskEventSupport();
    TaskPersistenceContext persistenceContext = context.getPersistenceContext();
    Task task = persistenceContext.findTask(taskId);
    // security check
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    long contentId = task.getTaskData().getDocumentContentId();
    if (!input) {
        contentId = task.getTaskData().getOutputContentId();
    }
    Content outputContent = persistenceContext.findContent(contentId);
    Map<String, Object> initialContent = new HashMap<>();
    Map<String, Object> mergedContent = new HashMap<>();
    if (outputContent != null) {
        ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
        Object unmarshalledObject = ContentMarshallerHelper.unmarshall(outputContent.getContent(), mcontext.getEnvironment(), mcontext.getClassloader());
        if (unmarshalledObject != null && unmarshalledObject instanceof Map) {
            mergedContent.putAll(((Map<String, Object>) unmarshalledObject));
            // set initial content for the sake of listeners
            initialContent.putAll(mergedContent);
            variableNames.forEach(name -> mergedContent.remove(name));
        }
        ContentData outputContentData = ContentMarshallerHelper.marshal(task, mergedContent, mcontext.getEnvironment());
        ((InternalContent) outputContent).setContent(outputContentData.getContent());
        persistenceContext.persistContent(outputContent);
    }
    if (input) {
        taskEventSupport.fireBeforeTaskInputVariablesChanged(task, context, initialContent);
        ((InternalTaskData) task.getTaskData()).setTaskInputVariables(mergedContent);
        taskEventSupport.fireAfterTaskInputVariablesChanged(task, context, mergedContent);
    } else {
        taskEventSupport.fireBeforeTaskOutputVariablesChanged(task, context, initialContent);
        ((InternalTaskData) task.getTaskData()).setTaskOutputVariables(mergedContent);
        taskEventSupport.fireAfterTaskOutputVariablesChanged(task, context, mergedContent);
    }
    return null;
}
Also used : Task(org.kie.api.task.model.Task) TaskContext(org.jbpm.services.task.commands.TaskContext) HashMap(java.util.HashMap) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) InternalContent(org.kie.internal.task.api.model.InternalContent) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) ContentData(org.kie.internal.task.api.model.ContentData) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport) HashMap(java.util.HashMap) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 29 with TaskPersistenceContext

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

the class ScheduleTaskDeadlineCommand method execute.

@Override
public Long execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    TaskPersistenceContext persistenceContext = context.getPersistenceContext();
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    logger.debug("About to schedule {} on a task {}", deadline, task);
    Deadlines deadlines = ((InternalTask) task).getDeadlines();
    if (type.equals(DeadlineType.START)) {
        deadlines.getStartDeadlines().add(deadline);
    } else {
        deadlines.getEndDeadlines().add(deadline);
    }
    doCallbackOperationForTaskDeadlines(deadlines, context);
    persistenceContext.persistDeadline(deadline);
    persistenceContext.updateTask(task);
    logger.debug("Task updated and deadline stored with id {}", deadline.getId());
    TaskDeadlinesService deadlinesService = context.getTaskDeadlinesService();
    long fireAfterDuration = DateTimeUtils.parseDuration(timeExpression);
    deadline.setDate(new Date(System.currentTimeMillis() + fireAfterDuration));
    logger.debug("Deadline expiration time set to {} and duration {}", deadline.getDate(), fireAfterDuration);
    deadlinesService.schedule(taskId, deadline.getId(), fireAfterDuration, type);
    logger.debug("Deadline on task {} successfully scheduled to fire at {}", task, deadline.getDate());
    return deadline.getId();
}
Also used : 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) TaskDeadlinesService(org.kie.internal.task.api.TaskDeadlinesService) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) Date(java.util.Date) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 30 with TaskPersistenceContext

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

the class UpdateTaskCommand method execute.

@SuppressWarnings("unchecked")
@Override
public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    TaskEventSupport taskEventSupport = context.getTaskEventSupport();
    TaskPersistenceContext persistenceContext = context.getPersistenceContext();
    Task task = persistenceContext.findTask(taskId);
    // security check
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context) && !isOwner(userId, task.getPeopleAssignments().getPotentialOwners(), task.getTaskData().getActualOwner(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin or potential owner of task " + taskId);
    }
    taskEventSupport.fireBeforeTaskUpdated(task, context);
    // process task meta data
    if (userTask.getFormName() != null) {
        ((InternalTask) task).setFormName(userTask.getFormName());
    }
    if (userTask.getName() != null) {
        ((InternalTask) task).setName(userTask.getName());
    }
    if (userTask.getDescription() != null) {
        ((InternalTask) task).setDescription(userTask.getDescription());
    }
    if (userTask.getPriority() != null) {
        ((InternalTask) task).setPriority(userTask.getPriority());
    }
    if (userTask.getDueDate() != null) {
        ((InternalTaskData) task.getTaskData()).setExpirationTime(userTask.getDueDate());
    }
    // process task inputs
    long inputContentId = task.getTaskData().getDocumentContentId();
    Content inputContent = persistenceContext.findContent(inputContentId);
    Map<String, Object> mergedContent = inputs;
    if (inputs != null) {
        if (inputContent == null) {
            ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
            ContentData outputContentData = ContentMarshallerHelper.marshal(task, inputs, mcontext.getEnvironment());
            Content content = TaskModelProvider.getFactory().newContent();
            ((InternalContent) content).setContent(outputContentData.getContent());
            persistenceContext.persistContent(content);
            ((InternalTaskData) task.getTaskData()).setOutput(content.getId(), outputContentData);
        } else {
            ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
            Object unmarshalledObject = ContentMarshallerHelper.unmarshall(inputContent.getContent(), mcontext.getEnvironment(), mcontext.getClassloader());
            if (unmarshalledObject != null && unmarshalledObject instanceof Map) {
                ((Map<String, Object>) unmarshalledObject).putAll(inputs);
                mergedContent = ((Map<String, Object>) unmarshalledObject);
            }
            ContentData outputContentData = ContentMarshallerHelper.marshal(task, unmarshalledObject, mcontext.getEnvironment());
            ((InternalContent) inputContent).setContent(outputContentData.getContent());
            persistenceContext.persistContent(inputContent);
        }
        ((InternalTaskData) task.getTaskData()).setTaskInputVariables(mergedContent);
    }
    if (outputs != null) {
        // process task outputs
        context.getTaskContentService().addOutputContent(taskId, outputs);
    }
    persistenceContext.updateTask(task);
    // finally trigger event support after the updates
    taskEventSupport.fireAfterTaskUpdated(task, context);
    return null;
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) TaskContext(org.jbpm.services.task.commands.TaskContext) InternalTask(org.kie.internal.task.api.model.InternalTask) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) InternalContent(org.kie.internal.task.api.model.InternalContent) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) ContentData(org.kie.internal.task.api.model.ContentData) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

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