activiti 根據模型ID匯出、預覽 xml檔案

FH-Admin發表於2021-10-27
    /**根據模型ID匯出xml檔案
     * @param response
     * @param modelId    //模型ID
     * @from  fhadmin.cn
     */
    protected void exportXmlFromModelId(HttpServletResponse response, String modelId) throws Exception{
        Model modelData = repositoryService.getModel(modelId);  
        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();  
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
        BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(modelNode);  
        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();  
        byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);  
        ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);  
        //IOUtils.copy(in, response.getOutputStream());  
        String filename = bpmnModel.getMainProcess().getId() + ".bpmn20.xml";  
        /*response.setHeader("Content-Disposition", "attachment; filename=" + filename);  
        response.flushBuffer(); */
        FileUpload.copyFile(in,PathUtil.getProjectpath()+Const.FILEACTIVITI,filename);         //把檔案上傳到檔案目錄裡面
        FileDownload.fileDownload(response, PathUtil.getProjectpath()+Const.FILEACTIVITI+filename, filename);
        in.close();
    }

    /**根據模型ID預覽xml檔案
     * @param response
     * @param modelId    //模型ID
     * @from  fhadmin.org
     */
    protected String viewXmlFromModelId(String modelId) throws Exception{
        Model modelData = repositoryService.getModel(modelId);  
        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();  
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId()));
        BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(modelNode);  
        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();  
        byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);  
        ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);  
        InputStreamReader isr = new InputStreamReader(in,"utf-8");
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuffer xmlContent = new StringBuffer(); 
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            xmlContent.append(lineTxt);
            xmlContent.append("\n");
        }
        isr.close();
        return xmlContent.toString();
    }
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章