?? workflowmanagerimpl.java
字號:
package com.bjsxt.oa.managers.impl;
import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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 org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.bjsxt.oa.managers.SystemException;
import com.bjsxt.oa.managers.WorkflowManager;
import com.bjsxt.oa.model.Document;
import com.bjsxt.oa.model.Workflow;
/**
* 集成方法之一:
*
* 直接注入JbpmConfiguration對象,在獲取JbpmContext對象的時候
* 將當前session與JbpmContext綁定!
* @author Administrator
*
*/
public class WorkflowManagerImpl extends HibernateDaoSupport implements
WorkflowManager {
private JbpmConfiguration jbpmConfiguration;
//獲取系統已定義的所有流程名稱列表!
public List searchAllProcessDefinition() {
JbpmContext context = getJbpmContext();
//獲得已定義的所有流程(最高的版本的記錄,不包含同一個流程的低版本記錄)
List list = context.getGraphSession().findLatestProcessDefinitions();
if(list != null && !list.isEmpty()){
List defs = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
ProcessDefinition def = (ProcessDefinition) iter.next();
defs.add(def.getName());
}
return defs;
}
return null;
}
//搜索流轉到用戶username的所有的流程實例(實際上就是公文!)
public List searchCurrentProcessInstances(String username) {
JbpmContext context = getJbpmContext();
List docIds = new ArrayList();
//從工作流引擎中,根據actor-id的值,獲得分配給這個actor-id的所有任務!
List taskInstances = context.getTaskMgmtSession().findTaskInstances(username);
for (Iterator iter = taskInstances.iterator(); iter.hasNext();) {
TaskInstance ti = (TaskInstance) iter.next();
//如果這個任務是正在等待被完成的狀態
if(ti.isSignalling()){
docIds.add(new Integer(ti.getProcessInstance().getKey()));
}
}
return docIds;
}
//流向下一步!
public void flowToNextStep(int documentId, String transitionName) {
Document document = (Document)getHibernateTemplate().load(Document.class, documentId);
JbpmContext context = getJbpmContext();
//根據流程名稱獲得流程定義對象
ProcessDefinition processDefinition = context.getGraphSession()
.findLatestProcessDefinition(document.getWorkflow());
//根據流程定義對象以及key值,獲得流程實例
ProcessInstance processInstance = context.getProcessInstance(processDefinition, ""+documentId);
if(transitionName == null){
processInstance.signal();
}else{
processInstance.signal(transitionName);
}
//設置公文的狀態為當前節點的名稱
document.setStatus(processInstance.getRootToken().getNode().getName());
//如果已經是終點,則設置公文為已完成狀態
//當流程到達終點
if(processInstance.getRootToken().hasEnded()){
document.setStatus(Document.STATUS_END);
}
getHibernateTemplate().update(document);
}
//獲得文檔的下一步流向,返回流向名稱列表
public List searchNextSteps(int documentId) {
Document document = (Document)getHibernateTemplate().load(Document.class, documentId);
JbpmContext context = getJbpmContext();
//根據流程名稱獲得流程定義對象
ProcessDefinition processDefinition = context.getGraphSession()
.findLatestProcessDefinition(document.getWorkflow());
//根據流程定義對象以及key值,獲得流程實例
ProcessInstance processInstance = context.getProcessInstance(processDefinition, ""+documentId);
//查看當前流程是否已經結束
if(processInstance.getRootToken().hasEnded()){
throw new SystemException("流程已結束");
}
//獲得當前節點的下一步都有哪些流向
List list = processInstance.getRootToken().getNode().getLeavingTransitions();
List transitions = new ArrayList();
for (Iterator iter = list.iterator(); iter.hasNext();) {
//獲得流向名稱列表
Transition transition = (Transition) iter.next();
transitions.add(transition.getName());
}
return transitions;
}
//刪除流程定義文件
public void delProcessDefinition(String name) {
JbpmContext context = getJbpmContext();
//搜索JBPM中所有版本的流程定義記錄
List allversions = context.getGraphSession().findAllProcessDefinitionVersions(name);
if(allversions != null){
//逐個刪除
for (Iterator iter = allversions.iterator(); iter.hasNext();) {
ProcessDefinition def = (ProcessDefinition) iter.next();
context.getGraphSession().deleteProcessDefinition(def);
}
}
//刪除對應的Workflow對象
getSession().delete(
(Workflow)getSession()
.createQuery("select w from Workflow w where w.name = ?")
.setParameter(0, name)
.uniqueResult()
);
}
//部署流程定義
public void deployProcessDefinition(byte[] processdef, byte[] processimage) {
if(processdef == null){
return;
}
ProcessDefinition def = ProcessDefinition.parseXmlInputStream(new ByteArrayInputStream(processdef));
JbpmContext context = getJbpmContext();
//將流程定義文件部署到JBPM
context.deployProcessDefinition(def);
Workflow wf = (Workflow)getSession()
.createQuery("select w from Workflow w where w.name = ?")
.setParameter(0, def.getName())
.uniqueResult();
if(wf != null){
wf.setProcessdefinition(processdef);
wf.setProcessimage(processimage);
getSession().update(wf);
return;
}
wf = new Workflow();
wf.setName(def.getName());
wf.setProcessdefinition(processdef);
wf.setProcessimage(processimage);
getSession().save(wf);
}
//查找特定流程
public Workflow findWorkflow(String name) {
return (Workflow)getSession()
.createQuery("select w from Workflow w where w.name = ?")
.setParameter(0, name)
.uniqueResult();
}
//根據公文標識創建流程實例
public void addProcessInstance(int documentId) {
JbpmContext context = getJbpmContext();
Document document = (Document)getHibernateTemplate().load(Document.class, documentId);
//根據公文對應的流程名稱,獲得流程定義對象
ProcessDefinition processDefinition = context.getGraphSession()
.findLatestProcessDefinition(document.getWorkflow());
//根據流程定義對象,創建一個流程實例
ProcessInstance processInstance = new ProcessInstance(processDefinition);
//將流程實例與公文綁定
processInstance.setKey(""+documentId);
context.save(processInstance);
}
//刪除流程實例!
public void delProcessInstance(int documentId) {
Document document = (Document)getHibernateTemplate().load(Document.class, documentId);
JbpmContext context = getJbpmContext();
//根據流程名稱獲得流程定義對象
ProcessDefinition processDefinition = context.getGraphSession()
.findLatestProcessDefinition(document.getWorkflow());
//根據流程定義對象以及key值,獲得流程實例
ProcessInstance processInstance = context.getProcessInstance(processDefinition, ""+documentId);
context.getGraphSession().deleteProcessInstance(processInstance);
}
//獲取JbpmContext對象
private JbpmContext getJbpmContext(){
JbpmContext context = jbpmConfiguration.createJbpmContext();
//關鍵的一個步驟,我們需要將JBPM使用的session對象跟OA系統所使用的
//session對象整合起來。
context.setSession(getSession());
return context;
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -