本次用的是一个特别流程,假设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 >
< 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 "";
}
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);
}
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