activiti自己定义流程之Spring整合activiti-modeler实例(六):启动流程

1.启动流程并分配任务是单个流程的正式開始,因此要使用到runtimeService接口。以及相关的启动流程的方法。我习惯于用流程定义的key启动,由于有多个版本号的流程定义时,用key启动默认会使用最新版本号。同一时候,由于启动中查询了流程部署时xml文件里流程节点的信息。也用到了repositoryService及相关方法。


2.后台业务代码,
  (1)自己定义的申请单实体类(为的目的仅仅为了跑通整个流程。因此仅仅定义了一个实体类。按公司标准开发来说,应该是和前台交互一个command类(事实上也还是实体类,叫法不一样而已),和数据库交互另一个实体类),在这里定义了一个申请单的基本信息:
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package model;  
  2.   
  3.   
  4. public class applyModel {  
  5.     /** 
  6.      * 流程定义id 
  7.      */  
  8.     private String proDefId;  
  9.     /** 
  10.      * 流程定义的key 
  11.      */  
  12.     private String key;  
  13.       
  14.     private String name;  
  15.     /** 
  16.      * 申请人 
  17.      */  
  18.     private String appPerson;  
  19.     /** 
  20.      * 原因 
  21.      */  
  22.     private String cause;  
  23.     /** 
  24.      * 内容 
  25.      */  
  26.     private String content;  
  27.     /** 
  28.      * 处理人。即下一个任务节点的受理人 
  29.      */  
  30.     private String proPerson;  
  31.   
  32.   
  33.     public String getKey() {  
  34.         return key;  
  35.     }  
  36.   
  37.   
  38.     public void setKey(String key) {  
  39.         this.key = key;  
  40.     }  
  41.   
  42.   
  43.     public String getAppPerson() {  
  44.         return appPerson;  
  45.     }  
  46.   
  47.   
  48.     public void setAppPerson(String appPerson) {  
  49.         this.appPerson = appPerson;  
  50.     }  
  51.   
  52.   
  53.     public String getCause() {  
  54.         return cause;  
  55.     }  
  56.   
  57.   
  58.     public void setCause(String cause) {  
  59.         this.cause = cause;  
  60.     }  
  61.   
  62.   
  63.     public String getContent() {  
  64.         return content;  
  65.     }  
  66.   
  67.   
  68.     public void setContent(String content) {  
  69.         this.content = content;  
  70.     }  
  71.   
  72.   
  73.     public String getProPerson() {  
  74.         return proPerson;  
  75.     }  
  76.   
  77.   
  78.     public void setProPerson(String proPerson) {  
  79.         this.proPerson = proPerson;  
  80.     }  
  81.   
  82.   
  83.     public String getName() {  
  84.         return name;  
  85.     }  
  86.   
  87.   
  88.     public void setName(String name) {  
  89.         this.name = name;  
  90.     }  
  91.   
  92.   
  93.     public String getProDefId() {  
  94.         return proDefId;  
  95.     }  
  96.   
  97.   
  98.     public void setProDefId(String proDefId) {  
  99.         this.proDefId = proDefId;  
  100.     }  
  101.   
  102.   
  103.     @Override  
  104.     public String toString() {  
  105.         return "applyModel [proDefId=" + proDefId + ", key=" + key + ", name="  
  106.                 + name + ", appPerson=" + appPerson + ", cause=" + cause  
  107.                 + ", content=" + content + ", proPerson=" + proPerson + "]";  
  108.     }  
  109.   
  110.   
  111. }  


(2)业务逻辑:


A,这种方法获取流程部署时xml文件中的各个节点相关信息。用来辨别当前是哪个节点,下一节点又是什么,共下边的B方法调用。

