/// <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> /// 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> /// 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(); } }
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); }); }