Activit的心路歷程:獲取當前節點的上一節點【可能存在多個】的nodeId
在我的開發任務中,突然給我提出了一個待辦任務需要獲取當前任務節點上以任務節點的表單信息,剛開始搞得我有點措手不及,後來仔細是靠後,靈感一下,直接操作流程的bpmn信息就可以獲取到節點信息嘛,順着這個思路,我整理出了自己的思路:
(1)將節點大體分為兩類,一類是網關節點,另外一類就是用戶任務節點,使用List集合,將網關與用戶任務進行分類
(2)獲取上一節點,我們就需要從bpmn的連線信息入手,固定連線的targtaetRef,辨別sourceRef節點的類型,當是用戶任務時,放進 List
/**
* 獲取當前節點上一節點id【可能多個節點id】
* @param task
* @return
*/
private List<String> getFrontUserTaskIds(Task task){
String nodeId=task.getTaskDefinitionKey();
//網關集合
List<Gateway> gateways = new ArrayList<>();
//用戶任務集合
List<UserTask> userTasks = new ArrayList<>();
//網關節點id
List<String> gatewayNodelIdList = new ArrayList<>();
//用戶任務節點id
List<String> usertaskNodelIdList = new ArrayList<>();
// ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(task.getProcessInstanceId()).singleResult();
// String processDefinitionId = processInstance.getProcessDefinitionId();
String processDefinitionId = task.getProcessDefinitionId();
// ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
List<Process> processes = bpmnModel.getProcesses();
Process process = processes.get(0);
Collection<FlowElement> flowElements = process.getFlowElements();
flowElements.forEach(flowElement -> {
if(flowElement instanceof Gateway){
gatewayNodelIdList.add(flowElement.getId());
gateways.add((Gateway) flowElement);
}
if(flowElement instanceof UserTask){
usertaskNodelIdList.add(flowElement.getId());
userTasks.add((UserTask) flowElement);
}
});
List<String> frontNodeIdlist = new ArrayList<>();
for (UserTask userTask : userTasks) {
List<SequenceFlow> incomingFlows = userTask.getIncomingFlows();
for (SequenceFlow incomingFlow : incomingFlows) {
String sourceRef = incomingFlow.getSourceRef();
String targetRef = incomingFlow.getTargetRef();
if(nodeId.equals(targtaetRef)){
//當前任務的上一節點是網關
if (gatewayNodelIdList.contains(sourceRef)) {
for (Gateway gateway : gateways) {
List<SequenceFlow> incomingFlowsGateWay = gateway.getIncomingFlows();
for (SequenceFlow sequenceFlow : incomingFlowsGateWay) {
String sourceRefGateWay = sequenceFlow.getSourceRef();
String targetRefGateWay = sequenceFlow.getTargetRef();
if(sourceRef.equals(targetRefGateWay)){
frontNodeIdlist.add(sourceRefGateWay);
}
}
}
}else{
frontNodeIdlist.add(sourceRef);
}
}
}
}
return frontNodeIdlist;
}