activiti自己定義流程之整合(五):啟動流程時獲取自己定義表單


流程定義部署之后,自然就是流程定義列表了,但和前一節一樣的是,這里也是和之前單獨的activiti沒什么差別。因此也不多說。我們先看看列表頁面以及相應的代碼,然后在一步步說明點擊啟動button時怎樣調用自己定義的form表單。


流程定義列表頁面例如以下:



相應的html代碼:

[html]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. <div id="logdiv1" ng-init="init();">    
  2.    <p style="font-size:24px;margin:3px">流程列表</p>  
  3.    <center>  
  4.    <table border="1px" style="margin-top:1px;width:87%;font-size:14px;text-align:center;margin-top:1px;margin-left:2px;position:relative;float:left;" cellSpacing="0px" cellPadding="0px">  
  5.       <tr style="background-color:#ccc">  
  6.          <td>ID</td>  
  7.          <td>NAME</td>  
  8.          <td>DeploymentID</td>  
  9.          <td>KEY</td>  
  10.          <td>版本號</td>  
  11.          <td>resourceName</td>  
  12.          <td>DiagramResourceName</td>  
  13.          <td>操 作</td>  
  14.       </tr>  
  15.       <tr ng-repeat="process in processList | orderBy:'id'" >  
  16.          <td>{{process.id}}</td>  
  17.          <td>{{process.name}}</td>  
  18.          <td>{{process.deploymentId}}</td>  
  19.          <td>{{process.key}}</td>  
  20.          <td>{{process.version}}</td>  
  21.          <td>{{process.resourceName}}</td>  
  22.          <td>{{process.diagramResourceName}}</td>  
  23.          <td><a href="script:;" ng-click="toProcess(process)">啟動</a>   
  24.          <a href="script:;" ng-click="deleteProcess(process)">刪除</a>   
  25.          </td>  
  26.       </tr>  
  27.    </table>    
  28.    <div id="handleTemplate" ></div>  
  29.    </center>    
  30. </div>    


相應的angularjs代碼:
[javascript]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. angular.module('activitiApp')    
  2. .controller('processCtr', ['$rootScope','$scope','$http','$location'function($rootScope,$scope,$http,$location){    
  3. $scope.init=function(){  
  4.         $http.post("./processList.do").success(function(result) {  
  5.             if(result.isLogin==="yes"){  
  6.             $rootScope.userName=result.userName;  
  7.             $scope.processList=result.data;  
  8.             }else{  
  9.                 $location.path("/login");  
  10.             }  
  11.         });  
  12. }       
  13.      
  14.         //開始流程  
  15.         $scope.toProcess= function(process){  
  16.             $rootScope.process=process;  
  17.             $('#handleTemplate').html('').dialog({  
  18.                 title:'流程名稱[' + process.name + ']',  
  19.                 modal: true,  
  20.                 width: $.common.window.getClientWidth() * 0.6,  
  21.                 height: $.common.window.getClientHeight() * 0.9,      
  22.                 open: function() {  
  23.                     // 獲取json格式的表單數據。就是流程定義中的全部field  
  24.                     readForm.call(this, process.deploymentId);  
  25.                 },  
  26.                 buttons: [{  
  27.                     text: '啟動流程',  
  28.                     click: function() {  
  29.                         $("#handleTemplate").dialog("close");  
  30.                         sendStartupRequest();  
  31.                         setTimeout(function(){  
  32.                             window.location.href =("#/findFirstTask");  
  33.                         },1500);  
  34.                           
  35.                     }  
  36.                 }]  
  37.             }).position({  
  38.                    //my: "center",  
  39.                    //at: "center",  
  40.                 offset:'300 300',  
  41.                    of: window,  
  42.                    collision:"fit"  
  43.                 });  
  44. ;  
  45.         };  
  46.         //讀取流程啟動表單  
  47.         function readForm(deploymentId) {  
  48.             var dialog = this;  
  49.             // 讀取啟動時的表單  
  50.             $.post('./getStartForm.do',deploymentId, function(result) {  
  51.                 // 獲取的form是字符行,html格式直接顯示在對話框內就能夠了。然后用form包裹起來  
  52.                   
  53.                 $(dialog).append("<div class='formContent' />");  
  54.                 $('.formContent').html('').wrap("<form id='startform' class='formkey-form' method='post' />");  
  55.                   
  56.                 var $form = $('.formkey-form');  
  57.   
  58.   
  59.                 // 設置表單action    getStartFormAndStartProcess  
  60.                 $form.attr('action''./getStartFormAndStartProcess');  
  61.                 //設置部署的Id  
  62.                 $form.append("<input type='hidden' name='deploymentId' value="+deploymentId+">");  
  63.                 $form.append(result.form);  
  64.                 // 初始化日期組件  
  65.                 $form.find('.datetime').datetimepicker({  
  66.                        stepMinute: 5  
  67.                  });  
  68.                 $form.find('.date').datepicker();  
  69.                   
  70.                 // 表單驗證  
  71.                 $form.validate($.extend({}, $.common.plugin.validator));  
  72.             });  
  73.         }  
  74.           
  75.         /** 
  76.          * 提交表單 
  77.          * @return {[type]} [description] 
  78.          */  
  79.         function sendStartupRequest() {  
  80.             if ($(".formkey-form").valid()) {  
  81.                 var url = './getStartFormAndStartProcess.do';  
  82.                 var args = $('#startform').serialize();  
  83.                 $.post(url, args, function(data){  
  84.                     $("#handleTemplate").dialog("close");  
  85.                     $location.path("/findFirstTask");  
  86.                 });  
  87.             }  
  88.         }  
  89.         
  90.     
  91. }])    


