美文网首页activiti
用户与组以及流程部署

用户与组以及流程部署

作者: 小鱼嘻嘻 | 来源:发表于2018-09-09 14:28 被阅读22次

    用户和组

    Activiti内置了一套相对简单的对用户和组的支持,可以满足基本的业务需求。其中,组可以理解为我们常说的角色,和用户的关系是多对对。

    用户

    先看一个例子:

    @Rule
        public ActivitiRule activitiRule = new ActivitiRule();
    
        /**
         * activiti  保存用户
         */
        @Test
        public void testUser() {
    
            IdentityService identityService = activitiRule.getIdentityService();
    
            User user = identityService.newUser("1");
    
            user.setFirstName("xi");
            user.setLastName("yu");
    
            identityService.saveUser(user);
    
            User userdb = identityService.createUserQuery().userId("1").singleResult();
    
            System.out.println(userdb);
    
            identityService.deleteUser("1");
    
            User user1 = identityService.createUserQuery().userId("1").singleResult();
    
            System.out.println(user1);
        }
    

    上面的代码块就是创建一个简单的用户,以及查询一个用户和删除一个用户,activiti都给我们提供好了现成的api.

    来看一个组的例子

    @Test
        public void testGroup() {
    
            IdentityService identityService = activitiRule.getIdentityService();
    
            Group group = identityService.newGroup("dept");
            group.setId("dept1");
            group.setName("leader");
    
            identityService.saveGroup(group);
    
            Group group1 = identityService.createGroupQuery().groupId("dept1").singleResult();
    
            identityService.deleteGroup("dept1");
    
            Group group2 = identityService.createGroupQuery().groupId("dept1").singleResult();
        }
    

    基本和用户的用法类似

    用户和组的关系

    了解了用户,也了解了组,现在我们要做的就是把用户和组联系起来。

     @Test
        public void testMemberShip() {
    
            IdentityService identityService = activitiRule.getIdentityService();
    
            Group group = identityService.newGroup("dept");
            group.setId("dept1");
            group.setName("leader");
            identityService.saveGroup(group);
    
            User user = identityService.newUser("1");
            user.setFirstName("xi");
            user.setLastName("yu");
            identityService.saveUser(user);
    
            identityService.createMembership("1", "dept1");
    
            User groupUser = identityService.createUserQuery().memberOfGroup("dept1").singleResult();
    
            System.out.println(groupUser);
    
            Group group1 = identityService.createGroupQuery().groupMember("1").singleResult();
    
            System.out.println(group1);
        }
    

    主要的代码就是:identityService.createMembership("1", "dept1"); 建立用户和组的关系。

    用户任务中的用户和组

    用户和组有了关系,现在应该把这个关系扩展到实际的应用当中,所谓的实际应用也就是一个个任务。

    候选组

    先来看一个简单的流程:
    先看一下xml 配置

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1533434256601" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
      <process id="user" isClosed="false" processType="None">
        <startEvent id="start" name="StartEvent"/>
        <userTask activiti:candidateGroups="dept1"  id="userGroup"/>
        <endEvent id="end" name="end"/>
        <sequenceFlow id="_3" sourceRef="start" targetRef="userGroup"/>
        <sequenceFlow id="_2" sourceRef="userGroup" targetRef="end"/>
      </process>
      <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
        <bpmndi:BPMNPlane bpmnElement="user">
          <bpmndi:BPMNShape bpmnElement="end" id="Shape-end">
            <dc:Bounds height="32.0" width="32.0" x="320.0" y="210.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="userGroup" id="Shape-userGroup">
            <dc:Bounds height="55.0" width="85.0" x="115.0" y="190.0"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="start" id="Shape-start">
            <dc:Bounds height="32.0" width="32.0" x="20.0" y="200.0"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNEdge bpmnElement="_2" id="BPMNEdge__2" sourceElement="userGroup" targetElement="end">
            <di:waypoint x="200.0" y="217.5"/>
            <di:waypoint x="320.0078144082805" y="226.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
          <bpmndi:BPMNEdge bpmnElement="_3" id="BPMNEdge__3" sourceElement="start" targetElement="userGroup">
            <di:waypoint x="52.0" y="216.0"/>
            <di:waypoint x="115.0" y="217.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
      </bpmndi:BPMNDiagram>
    </definitions>
    

    对于的流程图:


    image.png

    对应对代码:

    @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
    public class AbstractTest {
        @Rule
        public ActivitiRule activitiRule = new ActivitiRule();
        IdentityService identityService;
        RuntimeService runtimeService;
        TaskService taskService;
    
        @Test
        public void test() {
    
            identityService = activitiRule.getIdentityService();
            runtimeService = activitiRule.getRuntimeService();
            taskService = activitiRule.getTaskService();
        }
    }
    
    
    public class UserGroupTest extends AbstractTest {
    
        @Before
        public void setUp() {
            super.test();
            Group group = identityService.newGroup("dept1");
            group.setId("dept1");
            group.setType("assignment");
            group.setName("leader");
            identityService.saveGroup(group);
    
            User user = identityService.newUser("1");
            user.setFirstName("xi");
            user.setLastName("yu");
            identityService.saveUser(user);
    
            identityService.createMembership("1", "dept1");
    
            User groupUser = identityService.createUserQuery().memberOfGroup("dept1").singleResult();
            System.out.println(groupUser);
    
            Group group1 = identityService.createGroupQuery().groupMember("1").singleResult();
            System.out.println(group1);
    
        }
    
        @After
        public void afterDeal() {
            identityService.deleteMembership("1", "dept1");
            identityService.deleteUser("1");
            identityService.deleteGroup("dept1");
        }
    
        @Test
        @Deployment(resources = {"user.bpmn"})
        public void testUserTask() {
    
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("user");
    
            System.out.println(processInstance);
    
            Task task = taskService.createTaskQuery().taskCandidateUser("1").singleResult();
    
            taskService.claim(task.getId(), "1");
    
            taskService.complete(task.getId());
        }
    }
    

    关键的代码: Task task = taskService.createTaskQuery().taskCandidateUser("1").singleResult();
    taskService.claim(task.getId(), "1"); 这行代码的含义是,ID为1的用户签收了这个任务。

    候选人

    候选人和候选组类似
    对于的xml

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" xmlns:di="http://www.omg.org/spec/DD/20100524/DI" xmlns:tns="http://www.activiti.org/test" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expressionLanguage="http://www.w3.org/1999/XPath" id="m1533434256601" name="" targetNamespace="http://www.activiti.org/test" typeLanguage="http://www.w3.org/2001/XMLSchema">
      <process id="user" isClosed="false" processType="None">
        <startEvent id="start" name="StartEvent"/>
        <userTask activiti:candidateUsers="xixi,xiaoyu"  id="userGroup"/>
        <endEvent id="end" name="end"/>
        <sequenceFlow id="_3" sourceRef="start" targetRef="userGroup"/>
        <sequenceFlow id="_2" sourceRef="userGroup" targetRef="end"/>
      </process>
      <bpmndi:BPMNDiagram documentation="background=#3C3F41;count=1;horizontalcount=1;orientation=0;width=842.4;height=1195.2;imageableWidth=832.4;imageableHeight=1185.2;imageableX=5.0;imageableY=5.0" id="Diagram-_1" name="New Diagram">
        <bpmndi:BPMNPlane bpmnElement="user">
          <bpmndi:BPMNShape bpmnElement="end" id="Shape-end">
            <dc:Bounds height="32.0" width="32.0" x="320.0" y="210.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="userGroup" id="Shape-userGroup">
            <dc:Bounds height="55.0" width="85.0" x="115.0" y="190.0"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="55.0" width="85.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNShape bpmnElement="start" id="Shape-start">
            <dc:Bounds height="32.0" width="32.0" x="20.0" y="200.0"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="32.0" width="32.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNShape>
          <bpmndi:BPMNEdge bpmnElement="_2" id="BPMNEdge__2" sourceElement="userGroup" targetElement="end">
            <di:waypoint x="200.0" y="217.5"/>
            <di:waypoint x="320.0078144082805" y="226.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="-1.0" width="-1.0" x="-1.0" y="-1.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
          <bpmndi:BPMNEdge bpmnElement="_3" id="BPMNEdge__3" sourceElement="start" targetElement="userGroup">
            <di:waypoint x="52.0" y="216.0"/>
            <di:waypoint x="115.0" y="217.5"/>
            <bpmndi:BPMNLabel>
              <dc:Bounds height="0.0" width="0.0" x="0.0" y="0.0"/>
            </bpmndi:BPMNLabel>
          </bpmndi:BPMNEdge>
        </bpmndi:BPMNPlane>
      </bpmndi:BPMNDiagram>
    </definitions>
    

    对应的代码:

    
        @Before
        public void setUp() {
            super.test();
            Group group = identityService.newGroup("dept1");
            group.setId("dept1");
            group.setType("assignment");
            group.setName("leader");
            identityService.saveGroup(group);
    
            User user = identityService.newUser("1");
            user.setFirstName("xi");
            user.setLastName("yu");
            identityService.saveUser(user);
    
            identityService.createMembership("1", "dept1");
    
            User groupUser = identityService.createUserQuery().memberOfGroup("dept1").singleResult();
            System.out.println(groupUser);
    
            Group group1 = identityService.createGroupQuery().groupMember("1").singleResult();
            System.out.println(group1);
    
        }
    
        @After
        public void afterDeal() {
            identityService.deleteMembership("1", "dept1");
            identityService.deleteUser("1");
            identityService.deleteGroup("dept1");
        }
    
    
        @Test
        @Deployment(resources = {"user.bpmn"})
        public void testUserTask1() {
    
            ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("user");
    
            System.out.println(processInstance);
            // xixi签收
            Task task = taskService.createTaskQuery().taskCandidateUser("xixi").singleResult();
            taskService.claim(task.getId(), "xixi");
            taskService.complete(task.getId());
    
            //xiaoyu 签收
            Task task1 = taskService.createTaskQuery().taskCandidateUser("xiaoyu").singleResult();
            taskService.claim(task1.getId(), "xiaoyu");
            taskService.complete(task1.getId());
        }
    

    部署流程资源

    流程资源可以是工作类型的文件,在启动流程或者流程实例运行过程中会被读取。
    流程定义文件:扩展名为:bpmn20.xml 和bpmn
    流程定义的图片:用bmpn2.0规范的各种图形描绘,一般用PNG格式
    表单文件:把表单内容包材在一个文件里,扩展名为form
    流程总是在先部署一个流程定义,然后根据部署的流程定义启动流程实例。

    classpath 方式

    classpath方式就是要以class目录为基础寻找对应的资源再部署。

      //部署流程定义文件
            RepositoryService repositoryService = processEngine.getRepositoryService();
            // 读取classpath 文件
            Deployment deploy = processEngine.getRepositoryService()
                .createDeployment()
                .addClasspathResource("leave.bpmn")
                .deploy();
    

    InputStream 方式

    使用InputStream方式部署流程资源需要传入一个输出流以及资源名称,输出流的来源不限。

       String inputName = "";
            FileInputStream fileInputStream = null;
            try {
                fileInputStream = new FileInputStream(inputName);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            }
            processEngine.getRepositoryService()
                .createDeployment().addInputStream(inputName,fileInputStream)
                .deploy();
    

    字符串方式

    利用字符串方式可以直接传入纯文本作为资源的来源。

    // string
            String xml = "";
            processEngine.getRepositoryService()
                .createDeployment().addString("test.bpmn",xml)
            .deploy();
    

    zip/bar格式压缩包

    以上三种每次都只能部署一个流程资源,如果想要部署多个,就要打成bar或者zip的压缩文件。

      //bar zip
            InputStream inputStream = getClass().getClassLoader().getResourceAsStream("test.bar");
            processEngine.getRepositoryService()
                .createDeployment().addZipInputStream(new ZipInputStream(inputStream))
                .deploy();
    

    删除流程部署

    一个流程定义不能直接删除,需要通过流程定义的部署ID来删除。在执行删除的操作时,会同时删除所有和本次部署相关的资源,已经生产的流程,任务,历史的流程,任务都会删除掉。

       @Test
        public  void deleteDeployment() {
          //不做级联删除
            repositoryService.deleteDeployment("1");
            //做级联删除
            repositoryService.deleteDeployment("1",true);
        }
    

    相关文章

      网友评论

        本文标题:用户与组以及流程部署

        本文链接:https://www.haomeiwen.com/subject/jgyrgftx.html