Posted on 2010-10-02 11:15 
penngo 阅读(8057) 
评论(5)  编辑  收藏  所属分类: 
JBPM 
			 
			
		 
		上一篇
在spring mvc下发布jbpm流程只介绍了发布jpdl的流程定义文件,并没有把流程图也一起发布,本篇将把流程定义文件和流程图一起打包为zip格式发布。
先介绍
jbpm流程设计器开发(3)的代码修改
com.workflow.designer.view.Menu.java代码,主要是增加生成图片和把jpdl和图片打包为zip文件。
 1
 saveItem.addActionListener(new ActionListener()
saveItem.addActionListener(new ActionListener() {
{
 2 public void actionPerformed(ActionEvent e)
            public void actionPerformed(ActionEvent e)
 3
 
             {
{
 4 JFileChooser fileChooser = new JFileChooser();
                JFileChooser fileChooser = new JFileChooser();
 5 fileChooser.setFileFilter(new FileNameExtensionFilter("zip", "zip"));
                fileChooser.setFileFilter(new FileNameExtensionFilter("zip", "zip"));
 6 int result = fileChooser.showSaveDialog(Menu.this);
                int result = fileChooser.showSaveDialog(Menu.this);
 7
 if(result == fileChooser.APPROVE_OPTION)
                if(result == fileChooser.APPROVE_OPTION) {
{
 8 String path = fileChooser.getSelectedFile().getAbsolutePath();
                    String path = fileChooser.getSelectedFile().getAbsolutePath();
 9 Map<String, InputStream> map = new Hashtable<String, InputStream>();
                    Map<String, InputStream> map = new Hashtable<String, InputStream>();
10 SaveJpdl saveJpdl = new SaveJpdl();
                    SaveJpdl saveJpdl = new SaveJpdl();
11 saveJpdl.save(map, gv.getGraph());
                    saveJpdl.save(map, gv.getGraph());    
12
 try
                    try {
{
13 Color bg = null;
                        Color bg = null;
14 bg = gv.getBackground();
                        bg = gv.getBackground();
15 BufferedImage image = mxCellRenderer
                        BufferedImage image = mxCellRenderer
16 .createBufferedImage(gv.getGraph(), null, 1, bg,
                        .createBufferedImage(gv.getGraph(), null, 1, bg,
17 gv.isAntiAlias(), gv.getLayoutAreaSize(),
                                gv.isAntiAlias(), gv.getLayoutAreaSize(),
18 gv.getCanvas());
                                gv.getCanvas());
19 ByteArrayOutputStream bs = new ByteArrayOutputStream();
                        ByteArrayOutputStream bs = new ByteArrayOutputStream(); 
20 ImageOutputStream imOut = ImageIO.createImageOutputStream(bs);
                        ImageOutputStream imOut = ImageIO.createImageOutputStream(bs); 
21 ImageIO.write(image, "png", imOut);
                        ImageIO.write(image, "png", imOut);
22 InputStream is = new ByteArrayInputStream(bs.toByteArray());
                        InputStream is = new ByteArrayInputStream(bs.toByteArray());
23 map.put("process.png", is);
                        map.put("process.png", is);
24 ZipUtil.saveZip(path + ".zip", map);
                        ZipUtil.saveZip(path + ".zip", map);
25 }
                    }
26
 catch(Exception ex)
                    catch(Exception ex) {
{
27 ex.printStackTrace();
                        ex.printStackTrace();
28 }
                    }
29 }
                }
30 }
            }
31 });
        }); 
ZipUtil生成zip文件。
 1
 public class ZipUtil
