Activiti模板相关功能详解

声明版本:activiti 5.22.0
1、流程设计器保存模板

    public void saveModel(String modelId, String name, String description, String json_xml, String svg_xml) throws IOException {
        Model model = repositoryService.getModel(modelId);
        ObjectNode modelJson = (ObjectNode) new ObjectMapper().readTree(model.getMetaInfo());
        modelJson.put(MODEL_NAME, name);
        modelJson.put(MODEL_DESCRIPTION, description);
        model.setMetaInfo(modelJson.toString());
        model.setName(name);
        repositoryService.saveModel(model);
        repositoryService.addModelEditorSource(model.getId(), json_xml.getBytes("utf-8"));
        InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
        final byte[] result = IOUtils.toByteArray(svgStream);
        repositoryService.addModelEditorSourceExtra(model.getId(), result);
    }

2、直接从bpmn文件导入转换成模板

    public String convertInputStreamToModel(InputStream is, String name,
            String key, String description) {
        String modelId = null;
        
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader isr = null;
        XMLStreamReader xtr = null;
        try {
            isr = new InputStreamReader(is, "utf-8");
            xtr = xif.createXMLStreamReader(isr);
            BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
            // 处理异常
            if (bpmnModel.getMainProcess() == null
                    || bpmnModel.getMainProcess().getId() == null) {
                throw new BizException("模板文件可能存在问题,请检查后重试!");
            }
            
            ObjectNode modelNode = new BpmnJsonConverter().convertToJson(bpmnModel);
            Model modelData = repositoryService.newModel();
            modelData.setKey(key);
            modelData.setName(name);

            ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
            modelData.setMetaInfo(modelObjectNode.toString());
            
            //校验流程类型key,并自动校正
            updateProcessKey(modelNode);
            
            repositoryService.saveModel(modelData);
            repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
            
            modelId = modelData.getId();
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("模板文件存在问题,操作失败!");
        } finally {
            try {
                xtr.close();
            } catch (XMLStreamException e) {
                e.printStackTrace();
            }
            try {
                isr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }           
        }
        return modelId;
    }

3、模板导出bpmn文件

    public void exportModel(HttpServletResponse response, String modelId) {
        BufferedOutputStream bos = null;  
        try {            
            Model modelData = repositoryService.getModel(modelId);        
            byte[] modelEditorSource = repositoryService  
                    .getModelEditorSource(modelData.getId()); 
            JsonNode editorNode = new ObjectMapper().readTree(modelEditorSource);
            BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
            BpmnModel model = jsonConverter.convertToBpmnModel(editorNode);
            byte[] bpmnBytes;           
            try {
                bpmnBytes = new BpmnXMLConverter().convertToXML(model);
            } catch (Exception e) {
                bpmnBytes = new byte[10];
            }
      
            // 封装输出流  
            bos = new BufferedOutputStream(response.getOutputStream());  
            bos.write(bpmnBytes);// 写入流  
            String filename = modelData.getName();
            if(StringUtils.isNoneBlank(filename) && filename.length() > 20) {
                filename = filename.substring(0, 20);
            }
            filename +=".bpmn20.xml";  
            response.setContentType("application/x-msdownload;");  
            response.setHeader("Content-Disposition",  
                    "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));  
            response.flushBuffer();  
        } catch (Exception e) {    
            LogUtils.error("下载模板失败:"+modelId+"erro:"+e);
        }finally {  
            try {
                bos.flush();
            } catch (IOException e) {
                e.printStackTrace();
            }  
            try {
                bos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }  
        }
    }

4、模板发布为流程

public Deployment convertModelToProcess(String modelId) throws IOException, UnsupportedEncodingException {
        Deployment deployment = null;
        try {
            Model modelData = repositoryService.getModel(modelId);
            ObjectNode modelNode = (ObjectNode) new ObjectMapper()
                    .readTree(repositoryService.getModelEditorSource(modelData.getId()));
            byte[] bpmnBytes = null;

            BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode);
            bpmnBytes = new BpmnXMLConverter().convertToXML(model);

            String processName = modelData.getName() + ".bpmn20.xml";
            deployment = repositoryService.createDeployment().name(modelData.getName())
                    .addString(processName, new String(bpmnBytes, "UTF-8")).deploy();
        } catch (Exception e1) {
            throw new BizException("流程定义文件存在错误,请修改后重新部署!");
        } 
        return deployment;
    }

你可能感兴趣的:(Activiti模板相关功能详解)