当先锋百科网

首页 1 2 3 4 5 6 7
public void completeAllByTaskDefinitionKey(String processInstId,String taskDefinitionKey,Map<String,Object> variables,String taskFlag,String reason){
List<Task> taskList = taskService.createTaskQuery().processInstanceId(processInstId)
.taskDefinitionKey(taskDefinitionKey).list();
if(taskList.size()>1){//并行任务
for (Task t : taskList) {
// 完成任务(驳回完成)
zzTaskService.addComment(t.getId(),processInstId,taskFlag, reason);
taskService.complete(t.getId(),variables);
//修改历史任务 delete_reason
managementService.executeCommand(new UpdateHiTaskReasonCommand(taskList.get(0).getId(),taskFlag));
}
}else if(taskList.size()==1){//可能串行 可能单人任务
// 完成任务(驳回完成)
zzTaskService.addComment(taskList.get(0).getId(),processInstId,taskFlag, reason);
taskService.complete(taskList.get(0).getId(),variables);
//修改历史任务 delete_reason
managementService.executeCommand(new UpdateHiTaskReasonCommand(taskList.get(0).getId(),taskFlag));
completeAllByTaskDefinitionKey(processInstId,taskDefinitionKey,variables,taskFlag,reason);
}
}
@Transactional
@Override
public void jump(String taskId, String targetActivityId,String targetActivityAssignees, String reason,String taskFlag) {
Task nowTask = taskService.createTaskQuery().taskId(taskId)
.singleResult();
if ( nowTask == null ) {
throw new ZzpaasException("任务不存在!");
}
// 1. 获取流程模型实例
//BpmnModel bpmnModel = repositoryService.getBpmnModel(nowTask.getProcessDefinitionId());
ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
.getDeployedProcessDefinition(nowTask.getProcessDefinitionId());
// 2. 获取当前节点信息(强类型转换 FlowNode 获取连线)
//FlowNode curFlowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(nowTask.getTaskDefinitionKey());
ActivityImpl nowActivity = processDefinitionEntity.findActivity(nowTask.getTaskDefinitionKey());
// 3. 获取当前节点的原出口连线
//List<SequenceFlow> sequenceFlowList = curFlowNode.getOutgoingFlows();
List<PvmTransition> outgoingTransitions = nowActivity.getOutgoingTransitions();
// 4. 临时存储原出口连线(用于驳回后还原回来)
//List<SequenceFlow> oriSequenceFlows = new ArrayList<>(sequenceFlowList);
List<PvmTransition> oriOutgoingTransitions =new ArrayList<>(outgoingTransitions);
// 5. 清空原出口连线
//sequenceFlowList.clear();
nowActivity.getOutgoingTransitions().clear();
// 6. 获取驳回的目标节点信息
//FlowNode targetFlowNode = (FlowNode) bpmnModel.getFlowElement(targetActivityId);
ActivityImpl targetActivity = processDefinitionEntity.findActivity(targetActivityId);
boolean targetIsSingle=true;//目标节点是否单人任务
if(CommonUtils.isDuoren(targetActivity)){
targetIsSingle=false;
}
// 7. 获取驳回的新流向节点
//List<SequenceFlow> newSequenceFlowList = targetFlowNode.getIncomingFlows();
List<PvmTransition> newIncomingTransitions = new ArrayList<>(targetActivity.getIncomingTransitions());
// 8. 将新方向设置为当前节点出口
//curFlowNode.setOutgoingFlows(newSequenceFlowList);
nowActivity.getOutgoingTransitions().addAll(newIncomingTransitions);
// 9. 查询当前流程实例的所有任务,进行完成(若多人任务 则全部完成)
Map<String,Object> variables=new HashMap<>();
if(!targetIsSingle){
variables.put(ProcessConstants.TARGET_ASSIGNEE_TYPE_DUOREN_LIST_KEY+targetActivityId, Arrays.asList(targetActivityAssignees));
}else{
variables.put(ProcessConstants.TARGET_ASSIGNEE_TYPE_RYZD_VARIABLES_KEY+targetActivityId,targetActivityAssignees);
}
completeAllByTaskDefinitionKey(nowTask.getProcessInstanceId(),nowTask.getTaskDefinitionKey(),variables,taskFlag,reason);
// 10. 实现驳回功能后,将当前节点方向恢复为原来正常的方向
//curFlowNode.setOutgoingFlows(oriSequenceFlows);
nowActivity.getOutgoingTransitions().clear();
nowActivity.getOutgoingTransitions().addAll(oriOutgoingTransitions);
// 11. 查询驳回后的新任务
List<Task> newTaskList = taskService.createTaskQuery()
.processInstanceId(nowTask.getProcessInstanceId())
.taskDefinitionKey(targetActivityId)
.list();
if(targetIsSingle){
// 12. 设置新任务执行人
for (Task newTask : newTaskList) {
taskService.setAssignee(newTask.getId(), targetActivityAssignees);
}
}
}

这种经测试分支退主线 各种流程无法结束

由于activiti对于act_hi_taskinst表中的DELETE_REASON_列值是有限的,DELETE_REASON_类代表任务的完成原因,比如completed或者delete,因此我们在任意节点跳转或者撤销的时候,必须标记该字段,比如撤销可以标记可以定义为Revoke、跳转可以标记为jump等等。

UpdateHiTaskReasonCommand类

public class UpdateHiTaskReasonCommand implements Command<Object> {
protected String taskId;
protected String deleteReason;
public UpdateHiTaskReasonCommand(String taskId, String deleteReason) {
this.taskId = taskId;
this.deleteReason = deleteReason;
}
@Override
public Void execute(CommandContext commandContext) {
HistoricTaskInstanceEntity historicTaskInstance = commandContext
.getDbSqlSession().selectById(HistoricTaskInstanceEntity.class, taskId);
if (historicTaskInstance != null) {
historicTaskInstance.markEnded(deleteReason);
}
return null;
}
}

其中if(!targetIsSingle){
variables.put(ProcessConstants.TARGET_ASSIGNEE_TYPE_DUOREN_LIST_KEY+targetActivityId, Arrays.asList(targetActivityAssignees));
}else{
variables.put(ProcessConstants.TARGET_ASSIGNEE_TYPE_RYZD_VARIABLES_KEY+targetActivityId,targetActivityAssignees);
}
是我设置节点办理人从流程变量中获取,单人任务流程变量是user_节点id,多人是list_节点id

如图