研究JBPM的同志们,算是一点帮助吧,自己研究半个多月JBPM的成果,贡献出来,请指教!
import java.util.HashMap;
import java.util.List;import java.util.Set;
import org.hibernate.Session;
import org.jbpm.api.Execution;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.HistoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.cmd.Command;
import org.jbpm.api.cmd.Environment;
import org.jbpm.api.history.HistoryActivityInstance;
import org.jbpm.api.history.HistoryActivityInstanceQuery;
import org.jbpm.api.task.Task;
public class WithdrawCommand implements Command {
String execid;
String activityName;
HashMap var;
/**
*
* @param execid
* 流程ID
* @param activityName
* 要撤回的目的活动节点
* @param variables
* 变量,可选
*/
public WithdrawCommand(String execid, String activityName, String variables) {
this.execid = execid;
this.activityName = activityName;
this.var = new HashMap();
if (variables != null && variables.trim().length() > 0) {
String[] pairs = variables.split(";");
for (String s : pairs) {
String[] pair = s.split(":");
if (pair.length == 2) {
var.put(pair[0], pair[1]);
}
}
}
}
@Override
public Object execute(Environment environment) throws Exception {
// 1取得当前的活动节点
ExecutionService es = environment.get(ExecutionService.class);
TaskService ts = environment.get(TaskService.class);
HistoryService hs = environment.get(HistoryService.class);
Execution e = es.findExecutionById(execid);
Set<String> actiNames = e.findActiveActivityNames();
if (actiNames == null || actiNames.size() == 0) {
throw new Exception("没有可撤回的活动!");
}
if (actiNames.size() > 1) {
throw new Exception("发生异常,存在多个的活动!");
}
String actiName = actiNames.iterator().next(); // 当前活动节点名称
// 2 将流程推回
Task t = ts.createTaskQuery().executionId(execid).uniqueResult();
ts.completeTask(t.getId(), "退回至" + activityName, var);
Session session = environment.get(Session.class);
// 清除痕迹,注意是清除两步痕迹,很重要,要清除得天衣无缝
// 3.1 清除回退的痕迹
List<HistoryActivityInstance> hinsts1 = hs
.createHistoryActivityInstanceQuery().executionId(execid)
.activityName(actiName)
.orderDesc(HistoryActivityInstanceQuery.PROPERTY_ENDTIME)
.list();
if (hinsts1 == null || hinsts1.size() == 0) {
throw new Exception("清除回退历史记录时发生异常,历史不存在!");
}
session.delete(hinsts1.get(0));
// 3.2清除之前发送的痕迹
List<HistoryActivityInstance> hinsts2 = hs
.createHistoryActivityInstanceQuery().executionId(execid)
.activityName(activityName)
.orderDesc(HistoryActivityInstanceQuery.PROPERTY_ENDTIME)
.list();
if (hinsts2 == null || hinsts2.size() == 0) {
throw new Exception("清除发送历史记录时发生异常,历史不存在!");
}
for (HistoryActivityInstance hi : hinsts2) {
if (hi.getEndTime() != null) {
session.delete(hi);
break;
}
}
return null;
}
}
这个命令怎么执行呢?在你的service中:
WithdrawCommand command=new WithdrawCommand(execid,activityName,variables);
processEngine.execute(command);
不多说了,呵呵!