Search in sources :

Example 1 with TaskEvent

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

the class PerProcessInstanceRuntimeManagerTest method testCreationOfSessionWithCustomTaskListener.

@Test
public void testCreationOfSessionWithCustomTaskListener() {
    final List<Long> addedTasks = new ArrayList<Long>();
    RuntimeEnvironment environment = RuntimeEnvironmentBuilder.Factory.get().newDefaultInMemoryBuilder().userGroupCallback(userGroupCallback).entityManagerFactory(emf).addAsset(ResourceFactory.newClassPathResource("BPMN2-ScriptTask.bpmn2"), ResourceType.BPMN2).addAsset(ResourceFactory.newClassPathResource("BPMN2-UserTask.bpmn2"), ResourceType.BPMN2).registerableItemsFactory(new DefaultRegisterableItemsFactory() {

        @Override
        public List<TaskLifeCycleEventListener> getTaskListeners() {
            List<TaskLifeCycleEventListener> listeners = super.getTaskListeners();
            listeners.add(new DefaultTaskEventListener() {

                @Override
                public void afterTaskAddedEvent(TaskEvent event) {
                    addedTasks.add(event.getTask().getId());
                }
            });
            return listeners;
        }
    }).get();
    manager = RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
    assertNotNull(manager);
    // ksession for process instance #1
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession = runtime.getKieSession();
    assertNotNull(ksession);
    long ksession1Id = ksession.getIdentifier();
    assertTrue(ksession1Id == 1);
    // FIXME quick hack to overcome problems with same pi ids when not using persistence
    ksession.startProcess("ScriptTask");
    // ksession for process instance #2
    // since there is no process instance yet we need to get new session
    RuntimeEngine runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession2 = runtime2.getKieSession();
    assertNotNull(ksession2);
    long ksession2Id = ksession2.getIdentifier();
    assertTrue(ksession2Id == 2);
    ProcessInstance pi1 = ksession.startProcess("UserTask");
    ProcessInstance pi2 = ksession2.startProcess("UserTask");
    // both processes started
    assertEquals(ProcessInstance.STATE_ACTIVE, pi1.getState());
    assertEquals(ProcessInstance.STATE_ACTIVE, pi2.getState());
    runtime = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi1.getId()));
    ksession = runtime.getKieSession();
    assertEquals(ksession1Id, ksession.getIdentifier());
    runtime2 = manager.getRuntimeEngine(ProcessInstanceIdContext.get(pi2.getId()));
    ksession2 = runtime2.getKieSession();
    assertEquals(ksession2Id, ksession2.getIdentifier());
    assertEquals(2, addedTasks.size());
    manager.close();
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) RuntimeEnvironment(org.kie.api.runtime.manager.RuntimeEnvironment) ArrayList(java.util.ArrayList) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) JPATaskLifeCycleEventListener(org.jbpm.services.task.audit.JPATaskLifeCycleEventListener) TaskLifeCycleEventListener(org.kie.api.task.TaskLifeCycleEventListener) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 2 with TaskEvent

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

the class ExecutionErrorHandlingRuntimeManagerTest method testUserTaskFailure.

