这是很早写工作流时写的一个方法,今天正好整理下资料,顺便共享下吧。
/**
* get all the task instances for a given actor. type 1:未接收;2:办理中;3:已办结;4:全部工作
*/
public List<TaskInstance> findTaskInstances(String[] actorIds, String type) {
//结果1
List result1 = null;
try {
String hql = "";
if ("1".equals(type)) {// 1:未接收
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.actorId in (:actorIds)"
+ " and ti.isSuspended <> 1" + " and ti.isOpen = 1"
+ " and ti.start is null" + " and ti.end is null"
+ " order by ti.id desc";
} else if ("2".equals(type)) {// 2:办理中
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.actorId in (:actorIds)"
+ " and ti.isSuspended <> 1" + " and ti.isOpen = 1"
+ " and ti.start is not null" + " and ti.end is null"
+ " order by ti.id desc";
} else if ("3".equals(type)) {// 3:已办结
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.id in ("
+ " select max(t.id) from org.jbpm.taskmgmt.exe.TaskInstance as t"
+ " where t.actorId in (:actorIds) and t.isOpen <> 1"
+ " and t.start is not null and t.end is not null group by t.processInstance.id)"
+ " order by ti.id desc";
} else {// 4:全部工作
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.id in ("
+ " select max(t.id) from org.jbpm.taskmgmt.exe.TaskInstance as t"
+ " where t.actorId in (:actorIds) group by t.processInstance.id)"
+ " order by ti.id desc";
}
Query query = session.createQuery(hql);
query.setParameterList("actorIds", actorIds);
result1 = query.list();
} catch (Exception e) {
log.error(e);
jbpmSession.handleException();
throw new JbpmException("couldn't get task instances list for actor '" + actorIds + "'", e);
}
//结果2
List result2 = null;
try {
String hql = "";
if ("1".equals(type)) {// 1:未接收
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.PooledActor pa join pa.taskInstances ti "
+ "where pa.actorId in (:actorIds)"
+ " and ti.actorId is null"
+ " and ti.isSuspended <> 1" + " and ti.isOpen = 1"
+ " and ti.start is null" + " and ti.end is null"
+ " order by ti.id desc";
} else if ("2".equals(type)) {// 2:办理中
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.PooledActor pa join pa.taskInstances ti "
+ "where pa.actorId in (:actorIds)"
+ " and ti.actorId is null"
+ " and ti.isSuspended <> 1" + " and ti.isOpen = 1"
+ " and ti.start is not null" + " and ti.end is null"
+ " order by ti.id desc";
} else if ("3".equals(type)) {// 3:已办结
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.id in ( select max(t.id)"
+ " from org.jbpm.taskmgmt.exe.PooledActor pa join pa.taskInstances t"
+ " where pa.actorId in (:actorIds) and t.actorId is null and t.isOpen <> 1"
+ " and t.start is not null and t.end is not null group by t.processInstance.id"
+ " )order by ti.id desc";
} else {// 4:全部工作
hql = "select distinct ti "
+ "from org.jbpm.taskmgmt.exe.TaskInstance as ti "
+ "where ti.id in ( select max(t.id)"
+ " from org.jbpm.taskmgmt.exe.PooledActor pa join pa.taskInstances t"
+ " where pa.actorId in (:actorIds) and t.actorId is null group by t.processInstance.id"
+ " )order by ti.id desc";
}
Query query = session.createQuery(hql);
query.setParameterList("actorIds", actorIds);
result2 = query.list();
} catch (Exception e) {
log.error(e);
jbpmSession.handleException();
throw new JbpmException("couldn't get pooled task instances list for actor '" + actorIds + "'", e);
}
//合并结果
Set set = new HashSet();
set.addAll(result1);
set.addAll(result2);
return new ArrayList(set);
}