Activiti current process highlighted

table of Contents

 

Preface:

Function (1) to highlight the implementation of the process

Function (2) suspend a workflow instance

Function (3) activating the workflow instance

 


Preface:

Prior to this, small written two articles, one is what is Activiti (Beginners)  , the introductory chapter by the end of 19 I wrote, I did give myself a goal, the article must Activiti workflow was not entirely the work flow fully digested, 20,200,327 Activiti actual articles I wrote  , I am very happy, because this is a complete blog, I would like to add the contents of this article did not say that some of the articles.

 

 

I know not a lot, but I have been studying the way.

 

Function (1) to highlight the implementation of the process

1.1 First First draw a simple flow chart

Generating a workflow instance 1.2

    /**
     * 1.创建一个流程实例
     */
    @GetMapping(value = "/createLeaveFlow")
    public void createLeaveFlow() throws IOException {
        //1.举个例子,soup_tang 需要请假 ,我在页面上添加请假申请,任务申请人是soup_tang,
        // 我选择审核人为汤总,请汤总给我审核这个请假流程。
        String checkPeople = "汤总";
        activitiService.createLeaveWorkFlow(checkPeople);
    }

 

 @Override
    public void createLeaveWorkFlow(String checkPeople) throws IOException {
            //先判断这个流程文件有没有部署(只需部署一次)
            Model modelData = repositoryService.getModel("1");
            ObjectNode modelNode = (ObjectNode) new ObjectMapper()
                    .readTree(repositoryService.getModelEditorSource(modelData.getId()));
            byte[] bpmnBytes = null;
            BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            bpmnBytes = new BpmnXMLConverter().convertToXML(bpmnModel);

            String processName = modelData.getName() + ".bpmn20.xml";

            Deployment deployment = repositoryService.createDeployment()
                    .name(modelData.getName()).addString(processName, new String(bpmnBytes, "UTF-8"))
                    .deploy();
            //获取流程定义
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();
            //启动流程定义,返回流程实例
            Map<String,Object> map = new HashMap<>();
            map.put("checkPeople",checkPeople);
            ProcessInstance pi = runtimeService.startProcessInstanceById(processDefinition.getId(),map);
            System.out.println("流程定义的ID:" + pi.getProcessDefinitionId());
            System.out.println("流程实例的ID:" + pi.getId());

    }

Get the process instance id

FIG 1.3 shows a flowchart highlighting execution state

