public void TestCreateProcessDefinitionWithScriptAndRetrieve() { ProcessDefinition pd = BuildProcessdefinition("id.123", "test_definition", "description"); Md5CalcVisitor visitor = new Md5CalcVisitor(); pd.Accept(visitor); string md5 = visitor.CalculateMd5(); IProcessDefinitionPersisnenceService service = InstService(); service.Create(pd, ProcessDefStatusEnum.Active, 1); IReadOnlyList <ProcessDefinitionDigest> flows = service.LisAlltWorkflows(); Assert.IsNotNull(flows); Assert.AreEqual(1, flows.Count); ProcessDefStatusEnum stat; ProcessDefinition savedPd; AccountData[] accounts; Assert.IsTrue(service.TryFind(flows[0].Id, flows[0].Version, out savedPd, out stat, out accounts)); Assert.IsNotNull(savedPd); visitor.Reset(); savedPd.Accept(visitor); string savedMd5 = visitor.CalculateMd5(); Assert.AreEqual(md5, savedMd5); }
/// <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> /// Ceate process definition /// </summary> /// <param name="definition"></param> /// <param name="status"></param> /// <param name="version"></param> /// <param name="accounts"></param> /// <exception cref="NotImplementedException"></exception> public void Create(ProcessDefinition definition, ProcessDefStatusEnum status, int version, params AccountData[] accounts) { Md5CalcVisitor visitor = new Md5CalcVisitor(); definition.Accept(visitor); string md5 = visitor.CalculateMd5(); if (_collection.Find(f => f.FlowId == definition.FlowId && string.Equals(f.Md5, md5)).Count() != 0) { throw new ArgumentException($"Persistend Workflow definition FlowId={definition.FlowId} Md5={md5} already exists."); } List <AccountData> accountsList = new EditableList <AccountData>(); if (accounts != null) { accountsList.AddRange(accounts); } 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 = md5, JsonProcessDefinition = ToBase64(JsonConvert.SerializeObject(definition)), Accounts = accountsList }; _collection.InsertOne(pd); }
/// <summary> /// Create process runtime definition /// </summary> /// <param name="pd"></param> /// <param name="collection"></param> /// <returns></returns> /// <exception cref="ArgumentException"></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(); // save the collection var processDefinitions = _processDefinitionService.ActivetWorkflows(); var processDefinitionDigest = processDefinitions.SingleOrDefault(d => d.FlowId == pd.FlowId && d.Md5 == md5 && d.Status == ProcessDefStatusEnum.Active); if (processDefinitionDigest == null) { throw new ArgumentException($"Error find Process Definition Id={pd.FlowId} Md5={md5}"); } var rtp = CreateMongoProcessRuntimePersistence(runtime, collection, null, null, processDefinitionDigest); _collection.InsertOne(rtp); return(new ProcessRuntimePersistenProxy(runtime, this)); }