一个简单的流程定义文件
<?xml version="1.0" encoding="UTF-8"?>
<process-definition name="ExpenseAccountSwim">
<swimlane name="initiator">
<assignment actor-id="#{firstPerson}" />
</swimlane>
<start-state name="startLog">
<transition name="到填写报销单" to="startExpense"></transition>
</start-state>
<task-node name="startExpense">
<task name="填写报销单" swimlane="initiator">
</task>
<transition name="到经理审核" to="managerAudit"></transition>
</task-node>
<task-node name="managerAudit">
<task name="经理审核">
<assignment actor-id="1" />
</task>
<transition name="到会计审核" to="accountantAudit"></transition>
<transition name="重新填写报销单" to="startExpense"></transition>
</task-node>
<task-node name="accountantAudit">
<task name="会计审核">
<assignment actor-id="3" />
</task>
<transition name="通知领款" to="lingkuan"></transition>
</task-node>
<task-node name="lingkuan">
<task name="领款" swimlane="initiator">
</task>
<transition name="审核完毕" to="endLog"></transition>
</task-node>
<end-state name="endLog"></end-state>
</process-definition>
报销单对应的java是
public class ExpenseAccount implements java.io.Serializable {
private int id;
private int expense;//报销费用
private String type;//报销类型
/**
* 描述
* @hibernate.property
*/
private String description;
private long processInstanceId;//流程实例id
.....getter
.....setter
}
发布完流程定义文件后。
我们填写报销单,并建立一个流程实例,将填写的报销单和这个流程实例绑定。
public void saveExpenseAccount(IBean domain,String definitionName) {
ExpenseAccount expenseAccount = (ExpenseAccount) domain;
expenseAccount.setDeleteFlag("1");
expenseAccount.setCreateDate(Common.getCurrentTime());
allDao.insert(expenseAccount);
//绑定流程实例
allDao.createProcessInstance(definitionName,expenseAccount.getCreateUser(),expenseAccount.getId());
}
/**
* 建立流程实例并绑定流程实例
*
* @param definitionName
* 流程名称
* @param personId
* 提交人id
* @param docId
* 公文id(申请单id)
*/
public void createProcessInstance(String definitionName, String personId,
int docId) {
JbpmContext context = null;
try {
context = getContext();
// 从数据库中加载ProcessDefinition对象
ProcessDefinition definition = context.getGraphSession()
.findLatestProcessDefinition(definitionName);
// 从流程中创建一个流程实例
ProcessInstance processInstance = new ProcessInstance(definition);
// 加载公文
ExpenseAccount doc = (ExpenseAccount) context.getSession().load(
ExpenseAccount.class, docId);
// 绑定流程实例到公文
doc.setProcessInstanceId(processInstance.getId());
// 绑定公文到流程实例(ContextInstance-相当于一个变量的实例)
processInstance.getContextInstance().createVariable("document",
doc.getId());
processInstance.getContextInstance().setVariable("firstPerson",
personId);
processInstance.getContextInstance().setVariable("money",doc.getExpense());
// 触发流程实例走向下一步
processInstance.signal();
// 将第一个提交人改为动态的,<assignment actor-id="#{firstPerson}" />,要设置任务的执行者。
TaskInstance taskInstance = (TaskInstance) processInstance
.getTaskMgmtInstance().getTaskInstances().iterator().next();
taskInstance.setActorId(personId);
taskInstance.setVariable("firstPerson", personId);
// 存储流程实例的信息
context.save(processInstance);
} catch (Exception e) {
e.printStackTrace();
} finally {
context.close();
}
}
然后用户提交这个报销单。
/**
* 触发流程向下一步流动,适用于当前只用一个transition,通过页面选择的transition,直接end到transition
*
* @param personId
* 登录人id
* @param processInstanceId
* 流程实例id
*
* @param transitionName transition名称
*/
public String flowToNextNode(String personId, long processInstanceId,String transitionName) {
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(getSession());
try{
List tasks = context.getTaskMgmtSession().findTaskInstances(personId);
for (Iterator it = tasks.iterator(); it.hasNext();) {
TaskInstance taskInstance = (TaskInstance) it.next();
//判断流程实例是否一致
if (processInstanceId == taskInstance.getProcessInstance().getId()) {
taskInstance.end(transitionName);
} else {
continue;
}
}
}finally{
context.close();
}
return "";
}
此时报销单回流转到
managerAudit,他的执行者是actor-id="1" .
查询actor-id="1"的任务列表。
// 查找等待审批的公文
public List searchApprovingDocuments(String aoctor_Id) {
JbpmContext context = getContext();
List docIds = new ArrayList();
// 首先获得流转到用户的任务列表
List tasks = context.getTaskMgmtSession().findTaskInstances(aoctor_Id);
for (Iterator iter = tasks.iterator(); iter.hasNext();) {
System.out.println("tasks");
TaskInstance taskInstance = (TaskInstance) iter.next();
// 任务实例是否正在等待审批,如果不是,则忽略(即不应该被列到待审批列表中!)
if (!taskInstance.isSignalling()) {
continue;
}
Integer docId = Integer.parseInt(String.valueOf(taskInstance
.getProcessInstance().getContextInstance().getVariable(
"document")));
docIds.add(docId);
}
return docIds;
}
//上面只是返回的任务id,然后可以根据id查询出该用户要审核的报销单:如下面
String docIds="1,2,3,4,5";
//select * from ExpenseAccount where id in(docIds);
上面会列出所有要审核的报销单,选择一个报销单时,因为该节点有一个或多个Transition
,如果是多个时,就要让用户选择Transition,所以可以显示的在页面上列出Transition供用户选择。
/**
* 获得当前流程实例下的transition
* @param processInstanceId 流程实例id
* */
public List getTransitions(String personId,long processInstanceId){
List list=new ArrayList();
TaskInstance taskInstance=null;
JbpmContext context=jbpmConfiguration.createJbpmContext();
context.setSession(this.getSession());
try{
List tasks=context.getTaskMgmtSession().findTaskInstances(personId);
if(tasks!=null&&tasks.size()>0){
for(Iterator it=tasks.iterator();it.hasNext();){
taskInstance = (TaskInstance) it.next();
if(processInstanceId==taskInstance.getProcessInstance().getId()){
List listTrans=taskInstance.getTask().getTaskNode().getLeavingTransitions();
list.addAll(listTrans);
}
}
}
}catch(Exception e){
e.printStackTrace();
}finally{
context.close();
}
return list;
}
我用struts2的<s:select >来显示
<c:if test="${!empty transList}">
<s:select list="transList" name="transname" listKey="name" listValue="name" headerKey="0" headerValue="--请选择--"></s:select>
</c:if>
后面的审核都是反复的调用flowToNextNode方法。
actor-id="#{firstPerson}" 表示可以在java文件中定义执行者,可以看到上面在新增报销单时定义
processInstance.getContextInstance().setVariable("firstPerson",personId);
<swimlane name="initiator">
如果一个任务用同一个角色或人来执行,那么可以用swimlane 来定义任务参与者或执行者。在任务中只需要引用就行。
<task name="填写报销单" swimlane="initiator">
<task name="领款" swimlane="initiator">