Search in sources :

Example 1 with BpmnJsonConverter

use of org.flowable.editor.language.json.converter.BpmnJsonConverter in project plumdo-work by wengwh.

the class ModelEditorResource method saveModel.

@RequestMapping(value = "/models/{modelId}/editor", method = { RequestMethod.POST }, name = "模型设计器保存模型")
@ResponseStatus(value = HttpStatus.OK)
@Transactional(propagation = Propagation.REQUIRED)
public void saveModel(@PathVariable String modelId, @RequestBody ModelEditorJsonRequest values) {
    try {
        Model model = getModelFromRequest(modelId);
        ObjectNode modelJson = (ObjectNode) objectMapper.readTree(model.getMetaInfo());
        modelJson.put(MODEL_NAME, values.getName());
        modelJson.put(MODEL_DESCRIPTION, values.getDescription());
        model.setMetaInfo(modelJson.toString());
        model.setName(values.getName());
        if (model.getDeploymentId() != null) {
            model.setDeploymentId(null);
        }
        if (values.isAddVersion()) {
            model.setVersion(model.getVersion() + 1);
        }
        repositoryService.saveModel(model);
        repositoryService.addModelEditorSource(model.getId(), values.getJson_xml().getBytes("utf-8"));
        ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(values.getJson_xml().getBytes("utf-8"));
        BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(modelNode);
        ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
        InputStream resource = diagramGenerator.generateDiagram(bpmnModel, "png", Collections.<String>emptyList(), Collections.<String>emptyList(), processEngineConfiguration.getActivityFontName(), processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getAnnotationFontName(), processEngineConfiguration.getClassLoader(), 1.0);
        repositoryService.addModelEditorSourceExtra(model.getId(), IOUtils.toByteArray(resource));
    } catch (Exception e) {
        LOGGER.error("Error saving model", e);
        throw new FlowableException("Error saving model", e);
    }
}
Also used : FlowableException(org.flowable.engine.common.api.FlowableException) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ProcessDiagramGenerator(org.flowable.image.ProcessDiagramGenerator) InputStream(java.io.InputStream) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) FlowableException(org.flowable.engine.common.api.FlowableException) BpmnModel(org.flowable.bpmn.model.BpmnModel) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Example 2 with BpmnJsonConverter

use of org.flowable.editor.language.json.converter.BpmnJsonConverter in project syncope by apache.

the class FlowableUserWorkflowAdapter method importDefinition.

@Override
public void importDefinition(final String key, final WorkflowDefinitionFormat format, final String definition) {
    ProcessDefinition procDef = getProcessDefinitionByKey(key);
    String resourceName = procDef == null ? key + ".bpmn20.xml" : procDef.getResourceName();
    Deployment deployment;
    switch(format) {
        case JSON:
            JsonNode definitionNode;
            try {
                definitionNode = OBJECT_MAPPER.readTree(definition);
                if (definitionNode.has(MODEL_DATA_JSON_MODEL)) {
                    definitionNode = definitionNode.get(MODEL_DATA_JSON_MODEL);
                }
                if (!definitionNode.has(BpmnJsonConverter.EDITOR_CHILD_SHAPES)) {
                    throw new IllegalArgumentException("Could not find JSON node " + BpmnJsonConverter.EDITOR_CHILD_SHAPES);
                }
                BpmnModel bpmnModel = new BpmnJsonConverter().convertToBpmnModel(definitionNode);
                deployment = FlowableDeployUtils.deployDefinition(engine, resourceName, new BpmnXMLConverter().convertToXML(bpmnModel));
            } catch (Exception e) {
                throw new WorkflowException("While creating or updating process " + key, e);
            }
            break;
        case XML:
        default:
            deployment = FlowableDeployUtils.deployDefinition(engine, resourceName, definition.getBytes());
    }
    procDef = getProcessDefinitionByDeploymentId(deployment.getId());
    if (!key.equals(procDef.getKey())) {
        throw new WorkflowException("Mismatching key: expected " + key + ", found " + procDef.getKey());
    }
    FlowableDeployUtils.deployModel(engine, procDef);
}
Also used : WorkflowException(org.apache.syncope.core.workflow.api.WorkflowException) Deployment(org.flowable.engine.repository.Deployment) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) JsonNode(com.fasterxml.jackson.databind.JsonNode) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) SyncopeClientException(org.apache.syncope.common.lib.SyncopeClientException) WorkflowException(org.apache.syncope.core.workflow.api.WorkflowException) NotFoundException(org.apache.syncope.core.persistence.api.dao.NotFoundException) FlowableException(org.flowable.engine.common.api.FlowableException) IOException(java.io.IOException) InvalidEntityException(org.apache.syncope.core.persistence.api.attrvalue.validation.InvalidEntityException) ParsingValidationException(org.apache.syncope.core.persistence.api.attrvalue.validation.ParsingValidationException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter)

