Search in sources :

Example 21 with InternalTaskService

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

the class HumanTaskAssignmentTest method testGetTasksAssignedAsPotentialOwnerGroup.

@Test
@BZ("1103977")
public void testGetTasksAssignedAsPotentialOwnerGroup() {
    createRuntimeManager(GET_TASKS_OWNER_GROUP);
    KieSession ksession = getRuntimeEngine().getKieSession();
    InternalTaskService taskService = (InternalTaskService) getRuntimeEngine().getTaskService();
    long pid = ksession.startProcess(GET_TASKS_OWNER_GROUP_ID).getId();
    // Task is assigned to group HR.
    List<TaskSummary> taskList = taskService.getTasksAssignedAsPotentialOwner(null, Arrays.asList("HR"));
    Assertions.assertThat(taskList).hasSize(1);
    Assertions.assertThat(taskList.get(0).getStatus()).isEqualTo(Status.Ready);
    Long taskId = taskList.get(0).getId();
    taskService.claim(taskId, "mary");
    taskService.start(taskId, "mary");
    taskService.complete(taskId, "mary", null);
    Task task = taskService.getTaskById(taskId);
    Assertions.assertThat(task.getTaskData().getStatus()).isEqualTo(Status.Completed);
    // Next task is assigned to group PM.
    // So task list for HR will be empty.
    taskList = taskService.getTasksAssignedAsPotentialOwner(null, Arrays.asList("HR"));
    Assertions.assertThat(taskList).hasSize(0);
    taskList = taskService.getTasksAssignedAsPotentialOwner(null, Arrays.asList("PM"));
    Assertions.assertThat(taskList).hasSize(1);
    Assertions.assertThat(taskList.get(0).getStatus()).isEqualTo(Status.Ready);
    ksession.abortProcessInstance(pid);
}
Also used : Task(org.kie.api.task.model.Task) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) InternalTaskService(org.kie.internal.task.api.InternalTaskService) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 22 with InternalTaskService

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

the class HumanTaskAssignmentTest method testGetTasksAssignedAsPotentialOwnerUser.

@Test
@BZ("1178153")
public void testGetTasksAssignedAsPotentialOwnerUser() {
    createRuntimeManager(GET_TASKS_OWNER_USER);
    KieSession ksession = getRuntimeEngine().getKieSession();
    InternalTaskService taskService = (InternalTaskService) getRuntimeEngine().getTaskService();
    long pid = ksession.startProcess(GET_TASKS_OWNER_USER_ID).getId();
    // Task is potentially assigned to several users - mary,john,ibek.
    List<TaskSummary> taskList = taskService.getTasksAssignedAsPotentialOwner("jack", new ArrayList<String>());
    Assertions.assertThat(taskList).hasSize(0);
    taskList = taskService.getTasksAssignedAsPotentialOwner("mary", new ArrayList<String>());
    Assertions.assertThat(taskList).hasSize(1);
    Assertions.assertThat(taskList.get(0).getStatus()).isEqualTo(Status.Ready);
    ksession.abortProcessInstance(pid);
}
Also used : TaskSummary(org.kie.api.task.model.TaskSummary) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalTaskService(org.kie.internal.task.api.InternalTaskService) Test(org.junit.Test) BZ(qa.tools.ikeeper.annotation.BZ)

Example 23 with InternalTaskService

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

Example 24 with InternalTaskService

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

the class LocalHTWorkItemHandler method executeWorkItem.

