SpringBoot整合Activiti 6.0 (动态自定义流程)

 

本文非完整代码,仅提供动态自定义流程的部分方法和思路

 

1. Activiti工具类

public class ActivitiUtil {

    private static ProcessEngine getProcessEngine() {
        return ProcessEngines.getDefaultProcessEngine();
    }

    private static RepositoryService getRepositoryService() {
        return getProcessEngine().getRepositoryService();
    }

    private static TaskService getTaskService() {
        return getProcessEngine().getTaskService();
    }

    private static RuntimeService getRuntimeService() {
        return getProcessEngine().getRuntimeService();
    }

    private static HistoryService getHistoryService() {
        return getProcessEngine().getHistoryService();
    }

    private static IdentityService getIdentityService() {
        return getProcessEngine().getIdentityService();
    }



   
    /**
     * 创建任务节点
     * 多人审批
     */
    public static UserTask createUsersTask(String id, String name, List<String> assignee){
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        userTask.setCandidateUsers(assignee);
        return userTask;
    }

   /**
     * 创建任务节点
     * 单人审批
     */
    public static UserTask createUserTask(String id, String name, String assignee) {
        UserTask userTask = new UserTask();
        userTask.setName(name);
        userTask.setId(id);
        userTask.setAssignee(assignee);
        return userTask;
    }


    /**
     * 连线
     * @param from
     * @param to
     * @return
     */
    public static SequenceFlow createSequenceFlow(String from, String to) {
        SequenceFlow flow = new SequenceFlow();
        flow.setSourceRef(from);
        flow.setTargetRef(to);
        return flow;
    }

    /**
     * 开始节点
     * @return
     */
    public static StartEvent createStartEvent() {
        StartEvent startEvent = new StartEvent();
        startEvent.setId("startEvent");
        startEvent.setName("start");
        return startEvent;
    }

    /**
     * 结束节点
     * @return
     */
    public static EndEvent createEndEvent() {
        EndEvent endEvent = new EndEvent();
        endEvent.setId("endEvent");
        endEvent.setName("end");
        return endEvent;
    }



    /**
     * 申请人已申请任务(完成状态)[学习使用]
     * @return
     */
    public Object queryApply(String  user){
        List<HistoricProcessInstance> hisProInstance = getHistoryService().createHistoricProcessInstanceQuery()
                .processDefinitionKey(processDefinitionKey).startedBy(user).finished()
                .orderByProcessInstanceEndTime().desc().list();
        for (HistoricProcessInstance hisInstance : hisProInstance) {
            System.out.println("发起人 :"+hisInstance.getStartUserId());
            System.out.println("发起时间 :"+hisInstance.getStartTime());
        }
        return "已申请任务";
    }



   /**
     * 审批人已办理任务(完成状态)[学习使用]
     * @return
     */
    public Object queryFinished(String  user){
            List<HistoricProcessInstance> hisProInstance = getHistoryService().createHistoricProcessInstanceQuery()
                    .processDefinitionKey(processDefinitionKey).involvedUser(user).finished()
                    .orderByProcessInstanceEndTime().desc().list();
            for (HistoricProcessInstance hisInstance : hisProInstance) {
                List<HistoricTaskInstance> hisTaskInstanceList = getHistoryService().createHistoricTaskInstanceQuery()
                        .processInstanceId(hisInstance.getId()).processFinished()
                        .taskAssignee(user)
                        .orderByHistoricTaskInstanceEndTime().desc().list();
                boolean isMyAudit = false;
                for (HistoricTaskInstance taskInstance : hisTaskInstanceList) {
                    if (taskInstance.getAssignee().equals(user)) {
                        isMyAudit = true;
                    }
                }
                if (!isMyAudit) {
                    continue;
                }
                System.out.println("申请人 :"+hisInstance.getStartUserId());
                System.out.println("开始时间 :"+hisInstance.getStartTime());
                System.out.println("结束时间 :"+hisInstance.getEndTime());
            }
            return "已办理任务";
        }



   /**
     * 发起人查询执行中的任务[学习使用]
     * @return
     */
    public static Object queryNow(String user) {
        List<ProcessInstance> instanceList = getRuntimeService().createProcessInstanceQuery().startedBy(user).list();
        for (ProcessInstance instance : instanceList) {
            System.out.println("申请人 :" + instance.getStartUserId());
            System.out.println("开始时间 :" + instance.getStartTime());
        }
        return "查询执行中的任务";
    }



   /**
     * 根据人员查询待审批任务[学习使用]
     * @return
     */
    public static Object findUnApprove(String assignee) {
        List<Task> list = getTaskService().createTaskQuery().taskCandidateOrAssigned(assignee).list();
        if (list != null || list.size() > 0) {
            for (Task task : list) {
                HistoricProcessInstance historicProcessInstance =
                        getHistoryService().createHistoricProcessInstanceQuery()
                                .processInstanceId(task.getProcessInstanceId())
                                .singleResult();
                String user = historicProcessInstance.getStartUserId();
                Date date = (Date) getTaskService().getVariable(task.getId(), "请假日期");
                Integer day = (Integer) getTaskService().getVariable(task.getId(), "请假天数");
                String ms = (String) getTaskService().getVariable(task.getId(), "请假原因");
                System.out.println("申请人" + user + "请假日期" + date + "/" + "请假天数 :" + day + "/" + "请假原因 :" + ms);
                System.out.println("任务名称 :" + task.getName());
                System.out.println("任务创建时间 :" + task.getCreateTime());
            }
        }
        return "待审批";
    }

}

 

