Java源码示例:org.flowable.editor.language.json.converter.BpmnJsonConverter

示例1
@GetMapping(value = "/models/{modelId}/xml", name = "获取模型XML")
public ResponseEntity<byte[]> getModelXml(@PathVariable String modelId) {
    Model model = getModelFromRequest(modelId);
    try {
        BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
        JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(model.getId()));
        BpmnModel bpmnModel = jsonConverter.convertToBpmnModel(editorNode);
        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
        byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);
        ByteArrayInputStream in = new ByteArrayInputStream(bpmnBytes);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.TEXT_XML);
        return new ResponseEntity<>(IOUtils.toByteArray(in), responseHeaders, HttpStatus.OK);
    } catch (Exception e) {
        logger.error("获取模型XML信息异常", e);
        exceptionFactory.throwDefinedException(ErrorConstant.MODEL_XML_READ_ERROR, e.getMessage());
    }
    return null;
}
 
示例2
@Override
public Void execute(CommandContext commandContext) {
    ProcessEngineConfiguration processEngineConfiguration = CommandContextUtil.getProcessEngineConfiguration(commandContext);
    RepositoryService repositoryService = processEngineConfiguration.getRepositoryService();

    try {
        byte[] bytes = editorJson.getBytes("utf-8");
        repositoryService.addModelEditorSource(modelId, bytes);
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(bytes);
        BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
        BpmnXMLConverter xmlConverter = new BpmnXMLConverter();
        byte[] bpmnBytes = xmlConverter.convertToXML(bpmnModel);
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader xmlIn = new InputStreamReader(new ByteArrayInputStream(bpmnBytes), "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
        bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);

        ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
        InputStream resource = diagramGenerator.generateDiagram(bpmnModel, "png",
                Collections.emptyList(), Collections.emptyList(),
                processEngineConfiguration.getActivityFontName(),
                processEngineConfiguration.getLabelFontName(),
                processEngineConfiguration.getAnnotationFontName(),
                processEngineConfiguration.getClassLoader(), 1.0);

        repositoryService.addModelEditorSourceExtra(modelId, IOUtils.toByteArray(resource));
    } catch (Exception e) {
        throw new FlowableException("create model exception :" + e.getMessage());
    }
    return null;
}
 
示例3
@PostMapping(value = "/rest/model/validate", consumes = MediaType.APPLICATION_JSON_VALUE)
public List<ValidationError> validate(@RequestBody JsonNode body){
    if (body != null && body.has("stencilset")) {
        JsonNode stencilset = body.get("stencilset");
        if (stencilset.has("namespace")) {
            String namespace = stencilset.get("namespace").asText();
            if (namespace.endsWith("bpmn2.0#")) {
                BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(body);
                ProcessValidator validator = new ProcessValidatorFactory().createDefaultProcessValidator();
                List<ValidationError> errors = validator.validate(bpmnModel);
                return errors;
            }
        }
    }
    return Collections.emptyList();
}
 
示例4
@Test
public void dockerInfoShouldRemainIntact() throws Exception {
    InputStream stream = this.getClass().getClassLoader().getResourceAsStream(getResource());
    JsonNode model = new ObjectMapper().readTree(stream);
    BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(model);
    model = new BpmnJsonConverter().convertToJson(bpmnModel);
    validate(model);
}
 
