较之上一篇单线流程的不同在于,在经理审批阶段,提供2个候选,分别为正经理和副经理,即在经理审批阶段,会删除assignee的内容,在main config中的Candidate选项中添加正经理,副经理,以逗号隔开
package com.tabchanj.activiti; import java.util.List; import org.activiti.engine.HistoryService; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.RepositoryService; import org.activiti.engine.RuntimeService; import org.activiti.engine.TaskService; import org.activiti.engine.history.HistoricActivityInstance; import org.activiti.engine.history.HistoricActivityInstanceQuery; import org.activiti.engine.repository.DeploymentBuilder; import org.activiti.engine.runtime.ProcessInstance; import org.activiti.engine.runtime.ProcessInstanceQuery; import org.activiti.engine.task.Task; import org.activiti.engine.task.TaskQuery; import org.junit.Test; /** * 测试流程规则的创建和发布 Activiti整体思路: * 1.获取核心的ProcessEngine对象 2.根据需求,获取对应的服务对象 * 3.使用服务对象的方法完成相应的功能 Activiti的查询思路: 1.获取需求获取对应的服务对象 2.适应服务对象创建查询对象 3.设置查询条件 * 3.使用查询对象进行查找 * * @author tab * */ public class FlowTest6 { private ProcessEngine engine = ProcessEngines.getDefaultProcessEngine(); /** * 创建规则:使用仓库对象,用于存储规则 * * @throws Exception */ @Test public void testDeployProcess() throws Exception { // 1,获取仓库服务对象 RepositoryService rs = engine.getRepositoryService(); // --------完成功能------------------ DeploymentBuilder builder = rs.createDeployment();// 创建部署build对象 // 获取流程规则图 builder.addClasspathResource("LeaveFlow6.bpmn"); builder.addClasspathResource("LeaveFlow6.png"); // 部署 builder.deploy(); } /** * 开启一个流程 * * @throws Exception */ @Test public void testStartProcess() throws Exception { // 获取启动服务 RuntimeService run = engine.getRuntimeService(); // 开启Process String processDefinitionId = "LeaveFlow6:1:4"; ProcessInstance instance = run.startProcessInstanceById(processDefinitionId); System.out.println("instance==" + instance); } /** * 私有任务查询 * @throws Exception */ @SuppressWarnings("deprecation") @Test public void testQueryTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //创建查询对象 TaskQuery tq = ts.createTaskQuery(); //设置过滤条件 String assignee = "正经理"; tq.taskAssignee(assignee).orderByTaskCreateTime().asc(); //执行查询 List<Task> tasks = tq.list(); System.out.println("========================"+assignee+"的任务列表==================="); for (Task task : tasks) { System.out.println("执行人:"+task.getAssignee()+"任务id:"+task.getId()+",任务名称:"+task.getName()+",任务建立时间:"+task.getCreateTime().toLocaleString()); } } /** * 公有任务查询 * @throws Exception */ @SuppressWarnings("deprecation") @Test public void testQueryPublicTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //创建查询对象 TaskQuery tq = ts.createTaskQuery(); //设置过滤条件 String candidate = "副经理"; // tq.taskCandidateUser(candidate); //执行查询 List<Task> tasks = tq.taskCandidateUser(candidate).list(); System.out.println("========================"+candidate+"的公有任务列表==================="); for (Task task : tasks) { System.out.println("执行人:"+task.getAssignee()+"任务id:"+task.getId()+",任务名称:"+task.getName()+",任务建立时间:"+task.getCreateTime().toLocaleString()); } } /** * 公有任务认领 * @throws Exception */ @Test public void testClaimTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //认领任务 String userId ="正经理"; String taskId="5002"; ts.claim(taskId, userId); } /** * 查询当前进行的执行到哪个节点了 * @throws Exception */ @Test public void testState() throws Exception { RuntimeService rs = engine.getRuntimeService(); ProcessInstanceQuery pq = rs.createProcessInstanceQuery(); String processInstanceId="2501"; ProcessInstance pi = pq.processInstanceId(processInstanceId).singleResult(); if(pi!=null){ System.out.println("当前流程在:"+pi.getActivityId()); }else{ System.out.println("流程已执行完毕!!"); } } /** * 处理任务 * @throws Exception */ @Test public void testExecuteTask() throws Exception { //获取任务服务对象 TaskService ts = engine.getTaskService(); //执行任务 // ts.complete("7502"); ts.complete("5002"); testQueryTask(); } /** * 查询历史任务 * @throws Exception */ @Test public void testHistory() throws Exception { //获取历史服务对象 HistoryService hs = engine.getHistoryService(); //创建历史查询对象 HistoricActivityInstanceQuery ha = hs.createHistoricActivityInstanceQuery(); //设置过滤 String processInstanceId= "2501"; List<HistoricActivityInstance> hiss = ha.processInstanceId(processInstanceId).orderByHistoricActivityInstanceEndTime().desc().list(); System.out.println(hiss.size()); for (HistoricActivityInstance historicActivityInstance : hiss) { System.out.println(historicActivityInstance); } } }
时间: 2024-10-04 03:39:31