Search in sources :

Example 1 with BAMTaskSummaryImpl

use of org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl in project jbpm by kiegroup.

the class TaskAuditBaseTest method testComplete.

@Test
public void testComplete() {
    long initTimeMs = new Date().getTime();
    Task task = new TaskFluent().setName("This is my task name").addPotentialGroup("Knights Templer").setAdminUser("Administrator").getTask();
    taskService.addTask(task, new HashMap<String, Object>());
    long taskId = task.getId();
    List<TaskSummary> allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("salaboy", null, null, null);
    assertEquals(1, allGroupAuditTasks.size());
    assertTrue(allGroupAuditTasks.get(0).getStatusId().equals("Ready"));
    taskService.claim(taskId, "Darth Vader");
    allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("salaboy", null, null, null);
    assertEquals(0, allGroupAuditTasks.size());
    allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("Darth Vader", null, null, null);
    assertEquals(1, allGroupAuditTasks.size());
    assertTrue(allGroupAuditTasks.get(0).getStatusId().equals("Reserved"));
    taskService.release(taskId, "Darth Vader");
    allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("salaboy", null, null, null);
    assertEquals(1, allGroupAuditTasks.size());
    assertTrue(allGroupAuditTasks.get(0).getStatusId().equals("Ready"));
    taskService.claim(taskId, "Darth Vader");
    allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("salaboy", null, null, null);
    assertEquals(0, allGroupAuditTasks.size());
    allGroupAuditTasks = taskService.getTasksAssignedAsPotentialOwner("Darth Vader", null, null, null);
    assertEquals(1, allGroupAuditTasks.size());
    assertTrue(allGroupAuditTasks.get(0).getStatusId().equals("Reserved"));
    // 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());
    // Check is Complete
    taskService.complete(taskId, "Darth Vader", null);
    Task task2 = taskService.getTaskById(taskId);
    assertEquals(Status.Completed, task2.getTaskData().getStatus());
    assertEquals("Darth Vader", task2.getTaskData().getActualOwner().getId());
    List<TaskEvent> allTaskEvents = taskService.execute(new GetAuditEventsCommand(taskId, new QueryFilter(0, 0)));
    assertEquals(6, allTaskEvents.size());
    // test DeleteAuditEventsCommand
    int numFirstTaskEvents = allTaskEvents.size();
    task = new TaskFluent().setName("This is my task name 2").addPotentialGroup("Knights Templer").setAdminUser("Administrator").getTask();
    taskService.addTask(task, new HashMap<String, Object>());
    long secondTaskId = task.getId();
    taskService.claim(secondTaskId, "Darth Vader");
    taskService.start(secondTaskId, "Darth Vader");
    taskService.complete(secondTaskId, "Darth Vader", null);
    allTaskEvents = taskService.execute(new GetAuditEventsCommand());
    int numTaskEvents = allTaskEvents.size();
    assertTrue("Expected more than " + numFirstTaskEvents + " events: " + numTaskEvents, numTaskEvents > numFirstTaskEvents);
    taskService.execute(new DeleteAuditEventsCommand(taskId));
    allTaskEvents = taskService.execute(new GetAuditEventsCommand());
    assertEquals(numTaskEvents - numFirstTaskEvents, allTaskEvents.size());
    taskService.execute(new DeleteAuditEventsCommand());
    allTaskEvents = taskService.execute(new GetAuditEventsCommand());
    assertEquals(0, allTaskEvents.size());
    // test get/delete BAM Task summaries commands
    List<BAMTaskSummaryImpl> bamTaskList = taskService.execute(new GetBAMTaskSummariesCommand());
    assertEquals("BAM Task Summary list size: ", 2, bamTaskList.size());
    taskService.execute(new DeleteBAMTaskSummariesCommand(taskId));
    bamTaskList = taskService.execute(new GetBAMTaskSummariesCommand());
    assertEquals("BAM Task Summary list size after delete (task id: " + taskId + ") : ", 1, bamTaskList.size());
    bamTaskList = taskService.execute(new GetBAMTaskSummariesCommand(secondTaskId));
    assertEquals("BAM Task Summary list size after delete (task id: " + taskId + ") : ", 1, bamTaskList.size());
    taskService.execute(new DeleteBAMTaskSummariesCommand());
    bamTaskList = taskService.execute(new GetBAMTaskSummariesCommand());
    assertEquals("BAM Task Summary list size after delete (task id: " + taskId + ") : ", 0, bamTaskList.size());
    List<AuditTask> allHistoryAuditTasks = taskAuditService.getAllAuditTasks(new QueryFilter(0, 0));
    assertEquals(2, allHistoryAuditTasks.size());
    // test last modification date was generated
    long currentTimeMs = new Date().getTime();
    for (AuditTask at : allHistoryAuditTasks) {
        Date modDate = ((AuditTaskImpl) at).getLastModificationDate();
        assertNotNull(modDate);
        long modDateMs = modDate.getTime();
        assertTrue("Task " + at.getTaskId() + " modification date is not too much in the past", modDateMs >= initTimeMs);
        assertTrue("Task " + at.getTaskId() + " modification date is not in the future", modDateMs <= currentTimeMs);
    }
}
Also used : Task(org.kie.api.task.model.Task) AuditTask(org.kie.internal.task.api.AuditTask) TaskFluent(org.jbpm.services.task.utils.TaskFluent) DeleteBAMTaskSummariesCommand(org.jbpm.services.task.audit.commands.DeleteBAMTaskSummariesCommand) Date(java.util.Date) GetAuditEventsCommand(org.jbpm.services.task.audit.commands.GetAuditEventsCommand) AuditTask(org.kie.internal.task.api.AuditTask) AuditTaskImpl(org.jbpm.services.task.audit.impl.model.AuditTaskImpl) QueryFilter(org.kie.internal.query.QueryFilter) DeleteAuditEventsCommand(org.jbpm.services.task.audit.commands.DeleteAuditEventsCommand) GetBAMTaskSummariesCommand(org.jbpm.services.task.audit.commands.GetBAMTaskSummariesCommand) TaskSummary(org.kie.api.task.model.TaskSummary) TaskEvent(org.kie.internal.task.api.model.TaskEvent) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl) HumanTaskServicesBaseTest(org.jbpm.services.task.HumanTaskServicesBaseTest) Test(org.junit.Test)