2.部署流程

public Integer addActiviti(ProcessFlow processFlow) {
        try {
            BpmnModel model = new BpmnModel();
            Process process = new Process();
            model.addProcess(process);

            /**
             *process的id不能以数字开头
             */
            process.setId('A' + processFlow.getId());
            List<String> users = null;

            /**
             *获取流程的节点数量
             */
            int size = processFlow.getProcessNodes().size();
            process.addFlowElement(ActivitiUtil.createStartEvent());

            /**
             *生成流程
             */
            for (int i = 0, j = size; i < j; i++) {
                users = new ArrayList<>();
                for (AuditProcessNodeApprover approver : processFlow.getProcessNodes().get(i).getApprovers()) {
                    users.add(approver.getApproved());
                }
                process.addFlowElement(ActivitiUtil.createUsersTask(ProcessIDPrefix + processFlow.getProcessNodes().get(i).getId(), processFlow.getProcessNodes().get(i).getName(), users));
                if (i == 0)
                    process.addFlowElement(ActivitiUtil.createSequenceFlow("startEvent", ProcessIDPrefix + processFlow.getProcessNodes().get(i).getId()));
                else {
                    process.addFlowElement(ActivitiUtil.createSequenceFlow(ProcessIDPrefix + processFlow.getProcessNodes().get(i - 1).getId(), ProcessIDPrefix + processFlow.getProcessNodes().get(i).getId()));
                }
                if (i == size - 1)
                    process.addFlowElement(ActivitiUtil.createSequenceFlow(ProcessIDPrefix + processFlow.getProcessNodes().get(i).getId(), "endEvent"));
            }
            process.addFlowElement(ActivitiUtil.createEndEvent());


            /**
             * 生成图形信息
             */
            new BpmnAutoLayout(model).execute();

            //将流程部署到引擎
            Deployment deployment = getRepositoryService().createDeployment()
                    .addBpmnModel("ABC" + process.getId() + ".bpmn", model).name(processFlow.getName())
                    .deploy();

        } catch (Exception e) {
            throw new CustomException(e.getLocalizedMessage());
        }
        return 1;
    }

 

3.开始审批流程

 public Object beginActiviti(String creater, String processId) {
        
        /**
         *设置发起人
         */
        getIdentityService().setAuthenticatedUserId(creater);
        //启动流程实例
        ProcessInstance processInstance = getRuntimeService().startProcessInstanceByKey('A' + processId);

        Task task = getTaskService().createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        if (task == null) throw new RuntimeException("发起审批失败");
        ProcessFlow flow = flowRepository.getOne(processId);
        if (flow == null) throw new RuntimeException("对应审批流程为空");
        List<ProcessNodeApprover> processNodeApprovers= processNodeApproverRepository.findByFlowid(flow.getId());
        if (processNodeApprovers==null||processNodeApprovers.size()<1){
            throw new RuntimeException("对应审批人为空");
        }
}

 

4.审批

/**
     * 进行审批
     *
     * @param assignee  审批人
     * @param msg       审批意见
     * @param isAgree   是否同意 1 同意 0 拒绝
     * @param taskId    任务id
     * @param processId 流程id
     * @return
     */
 public Object approve(String assignee, String msg, Integer isAgree, String taskId, String processId) {
        Task task = getTaskService().createTaskQuery().taskId(taskId).singleResult();
        Employee assigneeEmployee = employeeRepository.getOne(assignee);
        if (assigneeEmployee == null)
            throw new RuntimeException("审批人对应得职员信息不存在!");
   
        //拒绝,结束流程
        if (isAgree == 0) {
                BpmnModel bpmnModel = getRepositoryService().getBpmnModel(task.getProcessDefinitionId());
                Execution execution = getRuntimeService().createExecutionQuery().executionId(task.getExecutionId()).singleResult();
                String activitId = execution.getActivityId();
                FlowNode flowNode = (FlowNode) bpmnModel.getMainProcess().getFlowElement(activitId);
                //清理流程未执行节点
                flowNode.getOutgoingFlows().clear();
                //建立新方向
                List<SequenceFlow> newSequenceFlowList = new ArrayList<>();
                SequenceFlow newSequenceFlow = new SequenceFlow();
                newSequenceFlow.setId(CommonUtil.getUUID());
                newSequenceFlow.setSourceFlowElement(flowNode);
                newSequenceFlow.setTargetFlowElement(ActivitiUtil.createEndEvent());
                newSequenceFlowList.add(newSequenceFlow);
                flowNode.setOutgoingFlows(newSequenceFlowList);
               
             }   
                
            //同意,继续下一节点
           else if (isAgree == 1) {
                getTaskService().addComment(task.getId(), task.getProcessInstanceId(), msg);
                getTaskService().complete(task.getId());
           }         
       
        return ResultUtil.success();
    }

5.删除流程

 getRepositoryService().deleteDeployment(deploymentId, true);
 //true  级联删除

6.根据启动key获取最新流程

 List<ProcessDefinition> list = getRepositoryService()
            .createProcessDefinitionQuery().processDefinitionKey(processId)
            .orderByProcessDefinitionVersion().desc()//使用流程定义的版本降序排列
            .list();

 

评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值