Example #1
0
        public virtual IProcessInstanceWithVariables Execute(CommandContext commandContext)
        {
            var processDefinition =
                new GetDeployedProcessDefinitionCmd(InstantiationBuilder, false).Execute(commandContext);

            foreach (var checker in commandContext.ProcessEngineConfiguration.CommandCheckers)
            {
                checker.CheckCreateProcessInstance(processDefinition);
            }

            var modificationBuilder = InstantiationBuilder.ModificationBuilder;

            EnsureUtil.EnsureNotEmpty(
                @"At least one instantiation instruction required (e.g. by invoking startBefore(..), startAfter(..) or startTransition(..))", "instructions", ListExt.ConvertToIlist(modificationBuilder.ModificationOperations));

            // instantiate the process
            var initialActivity = DetermineFirstActivity(processDefinition, modificationBuilder);

            ExecutionEntity processInstance = (ExecutionEntity)processDefinition.CreateProcessInstance(InstantiationBuilder.BusinessKey,
                                                                                                       InstantiationBuilder.CaseInstanceId, initialActivity);

            processInstance.SkipCustomListeners = modificationBuilder.SkipCustomListeners;
            var variables = modificationBuilder.ProcessVariables;

            ExecutionVariableSnapshotObserver variablesListener = new ExecutionVariableSnapshotObserver(processInstance);

            processInstance.StartWithoutExecuting(variables);

            // prevent ending of the process instance between instructions
            processInstance.PreserveScope = true;

            // apply modifications
            var instructions = modificationBuilder.ModificationOperations;

            for (var i = 0; i < instructions.Count; i++)
            {
                var instruction = instructions[i];
                Log.DebugStartingInstruction(processInstance.Id, i, instruction.Describe());

                instruction.ProcessInstanceId   = processInstance.Id;
                instruction.SkipCustomListeners = modificationBuilder.SkipCustomListeners;
                instruction.SkipIoMappings      = modificationBuilder.SkipIoMappings;
                instruction.Execute(commandContext);
            }

            if (!processInstance.HasChildren() && processInstance.IsEnded)
            {
                // process instance has ended regularly but this has not been propagated yet
                // due to preserveScope setting
                processInstance.PropagateEnd();
            }

            return(new ProcessInstanceWithVariablesImpl(processInstance, variablesListener.Variables));
        }