Example 2 with BAMTaskSummaryImpl

use of org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl in project jbpm by kiegroup.

the class BAMTaskEventListener method createTask.

/**
 * Creates or updates a bam task summary instance.
 *
 * @param ti The source task
 * @param newStatus The new state for the task.
 * @param worker Perform additional operations to the bam task summary instance.
 * @return The created or updated bam task summary instance.
 */
protected BAMTaskSummaryImpl createTask(TaskEvent event, Status newStatus, BAMTaskWorker worker) {
    BAMTaskSummaryImpl result = null;
    Task ti = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    try {
        if (ti == null) {
            logger.error("The task instance does not exist.");
            return result;
        }
        Status status = newStatus != null ? newStatus : ti.getTaskData().getStatus();
        String actualOwner = "";
        if (ti.getTaskData().getActualOwner() != null) {
            actualOwner = ti.getTaskData().getActualOwner().getId();
        }
        result = new BAMTaskSummaryImpl(ti.getId(), ti.getName(), status.toString(), event.getEventDate(), actualOwner, ti.getTaskData().getProcessInstanceId());
        if (worker != null)
            worker.createTask(result, ti);
        persistenceContext.persist(result);
        return result;
    } finally {
        cleanup(persistenceContext);
    }
}
Also used : Status(org.kie.api.task.model.Status) Task(org.kie.api.task.model.Task) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Example 3 with BAMTaskSummaryImpl

use of org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl in project jbpm by kiegroup.

the class ProcessPersistenceHumanTaskOnLaneTest method testProcessWithBAMListener.

@SuppressWarnings("unchecked")
@Test
public void testProcessWithBAMListener() throws Exception {
    createRuntimeManager("org/jbpm/test/functional/task/HumanTaskOnLane.bpmn2");
    RuntimeEngine runtimeEngine = getRuntimeEngine();
    KieSession ksession = runtimeEngine.getKieSession();
    TaskService taskService = runtimeEngine.getTaskService();
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new BAMTaskEventListener(true));
    ProcessInstance processInstance = ksession.startProcess("UserTask");
    assertProcessInstanceActive(processInstance.getId());
    long task1 = -1;
    long task2 = -1;
    // simulating a system restart
    logger.debug("Reloading the environemnt to simulate system restart");
    disposeRuntimeManager();
    createRuntimeManager("org/jbpm/test/functional/task/HumanTaskOnLane.bpmn2");
    runtimeEngine = getRuntimeEngine();
    ksession = runtimeEngine.getKieSession();
    taskService = runtimeEngine.getTaskService();
    ((EventService<TaskLifeCycleEventListener>) taskService).registerTaskEventListener(new BAMTaskEventListener(true));
    // let john execute Task 1
    String taskUser = "john";
    String locale = "en-UK";
    List<TaskSummary> list = taskService.getTasksAssignedAsPotentialOwner(taskUser, locale);
    assertEquals(1, list.size());
    TaskSummary task = list.get(0);
    task1 = task.getId();
    taskService.claim(task.getId(), taskUser);
    taskService.start(task.getId(), taskUser);
    taskService.complete(task.getId(), taskUser, null);
    List<Status> reservedOnly = new ArrayList<Status>();
    reservedOnly.add(Status.Reserved);
    list = taskService.getTasksAssignedAsPotentialOwnerByStatus(taskUser, reservedOnly, locale);
    assertEquals(1, list.size());
    task = list.get(0);
    task2 = task.getId();
    taskService.start(task.getId(), taskUser);
    taskService.complete(task.getId(), taskUser, null);
    assertProcessInstanceCompleted(processInstance.getId());
    EntityManager em = getEmf().createEntityManager();
    List<BAMTaskSummaryImpl> bamLogs = em.createQuery("from BAMTaskSummaryImpl").getResultList();
    em.close();
    assertNotNull(bamLogs);
    assertEquals(2, bamLogs.size());
    List<Long> taskIdsFromBAM = new ArrayList<Long>();
    for (BAMTaskSummaryImpl bamEntry : bamLogs) {
        taskIdsFromBAM.add(bamEntry.getTaskId());
    }
    assertTrue(taskIdsFromBAM.contains(task1));
    assertTrue(taskIdsFromBAM.contains(task2));
}
Also used : Status(org.kie.api.task.model.Status) RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) BAMTaskEventListener(org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener) TaskService(org.kie.api.task.TaskService) ArrayList(java.util.ArrayList) EventService(org.kie.internal.task.api.EventService) EntityManager(javax.persistence.EntityManager) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl) Test(org.junit.Test)

