IActivityInstance activityInstance) throws EngineException,
KernelException {
Activity activity = activityInstance.getActivity();
IPersistenceService persistenceService = rtCtx.getPersistenceService();
ICalendarService calService = rtCtx.getCalendarService();
IProcessInstance processInstance = token.getProcessInstance();
WorkflowSession workflowSession = (WorkflowSession) ((IWorkflowSessionAware) processInstance)
.getCurrentWorkflowSession();
if (workflowSession == null) {
throw new EngineException(token.getProcessInstance(),
activityInstance.getActivity(),
"The workflow session in process instance can NOT be null");
}
int createdTaskInstanceCount = 0;
for (int i = 0; i < activity.getTasks().size(); i++) {
Task task = activity.getTasks().get(i);
// 1、创建Task实例,并设置工作流系统定义的属性
ITaskInstance taskInstance = this.createTaskInstance(
workflowSession, processInstance, task, activity);
if (taskInstance == null) {
continue;
}
createdTaskInstanceCount = createdTaskInstanceCount + 1;
String taskType = task.getType();
((TaskInstance) taskInstance).setTaskType(taskType);
((TaskInstance) taskInstance).setStepNumber(token.getStepNumber());
// ((TaskInstance) taskInstance).setTokenId(token.getId());
((TaskInstance) taskInstance).setProcessInstanceId(processInstance
.getId());
((TaskInstance) taskInstance).setProcessId(processInstance
.getProcessId());
((TaskInstance) taskInstance).setVersion(processInstance
.getVersion());
((TaskInstance) taskInstance).setActivityId(activity.getId());
if (Task.FORM.equals(taskType)) {
((TaskInstance) taskInstance)
.setAssignmentStrategy(((FormTask) task)
.getAssignmentStrategy());
((TaskInstance) taskInstance).setCanBeWithdrawn(true);
} else {
((TaskInstance) taskInstance).setCanBeWithdrawn(false);
}
((TaskInstance) taskInstance).setCreatedTime(calService
.getSysDate());
((TaskInstance) taskInstance).setDisplayName(task.getDisplayName());
((TaskInstance) taskInstance).setName(task.getName());
((TaskInstance) taskInstance).setState(TaskInstance.INITIALIZED);
((TaskInstance) taskInstance).setTaskId(task.getId());
((TaskInstance) taskInstance).setFromActivityId(token
.getFromActivityId());
((IRuntimeContextAware) taskInstance).setRuntimeContext(rtCtx);
((IWorkflowSessionAware) taskInstance)
.setCurrentWorkflowSession(workflowSession);
// 计算超时
Duration duration = task.getDuration();
if (duration != null && calService != null) {
((TaskInstance) taskInstance).setExpiredTime(calService
.dateAfter(calService.getSysDate(), duration));
}
// 2、保存实例taskInstance
persistenceService.saveOrUpdateTaskInstance(taskInstance);