实战:流程管理

功能需求:首先查询出所有部署的流程信息上传流程图后自动部署,重新查询部署的流程信息。可以查看流程图以及删除流程信息

查询流程定义信息

@RequestMapping("process/queryProcessDefPageList")
public List<Map<String, Object>> queryProcessDefPageList(@RequestBody Map<String ,Object> paramMap){
    Integer startIndex = (Integer) paramMap.get("startIndex");
    Integer pagesize = (Integer) paramMap.get("pagesize");
    ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
    List<ProcessDefinition> processDefinitions = processDefinitionQuery.listPage(startIndex, pagesize);

    List<Map<String,Object>> res = new ArrayList<>();
    for (ProcessDefinition pd : processDefinitions) {
        Map<String,Object> map = new HashMap<>();
        map.put("pdId",pd.getId());
        map.put("pdName",pd.getName());
        map.put("pdVersion",pd.getVersion());
        map.put("pdKey",pd.getKey());
        map.put("pdDeploymentId",pd.getDeploymentId());

        res.add(map);
    }
    return res;
}

上传流程图

服务消费者接收上传的流程图:

/**
 * 上传表单处理
 */
@ResponseBody
@RequestMapping("process/procDefUpload")
public Object upload(HttpServletRequest request){
    start();
    try {
        MultipartRequest req = (MultipartRequest) request;
        MultipartFile file = req.getFile("procDefFile");

        String uuid = UUID.randomUUID().toString();
        //获得上传上来的原始文件名
        String fileName = file.getOriginalFilename();
        //创建一个临时文件  用于传输到远程服务
        final File tempFile = File.createTempFile(uuid,fileName.substring(fileName.lastIndexOf(".")));

        file.transferTo(tempFile);

        FileSystemResource resource = new FileSystemResource(tempFile);
        MultiValueMap<String,Object> param = new LinkedMultiValueMap<String,Object>();
        param.add("pdfile",resource);

        String s = restTemplate.postForObject("http://lecrowdfunding-activity-service/act/deploy",param,String.class);

        tempFile.delete();
        success(true);
    } catch (IOException e) {
        e.printStackTrace();
    }

    return end();
}

服务提供者接收文件:

@RequestMapping("/act/deploy")
public String depolyProcDef( @RequestParam("pdfile") MultipartFile file ) {
    start();

    try {
        // 部署流程定义图形
        repositoryService
                .createDeployment()
                .addInputStream(file.getOriginalFilename(), file.getInputStream())
                //.addClasspathResource(file.getOriginalFilename())
                .deploy();

        success(true);
    } catch ( Exception e ) {
        e.printStackTrace();
        success(false);
    }
    return "成功";
}

查看流程定义图片

html部分:

<img id="procDefImg" src="/ztree/img/loading.gif">
$("#procDefImg").attr("src", "${APP_PATH}/process/loadImg?pdId=${RequestParameters['pdId']}");

会自动向后台发起请求请求图片 由于后台数据库中是以流的方式存储,那么直接以流的方式返回就前台就可以解析成对应的图片。

服务消费者Controller:

    /**
     * 加载流程图
     * 以流的方式返回
     */
    @RequestMapping("process/loadImg")
    public void loadImg(@RequestParam("pdId") String pdId, HttpServletResponse resp) throws IOException {
        // 通过响应对象返回图形信息
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.IMAGE_PNG);

        //请求远程服务  服务返回一个字节数组
        String url = "http://lecrowdfunding-activity-service/act/loadImgById/" + pdId;
        ResponseEntity<byte[]> response =
                restTemplate.exchange(url, HttpMethod.POST, new HttpEntity<byte[]>(headers), byte[].class);
        byte[] result = response.getBody();

        InputStream in = new ByteArrayInputStream(result);
        OutputStream out = resp.getOutputStream();

        int i = -1;
        while ((i = in.read()) != -1) {
            out.write(i);
        }
    }

服务提供者Controller:

@RequestMapping("act/loadImgById/{id}")
public byte[] loadImgById(@PathVariable("id")String id) {
       // 部署ID ==>  流程定义ID
    // 从数据库中读取流程定义的图片
    //根据流程部署id和部署资源名称获取部署图片的输入流。
    ProcessDefinitionQuery query = repositoryService.createProcessDefinitionQuery();
    ProcessDefinition pd = query.processDefinitionId(id).singleResult();
    InputStream in =
            repositoryService.getResourceAsStream(pd.getDeploymentId(),pd.getDiagramResourceName());

    ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
    byte[] buff = new byte[100]; //buff用于存放循环读取的临时数据
    int rc = 0;
    try {
        while ((rc = in.read(buff, 0, 100)) > 0) {
            swapStream.write(buff, 0, rc);
        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    byte[] in_b = swapStream.toByteArray(); //in_b为转换之后的结果

    return in_b;
}

删除流程信息

服务消费者:

/**
 * 删除流程定义
 * @return
 */
@ResponseBody
@RequestMapping("process/deleteDeployment")
public Object deleteDeployment(String pdDeploymentId){
    Object o = processService.deleteDeployment(pdDeploymentId);
    return o;
}

服务提供者Controller:

/**
 * 删除流程
 * @param pdDeploymentId :流程部署id
 */
@RequestMapping("process/deleteDeployment")
public Object deleteDeployment(@RequestParam("pdDeploymentId")String pdDeploymentId){
    start();
    try {
        repositoryService.deleteDeployment(pdDeploymentId, true); //流程部署id.  级联删除.
        success(true);
    } catch ( Exception e ) {
        e.printStackTrace();
        success(false);
    }
    return end();
}
01-26 00:27