Activiti5工作流笔记三

2020-09-03 15:47:19 浏览数 (1)

组任务

直接指定办理人

流程图如下:

代码语言:javascript复制
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class Task3Test {
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
        .createDeployment()
        .addClasspathResource("com/itheima10/activiti/task/task3.bpmn")
        .deploy();
    }
    
    
    /*
     * 当启动完流程实例以后,进入了电脑维修的任务
     *         <userTask id="电脑维修" name="电脑维修" activiti:candidateUsers="工程师1,工程师2,工程师3"></userTask>
     *    所以在
     *        act_ru_identitylink
     *           当当前任务结束以后,任务的候选人就从这里删除了
     *        act_hi_identitylink
     *            这两张表中分别对应的数据工程师1有两个
     *                taskId:工程师1
     *                piid:工程师1
     */
    @Test
    public void testStartPI(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        .startProcessInstanceById("task3:1:1704");
    }
    
    /**
     *  根据候选人查询组任务
     */
    @Test
    public void testQueryTaskByCandidate(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<Task> tasks = processEngine.getTaskService()
        .createTaskQuery()
        .taskCandidateUser("工程师1")
        .list();
        for (Task task : tasks) {
            System.out.println(task.getName());
        }
    }
    
    /**
     * 根据组任务查看任务的候选人
     */
    @Test
    public void testQueryCandidateByTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        List<IdentityLink> identityLinks = processEngine.getTaskService()
        .getIdentityLinksForTask("1804");
        for (IdentityLink identityLink : identityLinks) {
            System.out.println(identityLink.getUserId());
        }
    }
    
    /**
     * 候选人认领任务
     */
    @Test
    public void testClaimTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
        .claim("1804","工程师1");
    }
    
    @Test
    public void testFinishTask(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
        .complete("1804");
    }
}

使用类

流程图:

代码语言:javascript复制
package com.itheima10.activiti.task;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class MyTask4Listener implements TaskListener{
    @Override
    public void notify(DelegateTask delegateTask) {
        // TODO Auto-generated method stub
        /**
         *  组任务的候选人应该来自于数据库的某一张表
         */
        delegateTask.addCandidateUser("张三");
        delegateTask.addCandidateUser("王五");
        
        /**
         * 直接把一个组赋值给一个任务的候选人
         */
        delegateTask.addCandidateGroup("2201");// 参数为groupid
    }
}
代码语言:javascript复制
package com.itheima10.activiti.identity;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.activiti.engine.identity.User;
import org.activiti.engine.impl.persistence.entity.GroupEntity;
import org.activiti.engine.impl.persistence.entity.UserEntity;
import org.junit.Test;

public class IdentityTest {
    @Test
    public void testIdentity(){
        //创建组
        Group group = new GroupEntity();
        group.setName("咨询组");
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getIdentityService()
        .saveGroup(group);
        
        //创建组员
        User user1 = new UserEntity();
        user1.setId("咨询员1");
        
        User user2 = new UserEntity();
        user2.setId("咨询员2");
        processEngine.getIdentityService().saveUser(user1);
        processEngine.getIdentityService().saveUser(user2);
        //建立组员与组的关系
        processEngine.getIdentityService().createMembership(user1.getId(), group.getId());
        processEngine.getIdentityService().createMembership(user2.getId(), group.getId());
    }
    
    @Test
    //根据组ID删除组
    public void testDeleteGroup(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getIdentityService()
        .deleteGroup("2201");
    }
}
代码语言:javascript复制
package com.itheima10.activiti.task;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;

public class Task4Test {
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
        .createDeployment()
        .addClasspathResource("com/itheima10/activiti/task/task4.bpmn")
        .deploy();
    }
    
    
    @Test
    public void testStartPI(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        .startProcessInstanceById("task4:1:2004");
    }

}

相关表:

act_id_group 组

act_id_membership 组员

连线

之前的流程图都是错的,只是为了演示如何快速使用,下面通过连线学习如何画一个正确的流程图

例子1:

代码语言:javascript复制
package com.itheima10.activiti.sequenceFlow;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.impl.pvm.PvmTransition;
import org.activiti.engine.impl.pvm.process.ActivityImpl;
import org.junit.Test;

public class SequenceFlowTest {
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
        .createDeployment()
        .addClasspathResource("com/itheima10/activiti/sequenceFlow/sequenceflow.bpmn")
        .deploy();
    }
    
    @Test
    public void testStartPi(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        .startProcessInstanceById("sequenceflow:2:3604");
    }
    
    /*
     * 完成请假申请的任务
     */
    @Test
    public void testFinisTask(){
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("day", 2);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
        .complete("3704",variables);
    }
    
    /**
     * 完成部门经理审批的任务
     */
    @Test
    public void testFinisTask2(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
        .complete("3803");//请假天数2天,直接审批通过
    }
    
    
    /**
     * 获取部门经理审批节点的出处有几个,每一个的名称
     */
    @Test
    public void testGetOutGoing(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        /**
         * ProcessDefinitionEntity  流程定义对象
         */
        ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity)processEngine.getRepositoryService()
        .getProcessDefinition("sequenceflow:2:3604");
        /**
         *  根据流程定义对象中的某一个节点的id的值,就可以得到该节点的ActitityImpl(节点对象)
         */
        ActivityImpl activityImpl = processDefinitionEntity.findActivity("部门经理审批");
        /**
         * 该节点对象有多少sequenceFlow
         */
        List<PvmTransition> pvmTransitions = activityImpl.getOutgoingTransitions();
        for (PvmTransition pvmTransition : pvmTransitions) {
            System.out.println(pvmTransition.getId());//输出sequenceFlow的id的值
            System.out.println(pvmTransition.getDestination());//输出sequenceFlow的目标节点
        }
    }
}

例子2:

代码语言:javascript复制
package com.itheima10.activiti.sequenceFlow;

import java.util.HashMap;
import java.util.Map;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;

public class SequenceFlow2Test {
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
        .createDeployment()
        .addClasspathResource("com/itheima10/activiti/sequenceFlow/sequenceflow2.bpmn")
        .deploy();
    }
    
    @Test
    public void testStartPi(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        .startProcessInstanceById("sequence2:1:4004");
    }
    
    
    @Test
    public void testFinishTask(){
        Person person = new Person();
        person.setDay(2);
        Map<String, Object> variables = new HashMap<String, Object>();
        variables.put("person", person);
        variables.put("day", 1);
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getTaskService()
        .complete("4104", variables);
    }
}

接收活动(receiveTask,即等待活动)

不需要审批的流程,例如审批后的归档操作

例子:

代码语言:javascript复制
package com.itheima10.activiti.receivetask;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.junit.Test;

/**
 * receiveTask不需要人工干预,不需要审批,直接把当前的事情做完以后,流向下一个节点即可
 * @author zd
 *
 */
public class ReceiveTaskTest {
    @Test
    public void testDeploy(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRepositoryService()
        .createDeployment()
        .addClasspathResource("com/itheima10/activiti/receivetask/receivetask.bpmn")
        .deploy();
    }
    
    @Test
    /**
     * act_ru_task表中不会有新的记录,因为不需要人审批此流程
     * 在act_ru_execution表中可以看到当前流程实例
     */
    public void testStartPI(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        .startProcessInstanceById("receive:1:4304");
    }
    
    @Test
    public void testNextNode(){
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        processEngine.getRuntimeService()
        /**
         * 给当前的流程实例发一个信号:往下一个节点走
         */
        .signal("4401");
    }
}

0 人点赞