public string Serialize(TaskInstance ti, out string taskTypeId)
 {
     StringWriter sw = new StringWriter();
     _ser.Serialize(new JsonTextWriter(sw), ti);
     taskTypeId = ti.GetType().FullName;
     return sw.ToString();
 }
 public virtual void SaveNew(TaskInstance ti)
 {
     if (string.IsNullOrEmpty(ti.InstanceId)) throw new Exception("Missing instance ID");
     if (_cache.ContainsKey(ti.InstanceId)) throw new Exception("Task already exists");
     string typeId;
     var th = new TaskHolder
     {
         State = RecordState.New,
         Deserialized = ti,
         TaskData = _ser.Serialize(ti, out typeId)
     };
     th.TaskTypeId = typeId;
     _cache[ti.InstanceId] = th;
 }
 public virtual void Update(TaskInstance ti)
 {
     TaskHolder th;
     if (!_cache.TryGetValue(ti.InstanceId, out th))
     {
         throw new Exception("Task not cached");
     }
     if (th.State == RecordState.Unmodified)
     {
         th.State = RecordState.Modified;
     }
     string typeId;
     th.TaskData = _ser.Serialize(ti, out typeId);
     th.TaskTypeId = typeId;
 }
        public override void Update(TaskInstance ti)
        {
            var ph = GetProcessRecord(ti.ProcessInstanceId, true);
            if (ph == null) throw new Exception("Process instance not found: " + ti.ProcessInstanceId);
            var i2 = ph.TaskInstances.FindIndex(x => x.InstanceId == ti.InstanceId);
            if (i2 < 0) throw new Exception("Task not found: " + ti.InstanceId);
            ph.TaskInstances[i2] = ti;

        }
 public override void SaveNew(TaskInstance ti)
 {
     var ph = GetProcessRecord(ti.ProcessInstanceId, true);
     if (ph == null)
     {
         //new instance
         ph = new ProcessHolder
         {
             ProcessInstance = ti.ProcessInstanceId,
             State = RecordState.New,
             IsForUpdate = true,
             DbVersion = null,
             TaskInstances = new List<TaskInstance>()
         };
         ph.TaskInstances.Add(ti);
         _cache[ph.ProcessInstance] = ph;
     }
     else
     {
         var i2 = ph.TaskInstances.FindIndex(x => x.InstanceId == ti.InstanceId);
         if (i2 >= 0) throw new Exception("Task already exists: " + ti.InstanceId);
         ph.TaskInstances.Add(ti);
         if (ph.State == RecordState.Unmodified) ph.State = RecordState.Modified;
     }
 }
 public void SetTaskInstanceInfo(TaskInstance ti, ITaskExecutionContext ctx)
 {
     this.Task = ti;
     this.TaskData = new QuackTaskDataWrapper(ti.TaskData);
     this.Context = ctx;
 }
 public Dictionary<string, object> GatherOutputData(TaskInstance ti, ITaskExecutionContext ctx)
 {
     _pd.SetTaskInstanceInfo(ti, ctx);
     string ks = DslUtil.TaskScriptKey(ti.TaskId, "AfterComplete");
     if (_pd._stmts.ContainsKey(ks)) _pd._stmts[ks]();
     var td = _def.GetRequiredTask(ti.TaskId);
     
     foreach (var bd in td.OutputParameterBindings)
     {
         string k = DslUtil.TaskParamOutBindingKey(td.Id, bd.Target);
         if (bd.BindType == DataBindingType.Expr)
         {
             ti.TaskData[bd.Target] = _pd._exprs[k]();
         }
         else if (bd.BindType == DataBindingType.CopyVar)
         {
             var pi = ti.GetType().GetProperty(bd.Source);
             if (pi == null) throw new NGinnBPM.ProcessModel.Exceptions.TaskParameterInvalidException(bd.Source, "Property not found: " + bd.Source).SetTaskId(ti.TaskId);
             ti.TaskData[bd.Target] = pi.GetValue(ti, null);
         }
         else if (bd.BindType == DataBindingType.Literal)
         {
             ti.TaskData[bd.Target] = bd.Source; //todo: type convert
         }
         else throw new Exception();
     }
     
     string k2 = DslUtil.TaskOutputDataBindingKey(td.Id);
     if (_pd._exprs.ContainsKey(k2))
     {
         SC.IDictionary dic = (SC.IDictionary)_pd._exprs[k2]();
         return ToTaskData(dic);
     }
     Dictionary<string, object> ret = new Dictionary<string, object>();
     foreach (var vd in td.Variables.Where(x => x.VariableDir == ProcessModel.Data.VariableDef.Dir.Out || x.VariableDir == ProcessModel.Data.VariableDef.Dir.InOut))
     {
         ret[vd.Name] = ti.TaskData[vd.Name];
     }
     return ret;
 }
 public void ExecuteTaskScriptBlock(TaskInstance ti, string blockName, ITaskExecutionContext ctx)
 {
     string k = DslUtil.TaskScriptKey(ti.TaskId, blockName);
     _pd.SetTaskInstanceInfo(ti, ctx);
     try
     {
         Action act;
         if (_pd._stmts.TryGetValue(k, out act) && act != null)
         {
             act();
         }
     }
     catch (Exception ex)
     {
         throw;
     }
 }
 public void InitializeNewTask(TaskInstance ti, Dictionary<string, object> inputData, ITaskExecutionContext ctx)
 {
     if (string.IsNullOrEmpty(ti.InstanceId) ||
         string.IsNullOrEmpty(ti.TaskId) ||
         string.IsNullOrEmpty(ti.ProcessDefinitionId) ||
         string.IsNullOrEmpty(ti.ProcessInstanceId))
         throw new Exception("Task not inited properly");
     _pd.SetTaskInstanceInfo(ti, ctx);
     
     TaskDef td = _def.GetRequiredTask(ti.TaskId);
     if (td.Variables != null)
     {
         foreach (var vd in td.Variables)
         {
             if (vd.VariableDir == ProcessModel.Data.VariableDef.Dir.In ||
                 vd.VariableDir == ProcessModel.Data.VariableDef.Dir.InOut)
             {
                 if (inputData.ContainsKey(vd.Name))
                     ti.TaskData[vd.Name] = inputData[vd.Name];
             }
             if (!ti.TaskData.ContainsKey(vd.Name))
             {
                 var k = DslUtil.TaskVariableDefaultKey(td.Id, vd.Name);
                 if (_pd._exprs.ContainsKey(k))
                 {
                     ti.TaskData[vd.Name] = _pd._exprs[k]();
                 }
                 else if (!string.IsNullOrEmpty(vd.DefaultValueExpr))
                 {
                     ti.TaskData[vd.Name] = vd.DefaultValueExpr; //TODO: add type conversion
                 }
                 else if (vd.IsRequired)
                     throw new NGinnBPM.ProcessModel.Exceptions.DataValidationException("Required variable missing: " + vd.Name).SetTaskId(ti.TaskId).SetProcessDef(ti.ProcessDefinitionId);
             }
         }
     }
     //now initialize task parameters
     if (td.InputParameterBindings != null)
     {
         foreach (var bd in td.InputParameterBindings)
         {
             var pi = ti.GetType().GetProperty(bd.Target);
             if (pi == null)
             {
                 throw new NGinnBPM.ProcessModel.Exceptions.TaskParameterInvalidException(bd.Target, "Property not found: " + bd.Target).SetTaskId(ti.TaskId);
             }
             string k = DslUtil.TaskParamInBindingKey(td.Id, bd.Target);
             if (bd.BindType == DataBindingType.Expr)
             {
                 pi.SetValue(ti, _pd._exprs[k](), null);
             }
             else if (bd.BindType == DataBindingType.CopyVar)
             {
                 pi.SetValue(ti, ti.TaskData.ContainsKey(bd.Source) ? ti.TaskData[bd.Source] : null, null);
             }
             else if (bd.BindType == DataBindingType.Literal)
             {
                 pi.SetValue(ti, Convert.ChangeType(bd.Source, pi.PropertyType), null);
             }
             else throw new Exception();
         }
     }
     string ks = DslUtil.TaskScriptKey(ti.TaskId, "BeforeEnable");
     if (_pd._stmts.ContainsKey(ks))
         _pd._stmts[ks]();
 }
 public void SetTaskScriptContext(TaskInstance task, Dictionary<string, object> inputData, Dictionary<string, object> outputData)
 {
     throw new NotImplementedException();
     //_pd.SetTaskInstanceInfo(task, ctx);
     _pd.SetInputData(inputData);
     _pd.SetOutputData(outputData);
 }
 public bool EvalFlowCondition(TaskInstance ti, ProcessModel.FlowDef fd, ITaskExecutionContext ctx)
 {
     _pd.SetTaskInstanceInfo(ti, ctx);
     string k = DslUtil.FlowConditionKey(fd.Parent.Id, fd.From, fd.To);
     if (!_pd._conds.ContainsKey(k)) throw new Exception("!no flow cond..");
     _pd.SetTaskInstanceInfo(ti, ctx);
     return _pd._conds[k]();
 }
 public void ExecuteTaskScriptBlock(TaskInstance ti, string blockName, ITaskExecutionContext ctx)
 {
     throw new NotImplementedException();
 }
 public void SetTaskScriptContext(TaskInstance task, Dictionary<string, object> inputData, Dictionary<string, object> outputData)
 {
     throw new NotImplementedException();
 }