public virtual IProcessInstance CreateAndStartProcessInstanceWithInitialFlowElement(IProcessDefinition processDefinition, string businessKey, string processInstanceName, FlowElement initialFlowElement, Process process, IDictionary <string, object> variables, IDictionary <string, object> transientVariables, bool startProcessInstance) { ICommandContext commandContext = Context.CommandContext; // Create the process instance string initiatorVariableName = null; if (initialFlowElement is StartEvent) { initiatorVariableName = ((StartEvent)initialFlowElement).Initiator; } IExecutionEntity processInstance = commandContext.ExecutionEntityManager.CreateProcessInstanceExecution(processDefinition, businessKey, processDefinition.TenantId, initiatorVariableName); commandContext.HistoryManager.RecordProcessInstanceStart(processInstance, initialFlowElement); processInstance.Variables = ProcessDataObjects(process.DataObjects); if (string.IsNullOrWhiteSpace(businessKey) == false) { processInstance.SetVariable(WorkflowVariable.GLOBAL_PROCESSINSTANCE_BUSINESSKEY_VARNAME, businessKey); } // Set the variables passed into the start command if (variables != null) { foreach (string varName in variables.Keys) { processInstance.SetVariable(varName, variables[varName]); } } if (transientVariables != null) { foreach (string varName in transientVariables.Keys) { processInstance.SetTransientVariable(varName, transientVariables[varName]); } } // Set processInstance name if (!(processInstanceName is null)) { processInstance.Name = processInstanceName; commandContext.HistoryManager.RecordProcessInstanceNameChange(processInstance.Id, processInstanceName); } // Fire events if (Context.ProcessEngineConfiguration.EventDispatcher.Enabled) { Context.ProcessEngineConfiguration.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityWithVariablesEvent(ActivitiEventType.ENTITY_INITIALIZED, processInstance, variables, false)); } // Create the first execution that will visit all the process definition elements IExecutionEntity execution = commandContext.ExecutionEntityManager.CreateChildExecution(processInstance); execution.CurrentFlowElement = initialFlowElement; if (startProcessInstance) { this.StartProcessInstance(processInstance, commandContext, variables); } return(processInstance); }
/// <summary> /// /// </summary> public virtual IProcessInstance CreateAndStartProcessInstanceWithInitialFlowElement(IProcessDefinition processDefinition, string businessKey, string processInstanceName, FlowElement initialFlowElement, Process process, IDictionary <string, object> variables, IDictionary <string, object> transientVariables, bool startProcessInstance) { ICommandContext commandContext = Context.CommandContext; // Create the process instance string initiatorVariableName = null; if (initialFlowElement is StartEvent) { initiatorVariableName = ((StartEvent)initialFlowElement).Initiator; } IExecutionEntity processInstance = commandContext.ExecutionEntityManager.CreateProcessInstanceExecution(processDefinition, businessKey, processDefinition.TenantId, initiatorVariableName); commandContext.HistoryManager.RecordProcessInstanceStart(processInstance, initialFlowElement); processInstance.Variables = ProcessDataObjects(process.DataObjects); if (string.IsNullOrWhiteSpace(businessKey) == false) { processInstance.SetVariable(WorkflowVariable.GLOBAL_PROCESSINSTANCE_BUSINESSKEY_VARNAME, businessKey); } var title = process.GetExtensionElementAttributeValue(WorkflowVariable.GLOBAL_PROCESSINSTANCE_TITLE); if (string.IsNullOrWhiteSpace(title) == false) { var reg = new Regex(@"(\$\{)(.*?)(}{1,1})", RegexOptions.Multiline); title = reg.Replace(title, (m) => { return(ExpressionEvaluator.GetValue(variables, m.Groups[2].Value)?.ToString()); }); processInstance.SetVariable(WorkflowVariable.GLOBAL_PROCESSINSTANCE_TITLE, title); } else { processInstance.SetVariable(WorkflowVariable.GLOBAL_PROCESSINSTANCE_TITLE, process.Name); } // Set the variables passed into the start command if (variables != null) { foreach (string varName in variables.Keys) { processInstance.SetVariable(varName, variables[varName]); } } if (transientVariables != null) { foreach (string varName in transientVariables.Keys) { processInstance.SetTransientVariable(varName, transientVariables[varName]); } } // Set processInstance name if (title is object) { processInstance.Name = title; commandContext.HistoryManager.RecordProcessInstanceNameChange(processInstance.Id, title); } else if (processInstanceName is object) { processInstance.Name = processInstanceName; commandContext.HistoryManager.RecordProcessInstanceNameChange(processInstance.Id, processInstanceName); } // Fire events if (Context.ProcessEngineConfiguration.EventDispatcher.Enabled) { Context.ProcessEngineConfiguration.EventDispatcher.DispatchEvent(ActivitiEventBuilder.CreateEntityWithVariablesEvent(ActivitiEventType.ENTITY_INITIALIZED, processInstance, variables, false)); } // Create the first execution that will visit all the process definition elements IExecutionEntity execution = commandContext.ExecutionEntityManager.CreateChildExecution(processInstance); execution.CurrentFlowElement = initialFlowElement; if (startProcessInstance) { this.StartProcessInstance(processInstance, commandContext, variables); } return(processInstance); }