1. 程式人生 > >activiti自定義流程之Spring整合activiti-modeler5.16例項(六):啟動流程

activiti自定義流程之Spring整合activiti-modeler5.16例項(六):啟動流程







1.啟動流程並分配任務是單個流程的正式開始,因此要使用到runtimeService介面,以及相關的啟動流程的方法。我習慣於用流程定義的key啟動,因為有多個版本的流程定義時,用key啟動預設會使用最新版本。同時,因為啟動中查詢了流程部署時xml檔案中流程節點的資訊,也用到了repositoryService及相關方法。

2.後臺業務程式碼,
  (1)自定義的申請單實體類(為的目的只為了跑通整個流程,因此只定義了一個實體類,按公司標準開發來說,應該是和前臺互動一個command類(其實也還是實體類,叫法不一樣而已),和資料庫互動還有一個實體類),在這裡定義了一個申請單的基本資訊:
package model;


public class applyModel {
	/**
	 * 流程定義id
	 */
	private String proDefId;
	/**
	 * 流程定義的key
	 */
	private String key;
	
	private String name;
	/**
	 * 申請人
	 */
	private String appPerson;
	/**
	 * 原因
	 */
	private String cause;
	/**
	 * 內容
	 */
	private String content;
	/**
	 * 處理人,即下一個任務節點的受理人
	 */
	private String proPerson;


	public String getKey() {
		return key;
	}


	public void setKey(String key) {
		this.key = key;
	}


	public String getAppPerson() {
		return appPerson;
	}


	public void setAppPerson(String appPerson) {
		this.appPerson = appPerson;
	}


	public String getCause() {
		return cause;
	}


	public void setCause(String cause) {
		this.cause = cause;
	}


	public String getContent() {
		return content;
	}


	public void setContent(String content) {
		this.content = content;
	}


	public String getProPerson() {
		return proPerson;
	}


	public void setProPerson(String proPerson) {
		this.proPerson = proPerson;
	}


	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}


	public String getProDefId() {
		return proDefId;
	}


	public void setProDefId(String proDefId) {
		this.proDefId = proDefId;
	}


	@Override
	public String toString() {
		return "applyModel [proDefId=" + proDefId + ", key=" + key + ", name="
				+ name + ", appPerson=" + appPerson + ", cause=" + cause
				+ ", content=" + content + ", proPerson=" + proPerson + "]";
	}


}


(2)業務邏輯:


A,這個方法獲取流程部署時xml檔案裡的各個節點相關資訊,用來辨別當前是哪個節點,下一節點又是什麼,共下邊的B方法呼叫。因為操作部署時的檔案,因此使用repositoryService:
/**
	 * @throws XMLStreamException
	 *             查詢流程節點
	 * 
	 * @author:tuzongxun
	 * @Title: findFlow
	 * @param @return
	 * @return Iterator<FlowElement>
	 * @date Mar 21, 2016 9:31:42 AM
	 * @throws
	 */
	public Iterator<FlowElement> findFlow(String processDefId)
			throws XMLStreamException {
		List<ProcessDefinition> lists = repositoryService
				.createProcessDefinitionQuery()
				.processDefinitionId(processDefId)
				.orderByProcessDefinitionVersion().desc().list();
		ProcessDefinition processDefinition = lists.get(0);
		processDefinition.getCategory();
		String resourceName = processDefinition.getResourceName();
		InputStream inputStream = repositoryService.getResourceAsStream(
				processDefinition.getDeploymentId(), resourceName);
		BpmnXMLConverter converter = new BpmnXMLConverter();
		XMLInputFactory factory = XMLInputFactory.newInstance();
		XMLStreamReader reader = factory.createXMLStreamReader(inputStream);
		BpmnModel bpmnModel = converter.convertToBpmnModel(reader);
		Process process = bpmnModel.getMainProcess();
		Collection<FlowElement> elements = process.getFlowElements();
		Iterator<FlowElement> iterator = elements.iterator();
		return iterator;
	}


B.這裡呼叫上一個方法,一起完成流程的啟動及相關資訊的設定:

