网上随便找了个请假的流程图,在此先谢谢提供图片的人:
使用jbpm工具画出流程图,中文好像是乱码,所以改为英文:
leave.jpdl.xml内容:
- <?xml version="1.0" encoding="UTF-8"?>
-
- <process name="leave" xmlns="http://jbpm.org/4.3/jpdl">
- <start g="159,47,48,48" name="start1">
- <transition to="exclusive1"/>
- </start>
- <decision expr="#{job}" g="161,152,48,48" name="exclusive1">
- <transition g="42,179:43,-27" name="isChief" to="boosApprove"/>
- <transition g="316,175:-83,-23" name="isnotChief" to="chiefApprove"/>
- </decision>
- <state g="-3,220,92,52" name="boosApprove">
- <transition g="47,340:" to="sendEmail"/>
- </state>
- <state g="270,214,92,52" name="chiefApprove">
- <transition to="exclusive2"/>
- </state>
- <decision expr="#{day}" g="160,219,48,48" name="exclusive2">
- <transition g="-2,-20" name="gt10" to="boosApprove"/>
- <transition g="186,323:12,-47" name="le10" to="sendEmail"/>
- </decision>
- <end g="171,410,48,48" name="end"/>
- <state g="146,313,92,52" name="sendEmail">
- <transition to="end"/>
- </state>
-
- </process>
分析之后,有如下一些表:
用户user_
角色role_(简化到user_)
请假单leave_
假设有这么几个用户:
陈均 --普通员工
唐平 --级别最高的,BOOS,老板
胡杰 --级别比较高的,chief主管
张小 --普通员工
用户测试数据:
- INSERT INTO `user_` VALUES ('9', '陈均', '普通员工');
- INSERT INTO `user_` VALUES ('10', '胡杰', '主管');
- INSERT INTO `user_` VALUES ('11', '唐平', '老板');
- INSERT INTO `user_` VALUES ('12', '张小', '普通员工');
现在集成jbpm4.3,hibernate3,spring2.5,struts2.1.8。
系统初步设计如图:
1.因为jbpm里面带有hibernate,所以创建web项目后,导入jbpm-4.3\lib下的所有包,导入jbpm-4.3\jbpm.jar,
把jbpm4.3\lib\下面得juel.jar,juel-engine.jar,juel-impl.jar放到tomcat的lib下面。导入spring2.5的jar,导入struts2.1.8所需jar包。以下jar包不是最简,有些不是必须的。
spring2.5所需jar包清单:
aspectjrt.jar
aspectjweaver.jar
cglib-nodep-2.1_3.jar
common-annotations.jar
commons-logging.jar
log4j-1.2.15.jar
spring.jar
spring-webmvc-struts.jar
------------------------------------
struts2.1.8所需jar包清单:
commons-fileupload-1.2.1.jar
commons-io-1.3.2.jar
freemarker-2.3.15.jar
ognl-2.7.3.jar
struts2-core-2.1.8.1.jar
struts2-dojo-plugin-2.1.8.1.jar
struts2-spring-plugin-2.1.8.1.jar
xwork-core-2.1.6.jar
-----------------------------------
数据库和数据源所需jar包:
c3p0-0.9.1.2.jar
mysql-connector-java-5.1.7-bin.jar
找到jbpm-4.3\install\src\cfg\jbpm\下的spring.jbpm.cfg.xml文件,放入项目的src处,改名为jbpm.cfg.xml.
在项目src下面创建applicationContext.xml配置文件,内容如下:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
-
-
- <context:annotation-config />
-
-
- <context:component-scan base-package="org.forever.leave" />
-
-
- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
- destroy-method="close">
- <property name="driverClass">
- <value>com.mysql.jdbc.Driver</value>
- </property>
- <property name="jdbcUrl">
- <value>jdbc:mysql://localhost:3306/jbpmdb</value>
- </property>
- <property name="user">
- <value>root</value>
- </property>
- <property name="password">
- <value>root</value>
- </property>
- </bean>
-
-
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="hibernateProperties" ref="hibernateProperties" />
- <property name="mappingLocations">
- <list>
- <value>classpath*:org/forever/leave/entities/*.hbm.xml</value>
- <value>classpath:jbpm.repository.hbm.xml</value>
-
- <value>classpath:jbpm.execution.hbm.xml</value>
- <value>classpath:jbpm.history.hbm.xml</value>
- <value>classpath:jbpm.task.hbm.xml</value>
- <value>classpath:jbpm.identity.hbm.xml</value>
- </list>
- </property>
- </bean>
-
- <bean name="hibernateProperties"
- class="org.springframework.beans.factory.config.PropertiesFactoryBean">
- <property name="properties">
- <props>
- <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
- <prop key="hibernate.show_sql">true</prop>
- <prop key="hibernate.hbm2ddl.auto">update</prop>
- </props>
- </property>
- </bean>
-
-
- <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper" />
- <bean id="processEngine" factory-bean="springHelper"
- factory-method="createProcessEngine" />
-
-
- <bean id="jbpmTemplate" class="org.forever.leave.jbpm.JbpmTemplate">
- <property name="processEngine" ref="processEngine"></property>
- </bean>
-
-
- <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
- <property name="sessionFactory" ref="sessionFactory"></property>
- </bean>
-
-
- <bean id="transactionManager"
- class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
-
-
- <aop:config>
- <aop:pointcut expression="execution(* org.forever.leave.service..*.*(..))"
- id="transactionPointcut" />
- <aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointcut" />
- </aop:config>
-
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="get*" read-only="true" propagation="NOT_SUPPORTED" />
- <tx:method name="find*" read-only="true" propagation="NOT_SUPPORTED" />
- <tx:method name="*" propagation="REQUIRED" />
- </tx:attributes>
- </tx:advice>
-
-
-
- </beans>
添加 log4j.properties文件;
模板类JbpmTemplate:
- package org.forever.leave.jbpm;
-
- import org.jbpm.api.ExecutionService;
- import org.jbpm.api.HistoryService;
- import org.jbpm.api.ManagementService;
- import org.jbpm.api.ProcessEngine;
- import org.jbpm.api.ProcessInstance;
- import org.jbpm.api.RepositoryService;
- import org.jbpm.api.TaskService;
-
-
-
-
-
-
-
- public class JbpmTemplate {
-
-
-
-
-
-
-
-
- public String Deploy(String resourceName) {
- return repositoryService.createDeployment().addResourceFromClasspath(
- resourceName).deploy();
- }
-
-
-
-
-
-
-
-
-
-
- public ProcessInstance addProcessInstance(String processDefinitionKey,
- String processInstanceKey) {
- return executionService.startProcessInstanceByKey(processDefinitionKey,
- processInstanceKey);
-
- }
-
-
-
-
-
-
-
-
- public ProcessInstance getProcessInstance(String key) {
- return executionService.createProcessInstanceQuery()
- .processInstanceKey(key).uniqueResult();
- }
-
-
-
-
-
-
- public void deleteDeploymentCascade(String deploymentId) {
- repositoryService.deleteDeploymentCascade(deploymentId);
- }
-
- public JbpmTemplate() {
-
- }
-
- public JbpmTemplate(ProcessEngine processEngine) {
- this.processEngine = processEngine;
- repositoryService = processEngine.getRepositoryService();
- executionService = processEngine.getExecutionService();
- taskService = processEngine.getTaskService();
- historyService = processEngine.getHistoryService();
- managementService = processEngine.getManagementService();
- }
-
- private ProcessEngine processEngine;
- private RepositoryService repositoryService = null;
- private ExecutionService executionService = null;
- private TaskService taskService = null;
- private HistoryService historyService = null;
- private ManagementService managementService = null;
-
- public ProcessEngine getProcessEngine() {
- return processEngine;
- }
-
- public void setProcessEngine(ProcessEngine processEngine) {
- this.processEngine = processEngine;
- System.out.println(processEngine);
- repositoryService = processEngine.getRepositoryService();
- executionService = processEngine.getExecutionService();
- taskService = processEngine.getTaskService();
- historyService = processEngine.getHistoryService();
- managementService = processEngine.getManagementService();
- }
-
- public RepositoryService getRepositoryService() {
- return repositoryService;
- }
-
- public void setRepositoryService(RepositoryService repositoryService) {
- this.repositoryService = repositoryService;
- }
-
- public ExecutionService getExecutionService() {
- return executionService;
- }
-
- public void setExecutionService(ExecutionService executionService) {
- this.executionService = executionService;
- }
-
- public TaskService getTaskService() {
- return taskService;
- }
-
- public void setTaskService(TaskService taskService) {
- this.taskService = taskService;
- }
-
- public HistoryService getHistoryService() {
- return historyService;
- }
-
- public void setHistoryService(HistoryService historyService) {
- this.historyService = historyService;
- }
-
- public ManagementService getManagementService() {
- return managementService;
- }
-
- public void setManagementService(ManagementService managementService) {
- this.managementService = managementService;
- }
-
- }
创建测试类Test:
- import java.util.UUID;
-
- import org.forever.leave.jbpm.JbpmTemplate;
- import org.jbpm.api.ProcessInstance;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
-
- public class Test {
-
- public static void main(String[] args) {
- ApplicationContext context = new ClassPathXmlApplicationContext(
- "applicationContext.xml");
-
- JbpmTemplate jbpmTemplate = (JbpmTemplate) context
- .getBean("jbpmTemplate");
- System.out.println(jbpmTemplate);
-
- jbpmTemplate.Deploy("org/forever/leave/jbpm/jpdl/leave.jpdl.xml");
- UUID uuid = UUID.randomUUID();
- }
- }
控制台输出没有报错说明jbpm和spring初步集成成功;
访问http://localhost:8080/leave/user/queryList.action获取到用户列表信息,说明集成成功。
项目中需要修改mysql方言为org.hibernate.dialect.MySQLInnoDBDialect,事务service改为biz,刚发现的,呵呵
下一步进行业务的实现。
未完
因为朋友需要,所以对其进行了简单的实现:
在实现中发现集成的时候,如果你用mysql数据库,请设置你的方言为:org.hibernate.dialect.MySQLInnoDBDialect
先看一哈这个实现了简单业务的一个图,在此用的中文,也是网上找的:
- <?xml version="1.0" encoding="UTF-8"?>
-
- <process key="leave" name="leave" xmlns="http://jbpm.org/4.3/jpdl">
- <start g="201,14,48,48" name="开始">
- <transition g="-42,-10" name="请假" to="填写请假单"/>
- </start>
- <task assignee="writerForm" g="178,87,92,52" name="填写请假单">
- <transition g="-97,2" name="判断是不是经理" to="是不是经理"/>
- </task>
- <decision expr="#{manager}" g="204,158,48,48" name="是不是经理">
- <transition g="-23,-11" name="否" to="经理审核"/>
- <transition g="14,-11" name="是" to="老板审批"/>
- </decision>
- <task assignee="#{username}" g="103,252,92,52" name="经理审核">
- <transition g="150,450:10,-21" name="经理批准" to="结束"/>
- <transition g="-22,-22" name="请假天数>5" to="老板审批"/>
- <transition g="-61,-1" name="经理不批准" to="终止"/>
- <transition g="149,114:-55,82" name="经理驳回" to="填写请假单"/>
- </task>
-
-
- <task assignee="张杰" g="278,251,92,52" name="老板审批">
- <transition g="326,450:-58,-24" name="老板批准" to="结束"/>
- <transition g="7,0" name="老板不批准" to="终止"/>
- <transition g="323,114:13,61" name="老板驳回" to="填写请假单"/>
- </task>
- <end g="219,429,48,48" name="结束" state="confirm"/>
- <end g="220,360,48,48" name="终止" state="dissent"/>
- </process>
写了个经理审批的测试类过程:
- package org.forever.leave.biz.test;
-
- import java.util.ArrayList;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.UUID;
-
- import org.forever.leave.entities.Leave;
- import org.forever.leave.entities.User;
- import org.jbpm.api.ProcessInstance;
- import org.jbpm.api.task.Task;
- import org.junit.Test;
-
-
-
-
-
-
-
-
- public class LeaveTest extends BaseTest{
-
-
- @Test
- public void testDeploy(){
- String resourceName ="org/forever/leave/jbpm/jpdl/leave.jpdl.xml";
- jbpmTemplate.Deploy(resourceName);
-
- }
-
-
- @Test
- public void testCresteLeave(){
-
-
- String loginName = "cj";
- String password = "cj";
- User user = userDao.get(loginName,password);
-
- UUID uuid = UUID.randomUUID();
- Leave leave = new Leave(user, 3,new Date(),"新建", "生病了");
- leave.setLeaveId(uuid.toString());
-
- leaveDao.save(leave);
- }
-
-
-
-
- @Test
- public void testStart() {
-
- String loginName = "cj";
- String password = "cj";
- User user = userDao.get(loginName,password);
-
- List<?> list = leaveDao.findByUserLeave(user.getUserId());
-
- Leave leave = (Leave)list.get(0);
-
-
- String position = user.getPosition();
- Map<String, Object> variables = new HashMap<String, Object>();
-
- variables.put("leaveId",leave.getLeaveId());
-
- if("员工".equals(position)){
- variables.put("manager", "否");
- variables.put("username","胡杰");
-
- }else if("经理".equals(position)){
- variables.put("manager", "是");
-
- }
-
-
- ProcessInstance processInstance = jbpmTemplate.addProcessInstance("leave",variables, leave.getLeaveId());
-
- System.out.println("请假单已填写:" + processInstance.isActive("填写请假单"));
-
- String taskId = jbpmTemplate.findPersonalTasks("writerForm").get(0).getId();
-
- jbpmTemplate.completeTask(taskId);
-
- }
-
-
- @Test
- public void testGetTask(){
-
- String username = "胡杰";
- List<Leave> leaves = new ArrayList<Leave>();
- List<Task> list = jbpmTemplate.findPersonalTasks(username);
- if(list.size()==0){
- System.out.println(username + "没有任务.........");
- }
- else{
- for (Task task : list) {
- System.out.println("任务名字:" + task.getName());
- System.out.println("任务参与者:" + task.getAssignee());
- String taskId = task.getId();
- String leaveId = (String) jbpmTemplate.getVariableByTaskId(taskId, "leaveId");
- Leave leave = leaveDao.findbyIdLeave(leaveId);
- leave.setTaskId(taskId);
- leaves.add(leave);
- }
- }
-
-
- for (Leave leave : leaves) {
- System.out.println(leave);
-
- ProcessInstance processInstance = jbpmTemplate.getProcessInstance(leave.getLeaveId());
- String taskId = leave.getTaskId();
- int day = leave.getDay();
- if(day>5 && true){
- jbpmTemplate.completeTask(taskId, "请假天数>5");
- }else{
- jbpmTemplate.completeTask(taskId,"经理批准");
- }
- System.out.println("审批结果:" + processInstance.getState());
- leave.setStatus("通过");
- leaveDao.update(leave);
- }
-
-
- }
-
-
- @Test
- public void testGetLeave(){
-
- String loginName = "cj";
- String password = "cj";
- User user = userDao.get(loginName,password);
-
- List<?> list = leaveDao.findByUserLeave(user.getUserId());
- for (Object object : list) {
- System.out.println(object);
-
-
-
- }
-
- }
-
- }
web版的我截几个效果图,具体过程你们下载来感受吧:
login.jsp登陆页面http://localhost:90/leave/login.jsp:第一次需要部署一哈

普通员工登陆用户名和密码为cj:

经理用户名和密码为hj,登陆后如图:

老板的用户名和密码zxp,进去后如图:
