Activiti 指定节点跳转(适合退回、撤回)流程

转载并重新整理下代码。

//方法一,画退回的路线,设置退回的条件值

Activiti 指定节点跳转(适合退回、撤回)流程_第1张图片

 

//方法二,退回到指定环节

@RequestMapping("/rollBackToAssgin.do")
public void rollBackToAssignWoekFlow(@RequestParam("processInstanceId") String processInstanceId, String destTaskkey) {
    // 取得当前任务.当前任务节点
    destTaskkey = "usertask1";
    // HistoricTaskInstance currTask = historyService.createHistoricTaskInstanceQuery().taskId(taskId).singleResult();
    Map variables;
    ExecutionEntity entity = (ExecutionEntity) runtimeService.createExecutionQuery()
.executionId(processInstanceId).singleResult();
    ProcessDefinitionEntity definition = (ProcessDefinitionEntity) ((RepositoryServiceImpl) repositoryService)
        .getDeployedProcessDefinition(entity.getProcessDefinitionId());

    variables = entity.getProcessVariables();
    //当前活动环节
    ActivityImpl currActivityImpl = definition.findActivity(entity.getActivityId());

    //目标活动节点
    ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition).findActivity(destTaskkey);
    if (currActivityImpl != null) {
        //所有的出口集合
        List pvmTransitions = currActivityImpl.getOutgoingTransitions();
        List oriPvmTransitions = new ArrayList();
        for (PvmTransition transition : pvmTransitions) {
            oriPvmTransitions.add(transition);
        }

        //清除所有出口
        pvmTransitions.clear();

        //建立新的出口
        List transitionImpls = new ArrayList();
        TransitionImpl tImpl = currActivityImpl.createOutgoingTransition();
        tImpl.setDestination(nextActivityImpl);
        transitionImpls.add(tImpl);

        List list = taskService.createTaskQuery().processInstanceId(entity.getProcessInstanceId())
            .taskDefinitionKey(entity.getActivityId()).list();
        for (Task task : list) {
            taskService.complete(task.getId(), variables);
            historyService.deleteHistoricTaskInstance(task.getId());
        }

        for (TransitionImpl transitionImpl : transitionImpls) {
            currActivityImpl.getOutgoingTransitions().remove(transitionImpl);
        }

        for (PvmTransition pvmTransition : oriPvmTransitions) {
            pvmTransitions.add(pvmTransition);
        }
    }
}

 

//方法三,强制改变当前运行中的流程环节

@RequestMapping("/rejectTaskTo.do")
public void rejectTaskTo(@RequestParam("taskId") String taskId, @RequestParam("activityId") String activityId) throws Exception {
    logger.info("开始驳回任务,任务原点:::" + taskId + ",驳回至:::" + activityId);
    HistoricTaskInstance historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
        .taskId(taskId).finished().singleResult();
    if (historicTaskInstance != null) {
        throw new Exception("任务已结束,不能进行回退操作!");
    }
    if (activityId == null || "".equals(activityId)) {
        throw new Exception("回退目标节点不能为空!");
    }
    long count = taskService.createTaskQuery().taskId(taskId).count();
    if (count == 0) {
        throw new Exception("要驳回的任务已不存在!");
    }

    Task currentTask = taskService.createTaskQuery().taskId(taskId).singleResult();
    ProcessDefinitionEntity definitionEntity = 
        (ProcessDefinitionEntity) 
            ((RepositoryServiceImpl) repositoryService).getProcessDefinition(currentTask.getProcessDefinitionId());
    String instanceId = currentTask.getProcessInstanceId();
    ActivityImpl activityImpl = definitionEntity.findActivity(activityId);
    if (activityImpl == null) {
        throw new Exception("要驳回的节点不存在!");
    }
    managerService.executeCommand(new JumpActivityCmd(activityId, instanceId)); //managerService activiti 七大服务之一
    logger.info("完成驳回操作============");
}

public class JumpActivityCmd implements Command {
    private String activityId;
    private String processInstanceId;
    private String jumpOrigin;

    public JumpActivityCmd(String activityId, String processInstanceId, String jumpOrigin) {
        this.activityId = activityId;
        this.processInstanceId = processInstanceId;
        this.jumpOrigin = jumpOrigin;
    }

    public JumpActivityCmd(String activityId, String processInstanceId) {
        this(activityId, processInstanceId, "jump");
    }

    public Object execute(CommandContext commandContext) {
        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findExecutionById(processInstanceId);
        executionEntity.destroyScope(jumpOrigin);
        ProcessDefinitionImpl processDefinition = executionEntity.getProcessDefinition();
        ActivityImpl activity = processDefinition.findActivity(activityId);
        executionEntity.executeActivity(activity);
        return executionEntity;
    }
} 
  

 

你可能感兴趣的:(Activiti,Activiti)