public class ZipUtil  {
{
 2 //调用该方法生成zip文件
    //调用该方法生成zip文件
 3
 public static void saveZip(String fileName, Map<String, InputStream> dataMap)
    public static void saveZip(String fileName, Map<String, InputStream> dataMap)  {
{
 4
 try
        try  {
{
 5 FileOutputStream fos = new FileOutputStream(fileName);
            FileOutputStream fos = new FileOutputStream(fileName);
 6 JarOutputStream jos = new JarOutputStream(fos);
            JarOutputStream jos = new JarOutputStream(fos);
 7 byte buf[] = new byte[256];
            byte buf[] = new byte[256];
 8
 if (dataMap != null)
            if (dataMap != null)  {
{
 9 Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
                Set<Entry<String, InputStream>> entrySet = dataMap.entrySet();
10 int len = -1;
                int len = -1;
11
 for (Entry<String, InputStream> entry : entrySet)
                for (Entry<String, InputStream> entry : entrySet)  {
{
12 String name = entry.getKey();
                    String name = entry.getKey();
13 InputStream inputStream = entry.getValue();
                    InputStream inputStream = entry.getValue();
14
 if (name != null && inputStream != null)
                    if (name != null && inputStream != null)  {
{
15 BufferedInputStream bis = new BufferedInputStream(
                        BufferedInputStream bis = new BufferedInputStream(
16 inputStream);
                                inputStream);
17 JarEntry jarEntry = new JarEntry(name);
                        JarEntry jarEntry = new JarEntry(name);
18 jos.putNextEntry(jarEntry);
                        jos.putNextEntry(jarEntry);
19
20
 while ((len = bis.read(buf)) >= 0)
                        while ((len = bis.read(buf)) >= 0)  {
{
21 jos.write(buf, 0, len);
                            jos.write(buf, 0, len);
22 }
                        }
23
24 bis.close();
                        bis.close();
25 jos.closeEntry();
                        jos.closeEntry();
26 }
                    }
27 }
                }
28 }
            }
29 jos.flush();
            jos.flush();
30 jos.close();
            jos.close();
31 fos.flush();
            fos.flush();
32 fos.close();
            fos.close();
33
 } catch (Exception ex)
        } catch (Exception ex)  {
{
34 throw new RuntimeException(ex);
            throw new RuntimeException(ex);
35 }
        }
36 }
    }
37 }
} 
继续使用上一篇
在spring mvc下发布jbpm流程代码发布流程
类com.mvc.rest.RestController修改
 1 @Controller
@Controller
 2
 public class RestController
