【Activiti系列学习】--15.Activiti核心API之TaskService

TaskService

TaskService为任务管理服务

1.提供的功能:

  • 对用户任务(UserTask)管理和流程的控制

  • 设置用户任务(UserTask)的权限信息(拥有者,候选人,办理人)

  • 针对用户任务添加任务附件、任务评论和事件记录

2.TaskService如何对Task管理与流程控制

  • Task对象的创建,删除

  • 查询Task,并驱动Task节点完成执行

  • Task相关参数变量(variable)设置

流程定义文件

我们在流程定义文件中定义了一个用户任务someTask





	

		
		
		
		
			
			some Task ${message}

		
		

		

	

TaskService对Task的创建,查询并驱动Task结点完成执行,Task相关参数变量设置

public class TaskServiceTest {
    private static final Logger LOGGER = LoggerFactory.getLogger(TaskServiceTest.class);

    @Rule
    public ActivitiRule activitiRule = new ActivitiRule();

    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskService(){
        Map variables = Maps.newHashMap();
        variables.put("message", "my test message !!!");
        ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();
        LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
        LOGGER.info("task.description = {}", task.getDescription());

        //对变量进行操作
        taskService.setVariable(task.getId(),"key1","value1");
        taskService.setVariableLocal(task.getId(), "localkey1", "localvalue1");
        //获取变量的三种方式
        Map taskServiceVariables = taskService.getVariables(task.getId());
        Map taskServiceVariablesLocal = taskService.getVariablesLocal(task.getId());
        Map processVariables1 = activitiRule.getRuntimeService().getVariables(task.getExecutionId());
        LOGGER.info("taskServiceVariables = {}",taskServiceVariables);
        LOGGER.info("taskServiceVariablesLocal = {}", taskServiceVariablesLocal);
        LOGGER.info("processVariables1 = {}",processVariables1);

        //task完成后,打印输出的结果为null
        Map completeVar = Maps.newConcurrentMap();
        completeVar.put("ckey1", "cValue1");
        taskService.complete(task.getId(), completeVar);
        Task task1 = taskService.createTaskQuery().taskId(task.getId()).singleResult();
        LOGGER.info("task1 = {}", task1);
    }
}

实现流程与权限的设置的相关操作

TaskService设置Task权限信息

  • 候选用户(candidateUser)和候选组(candidateGroup)

  • 指定拥有人(Ower)和办理人(Assignee)

  • 通过claim设置办理人(也可以使用setAssignee方式,但是不检查权限直接修改)

    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskServiceUser(){
        Map variables = Maps.newHashMap();
        variables.put("message", "my test message !!!");//这个message对应的值my test message就是流程定义文件中的message值
        ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();
        LOGGER.info("task = {}", ToStringBuilder.reflectionToString(task, ToStringStyle.JSON_STYLE));
        LOGGER.info("task.description = {}", task.getDescription());//描述为documentation的内容

        taskService.setOwner(task.getId(), "user1");//办理人,流程的发起人
        //taskService.setAssignee(task.getId(), "syc");//代办人,不检查权限直接修改
        List taskList = taskService.createTaskQuery().taskCandidateUser("syc").taskUnassigned().listPage(0, 100);//指定自己为候选人但这个task没有指定代办人
        for(Task task1 : taskList){
            try {
                taskService.claim(task1.getId(), "syc");//设置代办人
            } catch (Exception e) {
                LOGGER.error(e.getMessage(),e);
            }
        }

        //查询task与用户相关的关系
        List identityLinksForTask = taskService.getIdentityLinksForTask(task.getId());
        for(IdentityLink identityLink : identityLinksForTask){
            LOGGER.info("identityLink = {}",identityLink);
        }

        //继续向后执行,执行自己有权限的task
        List sycstask = taskService.createTaskQuery().taskAssignee("syc").listPage(0, 100);//当前办理人是自己的流程
        for(Task syc : sycstask){//让流程完成
            Map vars = Maps.newHashMap();
            vars.put("ckey1", "cvalue1");
            taskService.complete(syc.getId(), vars);
            LOGGER.info("syc = {}",syc);
        }

        sycstask = taskService.createTaskQuery().taskAssignee("syc").listPage(0, 100);
        LOGGER.info("是否存在 {}", CollectionUtils.isEmpty(sycstask));

    }

TaskService设置Task附加信息

  • 任务附件(Attachment)创建与查询;

  • 任务评论(Comment)创建与查询;

  • 事件记录(Event)创建与查询;

任务附件

    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskAttachment() {
        Map variables = Maps.newHashMap();
        variables.put("message", "my test message!");
        ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();
        taskService.createAttachment("url", task.getId(), task.getProcessInstanceId(), "name", "desc", "url/test.png");

        List taskAttachments = taskService.getTaskAttachments(task.getId());
        for(Attachment taskAttachment : taskAttachments){
            LOGGER.info("taskAttachment = {}", ToStringBuilder.reflectionToString(taskAttachment, ToStringStyle.JSON_STYLE));
        }
    }

评论与事件记录

    @Test
    @Deployment(resources = {"my-process-task.bpmn20.xml"})
    public void testTaskComment() {
        Map variables = Maps.newHashMap();
        variables.put("message", "my test message!");
        ProcessInstance processInstance = activitiRule.getRuntimeService().startProcessInstanceByKey("my-process", variables);
        TaskService taskService = activitiRule.getTaskService();
        Task task = taskService.createTaskQuery().singleResult();
        //taskService.createAttachment("url", task.getId(), task.getProcessInstanceId(), "name", "desc", "url/test.png");
        taskService.setOwner(task.getId(), "user1");
        taskService.setAssignee(task.getId(), "syc");
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"message 1");
        taskService.addComment(task.getId(),task.getProcessInstanceId(),"message 2");

        List taskComments = taskService.getTaskComments(task.getId());
        for(Comment taskComment : taskComments){
            LOGGER.info("comment1 = {}", ToStringBuilder.reflectionToString(taskComment));
            LOGGER.info("comment2 = {}", ToStringBuilder.reflectionToString(taskComment));
        }

        List taskEvents = taskService.getTaskEvents(task.getId());
        for(Event taskEvent : taskEvents){
            LOGGER.info("taskEvent = {}", ToStringBuilder.reflectionToString(taskEvent));
        }
    }

 

你可能感兴趣的:(Activiti,Activiti,TaskService)