Activiti7的配置和部分通用功能

SpringBoot+Activiti7的依赖配置

配置参见https://www.jianshu.com/p/d60d4acbb53f

Activiti7的通用功能

  1. 删除部署流程
  2. 挂起或激活流程
  3. 获取流程的所有节点的人员信息
  4. 获取流程的历史审核记录
  5. 处理task的通用方法

删除部署流程

    /**
     * 删除部署流程
     * @param deploymentId
     */
    void deleteDeployment(String deploymentId){
        repositoryService.deleteDeployment(deploymentId);
    }

挂起或激活流程

    /**
     * 挂起或激活部署流程
     * @param processDefinitionId
     */
    void suspendProcessDefinition(String processDefinitionId){
        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                .processDefinitionId(processDefinitionId).singleResult();
        if (processDefinition.isSuspended()){
            repositoryService.activateProcessDefinitionById(processDefinitionId);
        }else {
            repositoryService.suspendProcessDefinitionById(processDefinitionId);
        }
    }

获取流程的所有节点的人员信息

    /**
     * 获取流程节点人员方法,人员名称按照user1,user2,user3...依次类推
     * @param num
     * @param instanceId
     * @return
     */
    List<String> getVariablesUsers(int num,String instanceId){
        Map<String,String> map = Maps.newHashMap();
        List<String> users = Lists.newArrayList();
        for(int i = 1 ; i<=num; i++){
            String name = "user"+i;
            map.put(name,"");
        }
        List<HistoricVariableInstance> historicVariableInstances = historyService
                .createHistoricVariableInstanceQuery()
                .processInstanceId(instanceId)
                .list();
        for (HistoricVariableInstance historicVariableInstance : historicVariableInstances) {
            for(String name:map.keySet()){
                if (map.get(name).equals("") && historicVariableInstance.getVariableName().equals(name)){
                    map.put(name,(String) historicVariableInstance.getValue());
                }
            }
            boolean b = true;
            for(String value:map.values()){
                if (value.equals("") || value.isEmpty()){
                    b=false;
                    break;
                }
            }
            if (b){
                break;
            }
        }
        for(int i = 1 ; i <=num ; i++){
            users.add(map.get("user"+i));
        }
        return users;
    }

获取流程的历史审核记录

    /**
     *
     * 根据instanceId获取流程历史审核信息
     * @param instanceId
     * @return
     */
    List<HistoryParam> getProcessHistoryByInstanceId(String instanceId){
        List<HistoryParam> historyParamList = Lists.newArrayList();

        List<HistoricActivityInstance> activityInstances = historyService.createHistoricActivityInstanceQuery()
                .processInstanceId(instanceId)
                .orderByHistoricActivityInstanceEndTime()
                .desc()
                .list();

        for (HistoricActivityInstance activityInstance : activityInstances) {
            if (activityInstance.getTaskId()!= null && activityInstance.getEndTime() != null) {
                /**
                 * 获取处理信息
                 */
                HistoricVariableInstance historicVariableInstance = historyService
                        .createHistoricVariableInstanceQuery()
                        .taskId(activityInstance.getTaskId())
                        .variableName("result")
                        .singleResult();

                String val = (String) historicVariableInstance.getValue().toString();
                String info = "";
                if(val.equals("1")){
                    info = "通过";
                }else if(val.equals("0")){
                    info = "驳回";
                }

                String commentInfo = "我是***";
                List<Comment> comments = taskService.getTaskComments(activityInstance.getTaskId());
                if(comments!=null && comments.size()>0){
                    for (Comment comment : comments) {
                        commentInfo = comment.getFullMessage();
                    };
                }

                HistoryParam historyParam = new HistoryParam();
                historyParam.setEndTime(activityInstance.getEndTime());
                historyParam.setDealer(activityInstance.getAssignee());
                historyParam.setDealMethod(info);
                historyParam.setTaskName(activityInstance.getActivityName());
                if (commentInfo!="我是***"){
                    historyParam.setComment(commentInfo);
                }

                historyParamList.add(historyParam);

//                LOGGER.info("{} {} {} {}",activityInstance.getEndTime(),activityInstance.getAssignee(),info,activityInstance.getActivityName());
//                LOGGER.info("activityId = {},taskId = {},执行人 = {},处理时间 = {}",activityInstance,activityInstance.getTaskId(),activityInstance.getAssignee(),activityInstance.getEndTime());
//                LOGGER.info(historyService.createHistoricVariableInstanceQuery().taskId(activityInstance.getTaskId()).variableName("result").singleResult().toString());
//                LOGGER.info("******************************************");
            }
        }
        return historyParamList;
    }

处理task的通用方法

    /**
     * 处理task方法
     * @param instanceId
     * @param result
     * @param comment
     */
    void dealTask(String instanceId,String result,String comment){
        Task task = taskService.createTaskQuery().processInstanceId(instanceId).taskAssignee(RequestHolder.getCurrentUser().getUsername()).singleResult();
        String taskId ="";
        if (task!=null){
            taskId = task.getId();
            /**
             * 单个流程的变量使用setVariableLocal(taskId,paramName,paramValue)
             */
            taskService.setVariableLocal(taskId,"result",result);

            if (comment!=null){
                taskService.addComment(taskId,instanceId,comment);
            }
            taskService.complete(taskId);
        }
    }