Activiti6.0流程模型导入

Activiti是一个开源的工作流程引擎,而在流程引擎上执行的便是符合BPMN2.0规范的流程模型。在我们系统集成Activiti后,自然需要提供流程模型的导入功能。

导入模型

思路:后台接受前端传来的文件,读取为XML文件,然后转为BpmnModel,再获取其中的流程数据,最后保存为Model。

POJO

1
2
3
4
5
6
7
8
9
10
11
12
/**
* 流程模型基本参数
*/
@Data
public class ModelParam{
@ApiModelProperty(value = "模型名称",required = true)
private String name;
@ApiModelProperty(value = "模型key",required = true)
private String key;
@ApiModelProperty("模型描述")
private String description;
}

controller

1
2
3
4
5
6
7
8
9
10
@PostMapping("model/import")
@ApiOperation(value = "导入模型")
@ResponseBody
public AjaxResult importModel(HttpServletRequest request){
List<MultipartFile> file = ((MultipartHttpServletRequest) request).getFiles("file");
if (Collections.isEmpty(file)){
throw new CustomException("文件为空,上传失败!");
}
return modelService.importModelFiles(file);
}

ServiceImpl

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
@Override
@Transactional(rollbackFor = Exception.class)
public AjaxResult importModelFiles(List<MultipartFile> modelFiles) {
try {
for (MultipartFile modelFile : modelFiles) {

byte[] bytes = modelFile.getBytes();
String xml = new String(bytes, Charsets.UTF_8);
//创建转换对象
BpmnXMLConverter converter = new BpmnXMLConverter();
XMLInputFactory factory = XMLInputFactory.newInstance();
// 字节方式
XMLStreamReader reader = factory.createXMLStreamReader(new ByteArrayInputStream(xml.getBytes()));
//将xml文件转换成BpmnModel
BpmnModel bpmnModel = converter.convertToBpmnModel(reader);
//流程数据
Process process = bpmnModel.getMainProcess();
String modelKey = process.getId();
//判断导入的模型key是否重复
List<Model> modelList = repositoryService.createModelQuery().modelKey(modelKey).list();
if (CollectionUtils.isNotEmpty(modelList)) {
return AjaxResult.error(modelFile.getOriginalFilename() + ",模型Key已存在,导入失败!");
}
ModelParam modelParam = new ModelParam();
modelParam.setKey(process.getId());
modelParam.setName(process.getName());
modelParam.setDescription(process.getDocumentation());
//创建模型
String modelId =this.createModel(modelParam);
//转为json格式保存
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
ObjectNode jsonNodes = jsonConverter.convertToJson(bpmnModel);
repositoryService.addModelEditorSource(modelId, jsonNodes.toString().getBytes(Charsets.UTF_8));
}
} catch (IOException | XMLStreamException e) {
logger.error("模型导入异常:{}", e.getMessage());
throw new CustomException("导入异常,请联系管理员!");
}
return AjaxResult.success("导入成功!");
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/**
* 创建模型
*/
private String createModel(ModelParam modelParam) {
if (StringUtils.isBlank(modelParam.getKey()) || StringUtils.isBlank(modelParam.getName())) {
return AjaxResult.error("参数错误,模型创建失败!");
}
List<Model> list = this.repositoryService.createModelQuery().modelKey(modelParam.getKey()).list();
if (CollectionUtils.isNotEmpty(list)) {
return AjaxResult.error("模型Key重复!");
}
Model model = this.repositoryService.newModel();
ObjectNode modelNode = objectMapper.createObjectNode();
modelNode.put(ModelDataJsonConstants.MODEL_NAME, modelParam.getName());
modelNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, modelParam.getDescription());
modelNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
model.setName(modelParam.getName());
model.setKey(modelParam.getKey());
model.setMetaInfo(modelNode.toString());
this.repositoryService.saveModel(model);
this.createObjectNode(model);
logger.info("创建模型结束,返回模型ID:{}", model.getId());
return model.getId();
}
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
/**
* 创建模型时完善ModelEditorSource
*
* @param model
*/
@SuppressWarnings("deprecation")
private void createObjectNode(Model model) {
logger.info("创建模型完善ModelEditorSource入参模型ID:{}", model.getId());
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#");
ObjectNode propertiesNode = objectMapper.createObjectNode();
propertiesNode.put("process_id", model.getKey());
propertiesNode.put("name", model.getName());
editorNode.put("properties", propertiesNode);
editorNode.put("stencilset", stencilSetNode);
try {
this.repositoryService.addModelEditorSource(model.getId(), editorNode.toString().getBytes(Charsets.UTF_8));
} catch (Exception e) {
logger.info("创建模型时完善ModelEditorSource服务异常:{}", e);
throw new CustomException("模型创建失败");
}
logger.info("创建模型完善ModelEditorSource结束");
}