@SuppressWarnings("unchecked")
@Test
public void testUserTaskFailure() {
    RuntimeEngine runtime1 = manager.getRuntimeEngine(ProcessInstanceIdContext.get());
    KieSession ksession1 = runtime1.getKieSession();
    assertNotNull(ksession1);
    ksession1.startProcess("UserTaskWithRollback");
    TaskService taskService = runtime1.getTaskService();
    List<TaskSummary> tasks = taskService.getTasksAssignedAsPotentialOwner("john", "en-UK");
    assertEquals(1, tasks.size());
    long taskId = tasks.get(0).getId();
    try {
        ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

            @Override
            public void afterTaskStartedEvent(TaskEvent event) {
                throw new TaskExecutionException("On purpose");
            }
        });
        taskService.start(taskId, "john");
        fail("Start task should fail due to broken script");
    } catch (Throwable e) {
    // expected
    }
    manager.disposeRuntimeEngine(runtime1);
    ExecutionErrorManager errorManager = ((AbstractRuntimeManager) manager).getExecutionErrorManager();
    ExecutionErrorStorage storage = errorManager.getStorage();
    List<ExecutionError> errors = storage.list(0, 10);
    assertNotNull(errors);
    assertEquals(1, errors.size());
    assertExecutionError(errors.get(0), "Task", "UserTaskWithRollback", "Hello");
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) ExecutionError(org.kie.internal.runtime.error.ExecutionError) TaskService(org.kie.api.task.TaskService) AbstractRuntimeManager(org.jbpm.runtime.manager.impl.AbstractRuntimeManager) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskExecutionException(org.jbpm.services.task.exception.TaskExecutionException) ExecutionErrorManager(org.kie.internal.runtime.error.ExecutionErrorManager) TaskSummary(org.kie.api.task.model.TaskSummary) TaskEvent(org.kie.api.task.TaskEvent) ExecutionErrorStorage(org.kie.internal.runtime.error.ExecutionErrorStorage) KieSession(org.kie.api.runtime.KieSession) AbstractBaseTest(org.jbpm.test.util.AbstractBaseTest) Test(org.junit.Test)

Example 3 with TaskEvent

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

the class LifeCycleBaseTest method testCompleteWithMergeOfResults.

@Test
public void testCompleteWithMergeOfResults() {
    final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
    // 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' })";
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

        @Override
        public void afterTaskCompletedEvent(TaskEvent event) {
            Map<String, Object> outs = event.getTask().getTaskData().getTaskOutputVariables();
            if (outs != null) {
                outputsAfterCompletion.putAll(outs);
            }
        }
    });
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    task1 = taskService.getTaskById(taskId);
    Map<String, Object> outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    taskService.complete(taskId, "Darth Vader", null);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    // now let's check what was actually given to listeners
    assertEquals(0, outputsAfterCompletion.size());
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 4 with TaskEvent

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

the class LifeCycleBaseTest method testCompleteWithMergeOfResultsNoDataBeforeCompletion.

@Test
public void testCompleteWithMergeOfResultsNoDataBeforeCompletion() {
    final Map<String, Object> outputsAfterCompletion = new HashMap<String, Object>();
    // 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' })";
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new DefaultTaskEventListener() {

        @Override
        public void afterTaskCompletedEvent(TaskEvent event) {
            outputsAfterCompletion.putAll(event.getTask().getTaskData().getTaskOutputVariables());
        }
    });
    Task task = TaskFactory.evalTask(new StringReader(str));
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    taskService.start(taskId, "Darth Vader");
    Task task1 = taskService.getTaskById(taskId);
    assertEquals(Status.InProgress, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    task1 = taskService.getTaskById(taskId);
    Map<String, Object> outputs = getTaskOutput(task1);
    assertEquals(0, outputs.size());
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("response", "let's do it");
    params.put("feedback", "ok");
    taskService.complete(taskId, "Darth Vader", params);
    task1 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task1.getTaskData().getStatus());
    assertEquals("Darth Vader", task1.getTaskData().getActualOwner().getId());
    outputs = getTaskOutput(task1);
    assertEquals(2, outputs.size());
    assertEquals("let's do it", outputs.get("response"));
    assertEquals("ok", outputs.get("feedback"));
    // now let's check what was actually given to listeners
    assertEquals(2, outputsAfterCompletion.size());
    assertEquals("let's do it", outputsAfterCompletion.get("response"));
    assertEquals("ok", outputsAfterCompletion.get("feedback"));
}
Also used : Task(org.kie.api.task.model.Task) InternalTask(org.kie.internal.task.api.model.InternalTask) HashMap(java.util.HashMap) TaskEvent(org.kie.api.task.TaskEvent) StringReader(java.io.StringReader) EventService(org.kie.internal.task.api.EventService) DefaultTaskEventListener(org.jbpm.services.task.events.DefaultTaskEventListener) Test(org.junit.Test)

