Search in sources :

Example 11 with TaskContext

use of org.jbpm.services.task.commands.TaskContext in project jbpm by kiegroup.

the class AddPeopleAssignmentsCommand method execute.

@Override
public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    TaskEventSupport taskEventSupport = context.getTaskEventSupport();
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    // security check
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    List<OrganizationalEntity> entityList = Arrays.asList(entities);
    AssignmentType assignmentType = null;
    switch(type) {
        case POT_OWNER:
            if (removeExisting) {
                task.getPeopleAssignments().getPotentialOwners().clear();
            }
            task.getPeopleAssignments().getPotentialOwners().addAll(entityList);
            assignmentType = AssignmentType.POT_OWNER;
            break;
        case EXCL_OWNER:
            if (removeExisting) {
                ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners().clear();
            }
            ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners().addAll(entityList);
            assignmentType = AssignmentType.EXCL_OWNER;
            break;
        case ADMIN:
            if (removeExisting) {
                task.getPeopleAssignments().getBusinessAdministrators().clear();
            }
            task.getPeopleAssignments().getBusinessAdministrators().addAll(entityList);
            assignmentType = AssignmentType.ADMIN;
            break;
        default:
            break;
    }
    taskEventSupport.fireBeforeTaskAssignmentsAddedEvent(task, context, assignmentType, entityList);
    doCallbackOperationForPeopleAssignments(((InternalPeopleAssignments) task.getPeopleAssignments()), context);
    taskEventSupport.fireAfterTaskAssignmentsAddedEvent(task, context, assignmentType, entityList);
    return null;
}
Also used : Task(org.kie.api.task.model.Task) InternalPeopleAssignments(org.kie.internal.task.api.model.InternalPeopleAssignments) TaskContext(org.jbpm.services.task.commands.TaskContext) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) AssignmentType(org.kie.api.task.TaskLifeCycleEventListener.AssignmentType) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport)

Example 12 with TaskContext

use of org.jbpm.services.task.commands.TaskContext in project jbpm by kiegroup.

