activiti7 驳回 代码
时间: 2023-10-10 18:12:35 浏览: 131
您好,Activiti 7 中的驳回操作可以通过以下代码实现:
```java
public void rejectTask(String taskId) {
// 获取当前任务
Task task = taskService.createTaskQuery()
.taskId(taskId)
.singleResult();
if (task == null) {
throw new RuntimeException("任务不存在");
}
// 获取当前任务的流程实例
String processInstanceId = task.getProcessInstanceId();
// 获取当前任务的历史任务实例列表
List<HistoricTaskInstance> historicTaskList = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(processInstanceId)
.orderByHistoricTaskInstanceEndTime()
.asc()
.list();
// 获取需要驳回到的任务
HistoricTaskInstance targetTask = null;
for (int i = 0; i < historicTaskList.size(); i++) {
HistoricTaskInstance historicTask = historicTaskList.get(i);
if (historicTask.getId().equals(taskId)) {
if (i == 0) {
throw new RuntimeException("该任务已经是第一个任务,无法驳回");
}
targetTask = historicTaskList.get(i - 1);
break;
}
}
if (targetTask == null) {
throw new RuntimeException("未找到驳回目标任务");
}
// 驳回操作
BpmnModel bpmnModel = repositoryService.getBpmnModel(task.getProcessDefinitionId());
Process process = bpmnModel.getProcesses().get(0);
FlowNode sourceNode = (FlowNode) process.getFlowElement(task.getTaskDefinitionKey());
FlowNode targetNode = (FlowNode) process.getFlowElement(targetTask.getTaskDefinitionKey());
List<Execution> executionList = runtimeService.createExecutionQuery()
.processInstanceId(processInstanceId)
.list();
for (Execution execution : executionList) {
if (execution instanceof ExecutionEntity) {
ExecutionEntity executionEntity = (ExecutionEntity) execution;
executionEntity.setCurrentFlowElement(sourceNode);
runtimeService.createChangeActivityStateBuilder()
.processInstanceId(processInstanceId)
.moveExecutionToActivityId(executionEntity.getId(), targetNode.getId())
.changeState();
}
}
}
```
该代码实现了将当前任务驳回到上一步任务的操作,具体的实现过程请参考注释。需要注意的是,该代码中使用了 Activiti 7 中的一些 API,如 `BpmnModel`、`FlowNode` 等,如果您之前使用的是 Activiti 6,可能需要进行一些调整。
阅读全文