Activiti教程(十一)activiti流程和业务关联
一.业务数据和流程数据关联
1.如果业务数据要和流程数据关联 需要流程实例 ProcessInstanceId 也就是说 请假表中的每条数据都要有ProcessInstanceId 字段
=================请假实例======================
2.请假表实体和请假表对应 这是独立的表不属于工作流的表
3流程图
4.服务类
package com.jnms.activiti;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.impl.identity.Authentication;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.apache.log4j.Logger;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;
public class LeaveService {
public static Logger logger = Logger.getLogger(LeaveService.class);
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
private static ObjectMapper mapper = new ObjectMapper();
@Test
public void createLeaveProcess() throws Exception{
/**
* 流程部署
*/
System.out.println("##################[流程部署]######################################");
InputStream in = this.getClass().getClassLoader().getResourceAsStream("diagrams/leaveprocess.zip");
ZipInputStream zipInputStream = new ZipInputStream(in);
Deployment deployment = processEngine.getRepositoryService()
.createDeployment()
.name("请假申请")
.addZipInputStream(zipInputStream)
.deploy();
String jdeployment = mapper.writeValueAsString(deployment);
System.out.println("=====流程部署信息:"+jdeployment);
logger.info("=====流程部署信息:"+jdeployment);
/**
* 流程赋值和启动
*/
//获取前台页面请假单数据
Leave l = new Leave();
l.setLeaveId("leaveId-2019");
l.setDays("3");
l.setUserId("张三");
l.setLeaveStatus("审批中");
String processDefinitionKey = "leaveprocess";
Map<String,Object> leavemap = new HashMap<String,Object>();
leavemap.put("LeaveId", l.getLeaveId());
leavemap.put("Days", l.getDays());
leavemap.put("UserId", l.getUserId());
leavemap.put("LeaveStatus", l.getLeaveStatus());
leavemap.put("ProcessInstanceId", l.getProcessInstanceId());
String jleavemap = mapper.writeValueAsString(leavemap);
System.out.println("=====流程启动参数:"+jleavemap);
logger.info("=====流程启动参数:"+jleavemap);
/**
* 启动请假流程
*/
System.out.println("##################[流程启动]######################################");
ProcessInstance pins = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey,leavemap);
l.setProcessInstanceId(pins.getProcessInstanceId());
String jleave = mapper.writeValueAsString(l);
System.out.println("=====请假信息:"+jleave);
logger.info("=====请假信息:"+jleave);
logger.info("==============保存请假单完成=================");
System.out.println("流程实例ID:"+pins.getProcessInstanceId()+"&流程定义ID:"+pins.getProcessDefinitionId());
logger.info("流程实例ID:"+pins.getProcessInstanceId()+"&流程定义ID:"+pins.getProcessDefinitionId());
//1.根据张三的userid查询请假单表查询出分页列状态为已发起
System.out.println("##################[张三任务]-发起申请######################################");
//张三查询个人列表
List<Task> zhagnsanTask = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(l.getUserId())
.processInstanceId(l.getProcessInstanceId())
.list();
for (Task task : zhagnsanTask) {
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
processEngine.getTaskService().complete(task.getId());
}
System.out.println("##################[李四任务]-任务待办######################################");
//李四查询代办任务
List<Task> lisiTask = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee("李四")
.processInstanceId(l.getProcessInstanceId())
.list();
for (Task task : lisiTask) {
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
}
System.out.println("##################[李四任务]-同意申请######################################");
Authentication.setAuthenticatedUserId("李四");
System.out.println("审批意见:"+"同意请假-设置请假单状态为-已完成");
processEngine.getTaskService().addComment(lisiTask.get(0).getId(), lisiTask.get(0).getProcessInstanceId(), "同意我的员工请假");
processEngine.getTaskService().complete(lisiTask.get(0).getId());
System.out.println("##################[审批意见-同意]-同意申请######################################");
}
}
5.运行结果
工程:lpms-activiti.zip
源码地址:https://pan.baidu.com/s/1rex3hdi7DGy5DIBJEc33sA