Example 3 with BpmnJsonConverter

use of org.flowable.editor.language.json.converter.BpmnJsonConverter in project plumdo-work by wengwh.

the class DeployModelCmd method execute.

@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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Deployment(org.flowable.engine.repository.Deployment) FlowableObjectNotFoundException(org.flowable.engine.common.api.FlowableObjectNotFoundException) FlowableException(org.flowable.engine.common.api.FlowableException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) FlowableObjectNotFoundException(org.flowable.engine.common.api.FlowableObjectNotFoundException) FlowableException(org.flowable.engine.common.api.FlowableException) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) DeploymentBuilder(org.flowable.engine.repository.DeploymentBuilder) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) RepositoryService(org.flowable.engine.RepositoryService)

Example 4 with BpmnJsonConverter

use of org.flowable.editor.language.json.converter.BpmnJsonConverter in project plumdo-work by wengwh.

the class ModelXmlResource method getModelXml.

@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;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) JsonNode(com.fasterxml.jackson.databind.JsonNode) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 5 with BpmnJsonConverter

use of org.flowable.editor.language.json.converter.BpmnJsonConverter in project plumdo-work by wengwh.

the class ProcessDefinitionToModelResource method processDefinitionToModel.

@RequestMapping(value = "/process-definition/{processDefinitionId}/toModel", method = RequestMethod.PUT, produces = "application/json", name = "流程定义转换模型")
@ResponseStatus(value = HttpStatus.OK)
@Transactional(propagation = Propagation.REQUIRED)
public ModelResponse processDefinitionToModel(@PathVariable String processDefinitionId) {
    ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
    try {
        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);
        Model 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(repositoryService.getProcessDiagram(processDefinition.getId())));
        return restResponseFactory.createModelResponse(modelData);
    } catch (Exception e) {
        throw new FlowableException("Error converting process-definition to model", e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) FlowableException(org.flowable.engine.common.api.FlowableException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) FlowableException(org.flowable.engine.common.api.FlowableException) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) XMLInputFactory(javax.xml.stream.XMLInputFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

BpmnModel (org.flowable.bpmn.model.BpmnModel)9 BpmnJsonConverter (org.flowable.editor.language.json.converter.BpmnJsonConverter)9 BpmnXMLConverter (org.flowable.bpmn.converter.BpmnXMLConverter)7 FlowableException (org.flowable.engine.common.api.FlowableException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 Model (org.flowable.engine.repository.Model)6 InputStream (java.io.InputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 InputStreamReader (java.io.InputStreamReader)4 XMLStreamReader (javax.xml.stream.XMLStreamReader)4 XMLInputFactory (javax.xml.stream.XMLInputFactory)3 Deployment (org.flowable.engine.repository.Deployment)3 ProcessDefinition (org.flowable.engine.repository.ProcessDefinition)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 WorkflowException (org.apache.syncope.core.workflow.api.WorkflowException)2 RepositoryService (org.flowable.engine.RepositoryService)2 FlowableObjectNotFoundException (org.flowable.engine.common.api.FlowableObjectNotFoundException)2 ProcessDiagramGenerator (org.flowable.image.ProcessDiagramGenerator)2