Search in sources :

Example 36 with Content

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

the class PatientVariablePersistenceStrategyTest method getTaskContent.

@SuppressWarnings("rawtypes")
private MedicalRecord getTaskContent(RuntimeEngine runtimeEngine, TaskSummary summary) throws IOException, ClassNotFoundException {
    logger.info(" >>> Getting Task Content = {}", summary.getId());
    Task task = runtimeEngine.getTaskService().getTaskById(summary.getId());
    long documentContentId = task.getTaskData().getDocumentContentId();
    Content content = runtimeEngine.getTaskService().getContentById(documentContentId);
    Object readObject = ContentMarshallerHelper.unmarshall(content.getContent(), runtimeEngine.getKieSession().getEnvironment());
    logger.info(" >>> Object = {}", readObject);
    return (MedicalRecord) ((Map) readObject).get("Content");
}
Also used : Task(org.kie.api.task.model.Task) Content(org.kie.api.task.model.Content) MedicalRecord(org.jbpm.test.entity.MedicalRecord)

Example 37 with Content

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

the class LocalTaskServiceTest method testHumanTaskWithSingleTypeContent.

@Test
public void testHumanTaskWithSingleTypeContent() {
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("pVar", "sampleValue");
    kieSession.startProcess(TASK_SINGLE_TYPE_ID, params);
    // let john execute Task 1
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    TaskSummary task = list.get(0);
    logger.info("John is executing task {}", task.getName());
    taskService.start(task.getId(), "john");
    // let's verify content, first manually by using marshaler helper
    Content content = taskService.getContentById(taskService.getTaskById(task.getId()).getTaskData().getDocumentContentId());
    byte[] contentbyte = content.getContent();
    Object tmpObject = ContentMarshallerHelper.unmarshall(contentbyte, kieSession.getEnvironment());
    assertNotNull(tmpObject);
    assertTrue(tmpObject instanceof String);
    assertEquals("someContent", tmpObject);
    // then by using getTaskContent api method
    Map<String, Object> contentMap = taskService.getTaskContent(task.getId());
    assertNotNull(contentMap);
    assertEquals(1, contentMap.size());
    assertTrue(contentMap.containsKey("Content"));
    String actualContent = (String) contentMap.get("Content");
    assertNotNull(actualContent);
    assertEquals("someContent", actualContent);
    // let's move on to complete the tasks and process instance
    taskService.complete(task.getId(), "john", null);
    // let mary execute Task 2
    list = taskService.getTasksAssignedAsPotentialOwner("mary", "en-UK");
    task = list.get(0);
    logger.info("Mary is executing task {}", task.getName());
    taskService.start(task.getId(), "mary");
    taskService.complete(task.getId(), "mary", null);
}
Also used : HashMap(java.util.HashMap) Content(org.kie.api.task.model.Content) TaskSummary(org.kie.api.task.model.TaskSummary) Test(org.junit.Test)

Example 38 with Content

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

the class UserTaskServiceImpl method getAttachmentContentById.

@Override
public Object getAttachmentContentById(String deploymentId, Long taskId, Long attachmentId) {
    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 {
        // perform actual operation
        TaskService taskService = engine.getTaskService();
        Attachment attachment = ((InternalTaskService) taskService).getAttachmentById(attachmentId);
        long documentContentId = attachment.getAttachmentContentId();
        if (documentContentId > 0) {
            Content contentById = taskService.getContentById(documentContentId);
            if (contentById == null) {
                return null;
            }
            ContentMarshallerContext ctx = TaskContentRegistry.get().getMarshallerContext(task.getDeploymentId());
            Object unmarshall = ContentMarshallerHelper.unmarshall(contentById.getContent(), ctx.getEnvironment(), ctx.getClassloader());
            return unmarshall;
        }
        return null;
    } finally {
        disposeRuntimeEngine(manager, engine);
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskService(org.jbpm.services.api.UserTaskService) TaskService(org.kie.api.task.TaskService) Content(org.kie.api.task.model.Content) RuntimeManager(org.kie.api.runtime.manager.RuntimeManager) InternalRuntimeManager(org.kie.internal.runtime.manager.InternalRuntimeManager) Attachment(org.kie.api.task.model.Attachment) InternalAttachment(org.kie.internal.task.api.model.InternalAttachment) InternalTaskService(org.kie.internal.task.api.InternalTaskService) UserTaskInstanceDesc(org.jbpm.services.api.model.UserTaskInstanceDesc) ContentMarshallerContext(org.kie.internal.task.api.ContentMarshallerContext)

Example 39 with Content

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

the class UserTaskServiceImpl method getTaskInputContentByTaskId.

@SuppressWarnings("unchecked")
@Override
public Map<String, Object> getTaskInputContentByTaskId(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().getDocumentContentId();
        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 40 with Content

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

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