Search in sources :

Example 6 with BpmnModel

use of org.flowable.bpmn.model.BpmnModel 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)

Example 7 with BpmnModel

use of org.flowable.bpmn.model.BpmnModel in project syncope by apache.

the class FlowableDeployUtils method deployModel.

public static void deployModel(final ProcessEngine engine, final ProcessDefinition procDef) {
    XMLStreamReader xtr = null;
    try (InputStream bpmnStream = engine.getRepositoryService().getResourceAsStream(procDef.getDeploymentId(), procDef.getResourceName());
        InputStreamReader isr = new InputStreamReader(bpmnStream)) {
        xtr = XMLInputFactory.newInstance().createXMLStreamReader(isr);
        BpmnModel bpmnModel = new BpmnXMLConverter().convertToBpmnModel(xtr);
        Model model = engine.getRepositoryService().newModel();
        ObjectNode modelObjectNode = OBJECT_MAPPER.createObjectNode();
        modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, procDef.getName());
        model.setMetaInfo(modelObjectNode.toString());
        model.setName(procDef.getName());
        model.setDeploymentId(procDef.getDeploymentId());
        model.setVersion(procDef.getVersion());
        engine.getRepositoryService().saveModel(model);
        engine.getRepositoryService().addModelEditorSource(model.getId(), new BpmnJsonConverter().convertToJson(bpmnModel).toString().getBytes());
    } catch (Exception e) {
        throw new WorkflowException("While importing " + procDef.getResourceName(), e);
    } finally {
        if (xtr != null) {
            try {
                xtr.close();
            } catch (XMLStreamException e) {
            // ignore
            }
        }
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) XMLStreamException(javax.xml.stream.XMLStreamException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) WorkflowException(org.apache.syncope.core.workflow.api.WorkflowException) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) WorkflowException(org.apache.syncope.core.workflow.api.WorkflowException) FlowableException(org.flowable.engine.common.api.FlowableException) XMLStreamException(javax.xml.stream.XMLStreamException) BpmnModel(org.flowable.bpmn.model.BpmnModel) BpmnXMLConverter(org.flowable.bpmn.converter.BpmnXMLConverter)

Example 8 with BpmnModel

use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.

the class ProcessDefinitionGetModelResource method processDefinitionGetModel.

@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);
    }
}
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 9 with BpmnModel

use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.

the class SaveModelEditorCmd method execute.

@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;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) InputStreamReader(java.io.InputStreamReader) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) 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) ProcessEngineConfiguration(org.flowable.engine.ProcessEngineConfiguration) ByteArrayInputStream(java.io.ByteArrayInputStream) ProcessDiagramGenerator(org.flowable.image.ProcessDiagramGenerator) BpmnJsonConverter(org.flowable.editor.language.json.converter.BpmnJsonConverter) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XMLInputFactory(javax.xml.stream.XMLInputFactory) RepositoryService(org.flowable.engine.RepositoryService)

Example 10 with BpmnModel

use of org.flowable.bpmn.model.BpmnModel in project plumdo-work by wengwh.

the class ModelImportResource method importModel.

@PostMapping(value = "/models/import", name = "流程模型导入")
@ResponseStatus(value = HttpStatus.CREATED)
@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
public ModelResponse importModel(HttpServletRequest request) {
    if (!(request instanceof MultipartHttpServletRequest)) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.REQUEST_NOT_MULTIPART);
    }
    MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
    if (multipartRequest.getFileMap().size() == 0) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.MULTIPART_CONTENT_EMPTY);
    }
    MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
    String fileName = file.getOriginalFilename();
    if (fileName == null || (!fileName.endsWith(".bpmn") && !fileName.endsWith(".bpmn20.xml"))) {
        exceptionFactory.throwIllegalArgument(ErrorConstant.FILE_NOT_BPMN, fileName);
    }
    try {
        XMLInputFactory xif = XMLInputFactory.newInstance();
        InputStreamReader xmlIn = new InputStreamReader(file.getInputStream(), "UTF-8");
        XMLStreamReader xtr = xif.createXMLStreamReader(xmlIn);
        BpmnModel bpmnModel = bpmnXmlConverter.convertToBpmnModel(xtr);
        if (ObjectUtils.isEmpty(bpmnModel.getProcesses())) {
            exceptionFactory.throwObjectNotFound(ErrorConstant.MODEL_NOT_FOUND_PROCESS, fileName);
        }
        Process process = bpmnModel.getMainProcess();
        Model modelData = repositoryService.newModel();
        modelData.setKey(process.getId());
        Model lastModel = repositoryService.createModelQuery().modelKey(modelData.getKey()).latestVersion().singleResult();
        if (lastModel == null) {
            modelData.setVersion(TableConstant.MODEL_VESION_START);
        } else {
            modelData.setVersion(lastModel.getVersion() + 1);
        }
        if (ObjectUtils.isNotEmpty(process.getName())) {
            modelData.setName(process.getName());
        } else {
            modelData.setName(process.getId());
        }
        ObjectNode metaInfo = new ObjectMapper().createObjectNode();
        metaInfo.put("name", modelData.getName());
        metaInfo.put("description", process.getDocumentation());
        modelData.setMetaInfo(metaInfo.toString());
        repositoryService.saveModel(modelData);
        managementService.executeCommand(new SaveModelEditorCmd(modelData.getId(), bpmnJsonConverter.convertToJson(bpmnModel).toString()));
        return restResponseFactory.createModelResponse(modelData);
    } catch (Exception e) {
        logger.error("导入流程文件异常", e);
        exceptionFactory.throwDefinedException(ErrorConstant.MODEL_IMPORT_FILE_ERROR, fileName, e.getMessage());
    }
    return null;
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) InputStreamReader(java.io.InputStreamReader) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Process(org.flowable.bpmn.model.Process) SaveModelEditorCmd(com.plumdo.flow.cmd.SaveModelEditorCmd) BpmnModel(org.flowable.bpmn.model.BpmnModel) MultipartFile(org.springframework.web.multipart.MultipartFile) Model(org.flowable.engine.repository.Model) BpmnModel(org.flowable.bpmn.model.BpmnModel) XMLInputFactory(javax.xml.stream.XMLInputFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) MultipartHttpServletRequest(org.springframework.web.multipart.MultipartHttpServletRequest) PostMapping(org.springframework.web.bind.annotation.PostMapping) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

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