activiti自定义流程之Spring整合activiti-modeler5.16实例(六):启动流程

时间:2023-12-05 18:05:38
注:(1)环境搭建:activiti自定义流程之Spring整合activiti-modeler5.16实例(一):环境搭建
        (2)创建流程模型:activiti自定义流程之Spring整合activiti-modeler5.16实例(二):创建流程模型
        (3)流程模型列表展示:activiti自定义流程之Spring整合activiti-modeler5.16实例(三):流程模型列表展示
        (4)部署流程定义:activiti自定义流程之Spring整合activiti-modeler5.16实例(四):部署流程定义

(5)流程定义列表:activiti自定义流程之Spring整合activiti-modeler5.16实例(五):流程定义列表

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

2.后台业务代码,
  (1)自定义的申请单实体类(为的目的只为了跑通整个流程,因此只定义了一个实体类,按公司标准开发来说,应该是和前台交互一个command类(其实也还是实体类,叫法不一样而已),和数据库交互还有一个实体类),在这里定义了一个申请单的基本信息:

  1. package model;
  2. public class applyModel {
  3. /**
  4. * 流程定义id
  5. */
  6. private String proDefId;
  7. /**
  8. * 流程定义的key
  9. */
  10. private String key;
  11. private String name;
  12. /**
  13. * 申请人
  14. */
  15. private String appPerson;
  16. /**
  17. * 原因
  18. */
  19. private String cause;
  20. /**
  21. * 内容
  22. */
  23. private String content;
  24. /**
  25. * 处理人,即下一个任务节点的受理人
  26. */
  27. private String proPerson;
  28. public String getKey() {
  29. return key;
  30. }
  31. public void setKey(String key) {
  32. this.key = key;
  33. }
  34. public String getAppPerson() {
  35. return appPerson;
  36. }
  37. public void setAppPerson(String appPerson) {
  38. this.appPerson = appPerson;
  39. }
  40. public String getCause() {
  41. return cause;
  42. }
  43. public void setCause(String cause) {
  44. this.cause = cause;
  45. }
  46. public String getContent() {
  47. return content;
  48. }
  49. public void setContent(String content) {
  50. this.content = content;
  51. }
  52. public String getProPerson() {
  53. return proPerson;
  54. }
  55. public void setProPerson(String proPerson) {
  56. this.proPerson = proPerson;
  57. }
  58. public String getName() {
  59. return name;
  60. }
  61. public void setName(String name) {
  62. this.name = name;
  63. }
  64. public String getProDefId() {
  65. return proDefId;
  66. }
  67. public void setProDefId(String proDefId) {
  68. this.proDefId = proDefId;
  69. }
  70. @Override
  71. public String toString() {
  72. return "applyModel [proDefId=" + proDefId + ", key=" + key + ", name="
  73. + name + ", appPerson=" + appPerson + ", cause=" + cause
  74. + ", content=" + content + ", proPerson=" + proPerson + "]";
  75. }
  76. }

(2)业务逻辑:

A,这个方法获取流程部署时xml文件里的各个节点相关信息,用来辨别当前是哪个节点,下一节点又是什么,共下边的B方法调用。因为操作部署时的文件,因此使用repositoryService:

  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.这里调用上一个方法,一起完成流程的启动及相关信息的设置:

  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中配置路由:

  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)逻辑相关代码:

  1. angular.module('activitiApp')
  2. .controller('startProcessCtr', ['$rootScope','$scope','$http','$location', function($rootScope,$scope,$http,$location){
  3. $http.post("createFlush.do").success(function(result){
  4. if(result.isLogin==="yes"){
  5. $rootScope.userName=result.userName;
  6. $scope.process1={"proDefId":"","key":"","appPerson":"","cause":"","content":"","proPerson":"","name":""};
  7. if($rootScope.process==null||$rootScope.process.key==null){
  8. $location.path("/processList");
  9. }else{
  10. $scope.process1.proDefId=$rootScope.process.id;
  11. $scope.process1.key=$rootScope.process.key;
  12. $scope.process1.name=$rootScope.process.name;
  13. }
  14. }else{
  15. $location.path("/login");
  16. }
  17. });
  18. $scope.startProcess=function(process){
  19. console.log(process);
  20. $http.post("./startProcess.do",process).success(function(deployResult){
  21. $location.path("/taskList");
  22. });
  23. }
  24. }])

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

  1. <center>
  2. <div style="margin-top:20px;margin-left:200px;background-color:#9cc;height:500px;width: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. <input style="font-size:24px;cursor:pointer" type="button" value="返回">
  27. </div>
  28. </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中的数据条数是根据对应的流程变量多少来定的,其他都是增加了一条数据。