Search in sources :

Example 1 with BpmnXMLConverter

use of org.flowable.bpmn.converter.BpmnXMLConverter in project plumdo-work by wengwh.

the class DeployModelCmd method execute.

public Deployment execute(CommandContext commandContext) {
    Deployment deployment = null;
    RepositoryService repositoryService = commandContext.getProcessEngineConfiguration().getRepositoryService();
    Model model = repositoryService.getModel(modelId);
    if (model == null) {
        throw new FlowableObjectNotFoundException("Could not find a model with id '" + modelId + "'.", Model.class);
    }
    byte[] editorSource = 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();
        // 设置模型的部署ID
        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 2 with BpmnXMLConverter

use of org.flowable.bpmn.converter.BpmnXMLConverter in project plumdo-work by wengwh.

the class ModelXmlResource method getModelXml.

@RequestMapping(value = "/model/{modelId}/xml", method = RequestMethod.GET, 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<byte[]>(IOUtils.toByteArray(in), responseHeaders, HttpStatus.OK);
    } catch (Exception e) {
        throw new FlowableException("Error converting resource stream", e);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) FlowableException(org.flowable.engine.common.api.FlowableException) 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) FlowableException(org.flowable.engine.common.api.FlowableException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 3 with BpmnXMLConverter

use of org.flowable.bpmn.converter.BpmnXMLConverter in project plumdo-work by wengwh.

the class ProcessDefinitionCopyResource method copyProcessDefinition.

@RequestMapping(value = "/process-definition/{processDefinitionId}/copy", method = RequestMethod.POST, produces = "application/json", name = "复制流程定义")
@ResponseStatus(value = HttpStatus.CREATED)
public ProcessDefinitionResponse copyProcessDefinition(@PathVariable String processDefinitionId, @RequestBody(required = false) ProcessDefinitionCopyRequest processDefinitionCopyRequest) {
    ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
    try {
        String name = null;
        if (processDefinitionCopyRequest != null && processDefinitionCopyRequest.getName() != null) {
            name = processDefinitionCopyRequest.getName();
        } else {
            name = "CopyOf" + processDefinition.getName();
        }
        String key = null;
        if (processDefinitionCopyRequest != null && processDefinitionCopyRequest.getKey() != null) {
            key = processDefinitionCopyRequest.getKey();
        } else {
            // 保证key不重复使用时间戳
            key = "CopyOf" + System.currentTimeMillis();
        }
        DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
        BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinition.getId());
        bpmnModel.getMainProcess().setName(name);
        bpmnModel.getMainProcess().setId(key);
        byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(bpmnModel);
        String fileName = processDefinition.getResourceName();
        ByteArrayInputStream bis = new ByteArrayInputStream(bpmnBytes);
        deploymentBuilder.addInputStream(fileName, bis);
        deploymentBuilder.name(fileName);
        if (processDefinition.getTenantId() != null) {
            deploymentBuilder.tenantId(processDefinition.getTenantId());
        }
        String deploymentId = deploymentBuilder.deploy().getId();
        ProcessDefinition processDefinitionNew = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
        return restResponseFactory.createProcessDefinitionResponse(processDefinitionNew);
    } catch (Exception e) {
        throw new FlowableException("Error copy process-definition", e);
    }
}
Also used : FlowableException(org.flowable.engine.common.api.FlowableException) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) DeploymentBuilder(org.flowable.engine.repository.DeploymentBuilder) FlowableException(org.flowable.engine.common.api.FlowableException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with BpmnXMLConverter

use of org.flowable.bpmn.converter.BpmnXMLConverter in project plumdo-work by wengwh.

the class ProcessDefinitionGetModelResource method processDefinitionGetModel.

@RequestMapping(value = "/process-definition/{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);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) GetDeploymentProcessDiagramCmd(org.flowable.engine.impl.cmd.GetDeploymentProcessDiagramCmd) InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStream(java.io.InputStream) Deployment(org.flowable.engine.repository.Deployment) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) 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) 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) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 5 with BpmnXMLConverter

use of org.flowable.bpmn.converter.BpmnXMLConverter 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

BpmnXMLConverter (org.flowable.bpmn.converter.BpmnXMLConverter)5 BpmnModel (org.flowable.bpmn.model.BpmnModel)5 FlowableException (org.flowable.engine.common.api.FlowableException)5 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 BpmnJsonConverter (org.flowable.editor.language.json.converter.BpmnJsonConverter)4 Model (org.flowable.engine.repository.Model)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ProcessDefinition (org.flowable.engine.repository.ProcessDefinition)3 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 FlowableObjectNotFoundException (org.flowable.engine.common.api.FlowableObjectNotFoundException)2 Deployment (org.flowable.engine.repository.Deployment)2 DeploymentBuilder (org.flowable.engine.repository.DeploymentBuilder)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 RepositoryService (org.flowable.engine.RepositoryService)1