use of org.flowable.engine.common.api.FlowableException 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;
}
use of org.flowable.engine.common.api.FlowableException in project plumdo-work by wengwh.
the class ProcessDefinitionResource method createProcessDefinition.
@RequestMapping(value = "/process-definition", method = RequestMethod.POST, produces = "application/json", name = "流程定义创建")
@ResponseStatus(value = HttpStatus.CREATED)
public ProcessDefinitionResponse createProcessDefinition(@RequestParam(value = "tenantId", required = false) String tenantId, HttpServletRequest request) {
if (request instanceof MultipartHttpServletRequest == false) {
throw new FlowableIllegalArgumentException("Multipart request is required");
}
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
if (multipartRequest.getFileMap().size() == 0) {
throw new FlowableIllegalArgumentException("Multipart request with file content is required");
}
MultipartFile file = multipartRequest.getFileMap().values().iterator().next();
try {
DeploymentBuilder deploymentBuilder = repositoryService.createDeployment();
String fileName = file.getOriginalFilename();
if (StringUtils.isEmpty(fileName) || !(fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn") || fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip"))) {
fileName = file.getName();
}
if (fileName.endsWith(".bpmn20.xml") || fileName.endsWith(".bpmn")) {
deploymentBuilder.addInputStream(fileName, file.getInputStream());
} else if (fileName.toLowerCase().endsWith(".bar") || fileName.toLowerCase().endsWith(".zip")) {
deploymentBuilder.addZipInputStream(new ZipInputStream(file.getInputStream()));
} else {
throw new FlowableIllegalArgumentException("File must be of type .bpmn20.xml, .bpmn, .bar or .zip");
}
deploymentBuilder.name(fileName);
if (tenantId != null) {
deploymentBuilder.tenantId(tenantId);
}
String deploymentId = deploymentBuilder.deploy().getId();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploymentId).singleResult();
return restResponseFactory.createProcessDefinitionResponse(processDefinition);
} catch (Exception e) {
if (e instanceof FlowableException) {
throw (FlowableException) e;
}
throw new FlowableException(e.getMessage(), e);
}
}
use of org.flowable.engine.common.api.FlowableException 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);
}
}
use of org.flowable.engine.common.api.FlowableException 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);
}
}
use of org.flowable.engine.common.api.FlowableException in project plumdo-work by wengwh.
the class ProcessDefinitionImageResource method getProcessDefinitionImage.
@RequestMapping(value = "/process-definition/{processDefinitionId}/image", method = RequestMethod.GET, name = "流程定义流程图")
public ResponseEntity<byte[]> getProcessDefinitionImage(@PathVariable String processDefinitionId) {
ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId);
InputStream imageStream = repositoryService.getProcessDiagram(processDefinition.getId());
if (imageStream != null) {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setContentType(MediaType.IMAGE_PNG);
try {
return new ResponseEntity<byte[]>(IOUtils.toByteArray(imageStream), responseHeaders, HttpStatus.OK);
} catch (Exception e) {
throw new FlowableException("Error reading image stream", e);
}
} else {
throw new FlowableIllegalArgumentException("Process definition with id '" + processDefinition.getId() + "' has no image.");
}
}
Aggregations