@Override
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
    RuntimeEngine runtime = runtimeManager.getRuntimeEngine(ProcessInstanceIdContext.get(workItem.getProcessInstanceId()));
    KieSession ksessionById = runtime.getKieSession();
    Task task = createTaskBasedOnWorkItemParams(ksessionById, workItem);
    // ContentData content = createTaskContentBasedOnWorkItemParams(ksessionById, workItem);
    Map<String, Object> content = createTaskDataBasedOnWorkItemParams(ksessionById, workItem);
    try {
        long taskId = ((InternalTaskService) runtime.getTaskService()).addTask(task, content);
        if (isAutoClaim(ksessionById, workItem, task)) {
            try {
                runtime.getTaskService().claim(taskId, (String) workItem.getParameter("SwimlaneActorId"));
            } catch (PermissionDeniedException e) {
                logger.warn("User {} is not allowed to auto claim task due to permission violation", 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);
            // rethrow to cancel processing if the exception is not recoverable
            if (!(e instanceof TaskException) || ((e instanceof TaskException) && !((TaskException) e).isRecoverable())) {
                if (e instanceof RuntimeException) {
                    throw (RuntimeException) e;
                } else {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
Also used : RuntimeEngine(org.kie.api.runtime.manager.RuntimeEngine) Task(org.kie.api.task.model.Task) InternalTaskService(org.kie.internal.task.api.InternalTaskService) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException) TaskException(org.kie.internal.task.exception.TaskException) Date(java.util.Date) TaskException(org.kie.internal.task.exception.TaskException) KieSession(org.kie.api.runtime.KieSession) PermissionDeniedException(org.jbpm.services.task.exception.PermissionDeniedException)

Example 25 with InternalTaskService

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

the class HumanResourcesHiringTest method testHiringProcess.

@SuppressWarnings("unchecked")
private void testHiringProcess(RuntimeManager manager, Context<?> context) {
    RuntimeEngine runtime = manager.getRuntimeEngine(context);
    KieSession ksession = runtime.getKieSession();
    TaskService taskService = runtime.getTaskService();
    assertNotNull(runtime);
    assertNotNull(ksession);
    ksession.getWorkItemManager().registerWorkItemHandler("EmailService", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            manager.completeWorkItem(workItem.getId(), null);
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            // To change body of generated methods, choose Tools | Templates.
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
    ksession.getWorkItemManager().registerWorkItemHandler("TwitterService", new WorkItemHandler() {

        @Override
        public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
            manager.completeWorkItem(workItem.getId(), null);
        }

        @Override
        public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
            // To change body of generated methods, choose Tools | Templates.
            throw new UnsupportedOperationException("Not supported yet.");
        }
    });
    ProcessInstance processInstance = ksession.startProcess("hiring");
    Collection<NodeInstanceDesc> activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    Collection<NodeInstanceDesc> completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(1, completedNodes.size());
    List<TaskSummary> tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("HR");
    TaskSummary HRInterview = tasks.get(0);
    taskService.claim(HRInterview.getId(), "katy");
    taskService.start(HRInterview.getId(), "katy");
    Map<String, Object> hrOutput = new HashMap<String, Object>();
    hrOutput.put("out_name", "salaboy");
    hrOutput.put("out_age", 29);
    hrOutput.put("out_mail", "salaboy@gmail.com");
    hrOutput.put("out_score", 8);
    taskService.complete(HRInterview.getId(), "katy", hrOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(2, completedNodes.size());
    assertNotNull(processInstance);
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("IT");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary techInterview = tasks.get(0);
    Task techInterviewTask = taskService.getTaskById(techInterview.getId());
    Content contentById = taskService.getContentById(techInterviewTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    Map<String, Object> taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(7, taskContent.size());
    assertEquals("salaboy@gmail.com", taskContent.get("in_mail"));
    assertEquals(29, taskContent.get("in_age"));
    assertEquals("salaboy", taskContent.get("in_name"));
    taskService.claim(techInterview.getId(), "salaboy");
    taskService.start(techInterview.getId(), "salaboy");
    Map<String, Object> techOutput = new HashMap<String, Object>();
    techOutput.put("out_skills", "java, jbpm, drools");
    techOutput.put("out_twitter", "@salaboy");
    techOutput.put("out_score", 8);
    taskService.complete(techInterview.getId(), "salaboy", techOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(3, completedNodes.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("Accounting");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary createProposal = tasks.get(0);
    Task createProposalTask = taskService.getTaskById(createProposal.getId());
    contentById = taskService.getContentById(createProposalTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(6, taskContent.size());
    assertEquals(8, taskContent.get("in_tech_score"));
    assertEquals(8, taskContent.get("in_hr_score"));
    taskService.claim(createProposal.getId(), "john");
    taskService.start(createProposal.getId(), "john");
    Map<String, Object> proposalOutput = new HashMap<String, Object>();
    proposalOutput.put("out_offering", 10000);
    taskService.complete(createProposal.getId(), "john", proposalOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(1, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(5, completedNodes.size());
    tasks = ((InternalTaskService) taskService).getTasksAssignedByGroup("HR");
    assertNotNull(tasks);
    assertEquals(1, tasks.size());
    TaskSummary signContract = tasks.get(0);
    Task signContractTask = taskService.getTaskById(signContract.getId());
    contentById = taskService.getContentById(signContractTask.getTaskData().getDocumentContentId());
    assertNotNull(contentById);
    taskContent = (Map<String, Object>) ContentMarshallerHelper.unmarshall(contentById.getContent(), null);
    assertEquals(6, taskContent.size());
    assertEquals(10000, taskContent.get("in_offering"));
    assertEquals("salaboy", taskContent.get("in_name"));
    taskService.claim(signContract.getId(), "katy");
    taskService.start(signContract.getId(), "katy");
    Map<String, Object> signOutput = new HashMap<String, Object>();
    signOutput.put("out_signed", true);
    taskService.complete(signContract.getId(), "katy", signOutput);
    activeNodes = runtimeDataService.getProcessInstanceHistoryActive(processInstance.getId(), new QueryContext());
    assertNotNull(activeNodes);
    assertEquals(0, activeNodes.size());
    completedNodes = runtimeDataService.getProcessInstanceHistoryCompleted(processInstance.getId(), new QueryContext());
    assertNotNull(completedNodes);
    assertEquals(8, completedNodes.size());
    int removeAllTasks = ((InternalTaskService) taskService).removeAllTasks();
    logger.debug(">>> Removed Tasks > {}", removeAllTasks);
}
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) TaskService(org.kie.api.task.TaskService) InternalTaskService(org.kie.internal.task.api.InternalTaskService) QueryContext(org.kie.api.runtime.query.QueryContext) WorkItem(org.kie.api.runtime.process.WorkItem) WorkItemHandler(org.kie.api.runtime.process.WorkItemHandler) Content(org.kie.api.task.model.Content) TaskSummary(org.kie.api.task.model.TaskSummary) KieSession(org.kie.api.runtime.KieSession) ProcessInstance(org.kie.api.runtime.process.ProcessInstance) WorkItemManager(org.kie.api.runtime.process.WorkItemManager) NodeInstanceDesc(org.jbpm.services.api.model.NodeInstanceDesc) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

InternalTaskService (org.kie.internal.task.api.InternalTaskService)44 RuntimeEngine (org.kie.api.runtime.manager.RuntimeEngine)35 TaskService (org.kie.api.task.TaskService)27 RuntimeManager (org.kie.api.runtime.manager.RuntimeManager)25 InternalRuntimeManager (org.kie.internal.runtime.manager.InternalRuntimeManager)20 UserTaskInstanceDesc (org.jbpm.services.api.model.UserTaskInstanceDesc)18 UserTaskService (org.jbpm.services.api.UserTaskService)17 TaskSummary (org.kie.api.task.model.TaskSummary)13 Test (org.junit.Test)12 KieSession (org.kie.api.runtime.KieSession)11 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)9 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)9 Task (org.kie.api.task.model.Task)5 TaskCleanUpProcessEventListener (org.jbpm.services.task.admin.listener.TaskCleanUpProcessEventListener)4 AuditService (org.kie.api.runtime.manager.audit.AuditService)4 I18NText (org.kie.api.task.model.I18NText)4 Date (java.util.Date)3 EntityManager (javax.persistence.EntityManager)3 KModuleDeploymentUnit (org.jbpm.kie.services.impl.KModuleDeploymentUnit)3