Activiti系列(二)整合Activiti Modeler
https://blog.csdn.net/h1059141989/article/details/79870043
1.下載原始檔
activiti-5.22.0官方Demo
activiti5.22.0原始碼
2.copy原始檔
(一)複製前端檔案
解壓activiti-5.22.0官方Demo
解壓activiti-explorer.war
複製editor-app,diagram-viewer資料夾,以及modele.html到本地專案
(2)複製服務端檔案
解壓activiti5.22.0原始碼
複製ModelEditorJsonRestResource.java,ModelSaveRestResource.java,StencilsetRestResource.java到控制層資料夾下
3.新增maven引用
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>5.22.0</version>
</dependency>
1
2
3
4
5
4.啟動專案,訪問modeler頁面
http://localhost:8080/modeler.html
修改啟動類,遮蔽登入功能
@SpringBootApplication
@EnableAutoConfiguration(exclude = {
org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration.class,
org.activiti.spring.boot.SecurityAutoConfiguration.class,
})
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
1
2
3
4
5
6
7
8
9
10
11
再次訪問modeler頁面
處理 /activiti-explorer/service/model//json 請求的報錯
該請求的控制類為ModelEditorJsonRestResource.java
@RequestMapping(value="/model/{modelId}/json", method = RequestMethod.GET, produces = "application/json")
1
在/public/editor-app/app-cfg.js檔案中修改請求的地址
ACTIVITI.CONFIG = {
//'contextRoot' : '/activiti-explorer/service',
'contextRoot' : '',
};
1
2
3
4
再次訪問modeler頁面
報錯GET http://localhost:8080/model//json 404 ()
這是因為我們沒有已經建好的model模型,無法檢視
5.新建model
測試類
@Controller
@RequestMapping("model")
public class ModelTest {
@RequestMapping("create")
public void createModel(HttpServletRequest request, HttpServletResponse response){
try{
String modelName = "modelName";
String modelKey = "modelKey";
String description = "description";
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ObjectMapper objectMapper = new ObjectMapper();
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
Model modelData = repositoryService.newModel();
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, modelName);
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName(modelName);
modelData.setKey(modelKey);
//儲存模型
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());
}catch (Exception e){
}
}
}
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
訪問localhost:8080/model/create
處理http://localhost:8080/editor/stencilset?version=1523329753442請求的報錯
在StencilsetRestResource.java中,我們專案中少了stencilset.json
@RestController
public class StencilsetRestResource {
@RequestMapping(value="/editor/stencilset", method = RequestMethod.GET, produces = "application/json;charset=utf-8")
public @ResponseBody String getStencilset() {
//stencilset.json為Model中的工具欄的名稱字元,這裡在resources下面查詢
InputStream stencilsetStream = this.getClass().getClassLoader().getResourceAsStream("stencilset.json");
try {
return IOUtils.toString(stencilsetStream, "utf-8");
} catch (Exception e) {
throw new ActivitiException("Error while loading stencil set", e);
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
下載stencilset.json,並放置在resources目錄下
再次訪問localhost:8080/model/create
6.儲存Model
處理http://localhost:8080/model/102503/save的報錯
修改ModelSaveRestResource.java為
@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants {
protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);
@Autowired
private RepositoryService repositoryService;
@Autowired
private ObjectMapper objectMapper;
@RequestMapping(value="/model/{modelId}/save", method = RequestMethod.PUT)
@ResponseStatus(value = HttpStatus.OK)
public void saveModel(@PathVariable String modelId, String name, String description, String json_xml, String svg_xml) {
try {
Model model = repositoryService.getModel(modelId);
ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
modelJson.put(MODEL_NAME, name);
modelJson.put(MODEL_DESCRIPTION, description);
model.setMetaInfo(modelJson.toString());
model.setName(name);
repositoryService.saveModel(model);
repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));
InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
TranscoderInput input = new TranscoderInput(svgStream);
PNGTranscoder transcoder = new PNGTranscoder();
// Setup output
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
// Do the transformation
transcoder.transcode(input, output);
final byte[] result = outStream.toByteArray();
repositoryService.addModelEditorSourceExtra(model.getId(), result);
outStream.close();
} catch (Exception e) {
LOGGER.error("Error saving model", e);
throw new ActivitiException("Error saving model", e);
}
}
}
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
再次儲存,儲存成功。Activiti Modeler整合成功
原始碼下載
---------------------
作者:D丶ream
來源:CSDN
原文:https://blog.csdn.net/h1059141989/article/details/79870043
版權宣告:本文為博主原創文章,轉載請