Search in sources :

Example 16 with InternalTaskData

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

the class CompleteTaskCommand method execute.

public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    doCallbackUserOperation(userId, context, true);
    groupIds = doUserGroupCallbackOperation(userId, null, context);
    context.set("local:groups", groupIds);
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    if (task == null) {
        throw new PermissionDeniedException("Task '" + taskId + "' not found");
    }
    context.loadTaskVariables(task);
    Map<String, Object> outputdata = task.getTaskData().getTaskOutputVariables();
    if (outputdata != null) {
        // if there are data given with completion, merged them into existing outputs
        if (data != null) {
            outputdata.putAll(data);
        }
        // since output data was non null make it the actual data
        data = outputdata;
    }
    context.getTaskRuleService().executeRules(task, userId, data, TaskRuleService.COMPLETE_TASK_SCOPE);
    ((InternalTaskData) task.getTaskData()).setTaskOutputVariables(data);
    TaskInstanceService instanceService = context.getTaskInstanceService();
    instanceService.complete(taskId, userId, data);
    return null;
}
Also used : Task(org.kie.api.task.model.Task) TaskInstanceService(org.kie.internal.task.api.TaskInstanceService) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException)

Example 17 with InternalTaskData

use of org.kie.internal.task.api.model.InternalTaskData 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 18 with InternalTaskData

use of org.kie.internal.task.api.model.InternalTaskData 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 19 with InternalTaskData

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

Example 20 with InternalTaskData

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

the class PeopleAssignmentHelperTest method testAssignActorsWithCustomSeparator.

@Test
public void testAssignActorsWithCustomSeparator() {
    peopleAssignmentHelper = new PeopleAssignmentHelper(":");
    String actorId = "user1:user2";
    Task task = TaskModelProvider.getFactory().newTask();
    InternalTaskData taskData = (InternalTaskData) TaskModelProvider.getFactory().newTaskData();
    PeopleAssignments peopleAssignments = peopleAssignmentHelper.getNullSafePeopleAssignments(task);
    WorkItem workItem = new WorkItemImpl();
    workItem.setParameter(PeopleAssignmentHelper.ACTOR_ID, actorId);
    peopleAssignmentHelper.assignActors(workItem, peopleAssignments, taskData);
    OrganizationalEntity organizationalEntity1 = peopleAssignments.getPotentialOwners().get(0);
    assertTrue(organizationalEntity1 instanceof User);
    assertEquals("user1", organizationalEntity1.getId());
    assertEquals("user1", taskData.getCreatedBy().getId());
    OrganizationalEntity organizationalEntity2 = peopleAssignments.getPotentialOwners().get(1);
    assertTrue(organizationalEntity2 instanceof User);
    assertEquals("user2", organizationalEntity2.getId());
    workItem = new WorkItemImpl();
    peopleAssignments = peopleAssignmentHelper.getNullSafePeopleAssignments(task);
    workItem.setParameter(PeopleAssignmentHelper.ACTOR_ID, actorId + ": drbug  ");
    peopleAssignmentHelper.assignActors(workItem, peopleAssignments, taskData);
    assertEquals(3, peopleAssignments.getPotentialOwners().size());
    organizationalEntity1 = peopleAssignments.getPotentialOwners().get(0);
    assertEquals("user1", organizationalEntity1.getId());
    assertEquals("user1", taskData.getCreatedBy().getId());
    organizationalEntity2 = peopleAssignments.getPotentialOwners().get(1);
    assertTrue(organizationalEntity2 instanceof User);
    assertEquals("user2", organizationalEntity2.getId());
    OrganizationalEntity organizationalEntity3 = peopleAssignments.getPotentialOwners().get(2);
    assertEquals("drbug", organizationalEntity3.getId());
    workItem = new WorkItemImpl();
    peopleAssignments = peopleAssignmentHelper.getNullSafePeopleAssignments(task);
    workItem.setParameter(PeopleAssignmentHelper.ACTOR_ID, "");
    peopleAssignmentHelper.assignActors(workItem, peopleAssignments, taskData);
    assertEquals(0, peopleAssignments.getPotentialOwners().size());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) User(org.kie.api.task.model.User) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) InternalTaskData(org.kie.internal.task.api.model.InternalTaskData) WorkItemImpl(org.drools.core.process.instance.impl.WorkItemImpl) WorkItem(org.drools.core.process.instance.WorkItem) PeopleAssignments(org.kie.api.task.model.PeopleAssignments) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) Test(org.junit.Test) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest)

Aggregations

InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)45 InternalTask (org.kie.internal.task.api.model.InternalTask)23 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)22 Task (org.kie.api.task.model.Task)22 Test (org.junit.Test)21 User (org.kie.api.task.model.User)20 InternalPeopleAssignments (org.kie.internal.task.api.model.InternalPeopleAssignments)20 InternalContent (org.kie.internal.task.api.model.InternalContent)15 ContentData (org.kie.internal.task.api.model.ContentData)14 InternalOrganizationalEntity (org.kie.internal.task.api.model.InternalOrganizationalEntity)14 ArrayList (java.util.ArrayList)13 InputStreamReader (java.io.InputStreamReader)11 Reader (java.io.Reader)11 HashMap (java.util.HashMap)10 CountDownTaskEventListener (org.jbpm.services.task.util.CountDownTaskEventListener)10 Date (java.util.Date)7 StringReader (java.io.StringReader)6 WorkItemImpl (org.drools.core.process.instance.impl.WorkItemImpl)6 Content (org.kie.api.task.model.Content)6 Map (java.util.Map)5