在上邊的代碼中就有須要注意的地方了,從代碼中能夠看到。當我們點擊頁面的啟動button時,會觸發toProcess方法。而這種方法就使用到了dialog對話框。對話框中顯示的內容便是之前自己定義的表單,從后台數據庫中請求過來。




那么讀取的時候發送了getStartForm.do的請求,后台相應的代碼例如以下:

[java]  view plain  copy
  在CODE上查看代碼片 派生到我的代碼片
  1. @RequestMapping(value = "/getStartForm.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  
  2.     @ResponseBody  
  3.     public Object getStartForm(@RequestBody String deploymentId) {  
  4.         Map<String, String> map = new HashMap<String, String>();  
  5.         String deString = null;  
  6.         deString = deploymentId.replaceAll("=""");  
  7.         String form = this.getStartForm1(deString);  
  8.         map.put("form", form);  
  9.         return map;  
  10.     }  
  11.   
  12.   
  13.     public String getStartForm1(String deploymentId) {  
  14.         String deString = null;  
  15.         deString = deploymentId.replaceAll("=""");  
  16.         ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()  
  17.                 .deploymentId(deString).singleResult();  
  18.         String form = (String) formService.getRenderedStartForm(pd.getId());  
  19.         return form;  
  20.     }  




要說明的是這里之所以能使用formService.getRenderedStartForm方法,便是由於在上一節部署的時候進行了設置,否則這種方法是無法正常使用的。


那么這個對話框彈出界面視圖例如以下:


須要注意的是dialog的css樣式在jquery-ui.css中,不要忘了導入進來。當然了,也能夠按自己的喜好改動。


那么填寫好相關的數據點擊提交,同過上邊的js能夠知道就走到了后台getStartFormAndStartProcess這里,啟動流程實例:

[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 = "/getStartFormAndStartProcess.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  
  13.     @ResponseBody  
  14.     public Object startProcess1(HttpServletRequest req)  
  15.             throws XMLStreamException {  
  16.         Map<String, String[]> formMap = req.getParameterMap();  
  17.         String deploymentId = formMap.get("deploymentId")[0];  
  18.         // 拿到第一個data_1設置申請人  
  19.         String person1 = (String) formMap.get("data_1")[0];  
  20.         Map<String, String> map = new HashMap<String, String>();  
  21.         boolean isLogin = this.isLogin(req);  
  22.         if (isLogin) {  
  23.             if (deploymentId != null) {  
  24.                 HttpSession session = req.getSession();  
  25.                 String assginee = (String) session.getAttribute("userName");  
  26.                 ProcessDefinition pd = repositoryService  
  27.                         .createProcessDefinitionQuery()  
  28.                         .deploymentId(deploymentId).singleResult();  
  29.                 String processDefinitionId = pd.getId();  
  30.                 Map<String, String> formProperties = new HashMap<String, String>();  
  31.                 Iterator<FlowElement> iterator1 = this  
  32.                         .findFlow(processDefinitionId);  
  33.                 // 取第一個節點,開始節點的行號  
  34.                 String row = null;  
  35.                 while (iterator1.hasNext()) {  
  36.                     FlowElement flowElement = iterator1.next();  
  37.                     row = flowElement.getXmlRowNumber() + "";  
  38.                     break;  
  39.                 }  
  40.   
  41.                 // 從request中讀取參數然后轉換  
  42.                 Set<Entry<String, String[]>> entrySet = formMap.entrySet();  
  43.                 for (Entry<String, String[]> entry : entrySet) {  
  44.                     String key = entry.getKey();  
  45.                     String value = entry.getValue()[0];  
  46.                     if (!key.equals("deploymentId")) {  
  47.                         String keyString = key + row;  
  48.                         formProperties.put(keyString, value);  
  49.                     }  
  50.                 }  
  51.                 formProperties.put("deploymentId", deploymentId);  
  52.                 Iterator<FlowElement> iterator = this.findFlow(pd.getId());  
  53.                 int i = 1;  
  54.                 while (iterator.hasNext()) {  
  55.                     FlowElement flowElement = iterator.next(); // 申請人  
  56.                     if (flowElement.getClass().getSimpleName()  
  57.                             .equals("UserTask")  
  58.                             && i == 1) {  
  59.                         UserTask userTask = (UserTask) flowElement;  
  60.                         String assignee = userTask.getAssignee();  
  61.                         int index1 = assignee.indexOf("{");  
  62.                         int index2 = assignee.indexOf("}");  
  63.                         formProperties  
  64.                                 .put(assignee.substring(index1 + 1, index2),  
  65.                                         person1);  
  66.                         break;  
  67.                     }  
  68.                 }  
  69.                 identityService.setAuthenticatedUserId(assginee);  
  70.                 ProcessInstance processInstance = formService  
  71.                         .submitStartFormData(processDefinitionId,  
  72.                                 formProperties);  
  73.                 map.put("userName",  
  74.                         (String) req.getSession().getAttribute("userName"));  
  75.                 map.put("isLogin""yes");  
  76.                 map.put("result""success");  
  77.             } else {  
  78.                 map.put("result""fail");  
  79.             }  
  80.         } else {  
  81.             map.put("isLogin""no");  
  82.         }  
  83.         return map;  
  84.     }  

而這里最重要的是對前台數據的處理,假設大家使用了ueditor插件,會發現他傳遞到后台的數據是存放在request中的一個map中。而map的key都是data_1、data_2、data_3的形式。


這樣問題就來了。到后邊對任務進行操作的時候,這些數據還是這樣從data_1開始。那么假設我們原樣保存到數據庫,以后查詢時自然就會有問題了。所以這里就依據每一個流程中流程節點行號的唯一性進行了又一次組合,然后把這些數據保存為流程變量。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM