项目源码仓库

BPMN2.0(Business Process Model and Notation)是一套业务流程模型与符号建模标准,以XML为载体,以符号可视化业务,支持精准的执行语义来描述元素的操作。
Flowable诞生于Activiti,是一个使用Java编写的轻量级业务流程引擎。Flowable流程引擎可用于部署BPMN 2.0流程定义,可以十分灵活地加入你的应用/服务/构架。

本文给出两种从flowable导出流程定义bpmn20.xml的方式。

导入Maven依赖

        <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>

从流程模型导出流程定义bpmn20.xml

通过流程编辑器制作的流程模型(如下图所示), 可以通过模型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 导出效果 %}

从流程定义导出流程定义bpmn20.xml

对于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;
    }
}

运行效果如下:

项目源码仓库

posted on 2023-04-18 22:18  超然楼  阅读(402)  评论(0编辑  收藏  举报