the class AddTaskInputsCommand 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 inputContentId = task.getTaskData().getDocumentContentId();
    Content outputContent = persistenceContext.findContent(inputContentId);
    Map<String, Object> initialContent = new HashMap<String, Object>();
    Map<String, Object> mergedContent = values;
    if (outputContent == null) {
        ContentMarshallerContext mcontext = context.getTaskContentService().getMarshallerContext(task);
        ContentData outputContentData = ContentMarshallerHelper.marshal(task, values, 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(outputContent.getContent(), mcontext.getEnvironment(), mcontext.getClassloader());
        if (unmarshalledObject != null && unmarshalledObject instanceof Map) {
            // set initial content with data from storage before being altered by this values
            initialContent.putAll((Map<String, Object>) unmarshalledObject);
            ((Map<String, Object>) unmarshalledObject).putAll(values);
            mergedContent = ((Map<String, Object>) unmarshalledObject);
        }
        ContentData outputContentData = ContentMarshallerHelper.marshal(task, unmarshalledObject, mcontext.getEnvironment());
        ((InternalContent) outputContent).setContent(outputContentData.getContent());
        persistenceContext.persistContent(outputContent);
    }
    taskEventSupport.fireBeforeTaskInputVariablesChanged(task, context, initialContent);
    ((InternalTaskData) task.getTaskData()).setTaskInputVariables(mergedContent);
    taskEventSupport.fireAfterTaskInputVariablesChanged(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 13 with TaskContext

use of org.jbpm.services.task.commands.TaskContext in project jbpm by kiegroup.

the class RemovePeopleAssignmentsCommand method execute.

@Override
public Void execute(Context cntxt) {
    TaskContext context = (TaskContext) cntxt;
    TaskEventSupport taskEventSupport = context.getTaskEventSupport();
    Task task = context.getTaskQueryService().getTaskInstanceById(taskId);
    // security check
    if (!isBusinessAdmin(userId, task.getPeopleAssignments().getBusinessAdministrators(), context)) {
        throw new PermissionDeniedException("User " + userId + " is not business admin of task " + taskId);
    }
    List<OrganizationalEntity> entityList = Arrays.asList(entities);
    AssignmentType assignmentType = null;
    taskEventSupport.fireBeforeTaskUpdated(task, context);
    switch(type) {
        case POT_OWNER:
            assignmentType = AssignmentType.POT_OWNER;
            taskEventSupport.fireBeforeTaskAssignmentsRemovedEvent(task, context, assignmentType, entityList);
            task.getPeopleAssignments().getPotentialOwners().removeAll(entityList);
            break;
        case EXCL_OWNER:
            assignmentType = AssignmentType.EXCL_OWNER;
            taskEventSupport.fireBeforeTaskAssignmentsRemovedEvent(task, context, assignmentType, entityList);
            ((InternalPeopleAssignments) task.getPeopleAssignments()).getExcludedOwners().removeAll(entityList);
            break;
        case ADMIN:
            assignmentType = AssignmentType.ADMIN;
            taskEventSupport.fireBeforeTaskAssignmentsRemovedEvent(task, context, assignmentType, entityList);
            task.getPeopleAssignments().getBusinessAdministrators().removeAll(entityList);
            break;
        default:
            break;
    }
    taskEventSupport.fireAfterTaskAssignmentsRemovedEvent(task, context, assignmentType, entityList);
    return null;
}
Also used : Task(org.kie.api.task.model.Task) TaskContext(org.jbpm.services.task.commands.TaskContext) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) AssignmentType(org.kie.api.task.TaskLifeCycleEventListener.AssignmentType) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskEventSupport(org.jbpm.services.task.events.TaskEventSupport)

Example 14 with TaskContext

use of org.jbpm.services.task.commands.TaskContext in project jbpm by kiegroup.

the class JaxbTaskSerializationTest method setTaskPropertyCommandTest.

@Test
public void setTaskPropertyCommandTest() throws Exception {
    SetTaskPropertyCommand cmd;
    int taskId = 1;
    String userId = "user";
    FaultDataImpl faultData = new FaultDataImpl();
    faultData.setAccessType(AccessType.Inline);
    faultData.setContent("skinned shins".getBytes());
    faultData.setFaultName("Whoops!");
    faultData.setType("skates");
    List<I18NText> textList = new ArrayList<I18NText>();
    I18NText text = new I18NTextImpl("nl-NL", "Stroopwafel!");
    textList.add(text);
    Object[][] testData = { { FAULT_PROPERTY, faultData }, { OUTPUT_PROPERTY, new Object() }, { PRIORITY_PROPERTY, 23 }, { TASK_NAMES_PROPERTY, textList }, { EXPIRATION_DATE_PROPERTY, new Date() }, { DESCRIPTION_PROPERTY, new ArrayList<I18NText>() }, { SKIPPABLE_PROPERTY, false }, { SUB_TASK_STRATEGY_PROPERTY, SubTasksStrategy.EndParentOnAllSubTasksEnd } };
    TaskContext mockContext = mock(TaskContext.class);
    TaskInstanceService mockTaskService = mock(TaskInstanceService.class);
    UserGroupCallback mockUserGroupCallback = mock(UserGroupCallback.class);
    when(mockContext.getTaskInstanceService()).thenReturn(mockTaskService);
    when(mockContext.getUserGroupCallback()).thenReturn(mockUserGroupCallback);
    when(mockUserGroupCallback.existsUser(anyString())).thenReturn(false);
    for (Object[] data : testData) {
        int property = (Integer) data[0];
        cmd = new SetTaskPropertyCommand(taskId, userId, property, data[1]);
        try {
            cmd.execute(mockContext);
        } catch (IllegalArgumentException e) {
            // expected for fault and output
            assertTrue(e.getMessage().startsWith("User user was not found in callback "));
        }
    }
}
Also used : I18NText(org.kie.api.task.model.I18NText) TaskContext(org.jbpm.services.task.commands.TaskContext) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) UserGroupCallback(org.kie.internal.task.api.UserGroupCallback) Date(java.util.Date) FaultDataImpl(org.jbpm.services.task.impl.model.FaultDataImpl) TaskInstanceService(org.kie.internal.task.api.TaskInstanceService) SetTaskPropertyCommand(org.jbpm.services.task.commands.SetTaskPropertyCommand) I18NTextImpl(org.jbpm.services.task.impl.model.I18NTextImpl) Test(org.junit.Test)

Aggregations

TaskContext (org.jbpm.services.task.commands.TaskContext)14 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)9 Task (org.kie.api.task.model.Task)9 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)7 ArrayList (java.util.ArrayList)5 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)5 InternalTask (org.kie.internal.task.api.model.InternalTask)5 List (java.util.List)4 Deadlines (org.kie.internal.task.api.model.Deadlines)4 Map (java.util.Map)3 UserGroupCallbackTaskCommand (org.jbpm.services.task.commands.UserGroupCallbackTaskCommand)3 Context (org.kie.api.runtime.Context)3 Content (org.kie.api.task.model.Content)3 I18NText (org.kie.api.task.model.I18NText)3 ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)3 ContentData (org.kie.internal.task.api.model.ContentData)3 InternalContent (org.kie.internal.task.api.model.InternalContent)3 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3