activiti自定义流程之整合(四):整合自定义表单部署流程定义

时间:2023-03-09 04:32:07
activiti自定义流程之整合(四):整合自定义表单部署流程定义

综合前几篇博文内容,我想在整合这一部分中应该会有很多模块会跳过不讲,就如自定义表单的表单列表那一块,因为这些模块在整合的过程中都几乎没有什么改动,再多讲也是重复无用功。

正因为如此,在创建了流程模型之后,模型列表的展示也是和之前的没有什么区别,而且都是很简单的后台查询以及前台展示,这一部分也就不过多的讲了。

模型列表页面如下:

activiti自定义流程之整合(四):整合自定义表单部署流程定义

至于其中的修改和删除也没什么多讲的,删除很简单,而修改也是activiti-modeler实现的主要功能,我们只需要跳转过去就行。

重要的部分在于部署,因为点击部署到达后台以后,activiti就要和自定义的form表单打赏关系。
以上页面的html代码如下:

  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:18px;text-align:center;margin-left:2px;margin-top:auto;position:relative;float:left;" cellSpacing="0px" cellPadding="0px">
  5. <tr style="background-color:#ccc">
  6. <td>ID</td>
  7. <td>NAME</td>
  8. <td>KEY</td>
  9. <td>描 述</td>
  10. <td>版本</td>
  11. <td>创建时间</td>
  12. <td>修改时间</td>
  13. <td>操 作</td>
  14. </tr>
  15. <tr ng-repeat="model in modelList | orderBy:'id'" >
  16. <td>{{model.id}}</td>
  17. <td>{{model.name}}</td>
  18. <td>{{model.key}}</td>
  19. <td>{{model.metaInfo}}</td>
  20. <td>{{model.version}}</td>
  21. <td>{{model.createTime | date:"yyyy-MM-dd HH:mm:ss"}}</td>
  22. <td>{{model.lastUpdateTime | date:"yyyy-MM-dd HH:mm:ss"}}</td>
  23. <td><a href="script:;" ng-click="deploye(model)">部署</a>
  24. <a href="script:;" ng-click="delete(model)">删除</a>
  25. <a href="script:;" ng-click="update(model.id)">修改</a>
  26. </td>
  27. </tr>
  28. </table>
  29. </center>
  30. </div>

点击部署要走到后台,前台就需要js控制,相应的js代码如下:

  1. angular.module('activitiApp')
  2. .controller('modelCtr', ['$rootScope','$scope','$http','$location', function($rootScope,$scope,$http,$location){
  3. $scope.init=function(){
  4. $http.post("./modelList.do").success(function(result) {
  5. if(result.isLogin==="yes"){
  6. $rootScope.userName=result.userName;
  7. console.log(result.data);
  8. $scope.modelList=result.data;
  9. }else{
  10. $location.path("/login");
  11. }
  12. });
  13. }
  14. $scope.deploye=function(model){
  15. console.log(model);
  16. $http.post("./deploye.do",model).success(function(deployResult){
  17. $location.path("/processList");
  18. });
  19. }
  20. $scope.update=function(modelId){
  21. window.open("http://localhost:8080/activitiTest1/service/editor?id="+modelId);
  22. }
  23. }])

而后程序到达后台,后台代码如下:

  1. /**
  2. * 根据模型id部署流程定义
  3. *
  4. * @author:tuzongxun
  5. * @Title: deploye
  6. * @param @param activitiModel
  7. * @param @param redirectAttributes
  8. * @param @return
  9. * @return Object
  10. * @date Mar 17, 2016 12:30:05 PM
  11. * @throws
  12. */
  13. @RequestMapping(value = "/deploye.do", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
  14. @ResponseBody
  15. public Object deploye(@RequestBody ActivitiModel activitiModel,
  16. HttpServletRequest req) {
  17. Map<String, Object> map = new HashMap<String, Object>();
  18. boolean isLogin = this.isLogin(req);
  19. if (isLogin) {
  20. String modelId = activitiModel.getId();
  21. try {
  22. // 获取forms拿到formname
  23. Model modelData = repositoryService.getModel(modelId);
  24. ObjectNode modelNode = (ObjectNode) new ObjectMapper()
  25. .readTree(repositoryService
  26. .getModelEditorSource(modelData.getId()));
  27. byte[] bpmnBytes = null;
  28. BpmnModel model = new BpmnJsonConverter()
  29. .convertToBpmnModel(modelNode);
  30. bpmnBytes = new BpmnXMLConverter().convertToXML(model);
  31. DeploymentBuilder db = repositoryService.createDeployment()
  32. .name(modelData.getName());
  33. //区别在这里
  34. List<JsonNode> forms = modelNode
  35. .findValues("formkeydefinition");
  36. for (JsonNode node : forms) {
  37. // aaa.form
  38. String formName = node.textValue();
  39. if (!"".equals(formName)) {
  40. // 就是页面的html代码根据formName找到
  41. String formContent = myFormService
  42. .findFormByFormName(formName);
  43. ByteArrayInputStream bi = new ByteArrayInputStream(
  44. formContent.getBytes());
  45. db.addInputStream(formName, bi);
  46. break;
  47. }
  48. }
  49. Deployment deployment = db.addString(
  50. modelData.getName() + ".bpmn20.xml",
  51. new String(bpmnBytes)).deploy();
  52. if (deployment != null && deployment.getId() != null) {
  53. map.put("isLogin", "yes");
  54. map.put("userName",
  55. (String) req.getSession().getAttribute("userName"));
  56. map.put("result", "success");
  57. }
  58. } catch (Exception e) {
  59. e.printStackTrace();
  60. }
  61. } else {
  62. map.put("isLogin", "no");
  63. }
  64. return map;
  65. }

拿这段代码和之前单独的activiti流程部署的代码相比,就可以看到这里多出了查询form的操作以及部署时新的inputStream的设置。

在这段代码中,需要我们自己根据formKey(即自定义的表单的文件名)从数据中查询出相应的html表单代码,这段代码也是自己写的,如下:

  1. public Connection getDb() {
  2. Connection connection = null;
  3. try {
  4. Class.forName("com.mysql.jdbc.Driver");
  5. connection = DriverManager.getConnection(
  6. "jdbc:mysql://localhost:3306/testtu", "root", "123456");
  7. } catch (Exception e) {
  8. e.printStackTrace();
  9. }
  10. return connection;
  11. }
  12. public String findFormByFormName(String formName) {
  13. String formString = null;
  14. Connection connection = this.getDb();
  15. Statement statement;
  16. try {
  17. statement = connection.createStatement();
  18. PreparedStatement ps = connection
  19. .prepareStatement("select * from formtest where formType=?");
  20. ps.setString(1, formName);
  21. ResultSet resultSet = ps.executeQuery();
  22. while (resultSet.next()) {
  23. formString = resultSet.getString(3);
  24. }
  25. ;
  26. } catch (Exception e) {
  27. e.printStackTrace();
  28. }
  29. return formString;
  30. }

实现这个表单设置的目的实际上是为了之后启动流程时的操作,因为部署之后就有了流程定义列表,在流程定义列表中就可以启动流程,只有在这里设置了,那么点击启动流程时才能调用activitiService的相关方法获取对应节点的表单。

有了这个操作,在我们部署成功之后,可以看到与之前的部署相比,在数据库ac_ge_bytearray表中会再多出一条表单相关的数据,如图:

activiti自定义流程之整合(四):整合自定义表单部署流程定义

那么至此,整合自定义表单部署流程结束。