ProcessController.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.bootdo.activiti.controller;
  2. import com.bootdo.activiti.service.ProcessService;
  3. import com.bootdo.activiti.vo.ProcessVO;
  4. import com.bootdo.common.config.Constant;
  5. import com.bootdo.common.controller.BaseController;
  6. import com.bootdo.common.utils.PageUtils;
  7. import com.bootdo.common.utils.R;
  8. import org.activiti.engine.ActivitiException;
  9. import org.activiti.engine.RepositoryService;
  10. import org.activiti.engine.RuntimeService;
  11. import org.activiti.engine.repository.Deployment;
  12. import org.activiti.engine.repository.ProcessDefinition;
  13. import org.apache.commons.io.FilenameUtils;
  14. import org.springframework.beans.factory.annotation.Autowired;
  15. import org.springframework.transaction.annotation.Transactional;
  16. import org.springframework.web.bind.annotation.*;
  17. import org.springframework.web.multipart.MultipartFile;
  18. import org.springframework.web.servlet.ModelAndView;
  19. import org.springframework.web.servlet.mvc.support.RedirectAttributes;
  20. import javax.servlet.http.HttpServletResponse;
  21. import javax.xml.stream.XMLStreamException;
  22. import java.io.InputStream;
  23. import java.io.UnsupportedEncodingException;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. import java.util.zip.ZipInputStream;
  27. @RequestMapping("activiti/process")
  28. @RestController
  29. public class ProcessController extends BaseController{
  30. @Autowired
  31. private RepositoryService repositoryService;
  32. @Autowired
  33. private ProcessService processService;
  34. @Autowired
  35. private RuntimeService runtimeService;
  36. @GetMapping
  37. ModelAndView process() {
  38. return new ModelAndView("act/process/process");
  39. }
  40. @GetMapping("list")
  41. PageUtils list(int offset, int limit) {
  42. List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery()
  43. .listPage(offset, limit);
  44. int count = (int) repositoryService.createProcessDefinitionQuery().count();
  45. List<Object> list = new ArrayList<>();
  46. for(ProcessDefinition processDefinition: processDefinitions){
  47. list.add(new ProcessVO(processDefinition));
  48. }
  49. PageUtils pageUtils = new PageUtils(list, count);
  50. return pageUtils;
  51. }
  52. @GetMapping("/add")
  53. public ModelAndView add() {
  54. return new ModelAndView("act/process/add");
  55. }
  56. @PostMapping("/save")
  57. @Transactional(readOnly = false)
  58. public R deploy(String exportDir, String category, MultipartFile file) {
  59. if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
  60. return R.error(1, "演示系统不允许修改,完整体验请部署程序");
  61. }
  62. String message = "";
  63. String fileName = file.getOriginalFilename();
  64. try {
  65. InputStream fileInputStream = file.getInputStream();
  66. Deployment deployment = null;
  67. String extension = FilenameUtils.getExtension(fileName);
  68. if (extension.equals("zip") || extension.equals("bar")) {
  69. ZipInputStream zip = new ZipInputStream(fileInputStream);
  70. deployment = repositoryService.createDeployment().addZipInputStream(zip).deploy();
  71. } else if (extension.equals("png")) {
  72. deployment = repositoryService.createDeployment().addInputStream(fileName, fileInputStream).deploy();
  73. } else if (fileName.indexOf("bpmn20.xml") != -1) {
  74. deployment = repositoryService.createDeployment().addInputStream(fileName, fileInputStream).deploy();
  75. } else if (extension.equals("bpmn")) { // bpmn扩展名特殊处理,转换为bpmn20.xml
  76. String baseName = FilenameUtils.getBaseName(fileName);
  77. deployment = repositoryService.createDeployment().addInputStream(baseName + ".bpmn20.xml", fileInputStream).deploy();
  78. } else {
  79. message = "不支持的文件类型:" + extension;
  80. }
  81. List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).list();
  82. // 设置流程分类
  83. for (ProcessDefinition processDefinition : list) {
  84. // ActUtils.exportDiagramToFile(repositoryService, processDefinition, exportDir);
  85. repositoryService.setProcessDefinitionCategory(processDefinition.getId(), category);
  86. message += "部署成功,流程ID=" + processDefinition.getId() + "<br/>";
  87. }
  88. if (list.size() == 0) {
  89. message = "部署失败,没有流程。";
  90. }
  91. } catch (Exception e) {
  92. throw new ActivitiException("部署失败!", e);
  93. }
  94. return R.ok(message);
  95. }
  96. /**
  97. * 将部署的流程转换为模型
  98. *
  99. * @param procDefId
  100. * @param redirectAttributes
  101. * @return
  102. * @throws UnsupportedEncodingException
  103. * @throws XMLStreamException
  104. */
  105. @RequestMapping(value = "/convertToModel/{procDefId}")
  106. public R convertToModel(@PathVariable("procDefId") String procDefId, RedirectAttributes redirectAttributes) throws UnsupportedEncodingException, XMLStreamException {
  107. if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
  108. return R.error(1, "演示系统不允许修改,完整体验请部署程序");
  109. }
  110. org.activiti.engine.repository.Model modelData = null;
  111. try {
  112. modelData = processService.convertToModel(procDefId);
  113. return R.ok( "转换模型成功,模型ID=" + modelData.getId());
  114. } catch (Exception e) {
  115. e.printStackTrace();
  116. return R.ok( "转换模型失败");
  117. }
  118. }
  119. @RequestMapping(value = "/resource/read/{xml}/{id}")
  120. public void resourceRead(@PathVariable("xml") String resType, @PathVariable("id") String id, HttpServletResponse response) throws Exception {
  121. InputStream resourceAsStream = processService.resourceRead(id,resType);
  122. byte[] b = new byte[1024];
  123. int len = -1;
  124. while ((len = resourceAsStream.read(b, 0, 1024)) != -1) {
  125. response.getOutputStream().write(b, 0, len);
  126. }
  127. }
  128. @PostMapping("/remove")
  129. public R remove(String id){
  130. if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
  131. return R.error(1, "演示系统不允许修改,完整体验请部署程序");
  132. }
  133. repositoryService.deleteDeployment(id,true);
  134. return R.ok();
  135. }
  136. @PostMapping("/batchRemove")
  137. public R batchRemove(@RequestParam("ids[]") String[] ids) {
  138. if (Constant.DEMO_ACCOUNT.equals(getUsername())) {
  139. return R.error(1, "演示系统不允许修改,完整体验请部署程序");
  140. }
  141. for (String id : ids) {
  142. repositoryService.deleteDeployment(id,true);
  143. }
  144. return R.ok();
  145. }
  146. }