1. 程式人生 > >工作流Activity框架入門(一)

工作流Activity框架入門(一)

package activity.demo.test;

import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.impl.juel.ExpressionFactoryImpl.Profile;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.activiti.engine.task.TaskQuery;
import org.junit.Test;
import org.junit.validator.PublicClassValidator;


public class HelloWorld {
	// 使用框架的自動建表功能(不提供配置檔案)
	@Test
	public void testCreateTablesAutomaticallyWithoutConfigFiles() {
		// 建立流程引擎配置物件
		ProcessEngineConfiguration config = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
		// 設定jdbc連線引數資訊
		config.setJdbcDriver("com.mysql.jdbc.Driver");
		config.setJdbcUrl("jdbc:mysql:///eam-mirror");
		config.setJdbcUsername("root");
		config.setJdbcPassword("root");
		// 設定自動建表
		config.setDatabaseSchemaUpdate("true");
		// 使用配置物件建立流程引擎物件,建立物件過程中會自動建表
		ProcessEngine processEngine = config.buildProcessEngine();
	}

	// 使用框架的自動建表功能(提供配置檔案)
	@Test
	public void testCreateTablesAutomaticallyWithConfigFiles() {
		ProcessEngineConfiguration config = ProcessEngineConfiguration
				.createProcessEngineConfigurationFromResource("activiti-context.xml", "processEngineConfiguration");
		ProcessEngine pe = config.buildProcessEngine();
	}

	// 使用框架的自動建表功能(提供配置檔案---使用預設配置)
	@Test
	public void testCreateTablesAutomatically() {
		ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();
	}

	ProcessEngine pe = ProcessEngines.getDefaultProcessEngine();

	// 將設計的流程圖部署到資料庫中
	@Test
	public void testDeploy() {
		// 建立部署構建器物件,用於載入流程定義檔案(UserInfoAudit.bpmn,UserInfoAudit.myProcess.png),部署流程定義
		DeploymentBuilder deploymentBuilder = pe.getRepositoryService().createDeployment();
		deploymentBuilder.addClasspathResource("UserInfoAudit.bpmn");
		Deployment deployment = deploymentBuilder.deploy();
		System.out.println(deployment.getId());
	}

	// 查詢流程定義
	@Test
	public void testQueryProcessDefinition() {
		// 流程定義查詢物件,用於查詢流程定義表----act_re_procdef
		ProcessDefinitionQuery query = pe.getRepositoryService().createProcessDefinitionQuery();
		// 新增過濾條件,取最新版本
		query.latestVersion();
		// query.processDefinitionId(processDefinitionId)
		// query.processDefinitionKey(processDefinitionKey);
		// 新增排序條件
		query.orderByProcessDefinitionVersion().desc();

		// 新增分頁條件
		// query.listPage(firstResult, maxResults);

		// 查詢所有流程
		List<ProcessDefinition> processDefinitionList = query.list();
		for (ProcessDefinition pd : processDefinitionList) {
			System.out.println(pd.getId());
		}

	}
	// 根據流程定義啟動流程例項----操作的資料表:act_ru_execution act_ru_task
	@Test
	public void testStartProcess(){
		String processDefinitionId = "UserAuditProcess:2:504";
		//根據流程例項ID去啟動流程
		ProcessInstance pInstance = pe.getRuntimeService().startProcessInstanceById(processDefinitionId);
		System.out.println(pInstance.getId());
	}
	
	//查詢任務列表
	@Test
	public void testQueryTaskList(){
		//建立任務查詢物件,查詢表act_ru_task
		TaskQuery query = pe.getTaskService().createTaskQuery();
		String assignee ="張三";
		//新增過濾條件
		query.taskAssignee(assignee);
		//排序
		query.orderByTaskCreateTime().desc();
		List<Task> taskList = query.list();
		for(Task task : taskList){
			System.out.println("taskId:"+task.getId()+",taskName:"+task.getName());
		}
	}
	//辦理任務
	//辦理個人任務,操作的表是act_ru_execution,act_ru_task
	@Test
	public void testExecuteTask(){
		String taskId= "604";
		pe.getTaskService().complete(taskId);
	}
	
}