Search in sources :

Example 1 with Content

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

the class UserTaskServiceImpl method getTaskOutputContentByTaskId.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getTaskOutputContentByTaskId(String deploymentId, Long taskId) {
    UserTaskInstanceDesc task = dataService.getTaskById(taskId);
    validateTask(deploymentId, taskId, task);
    RuntimeManager manager = getRuntimeManager(task);
    if (manager == null) {
        logger.warn("Cannot find runtime manager for task {}", taskId);
        return null;
    }
    RuntimeEngine engine = manager.getRuntimeEngine(ProcessInstanceIdContext.get(task.getProcessInstanceId()));
    try {
        TaskService taskService = engine.getTaskService();
        // perform actual operation
        Task taskInstanceById = taskService.getTaskById(taskId);
        long documentContentId = taskInstanceById.getTaskData().getOutputContentId();
        if (documentContentId > 0) {
            Content contentById = taskService.getContentById(documentContentId);
            if (contentById == null) {
                return new HashMap<String, Object>();
            }
            ContentMarshallerContext ctx = TaskContentRegistry.get().getMarshallerContext(task.getDeploymentId());
            Object unmarshall = ContentMarshallerHelper.unmarshall(contentById.getContent(), ctx.getEnvironment(), ctx.getClassloader());
            Map<String, Object> data = (Map<String, Object>) unmarshall;
            for (Object variable : data.values()) {
                if (variable instanceof LazyLoaded<?>) {
                    ((LazyLoaded<?>) variable).load();
                }
            }
            return data;
        }
        return new HashMap<String, Object>();
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Task(org.kie.api.task.model.Task) HashMap(java.util.HashMap) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) LazyLoaded(org.kie.internal.utils.LazyLoaded) Content(org.kie.api.task.model.Content) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc) HashMap(java.util.HashMap) Map(java.util.Map) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 2 with Content

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

the class LifeCycleBaseTest method testCompleteWithResults.