ActivitiController

     @Autowired
    private HistoryService historyService;

    @Autowired
    ProcessEngineConfiguration processEngineConfiguration;

    @Autowired
    ProcessEngineFactoryBean processEngine;


 /**
     * 查询流程实例 已高亮的形式流程执行状态
     * @param response
     * @throws Exception
     */
    @GetMapping("/queryActivitiByProcessInstanceId")
    public void queryProPlan(HttpServletResponse response) throws Exception {
        //实例id
        String processInstanceId = "10";
        //获取历史流程实例
        HistoricProcessInstance processInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        //获取流程图
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processInstance.getProcessDefinitionId());
        processEngineConfiguration = processEngine.getProcessEngineConfiguration();
        Context.setProcessEngineConfiguration((ProcessEngineConfigurationImpl) processEngineConfiguration);

        ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
        ProcessDefinitionEntity definitionEntity = (ProcessDefinitionEntity) repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());

        List<HistoricActivityInstance> highLightedActivitList = historyService.createHistoricActivityInstanceQuery().processInstanceId(processInstanceId).list();
        //高亮环节id集合
        List<String> highLightedActivitis = new ArrayList<String>();

        //高亮线路id集合
        List<String> highLightedFlows = getHighLightedFlows(definitionEntity, highLightedActivitList);

        for (HistoricActivityInstance tempActivity : highLightedActivitList) {
            String activityId = tempActivity.getActivityId();
            highLightedActivitis.add(activityId);
        }

        //中文显示的是口口口,设置字体就好了
        InputStream imageStream = diagramGenerator.generateDiagram(bpmnModel, "png", highLightedActivitis, highLightedFlows, "", "宋体", "宋体", null, 1.0);
        //单独返回流程图,不高亮显示
        byte[] b = new byte[1024];
        int len;
        while ((len = imageStream.read(b, 0, 1024)) != -1) {
            response.getOutputStream().write(b, 0, len);
        }

    }
 /**
     * 获取需要高亮的线
     *
     * @param processDefinitionEntity
     * @param historicActivityInstances
     * @return
     */
    private List<String> getHighLightedFlows(
            ProcessDefinitionEntity processDefinitionEntity,
            List<HistoricActivityInstance> historicActivityInstances) {

        // 用以保存高亮的线flowId
        List<String> highFlows = new ArrayList<String>();
        // 对历史流程节点进行遍历
        for (int i = 0; i < historicActivityInstances.size() - 1; i++) {
            ActivityImpl activityImpl = processDefinitionEntity
                    .findActivity(historicActivityInstances.get(i)
                            .getActivityId());// 得到节点定义的详细信息
            // 用以保存后需开始时间相同的节点
            List<ActivityImpl> sameStartTimeNodes = new ArrayList<ActivityImpl>();
            ActivityImpl sameActivityImpl1 = processDefinitionEntity
                    .findActivity(historicActivityInstances.get(i + 1)
                            .getActivityId());
            // 将后面第一个节点放在时间相同节点的集合里
            sameStartTimeNodes.add(sameActivityImpl1);
            for (int j = i + 1; j < historicActivityInstances.size() - 1; j++) {
                // 后续第一个节点
                HistoricActivityInstance activityImpl1 = historicActivityInstances
                        .get(j);
                // 后续第二个节点
                HistoricActivityInstance activityImpl2 = historicActivityInstances
                        .get(j + 1);
                if (activityImpl1.getStartTime().equals(
                        activityImpl2.getStartTime())) {
                    // 如果第一个节点和第二个节点开始时间相同保存
                    ActivityImpl sameActivityImpl2 = processDefinitionEntity
                            .findActivity(activityImpl2.getActivityId());
                    sameStartTimeNodes.add(sameActivityImpl2);
                } else {
                    // 有不相同跳出循环
                    break;
                }
            }
            List<PvmTransition> pvmTransitions = activityImpl
                    .getOutgoingTransitions();// 取出节点的所有出去的线
            for (PvmTransition pvmTransition : pvmTransitions) {
                // 对所有的线进行遍历
                ActivityImpl pvmActivityImpl = (ActivityImpl) pvmTransition
                        .getDestination();
                // 如果取出的线的目标节点存在时间相同的节点里,保存该线的id,进行高亮显示
                if (sameStartTimeNodes.contains(pvmActivityImpl)) {
                    highFlows.add(pvmTransition.getId());
                }
            }
        }
        return highFlows;
    }

 

Access this interface in the browser:   HTTP: // localhost: 8005 / queryActivitiByProcessInstanceId

Can get this picture below, this picture can also be displayed on the page, this will need to define themselves.

 

Function (2) suspend a workflow instance

Workflow instance will execute normally completed in accordance with certain rules, sometimes need to abort the process, this time maybe you can use the following methods.

Click aborted after the normal process, the implementation process will stop, only the activation process, the process will continue.

   @Autowired
    private RuntimeService runtimeService;

 /**
     * 挂起工作流
     * @return
     */
    @GetMapping(value = "/hangUpBusinessWorkFlow")
    public void hangUpBusinessWorkFlow() {
        //挂起一个流程
        String processInstanceId = "10";
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if(processInstance!=null) {
            runtimeService.suspendProcessInstanceById(processInstanceId);
        }
    }

Function (3) activating the workflow instance

  @Autowired
    private RuntimeService runtimeService;

 /**
     * 激活工作流
     */
    @GetMapping(value = "/activateBusinessWorkFlow")
    public void activateBusinessWorkFlow() {
        //激活一个流程
        String processInstanceId = "10";
        ProcessInstance processInstance = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult();
        if(processInstance!=null) {
            runtimeService.activateProcessInstanceById(processInstanceId);
        }
    }

 

This article on it here, if you feel good to write small words, small series might give a praise Oh, welcome message exchange comments area, come together!

 

 

Published 73 original articles · won praise 59 · views 30000 +

Guess you like

Origin blog.csdn.net/tangthh123/article/details/105141755