Posted on 2009-12-09 00:51
penngo 閱讀(5214)
評論(36) 編輯 收藏 所屬分類:
JBPM
本次用的是一個特別流程,假設C提交一個表單,這個表單需要給A和B閱讀,而A和B閱讀過后,流程結束.
流程定義文件swing.jpdl.xml
<?xml version="1.0" encoding="UTF-8"?>

<process name="swing" xmlns="http://jbpm.org/4.0/jpdl">
<start g="94,64,48,48" name="start1">
<transition g="-52,-22" name="A" to="A"/>
</start>
<task assignee="A" g="73,195,92,52" name="A">
<transition g="-52,-22" name="B" to="B"/>
</task>
<task assignee="B" g="266,192,92,52" name="B">
<transition g="-40,-21" name="end" to="end1"/>
</task>
<end g="290,327,48,48" name="end1"/>
</process>

流程圖

增加兩個表:
用戶表p_user:
USER_ 用戶登錄ID
PASSWORD_ 用戶登錄密碼
表單數據表p_form:
ID_ 表單編號
TITLE_ 表單標題
CONTENT_ 表單內容
PROCESS_ID 流程實例ID
p_form除了記錄表單數據,還記錄啟動流程實例的ID,這個表負責把表單和流程關聯起來.
提交表單時的操作,FormPanel.java部分代碼:
okBtn = new JButton("提交表單");

okBtn.addActionListener(new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
String t = title.getText().trim();
String c = content.getText().trim();
String processId = startProcessInstance();
DataBase db = new DataBase();

if(db.saveForm(t, c, processId))
{
JOptionPane.showMessageDialog(null, "保存表單數據成功:");
title.setText("");
content.setText("");
}
}
});







private String startProcessInstance()
{
Configuration configuration = new Configuration();
ProcessEngine processEngine = configuration.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
ProcessInstance processInstance = executionService.startProcessInstanceByKey("swing");

if(processInstance == null)
{
JOptionPane.showMessageDialog(null, "請先發布流程.");
}

else
{
return processInstance.getId();
}
return "";
}


代碼很簡單,行獲取表單的數據,再啟動流程實例,并把表單數據和流程實例ID一起保存在數據庫表p_form.
在用戶處理任務時,先根據用戶的ID查找到該用戶的待處理任務,并把它添加到任務列表中,
DefaultListModel v = new DefaultListModel();
List list = getTaskList(Login.userName);

if(list.size() > 0)
{

for(int s = 0; s < list.size(); s++)
{
Task t = (Task)list.get(s);
String executionId = t.getExecutionId();
DataBase db = new DataBase();
TaskPojo tp = db.findForm(executionId);
tp.setTaskId(t.getId());

if(tp != null)
{
v.addElement(tp);
}
}
}
jList = new JList(v);





.

private List getTaskList(String userId)
{

if(configuration == null)
{
configuration = new Configuration();
}
ProcessEngine processEngine = configuration.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ExecutionService executionService = processEngine.getExecutionService();
TaskService taskService = processEngine.getTaskService();
HistoryService historyService = processEngine.getHistoryService();
ManagementService managementService = processEngine.getManagementService();
List<Task> taskList = taskService.findPersonalTasks(userId);
return taskList;
}


用戶處理任務,根據列表中選中記錄中的流程實例任務ID,調用taskService.completeTask(taskId)完成任務,
okBtn = new JButton("審閱通過");

okBtn.addActionListener( new ActionListener()
{

public void actionPerformed(ActionEvent e)
{
Object o = jList.getSelectedValue();

if(o instanceof TaskPojo)
{
TaskPojo tp = (TaskPojo)o;
completeTask(tp.getTaskId());
JOptionPane.showMessageDialog(null, "審核已經通過!");
DefaultListModel dl = (DefaultListModel)jList.getModel();
dl.remove(jList.getSelectedIndex());
buttomPanel.setVisible(false);
}
}
});






..

private void completeTask(String taskId)
{

if(configuration == null)
{
configuration = new Configuration();
}
ProcessEngine processEngine = configuration.buildProcessEngine();
TaskService taskService = processEngine.getTaskService();
taskService.completeTask(taskId);
}


數據庫中登錄用戶:
帳號:A 密碼:123
帳號:B 密碼:123
帳號:D 密碼:123
用戶D登錄
圖1

圖2

用戶A登錄
圖3

查看待辦任務
圖4

用戶B登錄
圖5

查看B的待辦任務
圖6

本人的開發環境
eclipse3.5
JBPM4.2
mysql-essential-5.1.40-win32
JDK1.6
要運行這個例子,需要修改jbpm.hibernate.cfg.xml,DataBase.java數據庫的連接信息
測試代碼:jbpmTest