java工作流的节点连接_工作流activiti的退回和跨节点退回实现java

本文介绍了如何使用Java Activiti工作流实现退回上一节点以及退回到特定提交人节点的功能。通过示例代码展示了如何操作流程实例、节点关系以及任务完成,以达到流程回溯的效果。
摘要由CSDN通过智能技术生成

以上是最简单的审批流程

875ff5ad00f0d327492f1948e9149e37.png

退回到上一级提交人的代码如下:

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是当前流程的定义

目前只研究到这个阶段如果需要交流,可以给我留言哦

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值