.getActivityId());
IActivityInstance thisActivityInstance = (IActivityInstance) obj;
// 一切检查通过之后进行“收回”处理
IWorkflowSession session = ((IWorkflowSessionAware) workItem)
.getCurrentWorkflowSession();
session.setWithdrawOrRejectOperationFlag(true);
int newStepNumber = thisTaskInstance.getStepNumber() + 2;
try {
DynamicAssignmentHandler dynamicAssignmentHandler = new DynamicAssignmentHandler();
List<String> actorIds = new ArrayList<String>();
actorIds.add(workItem.getActorId());
dynamicAssignmentHandler.setActorIdsList(actorIds);
((WorkflowSession) session)
.setCurrentDynamicAssignmentHandler(dynamicAssignmentHandler);
// 1、首先将后续环节的TaskInstance极其workItem变成Canceled状态
List<String> targetActivityIdList = new ArrayList<String>();
StringBuffer theFromActivityIds = new StringBuffer("");
for (int i = 0; i < targetTaskInstancesList.size(); i++) {
TaskInstance taskInstTemp = (TaskInstance) targetTaskInstancesList
.get(i);
persistenceService.abortTaskInstance(taskInstTemp);
if (!targetActivityIdList
.contains(taskInstTemp.getActivityId())) {
targetActivityIdList.add(taskInstTemp.getActivityId());
if (theFromActivityIds.length() == 0) {
theFromActivityIds.append(taskInstTemp.getActivityId());
} else {
theFromActivityIds.append(
IToken.FROM_ACTIVITY_ID_SEPARATOR).append(
taskInstTemp.getActivityId());
}
}
}
persistenceService.deleteTokensForNodes(thisTaskInstance
.getProcessInstanceId(), targetActivityIdList);
if (rtCtx.isEnableTrace()) {
for (int i = 0; targetActivityIdList != null
&& i < targetActivityIdList.size(); i++) {
String tmpActId = (String) targetActivityIdList.get(i);
ProcessInstanceTrace trace = new ProcessInstanceTrace();
trace.setProcessInstanceId(thisTaskInstance
.getProcessInstanceId());
trace.setStepNumber(newStepNumber);
trace.setType(ProcessInstanceTrace.WITHDRAW_TYPE);
trace.setFromNodeId(tmpActId);
trace.setToNodeId(thisActivity.getId());
trace.setEdgeId("");
rtCtx.getPersistenceService()
.saveOrUpdateProcessInstanceTrace(trace);
}
}
ITransitionInstance thisLeavingTransitionInstance = (ITransitionInstance) thisActivityInstance
.getLeavingTransitionInstances().get(0);
ISynchronizerInstance synchronizerInstance = (ISynchronizerInstance) thisLeavingTransitionInstance
.getLeavingNodeInstance();
if (synchronizerInstance.getEnteringTransitionInstances().size() > 1) {
Token supplementToken = new Token();
((Token) supplementToken).setAlive(false);
((Token) supplementToken).setNodeId(synchronizerInstance
.getSynchronizer().getId());
supplementToken.setProcessInstanceId(thisTaskInstance
.getProcessInstanceId());
supplementToken
.setProcessInstance(((TaskInstance) thisTaskInstance)
.getAliveProcessInstance());
supplementToken.setFromActivityId("Empty(created by withdraw)");
supplementToken.setStepNumber(newStepNumber);
supplementToken.setValue(synchronizerInstance.getVolume()
- thisLeavingTransitionInstance.getWeight());
persistenceService.saveOrUpdateToken(supplementToken);
}
Token newToken = new Token();
((Token) newToken).setAlive(true);
((Token) newToken).setNodeId(workItem.getTaskInstance()
.getActivityId());
newToken.setProcessInstanceId(thisTaskInstance
.getProcessInstanceId());
newToken.setProcessInstance(((TaskInstance) thisTaskInstance)
.getAliveProcessInstance());
newToken.setFromActivityId(theFromActivityIds.toString());
newToken.setStepNumber(newStepNumber);
newToken.setValue(0);
persistenceService.saveOrUpdateToken(newToken);
this.createTaskInstances(newToken, thisActivityInstance);
List<IWorkItem> workItems = persistenceService.findTodoWorkItems(workItem
.getActorId(), workItem.getTaskInstance().getProcessId(),
workItem.getTaskInstance().getTaskId());
if (workItems == null || workItems.size() == 0) {
throw new EngineException(thisTaskInstance
.getProcessInstanceId(), thisTaskInstance
.getWorkflowProcess(), thisTaskInstance.getTaskId(),
"Withdraw operation failed.No work item has been created for Task[id="
+ thisTaskInstance.getTaskId() + "]");
}
if (workItems.size() > 1) {
throw new EngineException(
thisTaskInstance.getProcessInstanceId(),
thisTaskInstance.getWorkflowProcess(),
thisTaskInstance.getTaskId(),
"Withdraw operation failed.More than one work item have been created for Task[id="
+ thisTaskInstance.getTaskId() + "]");
}
return (IWorkItem) workItems.get(0);
} finally {
session.setWithdrawOrRejectOperationFlag(false);
}
}