使用jBpm支持高级用户交互模式
2010-01-21 00:00:00 来源:WEB开发网清单2 在Task映射文件中指定新增域
为了让我们新加的属性能被流程定义和数据库正确读取,我们需要修改org.jbpm.jpdl.xml.JpdlXmlReader类以正确地读取我们的新属性(清单3)
String numSignatureText = taskElement.attributeValue("numSignatures");
if (numSignatureText != null) {
try{
task.setNumSignatures(Integer.parseInt(numSignatureText));
}
catch(Exception e){}
}
清单3 读取numSignature属性
最后,因为JpdlXmlReader根据模式来验证XML,因此我们需要在jpdl-3.2.xsd中增加一个属性定义(清单4):
<xs:element name="task">
………………….
<xs:attribute name="numSignatures" type="xs:string" />
清单4 在jpdl-3.2.xsd中增加numSignatures属性
当完成这些工作,任务定义就被扩展可以使用numSignatures属性(清单5):
<task name="task2" numSignatures = "2">
<assignment pooled-actors="Peter, John"></assignment>
</task>
清单5 给任务定义增加numSignatures属性
扩展TaskInstance类
在扩展完任务类后,我们还需要创建一个自定义的任务实例类来跟踪分配给该任务实例的参与者,并确保所有被分配的参与者完成类执行(清单6)。
package com.navteq.jbpm.extensions;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import org.jbpm.JbpmException;
import org.jbpm.taskmgmt.exe.TaskInstance;
public class AssignableTaskInstance extends TaskInstance {
private static final long serialVersionUID = 1L;
private List<Assignee> assignees = new LinkedList<Assignee>();
private String getAssigneeIDs(){
StringBuffer sb = new StringBuffer();
boolean first = true;
for(Assignee a : assignees){
if(!first)
sb.append(" ");
else
first = false;
sb.append(a.getUserID());
}
return sb.toString();
}
public List<Assignee> getAssignees() {
return assignees;
}
public void reserve(String userID) throws JbpmException{
if(task == null)
throw new JbpmException("can't reserve instance with no task");
// Duplicate assignment is ok
for(Assignee a : assignees){
if(userID.equals(a.getUserID()))
return;
}
// Can we add one more guy?
if(task.getNumSignatures() > assignees.size()){
assignees.add(new Assignee(userID));
return;
}
throw new JbpmException("task is already reserved by " +
getAssigneeIDs());
}
public void unreserve(String userID){
for(Assignee a : assignees){
if(userID.equals(a.getUserID())){
assignees.remove(a);
return;
}
}
}
private void completeTask(Assignee assignee, String transition){
assignee.setEndDate(new Date());
// Calculate completed assignments
int completed = 0;
for(Assignee a : assignees){
if(a.getEndDate() != null)
completed ++;
}
if(completed < task.getNumSignatures())
return;
if(transition == null)
end();
else
end(transition);
}
public void complete(String userID, String transition) throws JbpmException{
if(task == null)
throw new JbpmException("can't complete instance with no task");
// make sure it was reserved
for(Assignee a : assignees){
if(userID.equals(a.getUserID())){
completeTask(a, transition);
return;
}
}
throw new JbpmException("task was not reserved by " + userID);
}
public boolean isCompleted(){
return (end != null);
}
}
更多精彩
赞助商链接