注意:
查询任务方式的不一样(只能通过候选者参数查询)
处理的过程中有个拾取的动作
先获取连接
private ProcessEngine processEngine ;
@Before
public void initProcessEngine(){
processEngine = ProcessEngines.getDefaultProcessEngine();
}
/**
* 流程定义(流程图的设计)好以后,需要部署
* 流程部署涉及到的几个动作和表
* 流程部署表 act_re_deployment
* 流程定义表 act_re_procdef
* 流程资源表 act_ge_bytearray
* */
@Test
public void testProcessDeploy(){
/*//1:通过流程定义相关的接口 respositoryService 来创建部署构建器
RepositoryService respositoryService =
processEngine.getRepositoryService(); //流程部署和流程定义相关的服务接口
DeploymentBuilder deploymentBuilder = respositoryService.createDeployment();
//2:添加资源
deploymentBuilder.addClasspathResource("diagrams/leave.bpmn");
deploymentBuilder.addClasspathResource("diagrams/leave.png");
deploymentBuilder.name("请假流程");
//3 :进行部署
Deployment deploy = deploymentBuilder.deploy();*/
//activiti支持链式编程
Deployment deploy = processEngine.getRepositoryService()
.createDeployment()
.addClasspathResource("diagrams/usersTaskDelegate.bpmn")
.addClasspathResource("diagrams/usersTaskDelegate.png")
.name("分配团体任务")
.deploy();
System.out.println("流程部署ID:"+deploy.getId());
System.out.println("流程部署的名称:"+deploy.getName());
}
流程图
usersTaskDelegate.bpmn代码
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
<process id="usersTaskDelegate" name="usersTaskDelegateProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="报账申请" activiti:candidateUsers="张三,李四"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="主管审批" activiti:candidateUsers="${userIds}"></userTask>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="usertask2"></sequenceFlow>
<userTask id="usertask3" name="经理审批">
<extensionElements>
<activiti:taskListener event="create" class="com.tz.activitiTest.listener.UsersTaskListener"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="usertask3"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow4" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_usersTaskDelegate">
<bpmndi:BPMNPlane bpmnElement="usersTaskDelegate" id="BPMNPlane_usersTaskDelegate">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="70.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="250.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="370.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="470.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="105.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="150.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="357.0" y="205.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="250.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="357.0" y="305.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="370.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="357.0" y="425.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="470.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
/**
*
* 流程的启动(流程实例的产生) new Class->Object
* 涉及到的表 :
* act_ru_execution 流程实例表
* act_ru_task 会产生一条待执行的任务记录
* act_hi_taskinst 也会产生一条历史任务记录(注意:endtime is null)
* 注意: 以流程定义的key启动的话,默认会进入版本最新的流程
*/
@Test
public void testStartProcess(){
String processDefinitionKey = "usersTaskDelegate";
//流程定义的key启动的话,默认会进入版本最新的流程
ProcessInstance processInstance = processEngine.getRuntimeService() //与流程实例运行相关的服务类
.startProcessInstanceByKey(processDefinitionKey);//启动流程 ,生成一个流程实例
System.out.println("流程部署的ID:"+processInstance.getDeploymentId());
System.out.println("流程定义的ID:"+processInstance.getProcessDefinitionId());
System.out.println("流程实例的ID:"+processInstance.getProcessInstanceId());
}
/**
* 流程处理过程:查询个人任务
* 处理流程的步骤:查询个人任务 完成个人任务
* 涉及到的表:act_ru_task
*
*/
@Test
public void testQueryMyTask(){
String assignee = "张三";
String processinstanceId = "130001";
List<Task> taskList= processEngine.getTaskService() //跟任务处理相关的服务类
.createTaskQuery() //创建一个任务查询
.taskAssignee(assignee) //加入查询条件
.processInstanceId(processinstanceId)
.list(); //返回形式
if(taskList!=null&&taskList.size()>0){
for(Task task:taskList){
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("任务ID:"+task.getId());//任务ID:10004
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
}
}
}
@Test
public void testQueryMyGroupTask(){
String assignee = "田七";
String processinstanceId = "130001";
List<Task> taskList= processEngine.getTaskService() //跟任务处理相关的服务类
.createTaskQuery() //创建一个任务查询
.taskCandidateUser(assignee) //加入查询条件
.processInstanceId(processinstanceId)
.list(); //返回形式
if(taskList!=null&&taskList.size()>0){
for(Task task:taskList){
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("任务ID:"+task.getId());//任务ID:130004
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
}
}
}
//因为一组人都具有任务权限 ,我们得到任务ID之后,可以根据任务ID来查询哪些人具有此次任务的处理权限
@Test
public void testQueryGroupUserByTaskId(){
String taskId ="135002";
List<IdentityLink> identityLinkList = processEngine.getTaskService()
.getIdentityLinksForTask(taskId);
if(identityLinkList!=null&&identityLinkList.size()>0){
for(IdentityLink identityLink:identityLinkList){
System.out.println(identityLink.getUserId());
System.out.println(identityLink.getTaskId());
}
}
}
//任务拾取
//我们设置的任务处理人员暂时都只是候选人 ,并不是实际处理人,必须经过任务拾取的过程来确定谁来处理任务 (task assignee)
//任务拾取的过程,就是给执行任务表指定assginee字段值的过程
@Test
public void claimTask(){
String taskId = "135002";
String userId = "田七";
processEngine.getTaskService().claim(taskId, userId);
//任务拾取以后, 可以回退给组
//processEngine.getTaskService().setAssignee(taskId, null);
//任务拾取以后,可以转给别人去处理(别人可以是组成员也可以不是)
//processEngine.getTaskService().claim(taskId, "xiaoxi");
}
/**
* 流程处理过程:完成个人任务
* 处理流程的步骤:查询个人任务 完成个人任务
* 涉及到的表:act_ru_task
*
*/
@Test
public void testCompleteMyTask(){
String taskId = "135002"; //
Map<String,Object> mapVariables = new HashMap<String,Object>();
mapVariables.put("userIds", "王五,赵六");
processEngine.getTaskService()
.complete(taskId,mapVariables);
System.out.println("第二个任务完成");
/*String taskId = "25002";
processEngine.getTaskService()
.complete(taskId);
System.out.println("审批任务完成");*/
}
/**
* 在流程的执行过程中,我们需要查询流程执行到了那一个状态
*/
@Test
public void testQueryProinstanceState(){
String processInstanceId = "115001";
ProcessInstance processInstance = processEngine.getRuntimeService()
.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
if(processInstance!=null){
System.out.println("当前流程执行到:"+processInstance.getActivityId());
}else{
System.out.println("当前流程已结束");
}
}
/**
* 当某个任务结束以后,在任务表act_ru_task中的记录会被删除 ,
* 但是act_hi_taskinst记录的endtime会加上 ,所以我们可以从这个表查询我们的任务执行历史记录
*
*
*/
@Test
public void testQueryHistoryTask(){
String assignee = "vicky";
List<HistoricTaskInstance> historyTaskList = processEngine.getHistoryService()
.createHistoricTaskInstanceQuery()
.taskAssignee(assignee)
.list();
if(historyTaskList!=null&&historyTaskList.size()>0){
for(HistoricTaskInstance hisTask:historyTaskList){
System.out.println("流程定义ID:"+hisTask.getProcessDefinitionId());
System.out.println("流程实例ID:"+hisTask.getProcessInstanceId());
System.out.println("执行对象ID:"+hisTask.getExecutionId());
System.out.println("历史任务ID:"+hisTask.getId());//任务ID:12502
System.out.println("历史任务名称:"+hisTask.getName());
System.out.println("历史任务的结束时间:"+hisTask.getEndTime());
System.out.println("---------------------");
}
}
}
/**
* 当某个流程实例执行完以后以后,在流程实例表表act_ru_execution中的记录会被删除 ,
* 但是act_hi_procinst表中记录的endtime会加上 ,所以我们可以从这个表查询我们的历史流程实例记录
*
*/
@Test
public void testQueryHistoryProcessInstance(){
String processInstanceId = "130001";
HistoricProcessInstance hisProcessInst = processEngine.getHistoryService()
.createHistoricProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
if(hisProcessInst!=null){
System.out.println("流程定义ID:"+hisProcessInst.getProcessDefinitionId());
System.out.println("执行对象ID:"+hisProcessInst.getId());
System.out.println("---------------------");
}
}
【上一篇:Activiti工作流–任务的分配–之十】
【下一篇:Activiti工作流–组任务角色的设置–之十二(完结)】