@Test
public void testCompleteWithResults() {
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
    str += "name = 'This is my task name' })";
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    // Go straight from Ready to Inprogress
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("content", "content");
    taskService.complete(taskId, "Darth Vader", params);
    Task task2 = taskService.getTaskById(taskId);
    assertEquals(AccessType.Inline, ((InternalTaskData) task2.getTaskData()).getOutputAccessType());
    assertEquals("java.util.HashMap", task2.getTaskData().getOutputType());
    long contentId = task2.getTaskData().getOutputContentId();
    assertTrue(contentId != -1);
    Content content = taskService.getContentById(contentId);
    Map<String, Object> unmarshalledObject = (Map<String, Object>) ContentMarshallerHelper.unmarshall(content.getContent(), null);
    assertEquals("content", unmarshalledObject.get("content"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) Content(org.kie.api.task.model.Content) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 3 with Content

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

the class LifeCycleBaseTest method testFailWithContent.

@Test
public void testFailWithContent() {
    // One potential owner, should go straight to state Reserved
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet'), new User('Darth Vader') ], businessAdministrators = [ new User('Administrator') ],}),";
    str += "name =  'This is my task name' })";
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    // Go straight from Ready to Inprogress
    taskService.start(taskId, "Darth Vader");
    taskService.getTaskById(taskId);
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    Map<String, Object> faultData = new HashMap<String, Object>();
    faultData.put("faultType", "type");
    faultData.put("faultName", "faultName");
    faultData.put("content", "content");
    taskService.fail(taskId, "Darth Vader", faultData);
    List<Content> allContent = taskService.getAllContentByTaskId(taskId);
    assertNotNull(allContent);
    assertEquals(3, allContent.size());
    // only input(0) and fault(2) is present
    assertNotNull(allContent.get(0));
    assertNull(allContent.get(1));
    assertNotNull(allContent.get(2));
    Task task2 = taskService.getTaskById(taskId);
    assertEquals(Status.Failed, task2.getTaskData().getStatus());
    assertEquals(AccessType.Inline, ((InternalTaskData) task2.getTaskData()).getFaultAccessType());
    assertEquals("type", task2.getTaskData().getFaultType());
    assertEquals("faultName", task2.getTaskData().getFaultName());
    long contentId = task2.getTaskData().getFaultContentId();
    assertTrue(contentId != -1);
    Content content = taskService.getContentById(contentId);
    Map<String, Object> unmarshalledContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(content.getContent(), null);
    assertEquals("content", unmarshalledContent.get("content"));
    xmlRoundTripContent(content);
    // update fault
    FaultData data = TaskModelProvider.getFactory().newFaultData();
    data.setAccessType(AccessType.Inline);
    data.setType("type");
    data.setFaultName("faultName");
    data.setContent("updated content".getBytes());
    taskService.setFault(taskId, "Darth Vader", data);
    task = taskService.getTaskById(taskId);
    contentId = task.getTaskData().getFaultContentId();
    content = taskService.getContentById(contentId);
    String updated = new String(content.getContent());
    assertEquals("updated content", updated);
    // delete fault
    taskService.deleteFault(taskId, "Darth Vader");
    content = taskService.getContentById(contentId);
    assertNull(content);
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) FaultData(org.kie.internal.task.api.model.FaultData) HashMap(java.util.HashMap) Content(org.kie.api.task.model.Content) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Content

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

the class LifeCycleBaseTest method testNewTaskWithMapContentAndOutput.

/*
     * This test shows how to work with a task and save severeal intermediate steps of the content that the 
     * task is handling. 
     * The input parameters for this task are: (key1,value1) (key3,value3). 
     * 
     * (key2, null) is a variable that is input/output, this means that is a variable that comes defined, but it value can be changed
     * by the user
     * 
     * The expected outputs for the task are: (key2, value2), (key4, value4) (key5, value5) (key6, value6)
     */
@Test
public void testNewTaskWithMapContentAndOutput() {
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet') ],businessAdministrators = [ new User('Administrator') ], }),";
    str += "name =  'This is my task name' })";
    Map<String, Object> variablesMap = new HashMap<String, Object>();
    variablesMap.put("key1", "value1");
    variablesMap.put("key2", null);
    variablesMap.put("key3", "value3");
    ContentData data = ContentMarshallerHelper.marshal(null, variablesMap, null);
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, data);
    long taskId = task.getId();
    // Task should be assigned to the single potential owner and state set to Reserved
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(AccessType.Inline, ((InternalTaskData) task1.getTaskData()).getDocumentAccessType());
    assertEquals("java.util.HashMap", task1.getTaskData().getDocumentType());
    long contentId = task1.getTaskData().getDocumentContentId();
    assertTrue(contentId != -1);
    Content content = taskService.getContentById(contentId);
    Object unmarshalledObject = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    if (!(unmarshalledObject instanceof Map)) {
        fail("The variables should be a Map");
    }
    xmlRoundTripContent(content);
    Map<String, Object> unmarshalledvars = (Map<String, Object>) unmarshalledObject;
    assertEquals("value1", unmarshalledvars.get("key1"));
    assertNull(unmarshalledvars.get("key2"));
    assertEquals("value3", unmarshalledvars.get("key3"));
    taskService.start(taskId, "Bobba Fet");
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    // Once the task has being started the user decide to start working on it.
    Map<String, Object> intermediateOutputContentMap = new HashMap<String, Object>();
    intermediateOutputContentMap.put("key2", "value2");
    intermediateOutputContentMap.put("key4", "value4");
    taskService.addContent(taskId, intermediateOutputContentMap);
    Map<String, Object> finalOutputContentMap = new HashMap<String, Object>();
    finalOutputContentMap.put("key5", "value5");
    finalOutputContentMap.put("key6", "value6");
    taskService.complete(taskId, "Bobba Fet", finalOutputContentMap);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    long outputContentId = task1.getTaskData().getOutputContentId();
    Content contentById = taskService.getContentById(outputContentId);
    unmarshalledObject = ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertNotNull(unmarshalledObject);
    if (!(unmarshalledObject instanceof Map)) {
        fail("The variables should be a Map");
    }
    assertTrue(((Map<String, Object>) unmarshalledObject).containsKey("key2"));
    assertTrue(((Map<String, Object>) unmarshalledObject).containsKey("key4"));
    assertTrue(((Map<String, Object>) unmarshalledObject).containsKey("key5"));
    assertTrue(((Map<String, Object>) unmarshalledObject).containsKey("key6"));
    xmlRoundTripContent(contentById);
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) ContentData(org.kie.internal.task.api.model.ContentData) HashMap(java.util.HashMap) Content(org.kie.api.task.model.Content) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 5 with Content

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

the class LifeCycleBaseTest method testNewTaskWithContent.

@Test
public void testNewTaskWithContent() {
    String str = "(with (new Task()) { priority = 55, taskData = (with( new TaskData()) { } ), ";
    str += "peopleAssignments = (with ( new PeopleAssignments() ) { potentialOwners = [new User('Bobba Fet') ],businessAdministrators = [ new User('Administrator') ], }),";
    str += "name =  'This is my task name' })";
    ContentData data = ContentMarshallerHelper.marshal(null, "content", null);
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, data);
    long taskId = task.getId();
    // Task should be assigned to the single potential owner and state set to Reserved
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(AccessType.Inline, ((InternalTaskData) task1.getTaskData()).getDocumentAccessType());
    assertEquals("java.lang.String", task1.getTaskData().getDocumentType());
    long contentId = task1.getTaskData().getDocumentContentId();
    assertTrue(contentId != -1);
    Content content = taskService.getContentById(contentId);
    Object unmarshalledObject = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    assertEquals("content", unmarshalledObject.toString());
    xmlRoundTripContent(content);
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) ContentData(org.kie.internal.task.api.model.ContentData) Content(org.kie.api.task.model.Content) JaxbContent(org.jbpm.services.task.impl.model.xml.JaxbContent) StringReader(java.io.StringReader) Test(org.junit.Test)

Aggregations

Content (org.kie.api.task.model.Content)40 Task (org.kie.api.task.model.Task)32 HashMap (java.util.HashMap)23 Map (java.util.Map)21 InternalContent (org.kie.internal.task.api.model.InternalContent)17 ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)15 InternalTask (org.kie.internal.task.api.model.InternalTask)14 ContentData (org.kie.internal.task.api.model.ContentData)11 Test (org.junit.Test)10 JaxbContent (org.jbpm.services.task.impl.model.xml.JaxbContent)9 StringReader (java.io.StringReader)8 TaskService (org.kie.api.task.TaskService)6 TaskSummary (org.kie.api.task.model.TaskSummary)6 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)6 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)5 InternalTaskService (org.kie.internal.task.api.InternalTaskService)5 TaskEventSupport (org.jbpm.services.task.events.TaskEventSupport)4 PermissionDeniedException (org.jbpm.services.task.exception.PermissionDeniedException)4 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)4 Attachment (org.kie.api.task.model.Attachment)4