遍历flowable的所有节点:
用树状的结构,遍历所有节点,连线
废话不多说,直接贴代码
—这里是根据 ProcessDefinitionId查询到flowelement,采用递归算法,一步步往下找
/**
* 采用递归算法,获取其下所有节点的分支连线
* @param nodeId 节点id
* @param flowElements flowElement列表
* @return 对应的树状数据结构
*/
public FlowElementVo getFlowElement(String nodeId,Collection<FlowElement> flowElements,FlowElementVo flowElementVo){
if (CollectionUtils.isNotEmpty(flowElements)) {
for (FlowElement flowElement : flowElements) {
//System.out.println("节点名称:" + flowElement.getName());
if(nodeId.equals("StartEvent")){
//表示为获取其开始节点
if (flowElement instanceof StartEvent ) {
List<FlowElementVo> flowElementVoList=new ArrayList<>();
StartEvent startEvent=(StartEvent)flowElement;
System.out.println("StartEvent开始节点:" + flowElement.getName());
flowElementVo.setType("StartEvent");
flowElementVo.setNodeName(startEvent.getName());
flowElementVo.setNodeId(startEvent.getId());
List<SequenceFlow> sequenceFlowList=startEvent.getOutgoingFlows();
sequenceFlowList.forEach(sequenceFlow -> {
FlowElementVo flowElementNext= this.getFlowElement(sequenceFlow.getId(),flowElements,flowElementVo);
flowElementVoList.add(flowElementNext);
});
flowElementVo.setFlowElementVoList(flowElementVoList);
return flowElementVo;
}else{
//如果没有开始节点则表示该流程不能可视化
return null;
}
}else{
if (flowElement instanceof UserTask ) {
System.out.println("用户任务UserTask:" + flowElement.getName());
UserTask userTask=(UserTask)flowElement;
if(nodeId.equals(userTask.getId())){
FlowElementVo flowElementUserTask=new FlowElementVo();
List<FlowElementVo> flowElementVoList=new ArrayList<>();
//获取基本属性
flowElementUserTask.setType("UserTask");
flowElementUserTask.setNodeName(userTask.getName());
flowElementUserTask.setNodeId(userTask.getId());
//获取用户任务其它自定义属性参数
//获取审批岗位
//获取审批用户id
//获取下面的所有连线
List<SequenceFlow> sequenceFlowList=userTask.getOutgoingFlows();
sequenceFlowList.forEach(sequenceFlow -> {
FlowElementVo flowElementNext= this.getFlowElement(sequenceFlow.getId(),flowElements,flowElementVo);
flowElementVoList.add(flowElementNext);
});
flowElementUserTask.setFlowElementVoList(flowElementVoList);
return flowElementUserTask;
}
}
if (flowElement instanceof ExclusiveGateway ) {
ExclusiveGateway exclusiveGateway=(ExclusiveGateway)flowElement;
if(nodeId.equals(exclusiveGateway.getId())){
FlowElementVo flowElementExclusiveGateway=new FlowElementVo();
List<FlowElementVo> flowElementVoList=new ArrayList<>();
flowElementExclusiveGateway.setType("ExclusiveGateway");
System.out.println("ExclusiveGateway网关:" + flowElement.getName());
flowElementExclusiveGateway.setNodeName(exclusiveGateway.getName());
flowElementExclusiveGateway.setNodeId(exclusiveGateway.getId());
//获取下面的所有连线
List<SequenceFlow> sequenceFlowList=exclusiveGateway.getOutgoingFlows();
sequenceFlowList.forEach(sequenceFlow -> {
FlowElementVo flowElementNext= this.getFlowElement(sequenceFlow.getId(),flowElements,flowElementVo);
flowElementVoList.add(flowElementNext);
});
flowElementExclusiveGateway.setFlowElementVoList(flowElementVoList);
return flowElementExclusiveGateway;
}
}
if (flowElement instanceof ParallelGateway ) {
ParallelGateway parallelGateway=(ParallelGateway)flowElement;
System.out.println("ParallelGateway网关:" + flowElement.getName());
return flowElementVo;
}
if (flowElement instanceof SequenceFlow ) {
SequenceFlow sequenceFlow= (SequenceFlow)flowElement;
if(nodeId.equals(sequenceFlow.getId())){
FlowElementVo flowElementSequenceFlow=new FlowElementVo();
List<FlowElementVo> flowElementVoList=new ArrayList<>();
flowElementSequenceFlow.setType("SequenceFlow");
System.out.println("SequenceFlow连线:" + flowElement.getName());
//获取基本属性
flowElementSequenceFlow.setNodeName(sequenceFlow.getName());
flowElementSequenceFlow.setNodeId(sequenceFlow.getId());
//获取连线跳转条件
flowElementSequenceFlow.setCondition(sequenceFlow.getConditionExpression());
String targetRefId = sequenceFlow.getTargetRef();
//获取连线的下个节点
FlowElementVo flowElementNext=this.getFlowElement(targetRefId,flowElements,flowElementSequenceFlow);
flowElementVoList.add(flowElementNext);
flowElementSequenceFlow.setFlowElementVoList(flowElementVoList);
return flowElementSequenceFlow;
}
}
if (flowElement instanceof EndEvent ) {
EndEvent endEvent=(EndEvent)flowElement;
if(nodeId.equals(endEvent.getId())){
//表示为结束节点,不用再往下递归
FlowElementVo flowElementEndEvent=new FlowElementVo();
flowElementEndEvent.setType("EndEvent");
System.out.println("EndEvent结束节点:" + flowElement.getName());
flowElementEndEvent.setNodeName(endEvent.getName());
flowElementEndEvent.setNodeId(endEvent.getId());
return flowElementEndEvent;
}
}
}
System.out.println("结束");
}
}
return null;
}
调用方法
public AjaxResult getProcDefAll(String procDefId) throws Exception {
//获取所有节点信息
List<org.flowable.bpmn.model.Process> processes = repositoryService.getBpmnModel(procDefId).getProcesses();
System.out.println("processes size:" + processes.size());
for (Process process : processes) {
Collection<FlowElement> flowElements = process.getFlowElements();
//先获取其开始节点
FlowElementVo flowElementVo=new FlowElementVo();
FlowElementVo flowStartElement=getFlowElement("StartEvent",flowElements,flowElementVo);
flowElementVo.setType("StartEvent");
return AjaxResult.success(flowStartElement);
}
return null;
}
得到的数据结构
只截取了一部分,大概就是这种类型,FlowElementVo 是我自己定义的一个实体类,仅用于参考
public class FlowElementVo {
/**
* 节点类型
*/
private String type;
/**
* 跳转条件
*/
private String condition;
/**
* 当前节点所需审批人id
*/
private List<Long> userIds;
/**
* 当前节点所需用户名称(多个用逗号隔开)
*/
private String userName;
/**
* 节点名称
*/
private String nodeName;
/**
* 节点id
*/
private String nodeId;
/**
* 节点审批名称
*/
private String nodeFlowName;
/**
* 对应的下个节点列表
*/
private List<FlowElementVo> flowElementVoList;
}
有其它需要的参数,就可以在userTask,用户任务节点里面去获取需要的