BPMN2.0(Business Process Model and Notation)是一套業務流程模型與符號建模標準,以XML為載體,以符號視覺化業務,支援精準的執行語意來描述元素的操作。
Flowable誕生於Activiti,是一個使用Java編寫的輕量級業務流程引擎。Flowable流程引擎可用於部署BPMN 2.0流程定義,可以十分靈活地加入你的應用/服務/構架。
本文給出兩種從flowable匯出流程定義bpmn20.xml的方式。
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-basic</artifactId>
<version>6.4.1</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-json-converter</artifactId>
<version>6.4.1</version>
</dependency>
通過流程編輯器製作的流程模型(如下圖所示), 可以通過模型ID(Model.id),呼叫flowable 的 RepositoryService 來生成bpmn20.xml。
@Service
public class MyModelServiceImpl implements MyModelService {
@Autowired
private RepositoryService repositoryService;
/**
* 通過模型ID,生成模型BPMN20.xml
* @param guid 模型id,即model.id
* @return
* @throws Exception
*/
@Override
public ResultDTO genXml(String guid) throws Exception {
/**通過ID獲取模型 **/
Model modelData = repositoryService.getModel(guid);
byte[] bytes = repositoryService.getModelEditorSource(modelData.getId());
if (bytes == null) {
return ResultDTO.failureCustom("模型資料為空,請先設計流程併成功儲存,再進行釋出。");
}
JsonNode modelNode = new ObjectMapper().readTree(bytes);
BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
if (model.getProcesses().size() == 0) {
return ResultDTO.failureCustom("資料模型不符要求,請至少設計一條主線流程。");
}
/** 設定名稱 **/
model.getMainProcess().setName(modelData.getName());
/** 設定 targetNamespace **/
if(StringUtils.isNotBlank(modelData.getCategory())) {
model.setTargetNamespace(modelData.getCategory());
}
byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model);
String xml = new String(bpmnBytes, "UTF-8");
return ResultDTO.success(xml);
}
}
執行效果如下:
{% asset_img res1.gif 匯出效果 %}
對於flowable已經部署的流程,可根據流程定義(ProcessDefinition.id),呼叫flowable 的RepositoryService來匯出其bpmn20.xml。
@RestController
@Slf4j
public class ProcessController {
@Autowired
private MyProcessService processService;
/**
* 通過processDefinition.id和resType匯出流程XML或圖片資源
* @param id processDefinition.id
* @param resType 取值 「image/png」或「text/xml」
* @param response
* @throws Exception
*/
@GetMapping(value = "/res/exp")
@ApiOperation("通過processDefinition.id和resType匯出流程XML或圖片資源")
public void resourceRead(@RequestParam("id") String id,@RequestParam("resType") String resType, HttpServletResponse response) throws Exception {
/** resType取值 「image/png」或「text/xml」 **/
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);
}
}
}
@Service
public class MyProcessServiceImpl implements MyProcessService {
@Autowired
private RepositoryService repositoryService;
@Override
public InputStream resourceRead(String id, String resType) throws Exception {
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(id).singleResult();
String resourceName = "";
if (resType.equals("image/png")) {
resourceName = processDefinition.getDiagramResourceName();
} else if (resType.equals("text/xml")) {
resourceName = processDefinition.getResourceName();
}
InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName);
return resourceAsStream;
}
}
執行效果如下:
本文來自部落格園,作者:soft1314,轉載請註明原文連結:https://www.cnblogs.com/soft1314/p/17331429.html