activiti自定义流程之Spring整合activiti-modeler实例(七):任务列表展示

1.通过上一节的操作,可以知道流程启动以后会同时生成一个流程实例和用户任务,这个用户任务保存在act_ru_task和act_hi_task表中,从表明可以看出ru是runtime,hi是history。但是需要注意的是,和操作流程使用的service不同,操作正在发生任务不是使用runtimeService,而是专门的taskService。
2.后台业务代码,
  (1)自定义的任务实体类

[java]  view plain  copy
 
  1. package model;  
  2. import java.util.Date;  
  3. public class TaskModel {  
  4.     private String id;  
  5.     private String name;  
  6.     private String processInstanceId;  
  7.     private String assignee;  
  8.     private Date createTime;  
  9.     private String nextPerson;  
  10.     private String cause;  
  11.     private String content;  
  12.     private String taskType;  
  13.     private String processKey;  
  14.     private String processDefId;  
  15.   
  16.     public String getTaskType() {  
  17.         return taskType;  
  18.     }  
  19.   
  20.     public void setTaskType(String taskType) {  
  21.         this.taskType = taskType;  
  22.     }  
  23.   
  24.     public String getId() {  
  25.         return id;  
  26.     }  
  27.   
  28.     public void setId(String id) {  
  29.         this.id = id;  
  30.     }  
  31.   
  32.     public String getName() {  
  33.         return name;  
  34.     }  
  35.   
  36.     public void setName(String name) {  
  37.         this.name = name;  
  38.     }  
  39.   
  40.     public String getProcessInstanceId() {  
  41.         return processInstanceId;  
  42.     }  
  43.   
  44.     public void setProcessInstanceId(String processInstanceId) {  
  45.         this.processInstanceId = processInstanceId;  
  46.     }  
  47.   
  48.     public String getAssignee() {  
  49.         return assignee;  
  50.     }  
  51.   
  52.     public void setAssignee(String assignee) {  
  53.         this.assignee = assignee;  
  54.     }  
  55.   
  56.     public Date getCreateTime() {  
  57.         return createTime;  
  58.     }  
  59.   
  60.     public void setCreateTime(Date createTime) {  
  61.         this.createTime = createTime;  
  62.     }  
  63.   
  64.     public String getNextPerson() {  
  65.         return nextPerson;  
  66.     }  
  67.   
  68.     public void setNextPerson(String nextPerson) {  
  69.         this.nextPerson = nextPerson;  
  70.     }  
  71.   
  72.     public String getCause() {  
  73.         return cause;  
  74.     }  
  75.   
  76.     public void setCause(String cause) {  
  77.         this.cause = cause;  
  78.     }  
  79.   
  80.     public String getContent() {  
  81.         return content;  
  82.     }  
  83.   
  84.     public void setContent(String content) {  
  85.         this.content = content;  
  86.     }  
  87.   
  88.     public String getProcessKey() {  
  89.         return processKey;  
  90.     }  
  91.   
  92.     public void setProcessKey(String processKey) {  
  93.         this.processKey = processKey;  
  94.     }  
  95.   
  96.     public String getProcessDefId() {  
  97.         return processDefId;  
  98.     }  
  99.   
  100.     public void setProcessDefId(String processDefId) {  
  101.         this.processDefId = processDefId;  
  102.     }  
  103.   
  104.     @Override  
  105.     public String toString() {  
  106.         return "TaskModel [id=" + id + ", name=" + name  
  107.                 + ", processInstanceId=" + processInstanceId + ", assignee="  
  108.                 + assignee + ", createTime=" + createTime + ", nextPerson="  
  109.                 + nextPerson + ", cause=" + cause + ", content=" + content  
  110.                 + ", taskType=" + taskType + ", processKey=" + processKey  
  111.                 + ", processDefId=" + processDefId + "]";  
  112.     }  
  113. }  



