Search in sources :

Example 1 with Process

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

ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 SaveModelEditorCmd (com.plumdo.flow.cmd.SaveModelEditorCmd)1 InputStreamReader (java.io.InputStreamReader)1 XMLInputFactory (javax.xml.stream.XMLInputFactory)1 XMLStreamReader (javax.xml.stream.XMLStreamReader)1 BpmnModel (org.flowable.bpmn.model.BpmnModel)1 Process (org.flowable.bpmn.model.Process)1 Model (org.flowable.engine.repository.Model)1 Transactional (org.springframework.transaction.annotation.Transactional)1 PostMapping (org.springframework.web.bind.annotation.PostMapping)1 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)1 MultipartFile (org.springframework.web.multipart.MultipartFile)1 MultipartHttpServletRequest (org.springframework.web.multipart.MultipartHttpServletRequest)1