protected internal virtual void ValidateAndSwitchVersionOfExecution(CommandContext commandContext,
                                                                            ExecutionEntity execution, ProcessDefinitionEntity newProcessDefinition)
        {
            // check that the new process definition version contains the current activity
            if (execution.Activity != null)
            {
                string       activityId  = execution.Activity.Id;
                IPvmActivity newActivity = newProcessDefinition.FindActivity(activityId);

                if (newActivity == null)
                {
                    throw new ProcessEngineException("The new process definition " + "(key = '" + newProcessDefinition.Key + "') " + "does not contain the current activity " + "(id = '" + activityId + "') " + "of the process instance " + "(id = '" + _processInstanceId + "').");
                }

                // clear cached activity so that outgoing transitions are refreshed
                execution.Activity = (newActivity);
            }

            // switch the process instance to the new process definition version
            execution.SetProcessDefinition(newProcessDefinition);

            // and change possible existing tasks (as the process definition id is stored there too)
            IList <TaskEntity> tasks = commandContext.TaskManager.FindTasksByExecutionId(execution.Id);

            foreach (TaskEntity taskEntity in tasks)
            {
                taskEntity.ProcessDefinitionId = newProcessDefinition.Id;
            }
        }
        public override IPvmProcessInstance CreateProcessInstance(string businessKey, string caseInstanceId, ActivityImpl initial)
        {
            EnsureNotSuspended();

            ExecutionEntity processInstance = (ExecutionEntity)CreateProcessInstanceForInitial(initial);

            //// do not reset executions (CAM-2557)!
            processInstance.SetExecutions(new List <IActivityExecution>());//.SetExecutions(new ArrayList<ExecutionEntity>());


            processInstance.SetProcessDefinition(processDefinition);

            //// Do not initialize variable map (let it happen lazily)

            //// reset the process instance in order to have the db-generated process instance id available
            processInstance.SetProcessInstance(processInstance);

            //// initialize business key
            if (businessKey != null)
            {
                processInstance.BusinessKey = businessKey;
            }

            //// initialize case instance id
            if (caseInstanceId != null)
            {
                processInstance.CaseInstanceId = caseInstanceId;
            }

            if (TenantId != null)
            {
                processInstance.TenantId = TenantId;
            }

            return(processInstance);
        }