集成JBPM到OA系统,并实现JbpmFacade接口
- 第一步:创建数据库表
* 拷贝依赖包:bsh.jar/jcr-1.0.jar/jbpm-identity.jar/jbpm-jpdl.jar
* 修改hibernate.cfg.xml,添加JBPM的映射配置,以及缓存属性的配置
- 拷贝JBPM中的hibernate配置文件到当前项目
- 修改build.xml文件,在"生成Hibernate配置文件"的任务中,添加一个属性:
mergecfgxml="${basedir}/hibernate_for_jbpm.cfg.xml"
* 因为JBPM中的User类与OA系统中的User类在名称上有冲突,所以需要修改OA
系统中的User类的auto-import="false",并更改查询User对象的HQL语句,加上
全路径的类名进行查找,可以参考UserManagerImpl类。
- 第二步:与spring集成
* 集成spring与JBPM,需要用到第三方的类库:spring-modules-0.8.zip
* 将其中的spring-modules-jbpm31.jar拷贝到web-inf/lib下面即可
* 集成JBPM与spring的方法是:将JbpmConfiguration对象交给spring来创建
* 所以,需要在spring配置文件里面配置JbpmConfiguration对象的创建
<bean id="jbpmConfiguration" class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
<property name="configuration" value="classpath:jbpm.cfg.xml"></property>
</bean>
* 这个时候,需要拷贝jbpm.cfg.xml到类路径中
(可以从jbpm-jpdl-3.2.GA\src\jpdl\org\jbpm目录中拷贝(default.jbpm.cfg.xml)文件,
并重命名即可)
- 第三步:如何实现业务逻辑类
* 用到jbpmConfiguration的时候,需要注入
* 当我们使用jbpmContext对象来操纵JBPM的时候,需要将JbpmContext内部的Hibernate session对象设置为当前的
hibernate session对象。
private JbpmContext getJbpmContext(){
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(getSession());
return context;
}
* 当我们使用jbpmContext操纵JBPM数据结束的时候,注意不要关闭jbpmContext!!
package com.bjsxt.oa.manager;
import java.util.List;
import com.bjsxt.oa.model.Document;
public interface JbpmFacade {
/**
* 部署流程定义
* @param processDef 流程定义文件的数据
* @return 流程名称
*/
public String deployProcessDefinition(byte[] processDef);
/**
* 删除流程定义
* @param processName 流程名称
*/
public void delProcessDefinition(String processName);
/**
* 添加流程实例
* @param processName 流程实例对应的流程名称
* @param docId 公文ID
* @return 流程实例
*/
public long addProcessInstance(String processName,Document document);
/**
* 删除流程实例对象
* @param processInstanceId 删除流程实例对象
*/
public void delProcessInstance(long processInstanceId);
/**
* 搜索某个用户手上的公文列表
* @param actorId 用户帐号
* @return List中的元素是docId
*/
public List searchMyTaskList(String actorId);
/**
* 查询下一步Transition列表
* @param processInstanceId 流程实例标识
* @param actorId 用户帐号
* @return List中的元素是Transition对象的名称
*/
public List searchNextTransitions(long processInstanceId,String actorId);
/**
* 触发JBPM引擎流转到下一步
* @param processInstanceId 流程实例标识
* @param actorId 用户帐号
* @param transitionName transition Name
* @return 流转之后,当前流程实例所对应的rootToken所指向的节点的名称
*/
public String nextStep(long processInstanceId,String actorId,String transitionName);
}
package com.bjsxt.oa.manager.impl;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.def.Transition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.taskmgmt.exe.TaskInstance;
import com.bjsxt.oa.manager.JbpmFacade;
import com.bjsxt.oa.model.Document;
public class JbpmFacadeImpl extends AbstractManager implements JbpmFacade {
private JbpmConfiguration jbpmConfiguration;
public long addProcessInstance(String processName, Document document) {
JbpmContext context = getJbpmContext();
ProcessDefinition def = context.getGraphSession().findLatestProcessDefinition(processName);
ProcessInstance instance = new ProcessInstance(def);
instance.getContextInstance().setVariable("document", document.getId());
//将公文的标题放到流程实例变量中,以便在发送email的时候使用
instance.getContextInstance().setVariable("docTitle", document.getTitle());
//将公文的相关属性设置到流程实例变量中
Map props = document.getProperties();
if(props != null){
Set entries = props.entrySet();
for (Iterator iterator = entries.iterator(); iterator.hasNext();) {
Map.Entry entry = (Map.Entry) iterator.next();
String propertyName = (String)entry.getKey();
Object obj = document.getProperty(propertyName);
//将变量放入流程实例中
instance.getContextInstance().setVariable(propertyName, obj);
}
}
context.save(instance);
return instance.getId();
}
public void delProcessDefinition(String processName) {
JbpmContext context = getJbpmContext();
List defs = context.getGraphSession().findAllProcessDefinitionVersions(processName);
for (Iterator iterator = defs.iterator(); iterator.hasNext();) {
ProcessDefinition def = (ProcessDefinition) iterator.next();
context.getGraphSession().deleteProcessDefinition(def);
}
}
public void delProcessInstance(long processInstanceId) {
JbpmContext context = getJbpmContext();
context.getGraphSession().deleteProcessInstance(processInstanceId);
}
public String deployProcessDefinition(byte[] processDef) {
JbpmContext context = getJbpmContext();
ProcessDefinition def = ProcessDefinition.parseXmlInputStream(
new ByteArrayInputStream(processDef)
);
context.deployProcessDefinition(def);
return def.getName();
}
public String nextStep(long processInstanceId, String actorId,
String transitionName) {
JbpmContext context = getJbpmContext();
ProcessInstance instance = context.getProcessInstance(processInstanceId);
//当前节点
String currentNodeName = instance.getRootToken().getNode().getName();
//起点的名称
String startNodeName = instance.getProcessDefinition().getStartState().getName();
//如果是在起点
if(startNodeName.equals(currentNodeName)){
if(transitionName == null){
instance.signal();
}else{
instance.signal(transitionName);
}
}else{
List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
for (Iterator iterator = taskInstances.iterator(); iterator
.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
if(ti.getProcessInstance().getId() == processInstanceId){
if(transitionName == null){
ti.end();
}else{
ti.end(transitionName);
}
break;
}
}
//查询actorId所属组的任务实例
List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId);
for (Iterator iterator = pooledTaskInstances.iterator(); iterator
.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
if(ti.getProcessInstance().getId() == processInstanceId){
if(transitionName == null){
ti.end();
}else{
ti.end(transitionName);
}
break;
}
}
}
//返回转向之后的节点名称
return instance.getRootToken().getNode().getName();
}
public List searchMyTaskList(String actorId) {
JbpmContext context = getJbpmContext();
List docIds = new ArrayList();
//查询所有未结束的任务实例
List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
for (Iterator iterator = taskInstances.iterator(); iterator.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("document");
docIds.add(docId);
}
//查询actorId所属组的任务实例
List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId);
for (Iterator iterator = pooledTaskInstances.iterator(); iterator
.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
Integer docId = (Integer)ti.getProcessInstance().getContextInstance().getVariable("document");
docIds.add(docId);
}
return docIds;
}
public List searchNextTransitions(long processInstanceId, String actorId) {
JbpmContext context = getJbpmContext();
ProcessInstance instance = context.getProcessInstance(processInstanceId);
//当前节点
String currentNodeName = instance.getRootToken().getNode().getName();
//起点的名称
String startNodeName = instance.getProcessDefinition().getStartState().getName();
Collection transitions = null;
//如果是在起点
if(startNodeName.equals(currentNodeName)){
transitions = instance.getRootToken().getAvailableTransitions();
}else{
List taskInstances = context.getTaskMgmtSession().findTaskInstances(actorId);
for (Iterator iterator = taskInstances.iterator(); iterator
.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
if(ti.getProcessInstance().getId() == processInstanceId){
transitions = ti.getAvailableTransitions();
break;
}
}
//查询actorId所属组的任务实例
List pooledTaskInstances = context.getTaskMgmtSession().findPooledTaskInstances(actorId);
for (Iterator iterator = pooledTaskInstances.iterator(); iterator
.hasNext();) {
TaskInstance ti = (TaskInstance) iterator.next();
if(ti.getProcessInstance().getId() == processInstanceId){
transitions = ti.getAvailableTransitions();
break;
}
}
}
List transitionNames = new ArrayList();
//为了不把Transition对象直接暴露给OA系统,需要将其转换为名称列表
for (Iterator iterator = transitions.iterator(); iterator.hasNext();) {
Transition transition = (Transition) iterator.next();
transitionNames.add(transition.getName());
}
return transitionNames;
}
private JbpmContext getJbpmContext(){
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(getSession());
return context;
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
}