(2)业务逻辑:查询任务使用taskService调用相关的方法来完成,可以根据特定的条件,也可以不加条件查询所有。可以返回task为元素的list,也可以返回单独的task对象,但是需要注意的是,如果要返回单独的task对象,则必须确定返回值是唯一的对象,否则就会抛出异常。下边的例子中,我是根据当前登陆的用户名来查询出对应的所有task:
[java]  view plain  copy
 
  1. /** 
  2.      * @throws XMLStreamException 
  3.      *             查询个人任务 
  4.      *  
  5.      * @author:tuzongxun 
  6.      * @Title: findTask 
  7.      * @param @return 
  8.      * @return Object 
  9.      * @date Mar 17, 2016 2:44:11 PM 
  10.      * @throws 
  11.      */  
  12.     @RequestMapping(value = "/findTask.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")  
  13.     @ResponseBody  
  14.     public Object findTask(HttpServletRequest req) throws XMLStreamException {  
  15.         Map<String, Object> map = new HashMap<String, Object>();  
  16.         boolean isLogin = this.isLogin(req);  
  17.         if (isLogin) {  
  18.             List<TaskModel> taskList = new ArrayList<TaskModel>();  
  19.             HttpSession session = req.getSession();  
  20.             String assginee = (String) session.getAttribute("userName");  
  21.             List<Task> taskList1 = taskService.createTaskQuery()  
  22.                     .taskAssignee(assginee).list();  
  23.             if (taskList1 != null && taskList1.size() > 0) {  
  24.                 for (Task task : taskList1) {  
  25.                     TaskModel taskModel = new TaskModel();  
  26.                     taskModel.setAssignee(task.getAssignee());  
  27.                     taskModel.setCreateTime(task.getCreateTime());  
  28.                     taskModel.setId(task.getId());  
  29.                     taskModel.setName(task.getName());  
  30.                     taskModel.setProcessInstanceId(task.getProcessInstanceId());  
  31.                     taskModel.setProcessDefId(task.getProcessDefinitionId());  
  32.                     // 获取流程变量  
  33.                     Map<String, Object> variables = runtimeService  
  34.                             .getVariables(task.getProcessInstanceId());  
  35.                     Set<String> keysSet = variables.keySet();  
  36.                     Iterator<String> keySet = keysSet.iterator();  
  37.                     while (keySet.hasNext()) {  
  38.                         String key = keySet.next();  
  39.                         if (key.endsWith("cause")) {  
  40.                             taskModel.setCause((String) variables.get("cause"));  
  41.                         } else if (key.endsWith("content")) {  
  42.                             taskModel.setContent((String) variables  
  43.                                     .get("content"));  
  44.                         } else if (key.endsWith("taskType")) {  
  45.                             taskModel.setTaskType((String) variables  
  46.                                     .get("taskType"));  
  47.                         } else if (!assginee.equals(variables.get(key))) {  
  48.                             // 想办法查询是否还有下一个任务节点  
  49.                             Iterator<FlowElement> iterator = this.findFlow(task  
  50.                                     .getProcessDefinitionId());  
  51.                             while (iterator.hasNext()) {  
  52.                                 FlowElement flowElement = iterator.next();  
  53.                                 String classNames = flowElement.getClass()  
  54.                                         .getSimpleName();  
  55.                                 if (classNames.equals("UserTask")) {  
  56.                                     UserTask userTask = (UserTask) flowElement;  
  57.                                     String assginee11 = userTask.getAssignee();  
  58.                                     String assginee12 = assginee11.substring(  
  59.                                             assginee11.indexOf("{") + 1,  
  60.                                             assginee11.indexOf("}"));  
  61.                                     String assignee13 = (String) variables  
  62.                                             .get(assginee12);  
  63.                                     if (assginee.equals(assignee13)) {  
  64.                                         // 看下下一个节点是什么  
  65.                                         iterator.next();  
  66.                                         FlowElement flowElement2 = iterator  
  67.                                                 .next();  
  68.                                         String classNames1 = flowElement2  
  69.                                                 .getClass().getSimpleName();  
  70.                                         // 设置下一个任务人  
  71.                                         if (!(classNames1.equals("EndEvent"))) {  
  72.                                             UserTask userTask2 = (UserTask) flowElement2;  
  73.                                             String assginee21 = userTask2  
  74.                                                     .getAssignee();  
  75.                                             String assginee22 = assginee21  
  76.                                                     .substring(  
  77.                                                             assginee21  
  78.                                                                     .indexOf("{") + 1,  
  79.                                                             assginee21  
  80.                                                                     .indexOf("}"));  
  81.                                             String assignee23 = (String) variables  
  82.                                                     .get(assginee22);  
  83.                                             taskModel.setNextPerson(ToolUtils  
  84.                                                     .isEmpty(assignee23));  
  85.                                         }  
  86.                                     }  
  87.   
  88.   
  89.                                 }  
  90.                             }  
  91.                             // //////////  
  92.                         }  
  93.                     }  
  94.                     taskList.add(taskModel);  
  95.                 }  
  96.             }  
  97.             map.put("isLogin""yes");  
  98.             map.put("userName",  
  99.                     (String) req.getSession().getAttribute("userName"));  
  100.             map.put("result""success");  
  101.             map.put("data", taskList);  
  102.         } else {  
  103.             map.put("isLogin""no");  
  104.         }  
  105.         return map;  
  106.     }  




3.angular js前台代码(前台只是做简单的展示,不多讲):
  (1)app.js中配置路由:
   
  
[javascript]  view plain  copy
 
  1. $stateProvider    
  2.   .state('taskList', {    
  3.   url: "/taskList",    
  4.   views: {    
  5.      'view': {    
  6.       templateUrl: 'activi_views/taskList.html',    
  7.       controller: 'taskCtr'    
  8.      }    
  9.   }    
  10.  });    



  (2)逻辑相关代码:

   
[javascript]  view plain  copy
 
  1.   angular.module('activitiApp')    
  2. .controller('taskCtr', ['$rootScope','$scope','$http','$location','$state'function($rootScope,$scope,$http,$location,$state){    
  3. $scope.init=function(){  
  4.         $http.post("./findTask.do").success(function(result) {  
  5.             if(result.isLogin==="yes"){  
  6.             console.log(result.data);   
  7.             $rootScope.userName=result.userName;  
  8.             $scope.taskList=result.data;  
  9.             }else{  
  10.                 $location.path("/login");  
  11.             }  
  12.         });  
  13. }      
  14.         $scope.completeTaskTo=function(task){  
  15.             console.log(task);  
  16.             $rootScope.task=task;  
  17.             //$location.path("/completeTaskTo");  
  18.             $location.path("/completeTaskTo1");  
  19.         }  
  20.         
  21.     
  22. }])    



4.对应的填写相关信息的页面:

[html]  view plain  copy
 
  1. <div id="logdiv1" ng-init="init();">    
  2.    <p style="font-size:22px;margin-top:10px">当前任务列表</p>  
  3.    <center>   
  4.    <table border="1px" style="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>类型</td>  
  7.          <td>ID</td>  
  8.          <td>NAME</td>  
  9.          <td>ProcessIntanceId</td>  
  10.          <td>ProcessDefId</td>  
  11.          <td>创建时间</td>  
  12.          <td>申请人</td>  
  13.          <td>受理人</td>  
  14.          <td>申请原因</td>  
  15.          <td>申请内容</td>  
  16.          <td>操 作</td>  
  17.       </tr>  
  18.       <tr ng-repeat="task in taskList | orderBy:'id'" >  
  19.          <td>{{task.taskType}}</td>  
  20.          <td>{{task.id}}</td>  
  21.          <td>{{task.name}}</td>  
  22.          <td>{{task.processInstanceId}}</td>  
  23.          <td>{{task.processDefId}}</td>  
  24.          <td>{{task.createTime | date:"yyyy-MM-dd HH:mm:ss"}}</td>  
  25.          <td>{{task.assignee}}</td>  
  26.          <td>{{task.nextPerson}}</td>  
  27.          <td>{{task.cause}}</td>  
  28.          <td>{{task.content}}</td>  
  29.          <td><a href="script:;" ng-click="completeTaskTo(task)">完成任务</a>   
  30.          </td>  
  31.       </tr>  
  32.    </table>    
  33.    </center>   
  34. </div>    
  35.    

你可能感兴趣的:(java,spring,Activiti,modeler,任务列表)