activiti流程跳转方法

activiti流程跳转方法


import com.hhh.framework.tools.utils.StringUtil;
import org.activiti.bpmn.model.*;
import org.activiti.bpmn.model.Process;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.impl.bpmn.behavior.MultiInstanceActivityBehavior;
import org.activiti.engine.impl.interceptor.Command;
import org.activiti.engine.impl.interceptor.CommandContext;
import org.activiti.engine.ActivitiEngineAgenda;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.history.HistoryManager;
import org.activiti.engine.impl.persistence.entity.ExecutionEntity;
import org.activiti.engine.impl.persistence.entity.ExecutionEntityManager;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntityManager;
import org.activiti.engine.impl.util.ProcessDefinitionUtil;

import java.util.*;

public class JumpTestCmd implements Command<Void> {

    private String taskId;  //当前任务ID
    private String targetNodeName;    //跳转的目标节点ID
    private String operationId;     //指定处理人

    public JumpTestCmd(String taskId, String targetNodeName,String operationId) {
        this.taskId = taskId;
        this.targetNodeName = targetNodeName;
        this.operationId = operationId;
    }

    @Override
    public Void execute(CommandContext commandContext) {
//        ActivitiEngineAgenda contextAgenda = commandContext.getAgenda();
//        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
//        TaskEntity taskEntity = taskEntityManager.findById(taskId);
//        //执行实例ID
//        String executionId = taskEntity.getExecutionId();
//        //流程定义ID
//        String processDefinitionId = taskEntity.getProcessDefinitionId();
//        ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
//
//        //获取到执行实例对象,当前节点 【流程实例在执行过程中,一定是执行实例在运转,多实例情况下,指代的是三级执行实例】
//        ExecutionEntity executionEntity = executionEntityManager.findById(executionId);
//        //通过流程部署的ID获取整个流程对象
//        Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
//        Collection<FlowElement> flowElements = process.getFlowElements();
//        HistoryManager historyManager = commandContext.getHistoryManager();
//
//        //有了流程对象,可以获取到各个节点
//        FlowElement flowElement =null;
//       if(StringUtil.isNotEmpty(targetNodeName)){
//           for (FlowElement flow: flowElements) {
//               if(targetNodeName.equals(flow.getName())){
//                   flowElement = flow;
//                   break;
//               }
//           }
//       }
//
//        if (flowElement == null) {
//            throw new RuntimeException("target flow not exist !");
//        }
//        //todo 更新历史活动表 调整了顺序,在节点实例进行跳转之前记录一下历史活动表的数据
//        historyManager.recordActivityEnd(executionEntity,"移动节点");
//        executionEntity.setCurrentFlowElement(flowElement);
//        //使用contextAgenda让执行实例进行跳转
//        contextAgenda.planContinueProcessInCompensation(executionEntity);
//        //流程节点实例跳转之后,应该删除掉当前的任务
//        taskEntityManager.deleteTasksByProcessInstanceId(taskEntity.getProcessInstanceId(), "移动节点", true);
//        //更新历史任务表
//        historyManager.recordTaskEnd(taskId,"移动节点");
//        return null;

        //获取任务
        TaskEntityManager taskEntityManager = commandContext.getTaskEntityManager();
        ExecutionEntityManager executionEntityManager = commandContext.getExecutionEntityManager();
        TaskEntity currentTask = taskEntityManager.findById(taskId);
        String processDefinitionId = currentTask.getProcessDefinitionId();
        String executionId = currentTask.getExecutionId();
        ExecutionEntity executionEntity = commandContext.getExecutionEntityManager().findById(executionId);

        //根据节点名称查询对应的节点
        Process process = ProcessDefinitionUtil.getProcess(processDefinitionId);
        Collection<FlowElement> flowElements = process.getFlowElements();
        FlowElement flowElement =null;
       if(StringUtil.isNotEmpty(targetNodeName)){
           for (FlowElement flow: flowElements) {
               if(targetNodeName.equals(flow.getName())){
                   flowElement = flow;
                   break;
               }
           }
       }

        if (flowElement == null) {
            throw new RuntimeException("target flow not exist !");
        }

        // 获取目标节点的来源连线
        FlowNode targetNode = (FlowNode) flowElement;
        List<SequenceFlow> flows = targetNode.getIncomingFlows();
        if (flows == null || flows.isEmpty()) {
            throw new ActivitiException("回退错误,目标节点没有来源连线");
        }


        Object behavior = ((Activity)executionEntity.getCurrentFlowElement()).getBehavior();
        if (behavior instanceof MultiInstanceActivityBehavior) {
            //对当前节点处理
            executionEntity = executionEntity.getParent();
            executionEntityManager.deleteChildExecutions(executionEntity, "报告更改跳转", false);
            executionEntity.setActive(true);
            executionEntity.setMultiInstanceRoot(false);
            executionEntityManager.update(executionEntity);
        }else{
            //不是多实例
            //对当前节点处理
            taskEntityManager.deleteTask(currentTask, "报告更改跳转", false, false);
            HistoryManager historyManager = commandContext.getHistoryManager();
            historyManager.recordTaskEnd(taskId, "报告更改跳转");
            historyManager.recordActivityEnd(executionEntity, "报告更改跳转");
        }

        //判断当前节点的行为类,即是否多实例任务类型,主要做两件事,删除2级实例下的三级实例,然后重新设置2级实例的信息执行更新操作
        behavior = ((Activity)flowElement).getBehavior();
        if (behavior instanceof MultiInstanceActivityBehavior) {
            //设置目标节点流转
            executionEntity.setCurrentFlowElement(flows.get(0));
            //设置下一步处理人
            Map<String,Object> map = new HashMap();
            map.put("multiAssignees", Arrays.asList(new String[]{operationId}));
            executionEntity.setVariables(map);
            commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
        }else {
            //不是多实例
            //设置目标节点流转
            executionEntity.setCurrentFlowElement(flows.get(0));
            //设置下一步处理人
            Map<String,Object> map = new HashMap();
            map.put("assignee", operationId);
            executionEntity.setVariables(map);
            commandContext.getAgenda().planTakeOutgoingSequenceFlowsOperation(executionEntity, true);
        }

        return null;


    }
}

业务跳转

1、

  /**
     *  流程跳转
     * @param taskId    跳转任务id
     * @param targetNodeName    目标节点名称
     * @param operaionId 目标节点处理人
     * @param result    处理结果
     * @param opinion   处理意见
     */
    @Transactional
    public void jumpWorkflow(String taskId, String targetNodeName,String operaionId,String result,String opinion){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
//        processDisposeRecordService.save(new ProcessDisposeRecord(task, ShiroUtils.getUserId(),result,opinion));
        processEngine.getManagementService().executeCommand(new JumpTestCmd(taskId,targetNodeName,operaionId));
    }

2、

 @Transactional
    public void jumpWorkflow(ReportChange reportChange) throws Exception {
        Report report = reportService.findById(reportChange.getReportId());
        if(report== null){
            return ;
        }

        String remark = "变更前:"+reportChange.getChangeBefore() + ";变更后:" + reportChange.getChangeAfter();
        if(String.valueOf(ReportStatus.已归档.getNum()).equals(report.getState())){
            String sql = "SELECT BACKUP_TASK_ID_ from detect_report r, backup_ru_task t where r.instanceId = t.PROC_INST_ID_ AND r.id = ? ORDER BY t.CREATE_TIME_ DESC LIMIT 1";
            Query query = entityManager.createNativeQuery(sql.toString());
            query.setParameter(1, report.getId());
            String s = (String) query.getSingleResult();
            customizedTaskDao.withdrawTask(s);
            report.setState(String.valueOf(ReportStatus.valueOf("试验修改").getNum()));
            reportService.save(report);
            jcWorkflowService.jumpWorkflow(s,"试验修改",reportChange.getProcessPersonId(),"报告更新流转",remark);
        }else{
            String sql = "SELECT t.ID_ from detect_report r, act_ru_task t where r.instanceId = t.PROC_INST_ID_ AND r.id = ? LIMIT 1";
            Query query = entityManager.createNativeQuery(sql.toString());
            query.setParameter(1, report.getId());
            String s = (String) query.getSingleResult();
            jcWorkflowService.jumpWorkflow(s,"试验修改",reportChange.getProcessPersonId(),"报告更新流转",remark);
        }

    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值