Example 4 with BAMTaskSummaryImpl

use of org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl in project jbpm by kiegroup.

the class TaskAuditBaseTest method assertBAMTask.

protected void assertBAMTask(long taskId, String expectedStatus) {
    EntityManager em = getEntityManager();
    BAMTaskSummaryImpl task = (BAMTaskSummaryImpl) em.createQuery("select bt from BAMTaskSummaryImpl bt where bt.taskId = :taskId").setParameter("taskId", taskId).getSingleResult();
    assertNotNull(task);
    assertEquals(taskId, task.getTaskId());
    assertEquals(expectedStatus, task.getStatus());
    em.close();
}
Also used : EntityManager(javax.persistence.EntityManager) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl)

Example 5 with BAMTaskSummaryImpl

use of org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl in project jbpm by kiegroup.

the class BAMTaskEventListener method updateTask.

protected BAMTaskSummaryImpl updateTask(TaskEvent event, Status newStatus, BAMTaskWorker worker) {
    BAMTaskSummaryImpl result = null;
    Task ti = event.getTask();
    TaskPersistenceContext persistenceContext = getPersistenceContext(((TaskContext) event.getTaskContext()).getPersistenceContext());
    try {
        if (ti == null) {
            logger.error("The task instance does not exist.");
            return result;
        }
        Status status = newStatus != null ? newStatus : ti.getTaskData().getStatus();
        result = persistenceContext.queryStringWithParametersInTransaction("select bts from BAMTaskSummaryImpl bts where bts.taskId=:taskId", true, persistenceContext.addParametersToMap("taskId", ti.getId()), BAMTaskSummaryImpl.class);
        if (result == null) {
            logger.warn("Unable find bam task entry for task id {} '{}', skipping bam task update", ti.getId(), ti.getName());
            return null;
        }
        result.setStatus(status.toString());
        if (ti.getTaskData().getActualOwner() != null) {
            result.setUserId(ti.getTaskData().getActualOwner().getId());
        }
        if (worker != null)
            worker.updateTask(result, ti);
        persistenceContext.merge(result);
        return result;
    } finally {
        cleanup(persistenceContext);
    }
}
Also used : Status(org.kie.api.task.model.Status) Task(org.kie.api.task.model.Task) BAMTaskSummaryImpl(org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl) TaskPersistenceContext(org.kie.internal.task.api.TaskPersistenceContext)

Aggregations

BAMTaskSummaryImpl (org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl)5 Status (org.kie.api.task.model.Status)3 Task (org.kie.api.task.model.Task)3 EntityManager (javax.persistence.EntityManager)2 Test (org.junit.Test)2 TaskSummary (org.kie.api.task.model.TaskSummary)2 TaskPersistenceContext (org.kie.internal.task.api.TaskPersistenceContext)2 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HumanTaskServicesBaseTest (org.jbpm.services.task.HumanTaskServicesBaseTest)1 DeleteAuditEventsCommand (org.jbpm.services.task.audit.commands.DeleteAuditEventsCommand)1 DeleteBAMTaskSummariesCommand (org.jbpm.services.task.audit.commands.DeleteBAMTaskSummariesCommand)1 GetAuditEventsCommand (org.jbpm.services.task.audit.commands.GetAuditEventsCommand)1 GetBAMTaskSummariesCommand (org.jbpm.services.task.audit.commands.GetBAMTaskSummariesCommand)1 AuditTaskImpl (org.jbpm.services.task.audit.impl.model.AuditTaskImpl)1 BAMTaskEventListener (org.jbpm.services.task.lifecycle.listeners.BAMTaskEventListener)1 TaskFluent (org.jbpm.services.task.utils.TaskFluent)1 KieSession (org.kie.api.runtime.KieSession)1 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)1 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)1