以上是最简单的审批流程
退回到上一级提交人的代码如下:
public class ModelRollBack {
private Logger log = LogManager.getLogger(ModelRollBack.class);//日志文件
private RuntimeService runtimeService;//
private TaskService taskService;
private RepositoryService repositoryService;
private HistoryService historyService;
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//退回上一个节点
public String rollBackWorkFlow(String taskId) {
this.taskService=processEngine.getTaskService();
this.historyService = processEngine.getHistoryService();
this.repositoryService = processEngine.getRepositoryService();
this.runtimeService = processEngine.getRuntimeService();
try {
Mapvariables;
// 取得当前任务.当前任务节点
HistoricTaskInstance currTask = historyService
.createHistoricTaskInstanceQuery().taskId(taskId)
.singleResult();
// 取得流程实例,流程实例
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(currTask.getProcessInstanceId())
.singleResult();
if (instance == null) {
log.info("流程结束");
log.error("出错啦!流程已经结束");
return "ERROR";
}
variables = instance.getProcessVariables();
// 取得流程定义
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(currTask
.getProcessDefinitionId());
if (definition == null) {
log.info("流程定义未找到");
log.error("出错啦!流程定义未找到");
return "ERROR";
}
// 取得上一步活动
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
.findActivity(currTask.getTaskDefinitionKey());
//也就是节点间的连线
ListnextTransitionList = currActivity
.getIncomingTransitions();
// 清除当前活动的出口
ListoriPvmTransitionList = new ArrayList();
//新建一个节点连线关系集合
ListpvmTransitionList = currActivity
.getOutgoingTransitions();
//
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
// 建立新出口
ListnewTransitions = new ArrayList();
for (PvmTransition nextTransition : nextTransitionList) {
PvmActivity nextActivity = nextTransition.getSource();
ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition)
.findActivity(nextActivity.getId());
TransitionImpl newTransition = currActivity
.createOutgoingTransition();
newTransition.setDestination(nextActivityImpl);
newTransitions.add(newTransition);
}
// 完成任务
Listtasks = taskService.createTaskQuery()
.processInstanceId(instance.getId())
.taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
for (Task task : tasks) {
taskService.complete(task.getId(), variables);
historyService.deleteHistoricTaskInstance(task.getId());
}
// 恢复方向
for (TransitionImpl transitionImpl : newTransitions) {
currActivity.getOutgoingTransitions().remove(transitionImpl);
}
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
log.info("OK");
log.info("流程结束");
return "SUCCESS";
} catch (Exception e) {
log.error("失败");
log.error(e.getMessage());
return "ERROR";
}
}
//回退到指定节点
public String rollBackToAssignWorkFlow(String taskId,String destTaskkey) {
this.taskService=processEngine.getTaskService();
this.historyService = processEngine.getHistoryService();
this.repositoryService = processEngine.getRepositoryService();
this.runtimeService = processEngine.getRuntimeService();
try {
Mapvariables;
// 取得当前任务.当前任务节点
HistoricTaskInstance currTask = historyService
.createHistoricTaskInstanceQuery().taskId(taskId)
.singleResult();
// 取得流程实例,流程实例
ProcessInstance instance = runtimeService
.createProcessInstanceQuery()
.processInstanceId(currTask.getProcessInstanceId())
.singleResult();
if (instance == null) {
log.info("流程结束");
log.error("出错啦!流程已经结束");
return "ERROR";
}
variables = instance.getProcessVariables();
// 取得流程定义
ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(currTask
.getProcessDefinitionId());
if (definition == null) {
log.info("流程定义未找到");
log.error("出错啦!流程定义未找到");
return "ERROR";
}
//取得当前活动节点
ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
.findActivity(currTask.getTaskDefinitionKey());
log.info("currActivity"+currActivity);
// 取得上一步活动
//也就是节点间的连线
//获取来源节点的关系
ListnextTransitionList = currActivity.getIncomingTransitions();
// 清除当前活动的出口
ListoriPvmTransitionList = new ArrayList();
//新建一个节点连线关系集合
//获取出口节点的关系
ListpvmTransitionList = currActivity
.getOutgoingTransitions();
//
for (PvmTransition pvmTransition : pvmTransitionList) {
oriPvmTransitionList.add(pvmTransition);
}
pvmTransitionList.clear();
// 建立新出口
ListnewTransitions = new ArrayList();
for (PvmTransition nextTransition : nextTransitionList) {
PvmActivity nextActivity = nextTransition.getSource();
log.info("nextActivity"+nextActivity);
log.info("nextActivity.getId()"+nextActivity.getId());
//destTaskkey
ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition)
// .findActivity(nextActivity.getId());
.findActivity(destTaskkey);
TransitionImpl newTransition = currActivity
.createOutgoingTransition();
newTransition.setDestination(nextActivityImpl);
newTransitions.add(newTransition);
}
// 完成任务
Listtasks = taskService.createTaskQuery()
.processInstanceId(instance.getId())
.taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
for (Task task : tasks) {
taskService.complete(task.getId(), variables);
historyService.deleteHistoricTaskInstance(task.getId());
}
// 恢复方向
for (TransitionImpl transitionImpl : newTransitions) {
currActivity.getOutgoingTransitions().remove(transitionImpl);
}
for (PvmTransition pvmTransition : oriPvmTransitionList) {
pvmTransitionList.add(pvmTransition);
}
log.info("OK");
log.info("流程结束");
return "SUCCESS";
} catch (Exception e) {
log.error("失败");
log.error(e.getMessage());
return "ERROR";
}
}
public static void main(String []args){
ModelRollBack back = new ModelRollBack();
//back.rollBackWorkFlow(“1102”);
back.rollBackToAssignWorkFlow(“2402”, “usertask1”);
}
}
以上代码是退回到上一节点,和退回到提交人节点的代码
代码解析 : 例如 流程开始 走到张三 ,张三提交 ,走到李四 ,李四提交走到王五 。现在王五发现有问题,需要直接退回到 张三,让张三修改之后再次提交 。
这个过程中当前节点在王五 也就是usertask3 在此过程中我们要理清 节点之间的关系
usertask1—–>usertask2
usertask2——>usertask3
usertask3——->endevent
目前对于uesrtask3来说来源 是usertask2——>usertask3
出口usertask3——>enendevent
所以我们只需要根据流程引擎中的定义将出口替换成usertask1就可以。
在此过程中 PvmTransition 只工作流引擎中 的节点关系维护
ActivityImpl是具体的节点。
ProcessDefinitionEntity是当前流程的定义
目前只研究到这个阶段如果需要交流,可以给我留言哦