Springboot整合Flowable6.x导出bpmn20.xml

5 篇文章 1 订阅
3 篇文章 0 订阅

项目源码仓库

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);
    }
}

运行效果如下:
运行效果

从流程定义导出流程定义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;
    }
}

运行效果如下:
运行效果图

项目源码仓库

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是Vue3整合bpmn.js实现flowable流程设计器的代码实现。 首先,需要安装依赖: ``` npm install bpmn-js@8.0.1 npm install bpmn-js-properties-panel@0.44.0 npm install bpmn-js-properties-panel-provider@0.23.0 npm install camunda-bpmn-moddle@6.2.0 npm install vue-bpmn ``` 接着,在Vue组件中引入需要的文件: ```javascript import BpmnModeler from 'bpmn-js/lib/Modeler'; import propertiesPanelModule from 'bpmn-js-properties-panel'; import propertiesProviderModule from 'bpmn-js-properties-panel/lib/provider/camunda'; import camundaModdleDescriptor from 'camunda-bpmn-moddle/resources/camunda'; import 'bpmn-js-properties-panel/styles/properties.less'; import 'bpmn-js/dist/assets/bpmn-font/css/bpmn.css'; ``` 然后,在Vue组件中定义bpmn.js的Modeler和其他必要的变量: ```javascript data() { return { bpmnModeler: null, propertiesPanel: null, xml: null, }; }, ``` 接下来,在Vue组件的mounted生命周期函数中初始化bpmn.js的Modeler和其他必要的变量: ```javascript mounted() { this.bpmnModeler = new BpmnModeler({ container: '#canvas', propertiesPanel: { parent: '#properties', }, additionalModules: [ propertiesPanelModule, propertiesProviderModule, ], moddleExtensions: { camunda: camundaModdleDescriptor, }, }); this.propertiesPanel = this.bpmnModeler.get('propertiesPanel'); }, ``` 然后,在Vue组件的methods中定义一些必要的方法,比如打开一个流程图、保存一个流程图、导出一个流程图: ```javascript methods: { openDiagram(xml) { this.bpmnModeler.importXML(xml, (err) => { if (err) { console.log(err); } else { console.log('success'); } }); }, saveDiagram() { this.bpmnModeler.saveXML({ format: true }, (err, xml) => { if (err) { console.log(err); } else { console.log(xml); } }); }, exportDiagram() { this.bpmnModeler.saveSVG((err, svg) => { if (err) { console.log(err); } else { console.log(svg); } }); }, }, ``` 最后,在Vue组件的template中定义UI界面: ```html <template> <div> <div id="canvas"></div> <div id="properties"></div> <button @click="saveDiagram">保存</button> <button @click="exportDiagram">导出</button> </div> </template> ``` 在这个例子中,我们使用了Vue3和bpmn.js来实现了flowable流程设计器。通过这个例子,你可以了解到如何在Vue3中整合bpmn.js以及如何定义UI界面、打开、保存和导出流程图。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值