Activiti6

一、Activiti介绍与搭建开发环境

二、运行官方例子

三、编写第一个Activiti程序

3.1 流程部署

代码

	/**
	 * 部署流程
	 * 涉及到的表有: 
	 * 	1.act_ge_bytearray 两条记录,一条xml数据,一条png图片信息 
	 * 	2.act_re_deployment 一条记录 
	 * 	3.act_re_procdef 一条记录
	 */
	@Test
	public void test2() {
		// 创建流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		// 得到流程存储服务组件
		RepositoryService repositoryService = engine.getRepositoryService();
		// 部署流程文件
		repositoryService.createDeployment().addClasspathResource("flow/test1.bpmn20.xml").deploy();
	}

涉及到的表

act_ge_bytearray

在这里插入图片描述

act_re_deployment

Activiti6_第1张图片

act_re_procdef

在这里插入图片描述

3.2 流程发起

代码

/**
	 * 发起流程
	 * 
	 */
	@Test
	public void test3() {
		// 创建流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RuntimeService runtimeService = engine.getRuntimeService();
		ProcessInstance instance = runtimeService.startProcessInstanceByKey("myTest1");//该key值为act_re_procdef表中的KEY_字段值
	}

涉及到的表

act_hi_actinst

在这里插入图片描述

act_hi_procinst

在这里插入图片描述

act_hi_taskinst

在这里插入图片描述

act_ru_execution

在这里插入图片描述

act_ru_task

在这里插入图片描述

3.3 进行查询任务且进行审批

	/**
	 * 查询当前任务并进行审批
	 * 
	 */
	@Test
	public void test4() {
		// 创建流程引擎
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RuntimeService runtimeService = engine.getRuntimeService();
		ProcessInstance instance = runtimeService.startProcessInstanceByKey("myTest1");//该key值为act_re_procdef表中的KEY_字段值
		TaskService taskService = engine.getTaskService();
		Task task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
		System.out.println("当前流程节点:" + task.getName());
		//审批当前流程节点
		taskService.complete(task.getId());
		
		task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
		System.out.println("当前流程节点:" + task.getName());
		//审批当前流程节点
		taskService.complete(task.getId());
		
		task = taskService.createTaskQuery().processInstanceId(instance.getId()).singleResult();
		System.out.println("当前流程节点:" + task);
		
		engine.close();
		System.exit(0);
	}

四、API

4.1 数据查询

	/**
	 * 测试给act_id_group表添加数据
	 */
	@Test
	public void test1() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		for (int i = 0; i < 10; i++) {
			Group group = identityService.newGroup(String.valueOf(i));
			group.setName("group_"+i);
			group.setType("TYPE_"+ i);
			identityService.saveGroup(group);
		}
		
		engine.close();
		System.exit(0);
	}

Activiti6_第2张图片

package com.demo;

import java.util.Iterator;
import java.util.List;

import org.activiti.engine.IdentityService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.identity.Group;
import org.junit.Test;

/**
 * Activiti数据查询
 */
public class TestActiviti02 {
	
	/**
	 * 测试给act_id_group表添加数据
	 */
	@Test
	public void test1() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		for (int i = 0; i < 10; i++) {
			Group group = identityService.newGroup(String.valueOf(i));
			group.setName("group_"+i);
			group.setType("TYPE_"+ i);
			identityService.saveGroup(group);
		}
		
