// taskInstance.getTaskId(),
// "Complete task insatance failed.The state of the task insatnce[id=" +
// taskInstance.getId() + "] is " + taskInstance.getState());
// }
if (thisTaskInst.isSuspended()) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(),
"Abort task insatance failed. The task instance [id="
+ thisTaskInst.getId() + "] is suspended");
}
//
IPersistenceService persistenceService = this.rtCtx.getPersistenceService();
WorkflowProcess workflowProcess = thisTaskInst.getWorkflowProcess();
List<IToken> allTokens = null;
List<String> aliveActivityIdsAfterJump = new ArrayList<String>();
if (targetActivityId != null) {
String thisActivityId = thisTaskInst.getActivityId();
boolean isInSameLine = workflowProcess.isInSameLine(thisActivityId,
targetActivityId);
if (isInSameLine){
this.abortTaskInstance(currentSession, processInstance, thisTaskInst, targetActivityId);
}
//合法性检查
allTokens = persistenceService.findTokensForProcessInstance(thisTaskInst.getProcessInstanceId(), null);
aliveActivityIdsAfterJump.add(targetActivityId);
for (int i=0;allTokens!=null && i<allTokens.size();i++){
IToken tokenTmp = allTokens.get(i);
IWFElement workflowElement = workflowProcess.findWFElementById(tokenTmp.getNodeId());
if ((workflowElement instanceof Activity) && !workflowElement.getId().equals(thisActivityId)){
aliveActivityIdsAfterJump.add(workflowElement.getId());
if (workflowProcess.isReachable(targetActivityId, workflowElement.getId())
|| workflowProcess.isReachable(workflowElement.getId(), targetActivityId)){
throw new EngineException(
thisTaskInst.getProcessInstanceId(),
thisTaskInst.getWorkflowProcess(),
thisTaskInst.getTaskId(),
"Abort refused because of the business-logic conflict!");
}
}
}
//1)检查是否在同一个“执行线”上(不做该检查,20091008)
// if (!isInSameLine) {
// throw new EngineException(
// taskInstance.getProcessInstanceId(),
// taskInstance.getWorkflowProcess(),
// taskInstance.getTaskId(),
// "Jumpto refused because of the current activitgy and the target activity are NOT in the same 'Execution Thread'.");
// }
}
INetInstance netInstance = rtCtx.getKernelManager().getNetInstance(
workflowProcess.getId(), thisTaskInst.getVersion());
IActivityInstance targetActivityInstance = null;
if (targetActivityId!=null){
targetActivityInstance = (IActivityInstance) netInstance
.getWFElementInstance(targetActivityId);
}
IActivityInstance thisActivityInstance = (IActivityInstance) netInstance
.getWFElementInstance(thisTaskInst.getActivityId());
if (thisActivityInstance == null) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "系统没有找到与activityId="
+ thisTaskInst.getActivityId()
+ "对应activityInstance,无法执行abort操作。");
}
if (targetActivityInstance != null) {
((TaskInstance) thisTaskInst)
.setTargetActivityId(targetActivityInstance.getActivity()
.getId());
}
// 第一步,首先Abort当前taskInstance
persistenceService.abortTaskInstance((TaskInstance) thisTaskInst);
// 触发相应的事件
TaskInstanceEvent e = new TaskInstanceEvent();
e.setSource(thisTaskInst);
e.setWorkflowSession(currentSession);
e.setProcessInstance(processInstance);
e.setEventType(TaskInstanceEvent.AFTER_TASK_INSTANCE_COMPLETE);
if (this.defaultTaskInstanceEventListener != null) {
this.defaultTaskInstanceEventListener.onTaskInstanceEventFired(e);
}
this.fireTaskInstanceEvent(thisTaskInst, e);
// 第二步,检查ActivityInstance是否可以结束
if (!activityInstanceCanBeCompleted(thisTaskInst)) {
return;
}
// 第三步,尝试结束对应的activityInstance
List<IToken> tokens = persistenceService.findTokensForProcessInstance(
thisTaskInst.getProcessInstanceId(), thisTaskInst
.getActivityId());
// System.out.println("Inside TaskInstance.complete(targetActivityInstance):: tokens.size is "+tokens.size());
if (tokens == null || tokens.size() == 0) {
return;// 表明activityInstance已经结束了。
}
if (tokens.size() > 1) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "与activityId="
+ thisTaskInst.getActivityId() + "对应的token数量(="
+ tokens.size() + ")不正确,正确只能为1,因此无法完成complete操作");
}
IToken token = tokens.get(0);
// stepNumber不相等,不允许执行结束操作。
if (token.getStepNumber().intValue() != thisTaskInst.getStepNumber()
.intValue()) {
return;
}
if (token.isAlive() == false) {
WorkflowProcess process = thisTaskInst.getWorkflowProcess();
throw new EngineException(thisTaskInst.getProcessInstanceId(),
process, thisTaskInst.getTaskId(), "与activityId="
+ thisTaskInst.getActivityId()
+ "对应的token.alive=false,因此无法完成complete操作");
}