String targetActivityId, String comments) throws EngineException,
KernelException {
// 首先检查是否可以正确跳转
WorkflowProcess workflowProcess = workItem.getTaskInstance().getWorkflowProcess();
String thisActivityId = workItem.getTaskInstance().getActivityId();
TaskInstance thisTaskInst = (TaskInstance) workItem.getTaskInstance();
//如果是在同一条执行线上,那么可以直接跳过去,只是重复判断了是否在同一条执行线上
boolean isInSameLine = workflowProcess.isInSameLine(thisActivityId,targetActivityId);
if (isInSameLine){
this.completeWorkItemAndJumpTo(workItem, targetActivityId, comments);
return;
}
// 1)检查是否在同一个“执行线”上(关闭该检查,20091002)
// if (!isInSameLine) {
// throw new EngineException(
// thisTaskInst.getProcessInstanceId(),
// thisTaskInst.getWorkflowProcess(),
// thisTaskInst.getTaskId(),
// "Jumpto refused because of the current activitgy and the target activity are NOT in the same 'Execution Thread'.");
// }
// 2)检查目标Activity Form Task的数量(暂时关闭该检查项目)
// Activity targetActivity =
// (Activity)workflowProcess.findWFElementById(activityId);
// int count = getFormTaskCount(targetActivity);
// if (count!=1){
// if (!isInSameLine) throw new
// EngineException("Jumpto refused because of the FORM-type-task count of the target activitgy is NOT 1; the count is "+count);
// }
// 3)检查当前的 taskinstance是否可以结束
IPersistenceService persistenceService = rtCtx.getPersistenceService();
Integer aliveWorkItemCount = persistenceService
.getAliveWorkItemCountForTaskInstance(thisTaskInst.getId());
if (aliveWorkItemCount != null && aliveWorkItemCount > 1) {
throw new EngineException(
thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Jumpto refused because of current taskinstance can NOT be completed. some workitem of this taskinstance is in runing state or initialized state");
}
// 4)检查当前的activity instance是否可以结束
if (Activity.ALL.equals(workItem.getTaskInstance().getActivity().getCompletionStrategy())) {
Integer aliveTaskInstanceCount4ThisActivity = persistenceService
.getAliveTaskInstanceCountForActivity(workItem
.getTaskInstance().getProcessInstanceId(), workItem
.getTaskInstance().getActivityId());
if (aliveTaskInstanceCount4ThisActivity.intValue() > 1) {// 大于1表明当前Activity不可以complete
throw new EngineException(
thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Jumpto refused because of current activity instance can NOT be completed. some task instance of this activity instance is in runing state or initialized state");
}
}
//4)首先检查目标状态M是否存在冲突,如果存在冲突则不允许跳转;如果不存在冲突,则需要调整token
List<IToken> allTokens = persistenceService.findTokensForProcessInstance(thisTaskInst.getProcessInstanceId(), null);
WorkflowProcess thisProcess = thisTaskInst.getWorkflowProcess();//找到当前的工作里模型
List<String> aliveActivityIdsAfterJump = new ArrayList<String>();//计算跳转后,哪些activity节点复活
aliveActivityIdsAfterJump.add(targetActivityId);
for (int i=0;allTokens!=null && i<allTokens.size();i++){
IToken tokenTmp = allTokens.get(i);
IWFElement workflowElement = thisProcess.findWFElementById(tokenTmp.getNodeId()); //找到拥有此token的工作流元素
if ((workflowElement instanceof Activity) && !workflowElement.getId().equals(thisActivityId)){
//注意:不能自己跳转到自己,同时此工作流元素是activity类型
aliveActivityIdsAfterJump.add(workflowElement.getId());
if (thisProcess.isReachable(targetActivityId, workflowElement.getId())
|| thisProcess.isReachable(workflowElement.getId(), targetActivityId)){
throw new EngineException(
thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Jumpto refused because of the business-logic conflict!");
}
}
}
//所有检查结束,开始执行跳转操作
INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(
workflowProcess.getId(),
workItem.getTaskInstance().getVersion());
if (netInstance == null) {
throw new EngineException(thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Not find the net instance for workflow process [id="
+ workflowProcess.getId() + ", version="
+ workItem.getTaskInstance().getVersion() + "]");
}
Object obj = netInstance.getWFElementInstance(targetActivityId);
IActivityInstance targetActivityInstance = (IActivityInstance) obj;
if (targetActivityInstance == null) {
throw new EngineException(thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Not find the activity instance for activity[process id="
+ workflowProcess.getId() + ", version="
+ workItem.getTaskInstance().getVersion()
+ ",activity id=" + targetActivityId + "]");
}
if (rtCtx.isEnableTrace()) {
ProcessInstanceTrace trace = new ProcessInstanceTrace();
trace.setProcessInstanceId(workItem.getTaskInstance()
.getProcessInstanceId());
trace.setStepNumber(workItem.getTaskInstance().getStepNumber() + 1);
trace.setType(ProcessInstanceTrace.JUMPTO_TYPE);
trace.setFromNodeId(workItem.getTaskInstance().getActivityId());
trace.setToNodeId(targetActivityId);
trace.setEdgeId("");
rtCtx.getPersistenceService().saveOrUpdateProcessInstanceTrace(
trace);
}
//调整token布局
List<Synchronizer> allSynchronizersAndEnds = new ArrayList<Synchronizer>();
allSynchronizersAndEnds.addAll(thisProcess.getSynchronizers());
allSynchronizersAndEnds.addAll(thisProcess.getEndNodes());
for (int i=0;i<allSynchronizersAndEnds.size();i++){
Synchronizer synchronizer = allSynchronizersAndEnds.get(i);
int volumn = 0;
if (synchronizer instanceof EndNode){
volumn = synchronizer.getEnteringTransitions().size();
}else{
volumn = synchronizer.getEnteringTransitions().size()*synchronizer.getLeavingTransitions().size();
}
IToken tokenTmp = new Token();
tokenTmp.setNodeId(synchronizer.getId());
tokenTmp.setAlive(false);
tokenTmp.setProcessInstanceId(thisTaskInst.getProcessInstanceId());
tokenTmp.setStepNumber(-1);
List<String> incomingTransitionIds = new ArrayList<String>();
boolean reachable = false;
List<Transition> enteringTrans = synchronizer.getEnteringTransitions();
for (int m=0;m<aliveActivityIdsAfterJump.size();m++){
String aliveActivityId = aliveActivityIdsAfterJump.get(m);
if (thisProcess.isReachable(aliveActivityId, synchronizer.getId())){
Transition trans = null;
reachable = true;
for (int j=0;j<enteringTrans.size();j++){
trans = enteringTrans.get(j);
Node fromNode = trans.getFromNode();
if (thisProcess.isReachable(aliveActivityId, fromNode.getId())){
if (!incomingTransitionIds.contains(trans.getId())){
incomingTransitionIds.add(trans.getId());
}
}
}