单步回退
在这篇博文的基础上整理了注释,同时做了两步回退,上代码:
/**
* 多步回退
*/
@Test
public void rollback() {
// 当前任务Id
String taskId = "7503";
// 取得当前任务.当前任务节点
HistoricTaskInstance currTask = pe.getHistoryService().createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
// 取得流程定义
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) pe.getRepositoryService()).getDeployedProcessDefinition(currTask.getProcessDefinitionId());
// 取得本步活动
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition).findActivity(currTask.getTaskDefinitionKey());
// 本节点入口
List<PvmTransition> nextTransitionList = currActivity.getIncomingTransitions();
// 本节点出口
List<PvmTransition> pvmTransitionList = currActivity.getOutgoingTransitions();
// 新建一个节点连线关系集合(存储原流程出口)
List<PvmTransition> oriPvmTransitionList = new ArrayList<PvmTransition>();
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
// 清除当前活动的出口
pvmTransitionList.clear();
// 新建一个出口节点集合(存储新流程出口)
List<TransitionImpl> newTransitions = new ArrayList<TransitionImpl>();
// 循环本节点入口
for (PvmTransition nextTransition : nextTransitionList) {
PvmActivity nextActivity = nextTransition.getSource();
//查找上一节点
ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition).findActivity(nextActivity.getId());
List<PvmTransition> nextTransitionListN = nextActivityImpl.getIncomingTransitions();
// 循环上一节点入口
for (PvmTransition nextTransitionN : nextTransitionListN) {
PvmActivity nextActivityN = nextTransitionN.getSource();
//查找上上一节点
ActivityImpl nextNextActivityImpl = ((ProcessDefinitionImpl) definition).findActivity(nextActivityN.getId());
//获取上一步活动类型
String event = checkActivityImpl(nextNextActivityImpl);
//如果上一步是开始事件,则不允许回退
if(("startEvent").equalsIgnoreCase(event)){
System.out.println("开始节点,不允许回退!");
}
// 建立新出口
TransitionImpl newTransition = currActivity.createOutgoingTransition();
newTransition.setDestination(nextNextActivityImpl);
newTransitions.add(newTransition);
}
}
pe.getTaskService().complete(taskId);
// 恢复方向
for (TransitionImpl transitionImpl : newTransitions) {
currActivity.getOutgoingTransitions().remove(transitionImpl);
}
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
}
实际上我们想退回到哪个节点就找到那个节点就行了,而到底是回退还是前进取决于我们是在入口做文章还是出口做文章。这样的话我们应该可以做到任意节点的跳转,一般的业务需求足够了。
当然这里说的任意节点撤回不包括跳出子流程,多实例撤回等等,需要进一步研究。