/// <summary> /// Set task input data (validation included) /// </summary> /// <param name="inputData"></param> public virtual void SetTaskInputData(IDataObject inputData) { StructDef sd = ProcessTask.GetTaskInputDataSchema(); inputData.Validate(sd); DataObject taskData = new DataObject(); IScriptContext ctx = CreateTaskScriptContext(); ctx.SetItem("data", ContextItem.Variable, new DOBMutant(taskData)); foreach (VariableDef vd in ProcessTask.TaskVariables) { if (vd.VariableDir == VariableDef.Dir.In || vd.VariableDir == VariableDef.Dir.InOut) { taskData[vd.Name] = inputData[vd.Name]; } else { if (vd.DefaultValueExpr != null && vd.DefaultValueExpr.Length > 0) { taskData[vd.Name] = Script.RunCode(vd.DefaultValueExpr, ctx); } } } StructDef internalSchema = GetTaskInternalDataSchema(); taskData.Validate(internalSchema); _taskData = taskData; }
/// <summary> /// Prepare task data - validate the input and initialize task variables. Throws exception if input data structure /// does not validate against task data schema. /// </summary> /// <param name="inputData"></param> protected void InitializeTaskData(IDataObject inputData) { StructDef sd = Context.TaskDefinition.GetTaskInputDataSchema(); inputData.Validate(sd); DataObject taskData = new DataObject(); ITaskScript ts = CreateScriptContext(taskData); foreach (VariableDef vd in Context.TaskDefinition.TaskVariables) { if (vd.VariableDir == VariableDef.Dir.In || vd.VariableDir == VariableDef.Dir.InOut) { taskData[vd.Name] = inputData[vd.Name]; } else { if (vd.DefaultValueExpr != null && vd.DefaultValueExpr.Length > 0) { object defVal = ts.GetDefaultVariableValue(vd.Name); taskData[vd.Name] = defVal; } } } StructDef internalSchema = Context.TaskDefinition.GetTaskInternalDataSchema(); taskData.Validate(internalSchema); this._taskData = taskData; }
/// <summary> /// Set process input data /// </summary> /// <param name="data"></param> public void SetProcessInputData(IDataObject data) { StructDef procInput = Definition.GetProcessInputDataSchema(); data.Validate(procInput); DataObject dob = new DataObject(); IProcessScript ctx = CreateProcessScriptContext(); foreach (VariableDef vd in Definition.ProcessVariables) { if (data.ContainsKey(vd.Name)) { dob[vd.Name] = data[vd.Name]; } else { if (vd.DefaultValueExpr == null || vd.DefaultValueExpr.Length == 0) { if (vd.IsRequired && (vd.VariableDir == VariableDef.Dir.In || vd.VariableDir == VariableDef.Dir.InOut)) throw new ApplicationException("Missing required input variable: " + vd.Name); } else { object val = ctx.GetDefaultVariableValue(vd.Name); dob[vd.Name] = val; } } } StructDef internalSchema = GetProcessInternalDataSchema(); dob.Validate(internalSchema); _processInstanceData = new DataObject(); _processInstanceData["variables"] = dob; _processInstanceData["instanceInfo"] = new DataObject(); }
public string StartProcessInstance(string definitionId, IDataObject inputData, string userId, string correlationId) { try { ProcessDefinition pd = PackageRepository.GetProcess(definitionId); if (pd == null) throw new ApplicationException("Process definition not found: " + definitionId); StructDef sd = pd.GetProcessInputDataSchema(); inputData.Validate(sd); using (TransactionScope ts = new TransactionScope(_transactionOption)) { ProcessInstance pi = new ProcessInstance(); pi.StartDate = DateTime.Now; pi.StartedBy = System.Threading.Thread.CurrentPrincipal.Identity.Name; pi.InstanceId = Guid.NewGuid().ToString("N"); pi.ProcessDefinitionId = definitionId; pi.Environment = this; pi.CorrelationId = correlationId; pi.Activate(); log.Info("Created new process instance for process {0}.{1}: {2}", pd.Name, pd.Version, pi.InstanceId); pi.SetProcessInputData(inputData); pi.CreateStartToken(); pi.Passivate(); InstanceRepository.InsertNewProcessInstance(pi); ts.Complete(); NotifyReadyProcessSaved(); return pi.InstanceId; } } catch (Exception ex) { log.Error(string.Format("Error starting process: {0}", ex)); throw; } }