示例5
@RequestMapping(value = "/process-definitions/{processDefinitionId}/getModel", method = RequestMethod.GET, produces = "application/json", name = "流程定义获取对应模型")
public ModelResponse processDefinitionGetModel(@PathVariable String processDefinitionId) {
    ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
    try {
        Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(processDefinition.getDeploymentId()).singleResult();

        if (deployment == null) {
            throw new FlowableObjectNotFoundException("Could not find a process deployment with id '" + processDefinition.getDeploymentId() + "'.", Deployment.class);
        }
        Model modelData = null;

        if (deployment.getCategory() != null) {
            modelData = repositoryService.getModel(deployment.getCategory());
        }
        //如果不存在,创建对应模型
        if (modelData == null) {
            InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName());
            XMLInputFactory xif = XMLInputFactory.newInstance();
            InputStreamReader in = new InputStreamReader(bpmnStream, "UTF-8");
            XMLStreamReader xtr = xif.createXMLStreamReader(in);
            BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);

            BpmnJsonConverter converter = new BpmnJsonConverter();
            ObjectNode modelNode = converter.convertToJson(bpmnModel);
            modelData = repositoryService.newModel();
            modelData.setKey(processDefinition.getKey());
            modelData.setName(processDefinition.getName());
            modelData.setCategory(processDefinition.getCategory());

            ObjectNode modelObjectNode = new ObjectMapper().createObjectNode();
            modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, processDefinition.getName());
            modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
            modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, processDefinition.getDescription());
            modelData.setMetaInfo(modelObjectNode.toString());

            repositoryService.saveModel(modelData);

            repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8"));
            repositoryService.addModelEditorSourceExtra(modelData.getId(), IOUtils.toByteArray(managementService.executeCommand(new GetDeploymentProcessDiagramCmd(processDefinitionId))));

            repositoryService.setDeploymentCategory(processDefinition.getDeploymentId(), modelData.getId());
        }
        return restResponseFactory.createModelResponse(modelData);

    } catch (Exception e) {
        throw new FlowableException("Error  process-definition get model", e);
    }
}
 
示例6
@PostMapping(value = "/models/validate", name = "模型检查")
public List<ValidationError> validate(@RequestBody JsonNode body) {
    BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(body);
    ProcessValidator validator = new ProcessValidatorFactory().createDefaultProcessValidator();
    return validator.validate(bpmnModel);
}
 
示例7
@Override
public Deployment execute(CommandContext commandContext) {
    Deployment deployment;

    RepositoryService repositoryService = CommandContextUtil.getProcessEngineConfiguration(commandContext).getRepositoryService();
    Model model = repositoryService.getModel(modelId);
    if (model == null) {
        throw new FlowableObjectNotFoundException("Could not find a model with id '" + modelId + "'.", Model.class);
    }

    byte[] editorSource = CommandContextUtil.getProcessEngineConfiguration(commandContext).getModelEntityManager().findEditorSourceByModelId(modelId);
    if (editorSource == null) {
        throw new FlowableObjectNotFoundException("Model with id '" + modelId + "' does not have source available.", String.class);
    }

    try {
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(editorSource);
        BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
        byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(bpmnModel);
        String fileName = model.getId() + ".bpmn20.xml";
        ByteArrayInputStream bis = new ByteArrayInputStream(bpmnBytes);
        deploymentBuilder.addInputStream(fileName, bis);
        deploymentBuilder.name(fileName);
        // modelId设置为部署的分类字段作为后续关联的需要
        deploymentBuilder.category(model.getId());

        if (model.getTenantId() != null) {
            deploymentBuilder.tenantId(model.getTenantId());
        }
        deployment = deploymentBuilder.deploy();
        model.setDeploymentId(deployment.getId());
    } catch (Exception e) {
        if (e instanceof FlowableException) {
            throw (FlowableException) e;
        }
        throw new FlowableException(e.getMessage(), e);
    }

    return deployment;
}
 
示例8
protected BpmnModel readJsonFile() throws Exception {
    InputStream jsonStream = this.getClass().getClassLoader().getResourceAsStream(getResource());
    JsonNode modelNode = new ObjectMapper().readTree(jsonStream);
    BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
    return bpmnModel;
}
 
示例9
protected BpmnModel convertToJsonAndBack(BpmnModel bpmnModel) {
    ObjectNode modelNode = new BpmnJsonConverter().convertToJson(bpmnModel);
    bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
    return bpmnModel;
}