由于操作部署时的文件,因此使用repositoryService:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * @throws XMLStreamException 
  3.      *             查询流程节点 
  4.      *  
  5.      * @author:tuzongxun 
  6.      * @Title: findFlow 
  7.      * @param @return 
  8.      * @return Iterator<FlowElement> 
  9.      * @date Mar 21, 2016 9:31:42 AM 
  10.      * @throws 
  11.      */  
  12.     public Iterator<FlowElement> findFlow(String processDefId)  
  13.             throws XMLStreamException {  
  14.         List<ProcessDefinition> lists = repositoryService  
  15.                 .createProcessDefinitionQuery()  
  16.                 .processDefinitionId(processDefId)  
  17.                 .orderByProcessDefinitionVersion().desc().list();  
  18.         ProcessDefinition processDefinition = lists.get(0);  
  19.         processDefinition.getCategory();  
  20.         String resourceName = processDefinition.getResourceName();  
  21.         InputStream inputStream = repositoryService.getResourceAsStream(  
  22.                 processDefinition.getDeploymentId(), resourceName);  
  23.         BpmnXMLConverter converter = new BpmnXMLConverter();  
  24.         XMLInputFactory factory = XMLInputFactory.newInstance();  
  25.         XMLStreamReader reader = factory.createXMLStreamReader(inputStream);  
  26.         BpmnModel bpmnModel = converter.convertToBpmnModel(reader);  
  27.         Process process = bpmnModel.getMainProcess();  
  28.         Collection<FlowElement> elements = process.getFlowElements();  
  29.         Iterator<FlowElement> iterator = elements.iterator();  
  30.         return iterator;  
  31.     }  


