1. 程式人生 > >Activiti進階(三)——流程定義的CRUD

Activiti進階(三)——流程定義的CRUD

     流程定義的RCUD,也就是對流程的增刪該查,這篇博文簡單的為大家介紹一下使用Activiti的api對流程定義進行增刪改查操作。

一、檢視流程定義

	// 查詢流程定義
	@Test
	public void findProcessDifinitionList() {
		List<ProcessDefinition> list = processEngine.getRepositoryService()
				.createProcessDefinitionQuery()
				// 查詢條件
				.processDefinitionKey("myMyHelloWorld")// 按照流程定義的key
				// .processDefinitionId("helloworld")//按照流程定義的ID
				.orderByProcessDefinitionVersion().desc()// 排序
				// 返回結果
				// .singleResult()//返回惟一結果集
				// .count()//返回結果集數量
				// .listPage(firstResult, maxResults)
				.list();// 多個結果集
		
		if(list!=null && list.size()>0){
			for(ProcessDefinition pd:list){
				System.out.println("流程定義的ID:"+pd.getId());
				System.out.println("流程定義的名稱:"+pd.getName());
				System.out.println("流程定義的Key:"+pd.getKey());
				System.out.println("流程定義的部署ID:"+pd.getDeploymentId());
				System.out.println("流程定義的資源名稱:"+pd.getResourceName());
				System.out.println("流程定義的版本:"+pd.getVersion());
				System.out.println("########################################################");
			}
		}

	}
    

     流程定義和部署物件相關的Service都是RepositoryService,建立流程定義查詢物件,可以在

ProcessDefinitionQuery上設定查詢的相關引數,呼叫ProcessDefinitionQuery物件的list方法,執行查詢,獲得符

合條件的流程定義列表。

     執行結果如下:

     

二、刪除流程定義

	//刪除流程定義
	@Test
	public void deleteProcessDifinition(){
		//部署物件ID
		String deploymentId = "601";
		processEngine.getRepositoryService()//獲取流程定義和部署物件相關的Service
			.deleteDeployment(deploymentId,true);
		
		System.out.println("刪除成功~~~");//使用部署ID刪除流程定義,true表示級聯刪除
	}

     因為刪除的是流程定義,而流程定義的部署是屬於倉庫服務的,所以應該先得到RepositoryService

     如果該流程定義下沒有正在執行的流程,則可以用普通刪除。如果是有關聯的資訊,用級聯刪除。專案開發中使

用級聯刪除的情況比較多,刪除操作一般只開放給超級管理員使用。

     執行結果如下:

       

三、獲取流程定義文件的資源

	//檢視流程定義的資原始檔
	@Test
	public void viewPng() throws IOException{
		//部署ID
		String deploymentId = "1";
		//獲取的資源名稱
		List<String> list =  processEngine.getRepositoryService()
			.getDeploymentResourceNames(deploymentId);
		//獲得資源名稱字尾.png
		String resourceName = "";
		if(list != null && list.size()>0){
			for(String name:list){
				if(name.indexOf(".png")>=0){//返回包含該字串的第一個字母的索引位置
					resourceName = name;
				}
			}
		}
		
		//獲取輸入流,輸入流中存放.PNG的檔案
		InputStream in = processEngine.getRepositoryService()
				.getResourceAsStream(deploymentId, resourceName);
		
		//將獲取到的檔案儲存到本地
		FileUtils.copyInputStreamToFile(in, new File("D:/" + resourceName));
		
		System.out.println("檔案儲存成功!");
	}
     

     使用repositoryService的getDeploymentResourceNames方法可以獲取指定部署下得所有檔案的名稱;使用

repositoryService的getResourceAsStream方法傳入部署ID和資源圖片名稱可以獲取部署下指定名稱檔案的輸入流;

最後的有關IO流的操作,使用FileUtils工具的copyInputStreamToFile方法完成流程流程到檔案的拷貝,將資原始檔

以流的形式輸出到指定資料夾下。