springboot集成flowable工作流之梅开三度,常言道温故而知新,咱也回故一下。
1、使用flowable-ui制作流程图
运行flowable-6.6.0官方demo,打开网址:http://localhost:8080/flowable-ui,输入账号admin/test登录即可,如下
进入APP.MODELER.TITLE创建流程,之后可以导出流程到项目中直接使用。
流程图如下
导出的工作流文件如下
其中学生、教师、校长节点需指定固定值,如${studentId},后面在流程操作中输入指定的id即可。
2、创建springboot工程
pom依赖
org.springframework.boot
spring-boot-starter-parent
2.6.2
...
org.flowable
flowable-spring-boot-starter
6.6.0
application.yml文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/flowable?useSSL=false&characterEncoding=UTF-8
driver-class-name: com.mysql.jdbc.Driver
username:
password:
接口
@Autowired
private RuntimeService runtimeService;
@Autowired
private HistoryService historyService;
@Autowired
private TaskService taskService;
@Autowired
private RepositoryService repositoryService;
@Resource
private ProcessEngine processEngine;
/**
* @description: 发起请假流程(请假)
* @author: ldc
* @date: 2022/1/24 15:13
*/
@RequestMapping(value = "startLeave4")
public String startLeave4(String userId) {
Map map = new HashMap<>();
map.put("studentId", userId);
map.put("leaveDays",2);
map.put("leaveReason","生病");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave4", map);
return "请假流程processInstanceId:" + processInstance.getId();
}
/**
* @description: 获取用户的任务(可以是学生、教师、校长)
* @author: ldc
* @date: 2022/1/24 15:27
*/
@RequestMapping(value = "getTasks4")
public String getTasks4(String userId) {
StringBuilder sb = new StringBuilder();
List tasks = taskService.createTaskQuery().taskAssignee(userId).orderByTaskCreateTime().desc().list();
for (Task task : tasks) {
sb.append("任务taskId: " + task.getId() + ";");
}
return sb.toString();
}
/**
* @description: 审批(同意)
* @author: ldc
* @date: 2022/1/24 15:52
*/
@RequestMapping(value = "agree4")
public String agree4(String user,String userId,String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
System.out.println("该任务对应的流程不存在");
return "该任务对应的流程不存在";
}
Map map = new HashMap<>();
map.put(user, userId);
map.put("command", "agree");
taskService.complete(taskId, map);
return "审核通过";
}
/**
* @description: 审批(拒绝)
* @author: ldc
* @date: 2022/1/24 15:53
*/
@RequestMapping(value = "refuse4")
public String refuse4(String user,String userId,String taskId) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
System.out.println("该任务对应的流程不存在");
return "该任务对应的流程不存在";
}
Map map = new HashMap<>();
map.put(user, userId);
map.put("command", "refuse");
taskService.complete(taskId, map);
return "审核驳回";
}
/**
* @description: 查看流程图,待执行的标红
* @author: ldc
* @date: 2022/1/24 17:11
*/
@RequestMapping(value = "processDiagram4")
public void genProcessDiagram4(HttpServletResponse httpServletResponse, String processInstanceId) throws Exception {
// 根据流程实例Id查询该流程实例的具体信息
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
// 获得流程定义Id
String processDefinitionId;
// 流程走完的从历史中获取
if (processInstance == null) {
//return;
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
processDefinitionId = historicProcessInstance.getProcessDefinitionId();
} else {
processDefinitionId = processInstance.getProcessDefinitionId();
}
Task task = taskService.createTaskQuery().processInstanceId(processInstanceId).singleResult();
if (task == null) {
//return;
}
// 根据流程实例Id查询该流程实例正在执行的活动
List executions = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list();
// 得到正在执行的活动Id
List activityIds = new ArrayList<>();
List flows = new ArrayList<>();
for (Execution execution : executions) {
List ids = runtimeService.getActiveActivityIds(execution.getId());
activityIds.addAll(ids);
}
// 获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
ProcessEngineConfiguration engineConfiguration = processEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator diagramGenerator = engineConfiguration.getProcessDiagramGenerator();
InputStream in = diagramGenerator.generateDiagram(bpmnModel,
"png",
activityIds,
flows,
engineConfiguration.getActivityFontName(),
engineConfiguration.getLabelFontName(),
engineConfiguration.getAnnotationFontName(),
engineConfiguration.getClassLoader(),
1.0,
true);
OutputStream out = null;
byte[] buf = new byte[1024];
int length = 0;
try {
out = httpServletResponse.getOutputStream();
while ((length = in.read(buf)) != -1) {
out.write(buf, 0, length);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
/**
* @description: 查看流程图,完成的标红
* @author: ldc
* @date: 2022/1/26 11:07
*/
@RequestMapping(value = "processDiagram44")
public void genProcessDiagram44(HttpServletResponse httpServletResponse, String processInstanceId) {
// 获得流程定义Id
String processDefinitionId;
if (this.isFinished(processInstanceId)) {
// 如果流程已经结束
// 根据流程实例Id查询该流程实例的具体信息
HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
String id = historicProcessInstance.getId();
processDefinitionId = historicProcessInstance.getProcessDefinitionId();
String startActivityId = historicProcessInstance.getStartActivityId();
String endActivityId = historicProcessInstance.getEndActivityId();
System.out.println("id:" + id + ",processDefinitionId:" + processDefinitionId + ",startActivityId:" + startActivityId + ",endActivityId:" + endActivityId);
} else {
// 如果流程没有结束
// 根据流程实例Id查询该流程实例的具体信息
ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
processDefinitionId = processInstance.getProcessDefinitionId();
String id = processInstance.getId();
String activityId = processInstance.getActivityId();
System.out.println("id:" + id + ",processDefinitionId:" + processDefinitionId + ",activityId:" + activityId);
}
List highLightedActivityIds = new ArrayList<>();
List highLightedFlows = new ArrayList<>();
// 根据流程实例Id查询该流程实例下所有历史活动实例或查询正在执行的活动实例
List historicActivityInstanceList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).orderByHistoricActivityInstanceStartTime().asc().list();
for(HistoricActivityInstance historicActivityInstance : historicActivityInstanceList){
String activityId = historicActivityInstance.getActivityId();
highLightedActivityIds.add(activityId);
if ("startEvent".equals(historicActivityInstance.getActivityType())) {
System.out.println("startEvent" + historicActivityInstance.getActivityName());
//highLightedActivityIds.add(activityId);
}
if ("userTask".equals(historicActivityInstance.getActivityType())) {
System.out.println("userTask" + historicActivityInstance.getActivityName());
//highLightedActivityIds.add(activityId);
}
if ("sequenceFlow".equals(historicActivityInstance.getActivityType())) {
System.out.println("sequenceFlow" + historicActivityInstance.getActivityName());
highLightedFlows.add(historicActivityInstance.getActivityId());
}
if ("endEvent".equals(historicActivityInstance.getActivityType())) {
System.out.println("endEvent" + historicActivityInstance.getActivityName());
//highLightedActivityIds.add(activityId);
}
}
// 根据流程定义Id获取bpmn模型
// 获取流程图
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
ProcessEngineConfiguration engineConfiguration = processEngine.getProcessEngineConfiguration();
ProcessDiagramGenerator diagramGenerator = engineConfiguration.getProcessDiagramGenerator();
InputStream in = diagramGenerator.generateDiagram(bpmnModel,
"png",
highLightedActivityIds,
highLightedFlows,
engineConfiguration.getActivityFontName(),
engineConfiguration.getLabelFontName(),
engineConfiguration.getAnnotationFontName(),
engineConfiguration.getClassLoader(),
1.0,
true);
OutputStream out = null;
byte[] buf = new byte[1024];
int legth = 0;
try {
out = httpServletResponse.getOutputStream();
while ((legth = in.read(buf)) != -1) {
out.write(buf, 0, legth);
}
} catch (IOException e) {
} finally {
IOUtils.closeQuietly(out);
IOUtils.closeQuietly(in);
}
}
测试
学生发起请假
查看用户的任务
查看当前状态的流程图
其中红框表示最新的已经执行完成的节点。
或
其中红框表示所有的已经执行完成的节点。
教师审批同意
查看流程图
校长审批同意
查看流程图
结束审批同意
查看流程图