		engine.close();
		System.exit(0);
	}
	
	/**
	 * 使用list()
	 */
	@Test
	public void test2() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		List<Group> groups = identityService.createGroupQuery().list();
		for (Group group : groups) {
			System.out.println(group.getId() + "---" + group.getName() + "---" + group.getType());
		}
	}
	
	/**
	 * 使用listPage()
	 * 		默认是按照主键的升序开始
	 */
	@Test
	public void test3() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		List<Group> groups = identityService.createGroupQuery().listPage(0,2);
		for (Group group : groups) {
			System.out.println(group.getId() + "---" + group.getName() + "---" + group.getType());
		}
	}
	
	/**
	 * 使用count()
	 */
	@Test
	public void test4() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		long count = identityService.createGroupQuery().count();
		System.out.println(count);
	}
	
	/**
	 * 使用desc()
	 */
	@Test
	public void test5() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		// 第一种:只有单一的进行排序
		List<Group> groups = identityService.createGroupQuery().orderByGroupName().desc().list();
		for (Group group : groups) {
			System.out.println(group.getId());
		}
		// 第二种:多种排序
		List<Group> list = identityService.createGroupQuery().orderByGroupId().desc().orderByGroupName().asc().list();
		for (Group group : list) {
			System.out.println(group.getId());
		}
	}
	
	/**
	 * 使用singleResult()
	 */
	@Test
	public void test6() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		// singleResult必须保证查询只有一条数据,否则会报错
		Group group = identityService.createGroupQuery().groupName("group_0").singleResult();
		System.out.println(group.getId());
		
	}
	
	/**
	 * 根据字段名进行查询
	 */
	@Test
	public void test7() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		List<Group> list = identityService.createGroupQuery().groupType("TYPE_2").list();
		for (Group group : list) {
			System.out.println(group.getName());
		}
	}
	
	/**
	 * 根据多字段名进行查询
	 * 	注意:多字段查询是and关系
	 */
	@Test
	public void test8() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		List<Group> list = identityService.createGroupQuery().groupType("TYPE_2").groupName("group_2").list();
		for (Group group : list) {
			System.out.println(group.getName());
		}
	}
	
	/**
	 * 原生SQL查询
	 */
	@Test
	public void test9() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		IdentityService identityService = engine.getIdentityService();
		
		List<Group> list = identityService.createNativeGroupQuery()
			.sql("select * from act_id_group where name_ = #{name}")
			.parameter("name", "group_2")
			.list();
		for (Group group : list) {
			System.out.println(group.getId());
		}
	}

}

4.2 流程部署

package com.demo;

import java.io.IOException;
import java.io.InputStream;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.Test;

/**
 * 流程文件部署
 */
public class TestActiviti03 {

	/**
	 * zip压缩包资源部署
	 * addZipInputStream
	 */
	public void test1() {
		
	}
	
	/**
	 * bpmn模型方式部署
	 * addBpmnModel
	 */
	public void test2() {
		
	}
	
	/**
	 * 忽略校验部署
	 * 	当xml文件里出现不是BPMN规范时,是会报错(发现了以元素 '{"http://www.omg.org/spec/BPMN/20100524/MODEL":abc}' 开头的无效内容)的,
	 * 	使用disableSchemaValidation可以使其部署上去
	 */
	@Test
	public void test3() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RepositoryService rs = engine.getRepositoryService();
		
		DeploymentBuilder builder = rs.createDeployment();
		builder.addClasspathResource("flow/error.bpmn20.xml");
		builder.disableSchemaValidation(); //使activiti不校验xml是否符合规范
		builder.deploy();
	}
	
	/**
	 * 流程有问题
	 * 结束节点仍然是开始节点这是流程问题之一
	 * 
	 */
	@Test
	public void test4() {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RepositoryService rs = engine.getRepositoryService();
		
		DeploymentBuilder builder = rs.createDeployment();
		builder.addClasspathResource("flow/process_error.bpmn20.xml");
		builder.disableBpmnValidation(); //不验证流程是否有问题,直接给你部署上去
		builder.deploy();
	}
	
	/**
	 * 查询流程定义 text
	 * @throws IOException 
	 */
	@Test
	public void test5() throws IOException {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RepositoryService rs = engine.getRepositoryService();
		
		DeploymentBuilder builder = rs.createDeployment();
		builder.addClasspathResource("flow/a.txt");
		Deployment deploy = builder.deploy();
		
		//数据资源获取
		InputStream is = rs.getResourceAsStream(deploy.getId(), "flow/a.txt");
		int count = is.available();
		byte[] contents = new byte[count];
		is.read(contents);
		String result = new String(contents);
		System.out.println(result);
	}
	
	/**
	 * 查询xml
	 * @throws IOException
	 */
	@Test
	public void test6() throws IOException {
		ProcessEngine engine = ProcessEngines.getDefaultProcessEngine();
		RepositoryService rs = engine.getRepositoryService();
		
		DeploymentBuilder builder = rs.createDeployment();
		builder.addClasspathResource("flow/test1.bpmn20.xml");
		Deployment deploy = builder.deploy();
		
		//数据资源获取
		ProcessDefinition def = rs.createProcessDefinitionQuery()
				.deploymentId(deploy.getId()).singleResult();
		InputStream is = rs.getProcessModel(def.getId());
		int count = is.available();
		byte[] contents = new byte[count];
		is.read(contents);
		String result = new String(contents);
		System.out.println(result);
	}
	
	/**
	 * 图片资源
	 * pdf 111页
	 */
	@Test
	public void test7() {
		
	}
	
	/**
	 * 删除部署数据
	 * 7.4.5
	 */
	@Test
	public void test8() {
		
	}
}

你可能感兴趣的:(python,开发语言)