Activiti系列(七)在线修改流程图XML文件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/h1059141989/article/details/79970933

1.获取model的XMl

@RequestMapping(value = "editor/{modelId}")
public ModelAndView editor(@PathVariable("modelId") String modelId) {
    ModelAndView mav = new ModelAndView();
    try {
        Model modelData = repositoryService.getModel(modelId);
        String  description = modelData.getMetaInfo().substring(modelData.getMetaInfo().lastIndexOf(":")+2,modelData.getMetaInfo().lastIndexOf("}")-1);
        modelData.setMetaInfo(description);
        byte[] modelEditorSource = repositoryService.getModelEditorSource(modelData.getId());

        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
        JsonNode editorNode = new ObjectMapper().readTree((new String(modelEditorSource,"UTF-8")).getBytes("UTF-8"));
        BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);

        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
        byte[] exportBytes = xmlConverter.convertToXML(bpmnModel,"UTF-8");

        String XML =  new String(exportBytes,"UTF-8");
        mav.setViewName("bpm/modelEditor");
        mav.addObject("data",XML);
        mav.addObject("model",modelData);

    }catch (Exception e) {
        e.printStackTrace();
    }
    return mav;
}

2.页面展示

<div class="panel-body">
    <form id="demoForm" method="post" action="saveModel.do" class="form-horizontal">
        <input id="id" type="hidden" name="id" value="${model.id }">
        <input id="name" type="hidden" name="name" value="${model.name }">
        <input id="description" type="hidden" name="description" value="${model.metaInfo }">
        <div class="form-group">
            <div class="col-sm-11">
                <textarea id="xml" name="xml" rows="20" style="width:80%">${data }</textarea>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-5">
                <button class="mybtn mybtn-medium mybtn-green w100" id="submitButton" type="submit" class="btn">保存</button>
                &nbsp;
                <button class="mybtn mybtn-medium mybtn-green w100" type="button" onclick="history.back();" class="btn">返回</button>
            </div>
        </div>
    </form>
</div>

3.保存修改后的文件

@RequestMapping(value="editor/saveModel")
public String saveModel( String id,String xml,String name,String description,ModelAndView mav) {
    try{
    String unescapeXml = StringUtils.unescapeHtml(xml);//因过滤处理XSS时会对<,>等字符转码,此处需将字符串还原
    InputStream   in_nocode   =   new   ByteArrayInputStream(unescapeXml.getBytes("UTF-8"));
    XMLInputFactory xmlFactory  = XMLInputFactory.newInstance();  
    XMLStreamReader reader = xmlFactory.createXMLStreamReader(in_nocode);

    BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
    BpmnModel bpmnModel = xmlConverter.convertToBpmnModel(reader);

    BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
    JsonNode j =jsonConverter.convertToJson(bpmnModel);

    byte[] modelEditorSource = new ObjectMapper().writeValueAsBytes(j);

    MultiValueMap<String,String> values = new LinkedMultiValueMap<String,String>();
    values.add("json_xml", new String(modelEditorSource,"UTF-8"));
    values.add("svg_xml", "");
    values.add("name", name);
    values.add("description", description);

    Model model = repositoryService.getModel(id);

    ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());

    modelJson.put("name", values.getFirst("name"));
    modelJson.put("description", values.getFirst("description"));
    model.setMetaInfo(modelJson.toString());
    model.setName(values.getFirst("name"));

    repositoryService.saveModel(model);

    repositoryService.addModelEditorSource(model.getId(), values.getFirst("json_xml").getBytes("utf-8"));

    }catch(Exception e){
        e.printStackTrace();
    }
    return "redirect:/workflow/model/list.do";
} 
  • 因为编辑的时候要先获取model的XML数据,所以model必须先存在数据,不允许编辑空的model
阅读更多
想对作者说点什么?
相关热词

博主推荐

换一批

没有更多推荐了,返回首页