/**
	 * @throws XMLStreamException
	 *             啟動流程
	 * 
	 * @author:tuzongxun
	 * @Title: startProcess
	 * @param @return
	 * @return Object
	 * @date Mar 17, 2016 2:06:34 PM
	 * @throws
	 */
	@RequestMapping(value = "/startProcess.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
	@ResponseBody
	public Object startProcess(@RequestBody applyModel applyModel,
			HttpServletRequest req) throws XMLStreamException {
		Map<String, String> map = new HashMap<String, String>();
		boolean isLogin = this.isLogin(req);
		if (isLogin) {
			if (applyModel != null) {
				String processKey = applyModel.getKey();
				String processDefId = applyModel.getProDefId();
				// //////////////////////////
				Iterator<FlowElement> iterator = this.findFlow(processDefId);
				Map<String, Object> varables = new HashMap<String, Object>();
				int i = 1;
				while (iterator.hasNext()) {
					FlowElement flowElement = iterator.next();
					// 申請人
					if (flowElement.getClass().getSimpleName()
							.equals("UserTask")
							&& i == 1) {
						UserTask userTask = (UserTask) flowElement;
						String assignee = userTask.getAssignee();
						int index1 = assignee.indexOf("{");
						int index2 = assignee.indexOf("}");
						varables.put(assignee.substring(index1 + 1, index2),
								applyModel.getAppPerson());
						varables.put("cause", applyModel.getCause());
						varables.put("content", applyModel.getContent());
						varables.put("taskType", applyModel.getName());
						i++;
						// 下一個處理人
					} else if (flowElement.getClass().getSimpleName()
							.equals("UserTask")
							&& i == 2) {
						UserTask userTask = (UserTask) flowElement;
						String assignee = userTask.getAssignee();
						int index1 = assignee.indexOf("{");
						int index2 = assignee.indexOf("}");
						varables.put(assignee.substring(index1 + 1, index2),
								applyModel.getProPerson());
						break;
					}
				}
				// ///////////////////////////
				runtimeService.startProcessInstanceByKey(processKey, varables);
				map.put("userName",
						(String) req.getSession().getAttribute("userName"));
				map.put("isLogin", "yes");
				map.put("result", "success");
			} else {
				map.put("result", "fail");
			}
		} else {
			map.put("isLogin", "no");
		}
		return map;
	}



3.angular js前臺程式碼,:
  (1)app.js中配置路由:
   
$stateProvider  
   .state('startProcess', {  
   url: "/startProcess",  
   views: {  
      'view': {  
       templateUrl: 'activi_views/startProcess.html',  
       controller: 'startProcessCtr'  
      }  
   }  
  });  


  (2)邏輯相關程式碼:

     
angular.module('activitiApp')  
.controller('startProcessCtr', ['$rootScope','$scope','$http','$location', function($rootScope,$scope,$http,$location){  


	$http.post("createFlush.do").success(function(result){
		if(result.isLogin==="yes"){
			$rootScope.userName=result.userName;
			$scope.process1={"proDefId":"","key":"","appPerson":"","cause":"","content":"","proPerson":"","name":""};
			if($rootScope.process==null||$rootScope.process.key==null){
				$location.path("/processList");
			}else{
				$scope.process1.proDefId=$rootScope.process.id;
				 $scope.process1.key=$rootScope.process.key;
			       $scope.process1.name=$rootScope.process.name;
			}
		}else{
			$location.path("/login");
		}
	});
       
      
        $scope.startProcess=function(process){
        	console.log(process);
        	$http.post("./startProcess.do",process).success(function(deployResult){
        		$location.path("/taskList");
        	});
        }
      
  
}])  


4.對應的填寫相關資訊的頁面:
<center>  
<div style="margin-top:20px;margin-left:200px;background-color:#9cc;height:500px;width:50%;font-size:22px;position:relative;float:left;">  
    <p style="font-size:28px">新建申請</p>
    流程定義id:<input type="text" ng-model="process1.proDefId" readonly="readonly" style="background-color:#9dc"/> 
    </br>  
    </br>  
    流程定義key:<input type="text" ng-model="process1.key" readonly="readonly" style="background-color:#9dc"/> 
    </br>  
    </br>  
    申請型別:<input type="text" ng-model="process1.name" readonly="readonly" style="background-color:#9dc"/> 
    </br>  
    </br>  
    申請人:<input type="text" ng-model="process1.appPerson" /> 
    </br>  
    </br>  
    申請原因:<input type="text" ng-model="process1.cause"/>  
    </br>  
    </br>  
    申請內容:<input type="text" ng-model="process1.content"/>  
    </br>  
    </br>  
  受理人:<input type="text" ng-model="process1.proPerson"/>  
    </br>  
    </br>  
    <input style="font-size:24px;cursor:pointer" type="button" value="提交申請" ng-click="startProcess(process1);">  
           
    <input style="font-size:24px;cursor:pointer" type="button" value="返回">  
</div>  
</center>  



5.成功啟動一個流程例項後,會看到act_ru_execution、act_ru_identitylink、act_ru_task、act_ru_variable以及act_hi_actinst、act_hi_detail、act_hi_indentitylink、act_hi_procinst、act_hi_taskinst、act_hi_varinst表中都有了資料。除開variable和varinst中的資料條數是根據對應的流程變數多少來定的,其他都是增加了一條資料。