/**
* 流程跳转
*
* @param taskId taskId
* @param targetTaskKey 目标节点id
* @throws DCCException 跳转任务异常
* @author huikf
* @since 2021/3/13 12:46
*/
public void jump(String taskId, String targetTaskKey) {
Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
if (task == null) {
//确定流程是否存在和是否在流转
throw new DCCException("该任务不存在,无法执行该操作");
}
//获得当前流程的活动ID
ExecutionQuery executionQuery = runtimeService.createExecutionQuery();
Execution execution = executionQuery.executionId(task.getExecutionId()).singleResult();
String activityId = execution.getActivityId();
//获取当前节点和目标节点的节点信息
FlowNode currentFlowNode = getFlowNode(task.getProcessDefinitionId(), activityId);
FlowNode targetFlowNode = getFlowNode(task.getProcessDefinitionId(), targetTaskKey);
if (targetFlowNode == null) {
throw new DCCException("目标节点不存在,无法执行该操作");
}
if (currentFlowNode.getBehavior() instanceof MultiInstanceActivityBehavior) {
throw new DCCException("当前节点为会签节点不支持该操作");
}
//获取当前节点的向下流转信息并备份
List<SequenceFlow> oldFlowNodeOutgoingFlows = currentFlowNode.getOutgoingFlows();
//清空当前节点流转走向,若清空的话流程图的获取可能会出现流转线消失的情况
//创建新的流程走向并把当前节点执行目标节点
SequenceFlow sequenceFlow = new SequenceFlow();
sequenceFlow.setId("newTempSequenceId");
sequenceFlow.setSourceFlowElement(currentFlowNode);
sequenceFlow.setTargetFlowElement(targetFlowNode);
List<SequenceFlow> newSequenceFlows = new ArrayList<>();
newSequenceFlows.add(sequenceFlow);
currentFlowNode.setOutgoingFlows(newSequenceFlows);
taskService.complete(task.getId());
taskService.deleteTask(task.getId(), false);
//最后恢复原来的流转走向
currentFlowNode.setOutgoingFlows(oldFlowNodeOutgoingFlows);
}
/**
* 获取流节点
*
* @param processDefinitionId 流程定义id
* @param activityId activityId
* @return org.activiti.bpmn.model.FlowNode
* @author huikf
* @since 2021/3/13 12:47
*/
private FlowNode getFlowNode(String processDefinitionId, String activityId) {
BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionId);
return (FlowNode) bpmnModel.getMainProcess().getFlowElement(activityId);
}
Activiti 7流程跳转
最新推荐文章于 2023-09-06 14:52:10 发布