Search in sources :

Example 11 with ProcessDefinition

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

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

the class ProcessInstanceImageResource method getProcessInstanceImage.

@RequestMapping(value = "/process-instance/{processInstanceId}/image", method = RequestMethod.GET, name = "流程实例流程图")
public ResponseEntity<byte[]> getProcessInstanceImage(@PathVariable String processInstanceId) {
    ProcessInstance processInstance = getProcessInstanceFromRequest(processInstanceId);
    ProcessDefinition pde = repositoryService.getProcessDefinition(processInstance.getProcessDefinitionId());
    if (pde != null && pde.hasGraphicalNotation()) {
        BpmnModel bpmnModel = repositoryService.getBpmnModel(pde.getId());
        ProcessDiagramGenerator diagramGenerator = processEngineConfiguration.getProcessDiagramGenerator();
        InputStream resource = diagramGenerator.generateDiagram(bpmnModel, "png", runtimeService.getActiveActivityIds(processInstance.getId()), Collections.<String>emptyList(), processEngineConfiguration.getActivityFontName(), processEngineConfiguration.getLabelFontName(), processEngineConfiguration.getAnnotationFontName(), processEngineConfiguration.getClassLoader(), 1.0);
        HttpHeaders responseHeaders = new HttpHeaders();
        responseHeaders.setContentType(MediaType.IMAGE_PNG);
        try {
            return new ResponseEntity<byte[]>(IOUtils.toByteArray(resource), responseHeaders, HttpStatus.OK);
        } catch (Exception e) {
            throw new FlowableIllegalArgumentException("Error exporting diagram", e);
        }
    } else {
        throw new FlowableIllegalArgumentException("Process instance with id '" + processInstance.getId() + "' has no graphical notation defined.");
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ProcessDiagramGenerator(org.flowable.image.ProcessDiagramGenerator) InputStream(java.io.InputStream) FlowableIllegalArgumentException(org.flowable.engine.common.api.FlowableIllegalArgumentException) ProcessInstance(org.flowable.engine.runtime.ProcessInstance) ProcessDefinition(org.flowable.engine.repository.ProcessDefinition) FlowableIllegalArgumentException(org.flowable.engine.common.api.FlowableIllegalArgumentException) BpmnModel(org.flowable.bpmn.model.BpmnModel) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

ProcessDefinition (org.flowable.engine.repository.ProcessDefinition)12 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)12 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)7 FlowableException (org.flowable.engine.common.api.FlowableException)6 InputStream (java.io.InputStream)5 BpmnModel (org.flowable.bpmn.model.BpmnModel)4 FlowableIllegalArgumentException (org.flowable.engine.common.api.FlowableIllegalArgumentException)4 FlowableObjectNotFoundException (org.flowable.engine.common.api.FlowableObjectNotFoundException)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 BpmnXMLConverter (org.flowable.bpmn.converter.BpmnXMLConverter)3 Deployment (org.flowable.engine.repository.Deployment)3 Model (org.flowable.engine.repository.Model)3 HttpHeaders (org.springframework.http.HttpHeaders)3 ResponseEntity (org.springframework.http.ResponseEntity)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 FlowableForbiddenException (com.plumdo.flow.exception.FlowableForbiddenException)2 InputStreamReader (java.io.InputStreamReader)2 XMLInputFactory (javax.xml.stream.XMLInputFactory)2 XMLStreamReader (javax.xml.stream.XMLStreamReader)2 BpmnJsonConverter (org.flowable.editor.language.json.converter.BpmnJsonConverter)2