public class RestController  {
{
 3
 4
 public RestController()
    public RestController()  {
{
 5
 6 }
    }
 7
 8 @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
 9
 public String registPost()
    public String registPost()  {
{
10 return "/welcome";
        return "/welcome";
11 }
    }
12
13 @RequestMapping(value = "/deploy", method = RequestMethod.GET)
    @RequestMapping(value = "/deploy", method = RequestMethod.GET)
14
 public String deployRest()
    public String deployRest()  {
{
15 return "/deploy";
        return "/deploy";
16 }
    }
17
18
19 @RequestMapping(value = "/end/{processId}", method = RequestMethod.GET)
    @RequestMapping(value = "/end/{processId}", method = RequestMethod.GET)
20 public void deployend(HttpServletRequest request,
    public void deployend(HttpServletRequest request,
21
 HttpServletResponse response, @PathVariable("processId") String processId)
            HttpServletResponse response, @PathVariable("processId") String processId)  {
{    
22 ProcessEngine processEngine = (ProcessEngine) SpringContextTool
        ProcessEngine processEngine = (ProcessEngine) SpringContextTool
23 .getBean("processEngine");
        .getBean("processEngine");
24 ProcessInstance processInstance = processEngine.getExecutionService().startProcessInstanceByKey("myprocess");
        ProcessInstance processInstance = processEngine.getExecutionService().startProcessInstanceByKey("myprocess");
25 ProcessDefinition processDefinition = processEngine.getRepositoryService().createProcessDefinitionQuery()
        ProcessDefinition processDefinition = processEngine.getRepositoryService().createProcessDefinitionQuery()
26 .processDefinitionId(processInstance.getProcessDefinitionId())
      .processDefinitionId(processInstance.getProcessDefinitionId())
27 .uniqueResult();
      .uniqueResult();
28 //获取流程图
        //获取流程图
29 InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getImageResourceName());
        InputStream inputStream = processEngine.getRepositoryService().getResourceAsStream(processDefinition.getDeploymentId(), processDefinition.getImageResourceName());
30 byte[] b = new byte[256];
        byte[] b = new byte[256];
31 int len = -1;
        int len = -1;
32
 try
        try {
{
33
 while((len = inputStream.read(b)) > 0)
             while((len = inputStream.read(b)) > 0)  {
{
34 response.getOutputStream().write(b, 0, len);
                 response.getOutputStream().write(b, 0, len);
35 }
             }
36 
             
37 inputStream.close();
             inputStream.close();
38 response.getOutputStream().flush();
             response.getOutputStream().flush();
39 response.getOutputStream().close();
             response.getOutputStream().close();
40 }
        }
41
 catch(Exception e)
       catch(Exception e) {
{
42 e.printStackTrace();
           e.printStackTrace();
43 }
       }
44 }
    }
45 
    
46 @RequestMapping(value = "/deployAction", method = RequestMethod.POST)
    @RequestMapping(value = "/deployAction", method = RequestMethod.POST)
47 public ModelAndView deployAction(HttpServletRequest request,
    public ModelAndView deployAction(HttpServletRequest request,
48
 HttpServletResponse response, ModelMap modelMap)
            HttpServletResponse response, ModelMap modelMap)  {
{
49 String realPath = request.getSession().getServletContext().getRealPath(
        String realPath = request.getSession().getServletContext().getRealPath(
50 "")
                "")
51 + "/WEB-INF/deploy/";
                + "/WEB-INF/deploy/"; 
52
 try
        try  {
{
53
 if (ServletFileUpload.isMultipartContent(request))
            if (ServletFileUpload.isMultipartContent(request))  {
{
54 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
                MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
55 for (Iterator it = multipartRequest.getFileNames(); it
                for (Iterator it = multipartRequest.getFileNames(); it
56
 .hasNext();)
                        .hasNext();)  {
{
57 String key = (String) it.next();
                    String key = (String) it.next();
58 MultipartFile file = multipartRequest.getFile(key);
                    MultipartFile file = multipartRequest.getFile(key);
59
 if (file.getOriginalFilename().length() > 0)
                    if (file.getOriginalFilename().length() > 0)  {
{
60 String filename = file.getOriginalFilename();
                        String filename = file.getOriginalFilename();
61 
                        
62 File saveFile = new File(realPath + filename);
                        File saveFile = new File(realPath + filename);
63 FileOutputStream fos = new FileOutputStream(saveFile);
                        FileOutputStream fos = new FileOutputStream(saveFile);
64 fos.write(file.getBytes());
                        fos.write(file.getBytes());
65 fos.flush();
                        fos.flush();
66 fos.close();
                        fos.close();
67 ProcessEngine processEngine = (ProcessEngine) SpringContextTool
                        ProcessEngine processEngine = (ProcessEngine) SpringContextTool
68 .getBean("processEngine");
                                .getBean("processEngine");
69 RepositoryService rs = processEngine.getRepositoryService();
                        RepositoryService rs = processEngine.getRepositoryService();
70 File deployFile = new File(saveFile.getAbsolutePath());
                        File deployFile = new File(saveFile.getAbsolutePath());
71
 if (deployFile.exists())
                        if (deployFile.exists())  {
{
72 // 发布流程
                            // 发布流程 
73 FileInputStream in = new FileInputStream(deployFile.getAbsolutePath());
                            FileInputStream in = new FileInputStream(deployFile.getAbsolutePath());
74 ZipInputStream zin = new ZipInputStream(in);
                            ZipInputStream zin = new ZipInputStream(in);
75 String deploymentId = processEngine
                            String deploymentId = processEngine
76 .getRepositoryService().createDeployment().addResourcesFromZipInputStream(zin).deploy();
                            .getRepositoryService().createDeployment().addResourcesFromZipInputStream(zin).deploy();
77
78 modelMap.put("deploy", "发布成功,版本号为:" + deploymentId);
                            modelMap.put("deploy", "发布成功,版本号为:" + deploymentId);
79 in.close();
                            in.close();
80 zin.close();
                            zin.close();
81 }
                        }
82 }
                    }
83 }
                }
84 }
            }
85
 } catch (Exception e)
        } catch (Exception e)  {
{
86 modelMap.put("deploy", "发布失败!" );
            modelMap.put("deploy", "发布失败!" );
87 e.printStackTrace();
            e.printStackTrace();
88 }
        }
89
90 return new ModelAndView("/end", modelMap);
        return new ModelAndView("/end", modelMap);
91 }
    }
92 }
} 
WEB-INF/view增加文件end.jsp,主要用来显示流程图
 1 <html>
<html>
 2 <head>
<head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=GBK">
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
 4 <title>流程发布</title>
<title>流程发布</title>
 5 </head>
</head>
 6 <body>
<body>
 7 <label><%=request.getAttribute("deploy")%></label>
<label><%=request.getAttribute("deploy")%></label>
 8 <img src="end/myprocess" />
<img src="end/myprocess" />
 9 </body>
</body>
10 </html>
</html> 
运行效果:
设计器生成带jpdl和图片的zip流程包。

把该流程保存为Test.zip。
 
 

终于在国庆有空,把这一篇也写了。正好在国庆把做过的东西,整理下。
设计器:
设计器程序
源码:
jbpmspring