/// <summary> /// Create and persist runtime processor /// </summary> /// <param name="pd"></param> /// <param name="collection"></param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public override IProcessRuntime Create(ProcessDefinition pd, IPropertySetCollection collection) { IProcessRuntime runtime = base.Create(pd, collection); Md5CalcVisitor visitor = new Md5CalcVisitor(); pd.Accept(visitor); string md5 = visitor.CalculateMd5(); using (var ctx = new ProcessDbContext()) { var pdList = ctx.ProcessDefinition .Where(p => p.FlowId == pd.FlowId && p.Md5 == md5).ToList(); if (pdList.Count() != 1) { throw new ArgumentException($"Process definition is not persisted."); } // save or update the collection PersistentPropertyCollection persistedCollection = _propertySepPersistenceService.SaveCollection(ctx, runtime.Id, collection); ProcessRuntimePersistence rtp = new ProcessRuntimePersistence { Id = runtime.Id, SuspendedStepId = runtime.LastExecutedStep?.StepId, Status = (int)runtime.State, LastUpdated = DateTime.UtcNow, PropertyCollection = persistedCollection, ProcessDefinition = pdList.ElementAt(0) }; ctx.Process.Add(rtp); ctx.SaveChanges(); } return(new ProcessRuntimePersistenProxy(runtime, this)); }
/// <summary> /// Save process definition /// </summary> /// <param name="definition"></param> /// <param name="status"></param> /// <param name="version"></param> /// <param name="accounts">Security accounts that have access to the flow</param> public void Create(ProcessDefinition definition, ProcessDefStatusEnum status, int version, params AccountData[] accounts) { Md5CalcVisitor visitor = new Md5CalcVisitor(); definition.Accept(visitor); var accountsList = new List <ProcessDefinitionAccount>(); ProcessDefinitionPersistence pd = new ProcessDefinitionPersistence { Id = definition.Id, FlowId = definition.FlowId, Version = version, Name = definition.Name, Description = definition.Description, LastModified = DateTime.UtcNow, Status = (int)status, Md5 = visitor.CalculateMd5(), JsonProcessDefinition = JsonConvert.SerializeObject(definition), Accounts = accountsList }; try { using (var ctx = new ProcessDbContext()) { SetupAccounts(ctx, ctx.ProcessDefinition.Add(pd), accounts); ctx.SaveChanges(); } } catch (DbUpdateException ex) { throw new ArgumentException(ex.Message); } }
/// <summary> /// Unfreeze the process /// </summary> /// <param name="processRuntimeId"></param> /// <param name="runtime"></param> /// <param name="nextStep"></param> /// <param name="collection"></param> /// <returns></returns> public override bool TryUnfreeze(Guid processRuntimeId, out IProcessRuntime runtime, out StepRuntime nextStep, out IPropertySetCollection collection) { runtime = null; collection = null; nextStep = null; using (var ctx = new ProcessDbContext()) { var rtp = ctx.Process.Find(processRuntimeId); if (rtp == null) { return(false); } var definition = JsonConvert.DeserializeObject <ProcessDefinition>(rtp.ProcessDefinition.JsonProcessDefinition); definition.Id = rtp.ProcessDefinition.Id; collection = rtp.PropertyCollection.Deserialize(); if (!string.IsNullOrEmpty(rtp.NextStepId)) { StepDefinition stepDef = definition.Steps.Single(s => s.StepId == rtp.NextStepId); StepDefinitionId sid = new StepDefinitionId(stepDef.Id, stepDef.StepId); LinkDefinition[] links = definition.Links.Where(l => l.Source == sid).ToArray(); nextStep = new StepRuntime(stepDef, links.Select(l => new LinkRuntime(l)).ToArray()); } runtime = Create(rtp.Id, definition, rtp.SuspendedStepId, (ProcessStateEnum)rtp.Status); } return(true); }
/// <summary> /// Load and deserialize the process definition /// </summary> /// <param name="id"></param> /// <param name="version"></param> /// <param name="definition"></param> /// <param name="status"></param> /// <param name="accounts"></param> /// <returns></returns> public bool TryFind(Guid id, int version, out ProcessDefinition definition, out ProcessDefStatusEnum status, out AccountData[] accounts) { using (var ctx = new ProcessDbContext()) { ProcessDefinitionPersistence pd = ctx.ProcessDefinition.Find(id, version); if (pd == null) { definition = null; accounts = new AccountData[] {}; status = ProcessDefStatusEnum.NotActive; return(false); } definition = JsonConvert.DeserializeObject <ProcessDefinition>(pd.JsonProcessDefinition); status = (ProcessDefStatusEnum)pd.Status; accounts = pd.Accounts?.Select(a => new AccountData { Id = a.AccountDataId, Name = a.Account.Name, SourceSystem = a.Account.SourceSystem }).ToArray(); return(true); } }
/// <summary> /// Create Security Account Records /// </summary> /// <param name="accounts"></param> public void CreateAccounts(params AccountData[] accounts) { using (var ctx = new ProcessDbContext()) { accounts.ToList().ForEach(a => ctx.Accounts.Add(a)); ctx.SaveChanges(); } }
/// <summary> /// Save or Update Collection /// </summary> /// <param name="ctx"></param> /// <param name="collectionId"></param> /// <param name="collection"></param> public PersistentPropertyCollection SaveCollection(ProcessDbContext ctx, Guid collectionId, IPropertySetCollection collection) { List <PersistentSchemaElement> schemaElements; List <PersistentPropertyElement> dataElements; collection.CreatePersistentSchemaElements(out schemaElements, out dataElements); return(UpdateOrCreatePropertyCollection(collectionId, ctx, schemaElements, dataElements)); }
/// <summary> /// Save Runtime Process /// </summary> /// <param name="process"></param> public void SaveRuntimeProcess(ProcessRuntime process) { using (var ctx = new ProcessDbContext()) { var persistedProcess = ctx.Process.Find(process.Id); if (persistedProcess == null) { var processDefinitionPersistence = ctx.ProcessDefinition.SingleOrDefault(d => d.Id == Guid.NewGuid() && d.Md5 == "123"); } } }
/// <summary> /// Find and re-load the collection /// </summary> /// <param name="collectionId"></param> /// <returns></returns> /// <exception cref="NotImplementedException"></exception> public IPropertySetCollection FindCollection(Guid collectionId) { using (ProcessDbContext ctx = new ProcessDbContext()) { var persistancePropertyCollection = ctx.PropertySet.Find(collectionId); if (persistancePropertyCollection == null) { throw new ArgumentException($"Cannot find the property collection id={collectionId}"); } return(persistancePropertyCollection.Deserialize()); } }
/// <summary> /// Store the collection /// </summary> /// <param name="collectionId"></param> /// <param name="collection"></param> public void SaveCollection(Guid collectionId, IPropertySetCollection collection) { List <PersistentSchemaElement> schemaElements; List <PersistentPropertyElement> dataElements; collection.CreatePersistentSchemaElements(out schemaElements, out dataElements); using (ProcessDbContext ctx = new ProcessDbContext()) { UpdateOrCreatePropertyCollection(collectionId, ctx, schemaElements, dataElements); ctx.SaveChanges(); } }
/// <summary> /// Set or update status /// </summary> /// <param name="id"></param> /// <param name="version"></param> /// <param name="status"></param> public bool SetStatus(Guid id, int version, ProcessDefStatusEnum status) { using (var ctx = new ProcessDbContext()) { ProcessDefinitionPersistence pd = ctx.ProcessDefinition.Find(id, version); if (pd == null) { return(false); } pd.Status = (int)status; pd.LastModified = DateTime.UtcNow; ctx.SaveChanges(); return(true); } }
public void RemoveRoles(Guid id, int version, params AccountData[] accounts) { using (var ctx = new ProcessDbContext()) { ProcessDefinitionPersistence pd = ctx.ProcessDefinition.Find(id, version); if (pd == null) { throw new ArgumentException($"Process Definition id={id} version={version} not found."); } if (accounts != null) { SetupAccounts(ctx, pd, accounts); } ctx.SaveChanges(); } }
public override void Freeze(IProcessRuntime runtime, IPropertySetCollection collection) { using (var ctx = new ProcessDbContext()) { ProcessRuntimePersistence persistence = ctx.Process.Find(runtime.Id); if (persistence == null) { throw new ArgumentException("Invalid Workflow Id:" + runtime.Id.ToString()); } _propertySepPersistenceService.SaveCollection(ctx, runtime.Id, collection); persistence.Status = (int)runtime.State; persistence.LastUpdated = DateTime.UtcNow; persistence.SuspendedStepId = runtime.SuspendedInStep?.StepId; ctx.SaveChanges(); } }
protected virtual void OnExecute(IProcessRuntime runtime, Tuple <ExecutionResult, StepRuntime> result, IProcessRuntimeEnvironment env) { using (var ctx = new ProcessDbContext()) { ProcessRuntimePersistence persistence = ctx.Process.Find(runtime.Id); if (persistence == null) { throw new ArgumentException("Invalid Workflow Id:" + runtime.Id); } _propertySepPersistenceService.SaveCollection(ctx, runtime.Id, env.PropertySet); persistence.Status = (int)runtime.State; persistence.LastUpdated = DateTime.UtcNow; persistence.SuspendedStepId = runtime.SuspendedInStep?.StepId; persistence.NextStepId = runtime.State == ProcessStateEnum.Completed? null: result.Item2?.StepId; ctx.SaveChanges(); } }
private void SetupAccounts(ProcessDbContext ctx, ProcessDefinitionPersistence persistence, AccountData[] accounts) { accounts?.ToList().ForEach(a => { var acc = ctx.Accounts.Find(a.Id) ?? ctx.Accounts.Add(a); ProcessDefinitionAccount pdAccount = new ProcessDefinitionAccount { Account = acc, ProcessDefinition = persistence, AccountDataId = acc.Id, ProcessDefinitionId = persistence.Id, ProcessDefinitionVersion = persistence.Version }; var pd = ctx.ProcessDefinitionAccounts.Add(pdAccount); persistence.Accounts.Add(pd); }); }
/// <summary> /// List all Available workflow /// </summary> /// <returns></returns> public IReadOnlyList <ProcessDefinitionDigest> LisAlltWorkflows(params string[] accounts) { using (var ctx = new ProcessDbContext()) { return(ctx.ProcessDefinition.Select( r => new ProcessDefinitionDigest { Id = r.Id, FlowId = r.FlowId, Name = r.Name, Description = r.Description, Md5 = r.Md5, LastUpdated = r.LastModified, Status = (ProcessDefStatusEnum)r.Status, Version = r.Version }) .OrderBy(r => r.Name).ToList()); } }
/// <summary> /// Find the definition /// </summary> /// <param name="ctx"></param> /// <param name="comparer"></param> /// <returns></returns> public IEnumerable <ProcessDefinitionPersistence> Find(ProcessDbContext ctx, Func <ProcessDefinitionPersistence, bool> comparer) { return(ctx.ProcessDefinition.Where(p => comparer(p)).ToList()); }
protected virtual PersistentPropertyCollection UpdateOrCreatePropertyCollection(Guid collectionId, ProcessDbContext ctx, List <PersistentSchemaElement> schemaElements, List <PersistentPropertyElement> dataElements) { PersistentPropertyCollection persistancePropertyCollection = ctx.PropertySet.Find(collectionId); if (persistancePropertyCollection == null) { ctx.PropertySet.Add(persistancePropertyCollection = new PersistentPropertyCollection { Id = collectionId, Schemas = schemaElements, Elements = dataElements }); } else { schemaElements.ForEach( s => persistancePropertyCollection.Schemas.FirstOrDefault(c => c.SchemaName == s.SchemaName)? .CopyFrom(s)); dataElements.ForEach( e => persistancePropertyCollection.Elements.FirstOrDefault(c => c.Name == e.Name)?.CopyFrom(e)); } return(persistancePropertyCollection); }