求大侠帮忙,jbpm查找所有可选路径的奇怪问题
在jbpm中部署了流程,并创建了流程实例和公文绑定一起,为什么使用instance.getRootToken().getName();得到的是null,而使用instance.getProcessDefinition().getStartState().getName();可以得到“开始”节点。我的公文是刚创建并绑定的,还没有提交。就是想在提交的时候取得下一节点,当是为什么instance.getRootToken().getName()就取不到值呢?下面是我的完整代码,请赐教。谢谢
Action代码
public class DocumentAction extends BaseAction<Document> {
/**
*
*/
private static final long serialVersionUID = 1L;
private Long workflowId;
/**
* 公文内容,一般是上传Doc文档
*/
private String content;
/**
* 公文描述
*/
private String description;
/**
* 标题
*/
private String title;
/**
* 审批内容
*/
private String comment;
private String ids;
/**
* 下一步流程名称
*/
private String transitionName;
private DocumentManager documentManager;
private WorkflowManager workflowManager;
public List myDocuments() throws Exception {
return documentManager.findMyDocuments(currentuser().getId());
}
/**
* 已审核公文列表
*
* @throws Exception
*/
public List approvedList() throws Exception {
return documentManager.findApprovedDocuments(currentuser().getId());
}
/**
* 待审核公文列表
*
* @throws Exception
*/
public List approvingList() throws Exception {
return documentManager.findApprovingDocuments(currentuser().getId());
}
/**
* 审批历史
*
* @throws Exception
*/
public List approvedHistory() throws Exception {
return documentManager.findApproveInfos(this.entity.getId());
}
/**
* 添加审批信息
*
* @throws Exception
*/
public void approve() throws Exception {
Long approverId = currentuser().getId();
Long documentId = this.entity.getId();
ApproveInfo approveInfo = new ApproveInfo();
approveInfo.setApproveTime(new Date());
approveInfo.setComment(this.comment);
documentManager.addApproveInfo(approveInfo, documentId, approverId);
}
public void add() throws Exception {
Document doc = new Document();
doc.setContent(content);
doc.setCreateTime(new Date());
doc.setDescription(description);
doc.setTitle(title);
doc.setStatus(Document.STATUS_NEW);
//
this.entity.setId(documentManager.addDocument(doc, this.workflowId,
currentuser().getId()));
ServletActionContext.getResponse().getWriter().print(
"{success:true,id:" + entity.getId() + "}");
}
// 删除公文,可批量删除
public void del() throws Exception {
for (String str : ids.split(",")) {
try {
long docId = Long.parseLong(str.trim());
documentManager.delDocument(docId);
} catch (Exception e) {
continue;
}
}
ServletActionContext.getResponse().getWriter().print(
"{success:true}");
}
/**
* 添加公文时,列出所有流程
*
* @throws Exception
*/
public void selectFlow() throws Exception {
List workflows = workflowManager.findWorkflows();
for (Iterator iterator = workflows.iterator(); iterator.hasNext();) {
Workflow workflow = (Workflow) iterator.next();
System.out.println(workflow.getName());
}
}
/**
* 列出下一步所有可选步骤
* @throws Exception
* @throws IOException
*/
public void submitInput() throws IOException, Exception {
Document doc = documentManager.findDocument(this.entity.getId());
List transitions = workflowManager.findNextTransitions(currentuser().getUsername(), doc
.getProcessInstanceId());
JsonUtils.write(transitions, ServletActionContext.getResponse().getWriter(),
getExcludesForAll(), getDatePattern());
}
/**
* 用户选择其中一个步骤,提交
*/
public void submit() {
Document doc = documentManager.findDocument(this.entity.getId());
String status = workflowManager.flowToNextStep(currentuser().getUsername(), doc
.getProcessInstanceId(), transitionName);
doc.setStatus(status);
documentManager.updateDocument(doc);
}
public Long getWorkflowId() {
return workflowId;
}
public void setWorkflowId(Long workflowId) {
this.workflowId = workflowId;
}
public String getIds() {
return ids;
}
public void setIds(String ids) {
this.ids = ids;
}
public DocumentManager getDocumentManager() {
return documentManager;
}
public void setDocumentManager(DocumentManager documentManager) {
this.documentManager = documentManager;
}
public WorkflowManager getWorkflowManager() {
return workflowManager;
}
public void setWorkflowManager(WorkflowManager workflowManager) {
this.workflowManager = workflowManager;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getTransitionName() {
return transitionName;
}
public void setTransitionName(String transitionName) {
this.transitionName = transitionName;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String[] getExcludesForAll() {
return new String[] { "hibernateLazyInitializer", "class", "root",
"parent", "children", "users", "parentId" };
}
public String getDatePattern() {
return "yyyy-MM-dd";
}
}
public class DocumentManagerImpl extends HibernateDaoSupport implements
DocumentManager {
private WorkflowManager workflowManager;
//添加公文审批信息
@Override
public Long addApproveInfo(ApproveInfo approveInfo, Long documentId,
Long approverId) {
approveInfo.setDocument((Document) getHibernateTemplate().load(
Document.class, documentId));
approveInfo.setApprover((User) getHibernateTemplate().load(User.class,
approverId));
getHibernateTemplate().save(approveInfo);
return approveInfo.getId();
}
//添加公文信息,需绑定到流程实例
@Override
public Long addDocument(Document document, Long workflowId, Long userId) {
document.setWorkflow((Workflow) getHibernateTemplate().load(
Workflow.class, workflowId));
document.setCreator((User) getHibernateTemplate().load(User.class,
userId));
// 保存公文信息
getHibernateTemplate().save(document);
// 添加流程实例,绑定公文对象和流程实例
Long processInstanceId = workflowManager.addProcessInstance(document.getWorkflow().getName(),
document.getId());
document.setProcessInstanceId(processInstanceId);
getHibernateTemplate().update(document);
return document.getId();
}
//删除公文信息,必须连带删除绑定的流程实例
@Override
public void delDocument(Long documentId) {
Document doc = (Document) getHibernateTemplate().load(Document.class,
documentId);
// 删除公文信息
getHibernateTemplate().delete(doc);
// 删除流程实例
workflowManager.delProcessInstance(doc.getProcessInstanceId());
}
//查询公文的审批历史
@Override
public List findApproveInfos(Long documentId) {
return getHibernateTemplate().find(
"* from ApproveInfo ap where ap.document.id=", documentId);
}
//查找用户已审批过的公文列表
@Override
public List findApprovedDocuments(Long userId) {
return getHibernateTemplate()
.find(
"select distinct ap.document from ApproveInfo ap where ap.approver.id=",
userId);
}
//查找等待用户审批的公文列表
@Override
public List findApprovingDocuments(Long userId) {
User user = (User) getHibernateTemplate().load(User.class, userId);
//查找已流转到用户那里的公文标示列表
List docIds = workflowManager
.findApprovingDocuments(user.getUsername());
if (docIds == null || docIds.isEmpty()) {
return null;
}
//根据标示查找所有公文对象
return getSession().createQuery(
"select d from Document d where d.id in (:ids)").setParameter(
"ids", docIds).list();
}
@Override
public Document findDocument(Long documentId) {
return (Document) getHibernateTemplate().load(Document.class,
documentId);
}
@Override
public List findMyDocuments(Long userId) {
// TODO Auto-generated method stub
return null;
}
@Override
public void updateDocument(Document document) {
getHibernateTemplate().update(document);
}
public void setWorkflowManager(WorkflowManager workflowManager) {
this.workflowManager = workflowManager;
}
}
[解决办法]
WorkflowManagerImpl代码
public class WorkflowManagerImpl extends HibernateDaoSupport implements
WorkflowManager {
private JbpmConfiguration jbpmConfiguration;
/**
* 获取JbpmContext对象,需要将JbpmContext的session和hibernate的session绑定
*
* @return
*/
private JbpmContext getContext() {
JbpmContext context = jbpmConfiguration.createJbpmContext();
context.setSession(getSession());
return context;
}
//将公文对象和流程实例绑定
@Override
public Long addProcessInstance(String workflowName, Long documentId) {
System.out.println("processName: " + workflowName);
System.out.println("documentId: " + documentId);
JbpmContext context = getContext();
ProcessDefinition processDefinition = context.getGraphSession().findLatestProcessDefinition(workflowName);
ProcessInstance instance = new ProcessInstance(processDefinition);
//将公文标示绑定到流程实例
instance.getContextInstance().setVariable("document", documentId);
//存储流程实例的信息
context.save(instance);
//System.out.println(instance.getRootToken().getName());
//context.close();
return instance.getId();
}
//删除一个流程实例
@Override
public void delProcessInstance(Long processInstanceId) {
JbpmContext context = getContext();
context.getGraphSession().deleteProcessInstance(processInstanceId);
}
//删除系统中的流程定义
@Override
public void delWorkflow(Long workflowId) {
//删除系统中的流程定义
Workflow workflow = (Workflow)getHibernateTemplate().load(Workflow.class, workflowId);
getHibernateTemplate().delete(workflow);
//删除jbpm中的流程定义
//jbpm中存在多个版本
//需要删除所有版本,同一时刻,可能存在多版本流程定义
List defs = getContext().getGraphSession().findAllProcessDefinitionVersions(workflow.getName());
for (Iterator iterator = defs.iterator(); iterator.hasNext();) {
ProcessDefinition def = (ProcessDefinition)iterator.next();
getContext().getGraphSession().deleteProcessDefinition(def);
}
}
@Override
public String deployProcessDefinition(byte[] processDefinition,
byte[] processImage) {
ProcessDefinition def = ProcessDefinition
.parseXmlInputStream(new ByteArrayInputStream(processDefinition));
getContext().deployProcessDefinition(def);
Workflow workflow = (Workflow)getSession()
.createQuery("select w from Workflow w where w.name = ? ")
.setParameter(0, def.getName())
.uniqueResult();
//判断流程是否存在
if (workflow == null) {
workflow = new Workflow();
workflow.setName(def.getName());
workflow.setProcessDefinition(processDefinition);
workflow.setProcessImage(processImage);
getHibernateTemplate().save(workflow);
return workflow.getName();
}
//如流程存在则更新
workflow.setName(def.getName());
workflow.setProcessDefinition(processDefinition);
workflow.setProcessImage(processImage);
getHibernateTemplate().update(workflow);
return workflow.getName();
}
//查找流转到某个用户的所有公文
@Override
public List findApprovingDocuments(String username) {
JbpmContext context = getContext();
List docIds = new ArrayList();
//获得流转到用户的任务列表
List tasks = context.getTaskMgmtSession().findTaskInstances(username);
for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
TaskInstance taskInstance = (TaskInstance)iterator.next();
//判断任务实例是否在等待审批,如果不是,则不不显示
if (!taskInstance.isSignalling()) {
continue;
}
//根据对应的流程实例,取得公文标示,
//最后根据公文标示返回待审核公文列表
Long docId = (Long)taskInstance.getContextInstance().getVariable("document");
docIds.add(docId);
}
return docIds;
}
//查询下一步都有哪些流向
@Override
public List findNextTransitions(String username, Long processInstanceId) {
JbpmContext context = getContext();
List transitions = new ArrayList();
System.err.println("processID: " + processInstanceId);
System.err.println("username: " + username);
//根据流程实例标示查找流程实例
ProcessInstance instance = context.getProcessInstance(processInstanceId);
//取得当前节点
String currentNodeName = instance.getRootToken().getName();
//起始节点
String startNodeName = instance.getProcessDefinition().getStartState().getName();
问题在这里,currentNodeName是null
System.err.println("cuurentNodeName: " + currentNodeName);
System.err.println(" 起始节点: " + startNodeName);
//存放下一步所有节点
Collection nextSteps = null;
这里是悲剧,不能直接使用if (currentNodeName.equals(startNodeName))
if (currentNodeName == null
[解决办法]
currentNodeName == "") {
nextSteps = instance.getRootToken().getAvailableTransitions();
} else {
if (currentNodeName.equals(startNodeName)) {
//如果当前节点是起始节点
nextSteps = instance.getRootToken().getAvailableTransitions();
} else {
//如果当前节点不是起始节点,取得用户TaskInstance
List list = context.getTaskMgmtSession().findTaskInstances(username);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
TaskInstance taskInstance = (TaskInstance)iterator.next();
//如果相等,获得走向的Set集合
if (taskInstance.getProcessInstance().getId() == processInstanceId) {
nextSteps = taskInstance.getAvailableTransitions();
break;
}
}
}
}
//只取得名字
for (Iterator iterator = nextSteps.iterator(); iterator.hasNext();) {
Transition transition = (Transition)iterator.next();
transitions.add(transition.getName());
}
return transitions;
}
//查询单个流程
@Override
public Workflow findWorkflow(Long workflowId) {
return (Workflow)getHibernateTemplate().load(Workflow.class, workflowId);
}
//查询所有流程
@Override
public List findWorkflows() {
return getHibernateTemplate().find("from Workflow");
}
//根据用户名和流程实例标示,下一步名称
//流向下一步
@Override
public String flowToNextStep(String username, Long processInstanceId,
String transitionName) {
JbpmContext context = getContext();
String status = null;
ProcessInstance instance = context.getProcessInstance(processInstanceId);
//取得当前节点
String currentNodeName = instance.getRootToken().getNode().getName();
//取得节点起点
String startNodeName = instance.getProcessDefinition().getStartState().getName();
//如果是起始节点,起始节点不予任何人关联
if (currentNodeName.equals(startNodeName)) {
//触发向下一步流动
instance.getRootToken().signal(transitionName);
} else {
//当当前节点不是起始节点,获得用户任务实例
List tasks = context.getTaskMgmtSession().findTaskInstances(username);
for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
TaskInstance taskInstance = (TaskInstance)iterator.next();
if (taskInstance.getProcessInstance().getId() == processInstanceId) {
taskInstance.end(transitionName);
break;
}
}
}
status = instance.getRootToken().getName();
if(instance.hasEnded()){
status = Document.STATUS_FINISH;
}
//返回流向下一个名称
return status;
}
public JbpmConfiguration getJbpmConfiguration() {
return jbpmConfiguration;
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
}