1、JBPM实用2 查看文章 完整工作流JBPM项目实战全过程教程2-项目详细讲解2009年09月16日 星期三 下午 06:15当数据库建立完成后,下载源代码,部署到服务器后,即可以开始体验完整的JBPM+SSH应用了。 项目采用的是ssh+jbpm开发模式,而且,项目功能比较齐全,代码量比较大,所以,本教程就不介绍开发过程了。 如果你对SSH整合开发应用已经够熟悉了的话,相信本项目对你来说将并不会有多大难度 。 下面我们把重点放在JBPM上。当然你得先把项目部署成功,才好边做边理解jbpm是怎么在项目中管理流程的。 ok,假定你已经把项目跑起来了吧。 先以管理员manager登录系统,管理员具
2、有添加新文章类型的权限。当你添加一个文章类型后,需要指定该类型的文章到时候是按哪个流程来进行审批的。你需要上传一个zip格式的流程定义文件文件(其中压缩了gpd.xml,processdifinition.xml,和processimage.jpg)。点击发布,系统转到articletypeaddsub.do,执行ArticleTypeAddSubAction.java.import org.jbpm.JbpmConfiguration; import org.jbpm.JbpmContext; import org.jbpm.graph.def.ProcessDefinition;. /*
3、* * 增加文章类型操作Action类 * * struts.action path=/articletypeaddsub scope=request validate=false */ public class ArticleTypeAddSubAction extends Action private static final Log log = LogFactory.getLog(MainAction.class); /* * JBPM服务接口实现对象 */private JbpmConfiguration jbpmConfiguration; /参见spring的配置文件applica
4、tionContext.xml /* * 文章类型服务接口实现对象 */ private ArticleTypeService articleTypeService; /* * Method execute * * param mapping * param form * param request * param response * return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServl
5、etResponse response) log.debug(MainAction.execute(); UploadDeployForm theForm = (UploadDeployForm) form; FormFile file = theForm.getFiles();/ 取得上传的文件 /得到JBPM环境JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); try /解压zip文件流 ZipInputStream zipInputStream = new ZipInputStream(file .getIn
6、putStream(); ProcessDefinition processDefinition = ProcessDefinition .parseParZipInputStream(zipInputStream); /发布流程文件 jbpmContext.deployProcessDefinition(processDefinition); jbpmContext.close(); zipInputStream.close(); /增加文章类型 ArticleType articleType = new ArticleType(); articleType.setPdName(proces
7、sDefinition.getName(); articleType.setTypeName(theForm.getTypeName(); articleTypeService.addArticleType(articleType); catch (IOException e) log.error(exception, e); request.setAttribute(success,发布成功); return mapping.findForward(success); /* * 得到JBPM服务接口实现对象 * return jbpmConfiguration */ public JbpmC
8、onfiguration getJbpmConfiguration() return jbpmConfiguration; /* * 设置JBPM服务接口实现对象 * param jbpmConfiguration * 要设置的 jbpmConfiguration */ public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) this.jbpmConfiguration = jbpmConfiguration; /* * 得到文章类型服务接口实现对象 * return articleTypeService */
9、 public ArticleTypeService getArticleTypeService() return articleTypeService; /* * 设置文章类型服务接口实现对象 * param articleTypeService 要设置的 articleTypeService */ public void setArticleTypeService(ArticleTypeService articleTypeService) this.articleTypeService = articleTypeService; 执行到这步后,你可以去查看下数据库中表jbpm_proce
10、ssdifinition,你会发现表中多出里一条记录,并且名字就是你上传的压缩文件中processdifinition.xml中的name属性的值。其他的表也会有相应的变化,具体看字段的定义就会大概明白了。 好了,流程已经发布到了系统中了。当然你还可以增加其他的文章类型并且指定不同的流程定义。 我们退出系统,以guest用户登录系统,然后编写文章,这里需要说明的是,当你选择不同的文章类型后,该文章的审批过程就会与你刚才定义的执行流程相关了。 点击保存, 系统调用的ACTION为: MyArticleAddSubAction.java.import org.jbpm.JbpmConfigurat
11、ion; import org.jbpm.JbpmContext; import org.jbpm.graph.def.ProcessDefinition; import org.jbpm.graph.exe.ProcessInstance; import org.jbpm.graph.exe.Token; . /* * * 撰写文章操作Action类 * struts.action path=/myarticleaddsub scope=request validate=false */ public class MyArticleAddSubAction extends Action pr
12、ivate static final Log log = LogFactory.getLog(MyArticleAddSubAction.class); /* * 文章服务接口实现对象 */ private ArticleService articleService; /* * 文章类型服务接口实现对象 */ private ArticleTypeService articleTypeService; /* * JBPM服务接口实现对象 */ private JbpmConfiguration jbpmConfiguration; /* * Method execute * param map
13、ping * param form * param request * param response * return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) log.debug(MyArticleAction.execute(); UserSession userSesssion = UserSession.getSession(request,
14、response); /得到文章信息 String stypeNo = request.getParameter(typeNo); int typeNo = ConvertUtil.convertInt(stypeNo); String articleName = request.getParameter(articleName); String content = request.getParameter(content); Article article = new Article(); article.setArticleName(articleName); article.setCon
15、tent(content); article.setState(Article.EDITED); article.setTypeNo(new Integer(typeNo); article.setUserNo(new Integer(userSesssion.getUserNo();/得到相应的文章类型 ArticleType articleType = articleTypeService.getArticleType(article.getTypeNo().intValue(); /得到相应的流程定义,启动流程实例 JbpmContext jbpmContext = jbpmConfig
16、uration.createJbpmContext(); ProcessDefinition processDefinition = jbpmContext.getGraphSession().findLatestProcessDefinition(articleType.getPdName(); ProcessInstance processInstance = new ProcessInstance(processDefinition); /让流程往下进行一步 Token token = processInstance.getRootToken(); token.signal(); /保存
17、流程实例与状态 jbpmContext.save(processInstance); jbpmContext.save(token); jbpmContext.close(); article.setPiId(processInstance.getId(); /增加文章 articleService.addArticle(article); return mapping.findForward(success); /* * 得到文章服务接口实现对象 * return articleService */ public ArticleService getArticleService() retu
18、rn articleService; /* * 设置文章服务接口实现对象 * param articleService 要设置的 articleService */ public void setArticleService(ArticleService articleService) this.articleService = articleService; /* * 得到文章类型服务接口实现对象 * return articleTypeService */ public ArticleTypeService getArticleTypeService() return articleTyp
19、eService; /* * 设置文章类型服务接口实现对象 * param articleTypeService 要设置的 articleTypeService */ public void setArticleTypeService(ArticleTypeService articleTypeService) this.articleTypeService = articleTypeService; /* * 得到JBPM服务接口实现对象 * return jbpmConfiguration */ public JbpmConfiguration getJbpmConfiguration()
20、 return jbpmConfiguration; /* * 设置JBPM服务接口实现对象 * param jbpmConfiguration 要设置的 jbpmConfiguration */ public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) this.jbpmConfiguration = jbpmConfiguration; 执行该action后,则就创建了一个与之匹配的流程实例。 查看数据库中article表的变化。可以发现 PiId记录了一个数字编号,同时jbpm_processinstanc
21、e表中最大的一个id号与之匹配,这说明当保存文章时,系统后台创建了一个流程实例,该流程实例就是记录该文章的审批过程的JBPM实例。(你可以运行一个文章的审批全过程来跟踪与之匹配的流程实例变化情况)。 下一步就是发布该文章了。 到你的文章列表中点击“发布”。 系统调用ACTION:MyArticlePubAction.java继而转到MyArticlePubSubAction.java .import org.jbpm.JbpmConfiguration; import org.jbpm.JbpmContext; import org.jbpm.graph.def.Tra
22、nsition; import org.jbpm.graph.exe.ProcessInstance; import org.jbpm.graph.exe.Token;. /* * * 发布文章操作Action类 * struts.action path=/myarticle scope=request validate=false */ public class MyArticlePubAction extends Action private static final Log log = LogFactory.getLog(MyArticlePubAction.class); privat
23、e ArticleService articleService; private ArticleTypeService articleTypeService; private JbpmConfiguration jbpmConfiguration; /* * Method execute * param mapping * param form * param request * param response * return ActionForward */ public ActionForward execute(ActionMapping mapping, ActionForm form
24、, HttpServletRequest request, HttpServletResponse response) log.debug(MyArticleAction.execute(); UserSession userSesssion = UserSession.getSession(request, response); /得到文章信息 /得到文章号 String sarticleNo = request.getParameter(articleNo); int articleNo = ConvertUtil.convertInt(sarticleNo); Article artic
25、le = articleService.getArticle(articleNo); request.setAttribute(article, article); /判断是否是此用户文章 if(article.getUserNo() != null & article.getUserNo().intValue() = userSesssion.getUserNo() /创建相应的流程实例 /得到相应的文章类型 ArticleType articleType = articleTypeService.getArticleType(article.getTypeNo().intValue();
26、request.setAttribute(articleType, articleType); /得到相应的流程 JbpmContext jbpmContext = jbpmConfiguration.createJbpmContext(); ProcessInstance processInstance = jbpmContext.getProcessInstance(article.getPiId(); log.error(instance: + processInstance.getId(); /得到当前的执行令牌 Token token = processInstance.getRoo
27、tToken(); /得到当前的可执行转换 /Set transitions = token.getNode().getArrivingTransitions(); List transitions = token.getNode().getLeavingTransitions(); Set transitionnames = new HashSet(); if(transitions != null) for(int i=0; itransitions.size(); i+) Transition transition = (Transition)transitions.get(i); Sy
28、stem.err.println(transition.getName() + transition.getName(); transitionnames.add(transition.getName(); request.setAttribute(transitionnames, transitionnames); jbpmContext.close(); return mapping.findForward(success); public ArticleService getArticleService() return articleService; public void setAr
29、ticleService(ArticleService articleService) this.articleService = articleService; public JbpmConfiguration getJbpmConfiguration() return jbpmConfiguration; public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) this.jbpmConfiguration = jbpmConfiguration; public ArticleTypeService getA
30、rticleTypeService() return articleTypeService; public void setArticleTypeService(ArticleTypeService articleTypeService) this.articleTypeService = articleTypeService; ok,到这里,你仍然可以去查看数据库中article表的变化情况。你会发现表中的Auditstate字段由null变成了一级审批,这是为什么,因为该文章对应的流程的下一个节点就是一级审批。然后我们以one(一级审批)用户登录,就可以看到需要一级审批用户审批的文章列表了。需要说明的是:这些文
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1