B.这里调用上一个方法,一起完毕流程的启动及相关信息的设置:

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.      * @throws XMLStreamException 
  3.      *             启动流程 
  4.      *  
  5.      * @author:tuzongxun 
  6.      * @Title: startProcess 
  7.      * @param @return 
  8.      * @return Object 
  9.      * @date Mar 17, 2016 2:06:34 PM 
  10.      * @throws 
  11.      */  
  12.     @RequestMapping(value = "/startProcess.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  
  13.     @ResponseBody  
  14.     public Object startProcess(@RequestBody applyModel applyModel,  
  15.             HttpServletRequest req) throws XMLStreamException {  
  16.         Map<String, String> map = new HashMap<String, String>();  
  17.         boolean isLogin = this.isLogin(req);  
  18.         if (isLogin) {  
  19.             if (applyModel != null) {  
  20.                 String processKey = applyModel.getKey();  
  21.                 String processDefId = applyModel.getProDefId();  
  22.                 // //////////////////////////  
  23.                 Iterator<FlowElement> iterator = this.findFlow(processDefId);  
  24.                 Map<String, Object> varables = new HashMap<String, Object>();  
  25.                 int i = 1;  
  26.                 while (iterator.hasNext()) {  
  27.                     FlowElement flowElement = iterator.next();  
  28.                     // 申请人  
  29.                     if (flowElement.getClass().getSimpleName()  
  30.                             .equals("UserTask")  
  31.                             && i == 1) {  
  32.                         UserTask userTask = (UserTask) flowElement;  
  33.                         String assignee = userTask.getAssignee();  
  34.                         int index1 = assignee.indexOf("{");  
  35.                         int index2 = assignee.indexOf("}");  
  36.                         varables.put(assignee.substring(index1 + 1, index2),  
  37.                                 applyModel.getAppPerson());  
  38.                         varables.put("cause", applyModel.getCause());  
  39.                         varables.put("content", applyModel.getContent());  
  40.                         varables.put("taskType", applyModel.getName());  
  41.                         i++;  
  42.                         // 下一个处理人  
  43.                     } else if (flowElement.getClass().getSimpleName()  
  44.                             .equals("UserTask")  
  45.                             && i == 2) {  
  46.                         UserTask userTask = (UserTask) flowElement;  
  47.                         String assignee = userTask.getAssignee();  
  48.                         int index1 = assignee.indexOf("{");  
  49.                         int index2 = assignee.indexOf("}");  
  50.                         varables.put(assignee.substring(index1 + 1, index2),  
  51.                                 applyModel.getProPerson());  
  52.                         break;  
  53.                     }  
  54.                 }  
  55.                 // ///////////////////////////  
  56.                 runtimeService.startProcessInstanceByKey(processKey, varables);  
  57.                 map.put("userName",  
  58.                         (String) req.getSession().getAttribute("userName"));  
  59.                 map.put("isLogin""yes");  
  60.                 map.put("result""success");  
  61.             } else {  
  62.                 map.put("result""fail");  
  63.             }  
  64.         } else {  
  65.             map.put("isLogin""no");  
  66.         }  
  67.         return map;  
  68.     }  



3.angular js前台代码。:
  (1)app.js中配置路由:
   
[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. $stateProvider    
  2.    .state('startProcess', {    
  3.    url: "/startProcess",    
  4.    views: {    
  5.       'view': {    
  6.        templateUrl: 'activi_views/startProcess.html',    
  7.        controller: 'startProcessCtr'    
  8.       }    
  9.    }    
  10.   });    


  (2)逻辑相关代码:

     
[javascript] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. angular.module('activitiApp')    
  2. .controller('startProcessCtr', ['$rootScope','$scope','$http','$location'function($rootScope,$scope,$http,$location){    
  3.   
  4.   
  5.     $http.post("createFlush.do").success(function(result){  
  6.         if(result.isLogin==="yes"){  
  7.             $rootScope.userName=result.userName;  
  8.             $scope.process1={"proDefId":"","key":"","appPerson":"","cause":"","content":"","proPerson":"","name":""};  
  9.             if($rootScope.process==null||$rootScope.process.key==null){  
  10.                 $location.path("/processList");  
  11.             }else{  
  12.                 $scope.process1.proDefId=$rootScope.process.id;  
  13.                  $scope.process1.key=$rootScope.process.key;  
  14.                    $scope.process1.name=$rootScope.process.name;  
  15.             }  
  16.         }else{  
  17.             $location.path("/login");  
  18.         }  
  19.     });  
  20.          
  21.         
  22.         $scope.startProcess=function(process){  
  23.             console.log(process);  
  24.             $http.post("./startProcess.do",process).success(function(deployResult){  
  25.                 $location.path("/taskList");  
  26.             });  
  27.         }  
  28.         
  29.     
  30. }])    


4.相应的填写相关信息的页面:
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <center>    
  2. <div style="margin-top:20px;margin-left:200px;background-color:#9cc;height:500px;50%;font-size:22px;position:relative;float:left;">    
  3.     <p style="font-size:28px">新建申请</p>  
  4.     流程定义id:<input type="text" ng-model="process1.proDefId" readonly="readonly" style="background-color:#9dc"/>   
  5.     </br>    
  6.     </br>    
  7.     流程定义key:<input type="text" ng-model="process1.key" readonly="readonly" style="background-color:#9dc"/>   
  8.     </br>    
  9.     </br>    
  10.     申请类型:<input type="text" ng-model="process1.name" readonly="readonly" style="background-color:#9dc"/>   
  11.     </br>    
  12.     </br>    
  13.     申请人:<input type="text" ng-model="process1.appPerson" />   
  14.     </br>    
  15.     </br>    
  16.     申请原因:<input type="text" ng-model="process1.cause"/>    
  17.     </br>    
  18.     </br>    
  19.     申请内容:<input type="text" ng-model="process1.content"/>    
  20.     </br>    
  21.     </br>    
  22.   受理人:<input type="text" ng-model="process1.proPerson"/>    
  23.     </br>    
  24.     </br>    
  25.     <input style="font-size:24px;cursor:pointer" type="button" value="提交申请" ng-click="startProcess(process1);">    
  26.              
  27.     <input style="font-size:24px;cursor:pointer" type="button" value="返回">    
  28. </div>    
  29. </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中的数据条数是依据相应的流程变量多少来定的。其它都是添加了一条数据。

原文地址:https://www.cnblogs.com/wzzkaifa/p/7230967.html