package cn.itcast.activiti; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.util.zip.ZipInputStream; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; 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.apache.commons.io.FileUtils; import org.junit.Test; /** * 測試API * @author zhaoqx * */ public class ActivitiAPITest { ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine(); //部署流程定義兩種方式 @Test public void test1() throws Exception{ DeploymentBuilder deploymentBuilder = processEngine.getRepositoryService().createDeployment(); //方式一:加載單個的流程定義文件 //deploymentBuilder.addClasspathResource("qjlc.bpmn"); //deploymentBuilder.addClasspathResource("qjlc.png"); //deploymentBuilder.deploy(); //方式二:加載zip壓縮文件 ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(new File("d:\\Desktop.zip"))); deploymentBuilder.addZipInputStream(zipInputStream); deploymentBuilder.deploy(); } //查詢最新版本的流程定義 @Test public void test2() throws Exception{ //processEngine.getXXXService().createXXXQuery(); //processEngine.getRepositoryService().createDeploymentQuery().list(); //processEngine.getRuntimeService().createProcessInstanceQuery().list(); //processEngine.getTaskService().createTaskQuery().list(); //processEngine.getHistoryService().createHistoricActivityInstanceQuery().list(); ProcessDefinitionQuery query = processEngine.getRepositoryService().createProcessDefinitionQuery(); //添加過濾條件 //query.processDefinitionNameLike("請假%"); //添加過濾條件,查詢最新的 //query.latestVersion(); //添加排序條件 query.orderByProcessDefinitionId().asc(); List<ProcessDefinition> list = query.listPage(0, 10); for (ProcessDefinition processDefinition : list) { System.out.println(processDefinition); } } //刪除流程定義 @Test public void test3(){ String deploymentId = "101";//部署id processEngine.getRepositoryService().deleteDeployment(deploymentId); } //查詢流程定義文件(bpmn、png)名稱和對應的輸入流 @Test public void test4() throws Exception{ String deploymentId = "901";//部署id List<String> list = processEngine.getRepositoryService().getDeploymentResourceNames(deploymentId); for (String name : list) { InputStream in = processEngine.getRepositoryService().getResourceAsStream(deploymentId, name); System.out.println(name); //經過一個輸出流寫 OutputStream out = new FileOutputStream(new File("d:\\"+name)); /*byte[] b = new byte[1024];//1KB in.read(b); out.write(b); out.close();*/ FileUtils.copyInputStreamToFile(in, new File("d:\\"+name)); in.close(); } } //啓動流程實例兩種方式 @Test public void test5(){ //方式一:根據流程定義的id啓動流程實例 //String processDefinitionId;//流程定義id //processEngine.getRuntimeService().startProcessInstanceById(processDefinitionId); //方式二:根據流程定義key啓動流程實例(建議使用)----自動選擇最新版本的流程定義 String processDefinitionKey = "qjlc";//流程定義key ProcessInstance processInstance = processEngine.getRuntimeService().startProcessInstanceByKey(processDefinitionKey); System.out.println(processInstance.getId()); } //查詢流程實例 @Test public void test6(){ List<ProcessInstance> list = processEngine.getRuntimeService().createProcessInstanceQuery().list(); for (ProcessInstance processInstance : list) { System.out.println(processInstance); } } //刪除流程實例 @Test public void test7(){ String processInstanceId = "1301";//流程實例id String deleteReason = "不請假了";//刪除緣由 processEngine.getRuntimeService().deleteProcessInstance(processInstanceId, deleteReason); } //查詢我的任務 @Test public void test8(){ //任務查詢對象,能夠查詢act_ru_task TaskQuery query = processEngine.getTaskService().createTaskQuery(); query.taskAssignee("張三"); //添加排序條件 query.orderByTaskCreateTime().desc(); //分頁查詢 List<Task> list = query.listPage(0, 5); for (Task task : list) { System.out.println(task); } } //辦理我的任務 @Test public void test9(){ String taskId = "1504";//任務id processEngine.getTaskService().complete(taskId); } }
l 幾個接口(和表有對應關係)java
一、Deployment對應部署表act_re_deploymentapache
二、ProcessDefinition對應流程定義表act_re_procdef框架
三、ProcessInstance對應流程實例表act_ru_execution測試
四、Task對應任務表act_ru_taskui
l 幾個Query對象(查詢對應的數據表)spa
一、DeploymentQuery對應部署表act_re_deployment插件
二、ProcessDefinitionQuery對應流程定義表act_re_procdef設計
三、ProcessInstanceQuery對應流程實例表act_ru_executioncode
四、TaskQuery對應任務表act_ru_task對象
l 幾個Service對象
一、RepositoryService:操做流程定義相關信息的、部署流程定義、刪除流程定義、查詢流程定義、查詢流程定義文件名稱和輸入流
二、RuntimeService:操做流程實例的
三、TaskService:操做任務的