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