Search in sources :

Example 6 with Model

use of org.flowable.engine.repository.Model 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 7 with Model

use of org.flowable.engine.repository.Model 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 8 with Model

use of org.flowable.engine.repository.Model in project plumdo-work by wengwh.

the class ModelResource method createModel.

@RequestMapping(value = "/model", method = RequestMethod.POST, produces = "application/json", name = "模型创建")
@ResponseStatus(value = HttpStatus.CREATED)
@Transactional(propagation = Propagation.REQUIRED)
public ModelResponse createModel(@RequestBody ModelRequest modelRequest) {
    Model model = repositoryService.newModel();
    model.setCategory(modelRequest.getCategory());
    model.setKey(modelRequest.getKey());
    model.setMetaInfo(modelRequest.getMetaInfo());
    model.setName(modelRequest.getName());
    model.setVersion(modelRequest.getVersion());
    model.setTenantId(modelRequest.getTenantId());
    repositoryService.saveModel(model);
    ObjectMapper objectMapper = new ObjectMapper();
    ObjectNode editorNode = objectMapper.createObjectNode();
    editorNode.put(EditorJsonConstants.EDITOR_STENCIL_ID, "canvas");
    editorNode.put(EditorJsonConstants.EDITOR_SHAPE_ID, "canvas");
    // 设置流程定义初始化的key和name
    ObjectNode propertieNode = objectMapper.createObjectNode();
    if (StringUtils.isNotEmpty(model.getKey())) {
        propertieNode.put(StencilConstants.PROPERTY_PROCESS_ID, model.getKey());
    } else {
        propertieNode.put(StencilConstants.PROPERTY_PROCESS_ID, "model_" + model.getId());
    }
    propertieNode.put(StencilConstants.PROPERTY_NAME, model.getName());
    editorNode.set(EditorJsonConstants.EDITOR_SHAPE_PROPERTIES, propertieNode);
    ObjectNode stencilSetNode = objectMapper.createObjectNode();
    stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
    editorNode.set("stencilset", stencilSetNode);
    try {
        repositoryService.addModelEditorSource(model.getId(), editorNode.toString().getBytes("utf-8"));
    } catch (UnsupportedEncodingException e) {
        throw new FlowableConflictException("create model exception :" + e.getMessage());
    }
    return restResponseFactory.createModelResponse(model);
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Model(org.flowable.engine.repository.Model) UnsupportedEncodingException(java.io.UnsupportedEncodingException) FlowableConflictException(com.plumdo.flow.exception.FlowableConflictException) 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 9 with Model

use of org.flowable.engine.repository.Model in project plumdo-work by wengwh.

the class ModelResource method updateModel.

@RequestMapping(value = "/model/{modelId}", method = RequestMethod.PUT, produces = "application/json", name = "模型修改")
public ModelResponse updateModel(@PathVariable String modelId, @RequestBody ModelRequest modelRequest) {
    Model model = getModelFromRequest(modelId);
    if (modelRequest.isCategoryChanged()) {
        model.setCategory(modelRequest.getCategory());
    }
    if (modelRequest.isKeyChanged()) {
        model.setKey(modelRequest.getKey());
    }
    if (modelRequest.isMetaInfoChanged()) {
        model.setMetaInfo(modelRequest.getMetaInfo());
    }
    if (modelRequest.isNameChanged()) {
        model.setName(modelRequest.getName());
    }
    if (modelRequest.isVersionChanged()) {
        model.setVersion(modelRequest.getVersion());
    }
    if (modelRequest.isTenantIdChanged()) {
        model.setTenantId(modelRequest.getTenantId());
    }
    if (modelRequest.isClearDeployChanged()) {
        if (modelRequest.getClearDeployId()) {
            model.setDeploymentId(null);
        }
    }
    repositoryService.saveModel(model);
    return restResponseFactory.createModelResponse(model);
}
Also used : Model(org.flowable.engine.repository.Model) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 10 with Model

use of org.flowable.engine.repository.Model in project plumdo-work by wengwh.

the class ModelCopyResource method copyModel.

@RequestMapping(value = "/model/{modelId}/copy", method = RequestMethod.POST, produces = "application/json", name = "模型复制")
@ResponseStatus(value = HttpStatus.OK)
@Transactional(propagation = Propagation.REQUIRED)
public ModelResponse copyModel(@PathVariable String modelId, @RequestBody(required = false) ModelRequest modelRequest) {
    Model model = getModelFromRequest(modelId);
    try {
        Model modelData = repositoryService.newModel();
        if (modelRequest != null && modelRequest.isKeyChanged()) {
            modelData.setKey(modelRequest.getKey());
        } else {
            modelData.setKey(model.getKey());
        }
        if (modelRequest != null && modelRequest.isNameChanged()) {
            modelData.setName(modelRequest.getName());
        } else {
            modelData.setName(model.getName());
        }
        if (modelRequest != null && modelRequest.isCategoryChanged()) {
            modelData.setCategory(modelRequest.getCategory());
        } else {
            modelData.setCategory(model.getCategory());
        }
        if (modelRequest != null && modelRequest.isMetaInfoChanged()) {
            modelData.setMetaInfo(modelRequest.getMetaInfo());
        } else {
            modelData.setMetaInfo(model.getMetaInfo());
        }
        if (modelRequest != null && modelRequest.isVersionChanged()) {
            modelData.setVersion(modelRequest.getVersion());
        } else {
            modelData.setVersion(model.getVersion());
        }
        if (modelRequest != null && modelRequest.isTenantIdChanged()) {
            modelData.setTenantId(modelRequest.getTenantId());
        } else {
            modelData.setTenantId(model.getTenantId());
        }
        repositoryService.saveModel(modelData);
        repositoryService.addModelEditorSource(modelData.getId(), repositoryService.getModelEditorSource(modelId));
        repositoryService.addModelEditorSourceExtra(modelData.getId(), repositoryService.getModelEditorSourceExtra(modelId));
        return restResponseFactory.createModelResponse(modelData);
    } catch (Exception e) {
        throw new FlowableException("Error copy model", e);
    }
}
Also used : FlowableException(org.flowable.engine.common.api.FlowableException) Model(org.flowable.engine.repository.Model) FlowableException(org.flowable.engine.common.api.FlowableException) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

Model (org.flowable.engine.repository.Model)11 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)10 FlowableException (org.flowable.engine.common.api.FlowableException)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)6 BpmnModel (org.flowable.bpmn.model.BpmnModel)6 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)6 BpmnJsonConverter (org.flowable.editor.language.json.converter.BpmnJsonConverter)5 Transactional (org.springframework.transaction.annotation.Transactional)5 BpmnXMLConverter (org.flowable.bpmn.converter.BpmnXMLConverter)4 InputStream (java.io.InputStream)3 Deployment (org.flowable.engine.repository.Deployment)3 ProcessDefinition (org.flowable.engine.repository.ProcessDefinition)3 ByteArrayInputStream (java.io.ByteArrayInputStream)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 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 DeployModelCmd (com.plumdo.flow.cmd.DeployModelCmd)1