Search in sources :

Example 11 with ContentData

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

the class EmailDeadlinesBaseTest method testDelayedEmailNotificationOnDeadlineContentSingleObject.

@Test(timeout = 10000)
public void testDelayedEmailNotificationOnDeadlineContentSingleObject() throws Exception {
    CountDownTaskEventListener countDownListener = new CountDownTaskEventListener(1, false, true);
    addCountDownListner(countDownListener);
    Map<String, Object> vars = new HashMap<String, Object>();
    vars.put("now", new Date());
    Reader reader = new InputStreamReader(getClass().getResourceAsStream(MvelFilePath.DeadlineWithNotificationContentSingleObject));
    Task task = (Task) TaskFactory.evalTask(reader, vars);
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    InternalContent content = (InternalContent) TaskModelProvider.getFactory().newContent();
    ContentData marshalledObject = ContentMarshallerHelper.marshal(task, "'singleobject'", null);
    content.setContent(marshalledObject.getContent());
    taskService.addContent(taskId, content);
    long contentId = content.getId();
    content = (InternalContent) taskService.getContentById(contentId);
    Object unmarshallObject = ContentMarshallerHelper.unmarshall(content.getContent(), null);
    assertEquals("'singleobject'", unmarshallObject.toString());
    // emails should not be set yet
    assertEquals(0, getWiser().getMessages().size());
    countDownListener.waitTillCompleted();
    // 1 email with two recipients should now exist
    assertEquals(2, getWiser().getMessages().size());
    List<String> list = new ArrayList<String>(2);
    list.add(getWiser().getMessages().get(0).getEnvelopeReceiver());
    list.add(getWiser().getMessages().get(1).getEnvelopeReceiver());
    assertTrue(list.contains("tony@domain.com"));
    assertTrue(list.contains("darth@domain.com"));
    MimeMessage msg = ((WiserMessage) getWiser().getMessages().get(0)).getMimeMessage();
    assertEquals("'singleobject'", msg.getContent());
    assertEquals("'singleobject'", msg.getSubject());
    assertEquals("from@domain.com", ((InternetAddress) msg.getFrom()[0]).getAddress());
    assertEquals("replyTo@domain.com", ((InternetAddress) msg.getReplyTo()[0]).getAddress());
    final Address[] recipients = msg.getRecipients(RecipientType.TO);
    assertNotNull(recipients);
    assertTrue(recipients.length == 2);
    list = new ArrayList<String>(2);
    list.add(((InternetAddress) recipients[0]).getAddress());
    list.add(((InternetAddress) recipients[1]).getAddress());
    assertTrue(list.contains("tony@domain.com"));
    assertTrue(list.contains("darth@domain.com"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) CountDownTaskEventListener(org.jbpm.services.task.util.CountDownTaskEventListener) InputStreamReader(java.io.InputStreamReader) Address(javax.mail.Address) InternetAddress(javax.mail.internet.InternetAddress) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) InternalContent(org.kie.internal.task.api.model.InternalContent) WiserMessage(org.subethamail.wiser.WiserMessage) Date(java.util.Date) ContentData(org.kie.internal.task.api.model.ContentData) MimeMessage(javax.mail.internet.MimeMessage) Test(org.junit.Test)

Example 12 with ContentData

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

the class TaskInstanceServiceImpl method addTask.

public long addTask(Task task, Map<String, Object> params) {
    taskEventSupport.fireBeforeTaskAdded(task, context);
    persistenceContext.persistTask(task);
    resolveTaskDetailsForTaskProperties(task);
    if (params != null) {
        taskEventSupport.fireBeforeTaskInputVariablesChanged(task, context, Collections.emptyMap());
        resolveTaskDetails(params, task);
        ContentData contentData = ContentMarshallerHelper.marshal(task, params, TaskContentRegistry.get().getMarshallerContext(task).getEnvironment());
        Content content = TaskModelProvider.getFactory().newContent();
        ((InternalContent) content).setContent(contentData.getContent());
        persistenceContext.persistContent(content);
        persistenceContext.setDocumentToTask(content, contentData, task);
        taskEventSupport.fireAfterTaskInputVariablesChanged(task, context, params);
    }
    taskEventSupport.fireAfterTaskAdded(task, context);
    return task.getId();
}
Also used : ContentData(org.kie.internal.task.api.model.ContentData) Content(org.kie.api.task.model.Content) InternalContent(org.kie.internal.task.api.model.InternalContent) InternalContent(org.kie.internal.task.api.model.InternalContent)

Example 13 with ContentData

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

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

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

the class NonManagedLocalHTWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    Task task = createTaskBasedOnWorkItemParams(ksession, workItem);
    ContentData content = createTaskContentBasedOnWorkItemParams(ksession, workItem);
    try {
        long taskId = ((InternalTaskService) taskService).addTask(task, content);
        if (isAutoClaim(ksession, workItem, task)) {
            taskService.claim(taskId, (String) workItem.getParameter("SwimlaneActorId"));
        }
    } catch (Exception e) {
        if (action.equals(OnErrorAction.ABORT)) {
            manager.abortWorkItem(workItem.getId());
        } else if (action.equals(OnErrorAction.RETHROW)) {
            if (e instanceof RuntimeException) {
                throw (RuntimeException) e;
            } else {
                throw new RuntimeException(e);
            }
        } else if (action.equals(OnErrorAction.LOG)) {
            StringBuilder logMsg = new StringBuilder();
            logMsg.append(new Date()).append(": Error when creating task on task server for work item id ").append(workItem.getId());
            logMsg.append(". Error reported by task server: ").append(e.getMessage());
            logger.error(logMsg.toString(), e);
        }
    }
}
Also used : Task(org.kie.api.task.model.Task) ContentData(org.kie.internal.task.api.model.ContentData) InternalTaskService(org.kie.internal.task.api.InternalTaskService) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) Date(java.util.Date)

Aggregations

ContentData (org.kie.internal.task.api.model.ContentData)30 InternalContent (org.kie.internal.task.api.model.InternalContent)21 InternalTask (org.kie.internal.task.api.model.InternalTask)21 Test (org.junit.Test)19 Task (org.kie.api.task.model.Task)16 InputStreamReader (java.io.InputStreamReader)14 Reader (java.io.Reader)14 HashMap (java.util.HashMap)14 CountDownTaskEventListener (org.jbpm.services.task.util.CountDownTaskEventListener)14 InternalTaskData (org.kie.internal.task.api.model.InternalTaskData)14 Content (org.kie.api.task.model.Content)11 User (org.kie.api.task.model.User)11 InternalOrganizationalEntity (org.kie.internal.task.api.model.InternalOrganizationalEntity)11 OrganizationalEntity (org.kie.api.task.model.OrganizationalEntity)10 InternalPeopleAssignments (org.kie.internal.task.api.model.InternalPeopleAssignments)10 Date (java.util.Date)8 Map (java.util.Map)8 ArrayList (java.util.ArrayList)7 ContentMarshallerContext (org.kie.internal.task.api.ContentMarshallerContext)6 StringReader (java.io.StringReader)5