package com.java1234.activiti.procdef; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; import org.activiti.engine.ProcessEngine; import org.activiti.engine.ProcessEngines; import org.activiti.engine.repository.Deployment; import org.activiti.engine.repository.ProcessDefinition; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.junit.Test; /** * 查詢 流程定義 * @author Administrator * */ public class ProcessDefinitionTest { /** * 獲取默認流程引擎實例,默認讀取配置文件activiti.cfg.xml */ private ProcessEngine processEngine=ProcessEngines.getDefaultProcessEngine(); /** * 全部的流程流程實例和流程版本 */ @Test public void find全部的流程流程實例和流程版本(){ //* act_re_deployment 關聯這張表 //查詢因此的部署 List<Deployment> deploymentlist= processEngine.getRepositoryService()//獲取servi .createDeploymentQuery().list(); System.out.println(deploymentlist); List<ProcessDefinition> processDefinitionlist = processEngine.getRepositoryService().createProcessDefinitionQuery().list(); System.out.println(processDefinitionlist); } /** * 查詢流程定義,返回流程定義集合,對應表act_re_procdef */ @Test public void list(){ String processDefinitionKey="myFirstProcess"; List<ProcessDefinition> pdList= processEngine.getRepositoryService()//獲取servi .createProcessDefinitionQuery() //建立流程定義查詢 .processDefinitionKey(processDefinitionKey) //經過key查詢 .list();// for(ProcessDefinition pd:pdList){ System.out.println("ID_:"+pd.getId()); System.out.println("name_:"+pd.getName()); System.out.println("KEY_:"+pd.getKey()); System.out.println("version_:"+pd.getVersion()); System.out.println("-----------"); } /* 管理員查詢的時候 ID_:myFirstProcess:1:4 name_:My First process KEY_:myFirstProcess version_:1 ----------- ID_:myFirstProcess:2:5004 name_:My First process KEY_:myFirstProcess version_:2 ----------- */ } /** * 經過ID查詢某個流程定義 ,對應表act_re_procdef */ @Test public void getByid(){ String processDefinitionId="myFirstProcess:2:5004"; ProcessDefinition pd= processEngine.getRepositoryService()//獲取servi .createProcessDefinitionQuery() //建立流程定義查詢 .processDefinitionId(processDefinitionId) //經過key查詢 .singleResult(); System.out.println("ID_:"+pd.getId()); System.out.println("name_:"+pd.getName()); System.out.println("KEY_:"+pd.getKey()); System.out.println("version_:"+pd.getVersion()); System.out.println("-----------"); /* ID_:myFirstProcess:2:5004 name_:My First process KEY_:myFirstProcess version_:2 ----------- */ } /** * 根據流程部署id 和 資源流程名稱 來查詢,資源圖片 * @throws IOException */ @Test public void getImageById() throws IOException{ String processDefinitionId="myFirstProcess:2:5004"; InputStream inputStream= processEngine.getRepositoryService()//獲取servi .getResourceAsStream("5001", "HelloWorld.png"); //(String deploymentId, String resourceName) // 1. 把圖片存放到本地路徑/ FileUtils.copyInputStreamToFile( inputStream, new File("d:/1.png") ); } /** * 查詢最新流程版本的定義___最高最高的版本 ## 流程定義表- 查詢最新版本===查詢sql select * from act_re_procdef order by VERSION_ ASC; */ @Test public void findLastVersionDeployment(){ List<ProcessDefinition> pdList= processEngine.getRepositoryService()//獲取servi .createProcessDefinitionQuery() //建立流程定義查詢 .orderByProcessDefinitionVersion().asc() ///根據流程版本升序; .list();// // 定義有序Map,相同的Key ,加入添加的Map值後者會覆蓋 Map<String ,ProcessDefinition> map=new LinkedHashMap<String, ProcessDefinition>(); Map<String ,ProcessDefinition> hsmap=new HashMap<String, ProcessDefinition>(); for(ProcessDefinition pd: pdList){ map.put(pd.getKey(), pd); //插入新的 hsmap.put(pd.getKey(), pd); //測試hashMap } // map.k for(String key : map.keySet() ){ ProcessDefinition pd= map.get(key); System.out.println("ID_:"+pd.getId()); System.out.println("name_:"+pd.getName()); System.out.println("KEY_:"+pd.getKey()); System.out.println("version_:"+pd.getVersion()); System.out.println("-----------"); } System.out.println("edn"); /* ID_:helloworld2:3:12504 name_:My process KEY_:helloworld2 version_:3 ----------- ID_:myFirstProcess:2:5004 name_:My First process KEY_:myFirstProcess version_:2 ----------- */ } /** * 以key查詢因此的流程, 而後按id級聯刪除流程! */ @Test public void deleteByid(){ String processDefinitionKey="helloworld2"; List<ProcessDefinition> pdList= processEngine.getRepositoryService()//獲取servi .createProcessDefinitionQuery() //建立流程定義查詢 .processDefinitionKey(processDefinitionKey) //根據key查詢 .list();// for(ProcessDefinition pd:pdList){ processEngine.getRepositoryService() .deleteDeployment(pd.getDeploymentId(),true); //級聯刪除 流程 //Deletes the given deployment and cascade deletion to process instances, history process instances and jobs. } System.out.println("end"); } }