/** * 读取流程资源 * * @param processDefinitionId 流程定义ID * @param resourceName 资源名称 */ @GetMapping(value = "/{processDefinitionId}/resource/{resourceName}") @ApiOperation("读取流程资源") @Authorize(action = Permission.ACTION_QUERY) @SneakyThrows public void readResource(@PathVariable String processDefinitionId , @PathVariable String resourceName, HttpServletResponse response) { ProcessDefinitionQuery pdq = repositoryService.createProcessDefinitionQuery(); ProcessDefinition pd = pdq.processDefinitionId(processDefinitionId).singleResult(); // 通过接口读取 try (InputStream resourceAsStream = repositoryService.getResourceAsStream(pd.getDeploymentId(), resourceName)) { StreamUtils.copy(resourceAsStream, response.getOutputStream()); } }
/** * 部署流程资源 * 加载ZIP文件中的流程 */ @PostMapping(value = "/deploy") @ApiOperation("上传流程定义文件并部署流程") @Authorize(action = "deploy") public ResponseMessage<Deployment> deploy(@RequestPart(value = "file") MultipartFile file) throws IOException { // 获取上传的文件名 String fileName = file.getOriginalFilename(); // 得到输入流(字节流)对象 InputStream fileInputStream = file.getInputStream(); // 文件的扩展名 String extension = FilenameUtils.getExtension(fileName); // zip或者bar类型的文件用ZipInputStream方式部署 DeploymentBuilder deployment = repositoryService.createDeployment(); if ("zip".equals(extension) || "bar".equals(extension)) { ZipInputStream zip = new ZipInputStream(fileInputStream); deployment.addZipInputStream(zip); } else { // 其他类型的文件直接部署 deployment.addInputStream(fileName, fileInputStream); } Deployment result = deployment.deploy(); return ResponseMessage.ok(result); }
@Override public void afterTestClass(TestContext testContext) throws Exception { RepositoryService repositoryService = testContext.getApplicationContext().getBean(RepositoryService.class); for (Deployment deployment : repositoryService.createDeploymentQuery().list()) { repositoryService.deleteDeployment(deployment.getId(), true); } }
@Override public ProcessDefinition processDefinition(String processDefinitionId) { org.activiti.engine.repository.ProcessDefinition processDefinition; // try searching by Key if there is no matching by Id List<org.activiti.engine.repository.ProcessDefinition> list = repositoryService .createProcessDefinitionQuery() .processDefinitionKey(processDefinitionId) .orderByProcessDefinitionVersion() .asc() .list(); if (!list.isEmpty()) { processDefinition = list.get(0); } else { processDefinition = repositoryService.getProcessDefinition(processDefinitionId); } return processDefinitionConverter.from(processDefinition); }
@ApiOperation(value = "List resources in a deployment", tags = {"Deployment"}, notes="The dataUrl property in the resulting JSON for a single resource contains the actual URL to use for retrieving the binary resource.") @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates the deployment was found and the resource list has been returned."), @ApiResponse(code = 404, message = "Indicates the requested deployment was not found.") }) @RequestMapping(value = "/repository/deployments/{deploymentId}/resources", method = RequestMethod.GET, produces = "application/json") public List<DeploymentResourceResponse> getDeploymentResources(@ApiParam(name = "deploymentId", value = "The id of the deployment to get the resources for.") @PathVariable String deploymentId, HttpServletRequest request) { // Check if deployment exists Deployment deployment = repositoryService.createDeploymentQuery().deploymentId(deploymentId).singleResult(); if (deployment == null) { throw new ActivitiObjectNotFoundException("Could not find a deployment with id '" + deploymentId + "'.", Deployment.class); } List<String> resourceList = repositoryService.getDeploymentResourceNames(deploymentId); return restResponseFactory.createDeploymentResourceResponseList(deploymentId, resourceList, contentTypeResolver); } }
@ApiOperation(value = "Get a process definition BPMN model", tags = {"Process Definitions"}, nickname = "getBpmnModelResource") @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates the process definition was found and the model is returned. The response contains the full process definition model."), @ApiResponse(code = 404, message = "Indicates the requested process definition was not found.") }) @RequestMapping(value = "/repository/process-definitions/{processDefinitionId}/model", method = RequestMethod.GET, produces = "application/json") public BpmnModel getModelResource(@ApiParam(name = "processDefinitionId", value="The id of the process definition to get the model for.") @PathVariable String processDefinitionId) { ProcessDefinition processDefinition = getProcessDefinitionFromRequest(processDefinitionId); return repositoryService.getBpmnModel(processDefinition.getId()); }
/** * 部署 * * @param file * @param request * @return * @throws IOException */ @RequestMapping(value = "/admin/flow/deploy") public String deploymentProcessDefinition_zip(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException { // File upload = // (File)file;org.springframework.web.multipart.commons.CommonsMultipartFile // cannot be cast to java.io.File InputStream upload = file.getInputStream(); ZipInputStream zipInputStream = new ZipInputStream(upload); repositoryService.createDeployment().addZipInputStream(zipInputStream).deploy(); return "redirect:/admin/flow/view_list.htm"; }
@RequestMapping(value = "/processdef/export/{type}/{id}", method = RequestMethod.GET) public void downloadFlow(@PathVariable("type") String type, @PathVariable("id") String id, HttpServletResponse response) { try { ProcessDefinition processDefinition = repositoryService.getProcessDefinition(id); String resourceName = ""; if (type.equals("image")) { resourceName = processDefinition.getDiagramResourceName(); } else if (type.equals("xml")) { resourceName = processDefinition.getResourceName(); } InputStream resourceAsStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), resourceName); IOUtils.copy(resourceAsStream, response.getOutputStream()); response.setHeader("Content-Disposition", "attachment; filename=" + resourceName); response.flushBuffer(); } catch (Exception e) { LOGGER.error("导出流程定义的" + type + "文件失败:processDefId={}", id, e); } }
@ApiOperation(value = "Create a new deployment", tags = {"Deployment"}, consumes = "multipart/form-data", produces = "application/json", notes = "The request body should contain data of type multipart/form-data. There should be exactly one file in the request, any additional files will be ignored. If multiple resources need to be deployed in a single deployment, compress the resources in a zip and make sure the file-name ends with .bar or .zip.\n" + "\n" + "An additional parameter (form-field) can be passed in the request body with name tenantId. The value of this field will be used as the id of the tenant this deployment is done in.") @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates the deployment was created."), @ApiResponse(code = 400, message = "Indicates there was no content present in the request body or the content mime-type is not supported for deployment. The status-description contains additional information.") }) @RequestMapping(value = "/repository/deployments", method = RequestMethod.POST, produces = "application/json") public DeploymentResponse uploadDeployment(@ApiParam(name = "tenantId") @RequestParam(value = "tenantId", required = false) String tenantId, HttpServletRequest request, HttpServletResponse response) { 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"))) {
@ApiOperation(value = "List of process definitions", tags = {"Process Definitions"}) @ApiImplicitParams({ @ApiImplicitParam(name = "version", dataType = "integer", value = "Only return process definitions with the given version.", paramType = "query"), @ApiImplicitParam(name = "sort", dataType = "string", value = "Property to sort on, to be used together with the order.", allowableValues ="name,id,key,category,deploymentId,version", paramType = "query"), }) @ApiResponses(value = { @ApiResponse(code = 200, message = "Indicates request was successful and the process-definitions are returned"), @ApiResponse(code = 400, message = "Indicates a parameter was passed in the wrong format or that latest is used with other parameters other than key and keyLike. The status-message contains additional information.") }) @RequestMapping(value = "/repository/process-definitions", method = RequestMethod.GET, produces = "application/json") public DataResponse getProcessDefinitions(@ApiParam(hidden = true) @RequestParam Map<String, String> allRequestParams, HttpServletRequest request) { ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
@ApiOperation(value = "Delete a deployment", tags = {"Deployment"}) @ApiResponses(value = { @ApiResponse(code = 204, message = "Indicates the deployment was found and has been deleted. Response-body is intentionally empty."), @ApiResponse(code = 404, message = "Indicates the requested deployment was not found.") }) @RequestMapping(value = "/repository/deployments/{deploymentId}", method = RequestMethod.DELETE, produces = "application/json") public void deleteDeployment(@ApiParam(name = "deploymentId" , value = "The id of the deployment to delete.") @PathVariable String deploymentId, @RequestParam(value = "cascade", required = false, defaultValue = "false") Boolean cascade, HttpServletResponse response) { if (cascade) { repositoryService.deleteDeployment(deploymentId, true); } else { repositoryService.deleteDeployment(deploymentId); } response.setStatus(HttpStatus.NO_CONTENT.value()); } }
@RequestMapping(value = "/processdef/delete/{delType}/{pdId}", method = RequestMethod.POST) @ResponseBody public Result deleteDeployment(@PathVariable("delType") String delType, @PathVariable("pdId") String pdId) { ProcessDefinition pd = repositoryService.getProcessDefinition(pdId); try { if ("0".equals(delType)) repositoryService.deleteDeployment(pd.getDeploymentId()); else repositoryService.deleteDeployment(pd.getDeploymentId(), true); return new Result(true, pdId, "成功删除"); } catch (Exception e) { return new Result(false, pdId, "删除失败,该流程定义已经关联了正在执行的流程"); } }
@RequestMapping(value = "/model/deploy/{id}", method = RequestMethod.POST) @ResponseBody public Result deploy(@PathVariable("id") String id, HttpServletRequest request) throws Exception { Model modelData = repositoryService.getModel(id); byte[] bytes = repositoryService.getModelEditorSource(modelData.getId()); DeploymentBuilder deploymentBuilder = repositoryService.createDeployment() .name(modelData.getName()) .category(modelData.getCategory()) ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId (deployment.getId()).singleResult(); if (processDefinition != null) repositoryService.setProcessDefinitionCategory(processDefinition.getId(), deployment.getCategory()); repositoryService.saveModel(modelData); return new Result(true); } catch (Exception ex) {
@RequestMapping(value = "/processinstance/generate/{instanceId}", method = RequestMethod.POST) @ResponseBody public Result generateImage(@PathVariable("instanceId") String instanceId, HttpServletRequest request) { bpmnModel = repositoryService.getBpmnModel(processDefinitionId); ProcessDiagramGenerator pdg = processEngine.getProcessEngineConfiguration().getProcessDiagramGenerator(); ProcessDefinition processDefinition = repositoryService.getProcessDefinition(processDefinitionId); String resourceName = instanceId + "_" + processDefinition.getDiagramResourceName(); processEngine.getProcessEngineConfiguration().getActivityFontName(), processEngine.getProcessEngineConfiguration().getLabelFontName(), processEngine.getProcessEngineConfiguration().getActivityFontName(), processEngine.getProcessEngineConfiguration().getProcessEngineConfiguration().getClassLoader(), 1);
/** * 系统中部署了多少工作流 * * @param model * @param request * @return */ @RequestMapping(value = "/admin/flow/view_list") public String processDefinitionList(Model model, String name, HttpServletRequest request) { /* * 只读取表单:leave-formkey */ if (name == null) { name = ""; } ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery().processDefinitionNameLike("%" + name + "%").latestVersion() .orderByProcessDefinitionName().desc(); List<ProcessDefinition> list = query.list(); model.addAttribute("list", list); model.addAttribute("name", name); return "/admin/flow/list"; }
@RequestMapping(value = "/admin/flow/image") public String image(Model model, String taskid, Pageable pageable, HttpServletRequest request) { Task task = taskService.createTaskQuery().taskId(taskid).singleResult(); model.addAttribute("task", task); if (task != null) { try { ProcessInstance processInstance = runtimeService.createProcessInstanceQuery() .processInstanceId(task.getProcessInstanceId()).singleResult(); ProcessDefinition definition = repositoryService.getProcessDefinition(task.getProcessDefinitionId()); ProcessDefinitionEntity processDefinitionEntity = (ProcessDefinitionEntity) definition; BpmnModel bpmnModel = repositoryService.getBpmnModel(processDefinitionEntity.getId()); GraphicInfo activityImpl = bpmnModel.getGraphicInfo(task.getTaskDefinitionKey()); model.addAttribute("x", activityImpl.getX()); model.addAttribute("y", activityImpl.getY()); model.addAttribute("width", activityImpl.getWidth()); model.addAttribute("height", activityImpl.getHeight()); } catch (Exception e) { } } return "/admin/flow/image"; }
/** * 流程定义列表 */ @GetMapping @ApiOperation("查询流程定义列表") @Authorize(action = Permission.ACTION_QUERY) public ResponseMessage<PagerResult<ProcessDefinitionInfo>> queryProcessList(QueryParamEntity param) { ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery(); return ResponseMessage.ok(QueryUtils.doQuery(processDefinitionQuery, param, ProcessDefinitionInfo::of)); }
@ApiOperation("流程定义转换模型") @Authorize(action = Permission.ACTION_UPDATE) public ResponseMessage<String> convertToModel(@PathVariable("processDefinitionId") String processDefinitionId) throws UnsupportedEncodingException, XMLStreamException { ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery() .processDefinitionId(processDefinitionId).singleResult(); if (null == processDefinition) { throw new NotFoundException(); InputStream bpmnStream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getResourceName()); org.activiti.engine.repository.Model modelData = repositoryService.newModel(); modelData.setKey(processDefinition.getKey()); modelData.setName(processDefinition.getResourceName().substring(0, processDefinition.getResourceName().indexOf("."))); modelData.setMetaInfo(modelObjectNode.toString()); repositoryService.saveModel(modelData); repositoryService.addModelEditorSource(modelData.getId(), modelNode.toString().getBytes("utf-8")); return ResponseMessage.ok(modelData.getId());
/** * 删除部署的流程,如果流程下有正在运行的流程实例则报错 * * @param deploymentId 流程部署ID */ @DeleteMapping(value = "/deployment/{deploymentId}") @ApiOperation("删除部署的流程") @Authorize(action = Permission.ACTION_DELETE) public ResponseMessage<Void> deleteProcessDefinition( @PathVariable("deploymentId") String deploymentId , @RequestParam(defaultValue = "false") boolean cascade) { repositoryService.deleteDeployment(deploymentId, cascade); return ResponseMessage.ok(); }
@PostMapping("/{modelId}/deploy") @ApiOperation("发布模型") @Authorize(action = "deploy") public ResponseMessage<Deployment> deployModel(@PathVariable String modelId) throws Exception { Model modelData = repositoryService.getModel(modelId); if (modelData == null) { throw new NotFoundException("模型不存在!"); } ObjectNode modelNode = (ObjectNode) new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelData.getId())); BpmnModel model = new BpmnJsonConverter().convertToBpmnModel(modelNode); byte[] bpmnBytes = new BpmnXMLConverter().convertToXML(model); String processName = modelData.getName() + ".bpmn20.xml"; Deployment deployment = repositoryService.createDeployment() .name(modelData.getName()) .addString(processName, new String(bpmnBytes, "utf8")) .deploy(); return ResponseMessage.ok(deployment).include(Deployment.class, "id", "name", "new"); }