Example 5 with TaskEvent

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

the class JPATaskLifeCycleEventListener method assignmentsUpadted.

protected void assignmentsUpadted(TaskEvent event, AssignmentType type, List<OrganizationalEntity> entities, String messageSufix) {
    if (entities == null || entities.isEmpty()) {
        return;
    }
    String userId = event.getTaskContext().getUserId();
    Task task = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    StringBuilder message = new StringBuilder();
    switch(type) {
        case POT_OWNER:
            message.append("Potential owners [");
            break;
        case EXCL_OWNER:
            message.append("Excluded owners [");
            break;
        case ADMIN:
            message.append("Business administrators [");
            break;
        default:
            break;
    }
    String entitiesAsString = entities.stream().map(oe -> oe.getId()).collect(Collectors.joining(","));
    message.append(entitiesAsString);
    message.append(messageSufix);
    persistenceContext.persist(new TaskEventImpl(task.getId(), org.kie.internal.task.api.model.TaskEvent.TaskEventType.UPDATED, task.getTaskData().getProcessInstanceId(), task.getTaskData().getWorkItemId(), userId, message.toString()));
}
Also used : PersistableEventListener(org.jbpm.services.task.persistence.PersistableEventListener) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Task(org.kie.api.task.model.Task) Date(java.util.Date) TaskVariable(org.kie.internal.task.api.TaskVariable) TaskEventImpl(org.jbpm.services.task.audit.impl.model.TaskEventImpl) VariableType(org.kie.internal.task.api.TaskVariable.VariableType) LoggerFactory(org.slf4j.LoggerFactory) AuditTaskImpl(org.jbpm.services.task.audit.impl.model.AuditTaskImpl) HashMap(java.util.HashMap) ClassUtil(org.jbpm.services.task.utils.ClassUtil) Collectors(java.util.stream.Collectors) TaskLifeCycleEventListener(org.jbpm.services.task.lifecycle.listeners.TaskLifeCycleEventListener) TaskEvent(org.kie.api.task.TaskEvent) OrganizationalEntity(org.kie.api.task.model.OrganizationalEntity) TaskIndexerManager(org.jbpm.services.task.audit.variable.TaskIndexerManager) List(java.util.List) TaskContext(org.kie.internal.task.api.TaskContext) EntityManagerFactory(javax.persistence.EntityManagerFactory) Map(java.util.Map) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext) Task(org.kie.api.task.model.Task) TaskEventImpl(org.jbpm.services.task.audit.impl.model.TaskEventImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Aggregations

TaskEvent (org.kie.api.task.TaskEvent)10 HashMap (java.util.HashMap)8 DefaultTaskEventListener (org.jbpm.services.task.events.DefaultTaskEventListener)8 Test (org.junit.Test)8 Task (org.kie.api.task.model.Task)7 EventService (org.kie.internal.task.api.EventService)7 StringReader (java.io.StringReader)5 InternalTask (org.kie.internal.task.api.model.InternalTask)5 Map (java.util.Map)3 KieSession (org.kie.api.runtime.KieSession)3 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)3 Arrays (java.util.Arrays)2 Date (java.util.Date)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 EntityManagerFactory (javax.persistence.EntityManagerFactory)2 AbstractRuntimeManager (org.jbpm.runtime.manager.impl.AbstractRuntimeManager)2 AuditTaskImpl (org.jbpm.services.task.audit.impl.model.AuditTaskImpl)2 TaskEventImpl (org.jbpm.services.task.audit.impl.model.TaskEventImpl)2 TaskIndexerManager (org.jbpm.services.task.audit.variable.TaskIndexerManager)2