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);
        }
Exemple #2
0
        /// <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));
        }
Exemple #3
0
        /// <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);
            }
        }
Exemple #4
0
        protected virtual void OnTestLoadSimpleRuntimeProcess()
        {
            IProcessDefinitionPersisnenceService service = GetProcessDefinitionPersistenceService();
            var processDefinition = BuildProcessdefinition();

            service.Create(processDefinition, ProcessDefStatusEnum.Active, 1);
            Md5CalcVisitor visitor = new Md5CalcVisitor();

            processDefinition.Accept(visitor);
            string md5 = visitor.CalculateMd5();
            IProcessRuntimeService pservice   = GetProcessRuntime();
            PropertySetCollection  collection = new PropertySetCollection(new PropertySchemaSet(new PropertySchemaFactory()));
            IProcessRuntime        runtime    = pservice.Create(processDefinition, collection);

            IProcessRuntime        ufRuntime;
            StepRuntime            nextStep;
            IPropertySetCollection ufCollection;
            bool val = pservice.TryUnfreeze(runtime.Id, out ufRuntime, out nextStep, out ufCollection);

            Assert.IsTrue(val);
            Assert.IsNotNull(ufRuntime);
            Assert.IsNotNull(ufCollection);
            Assert.AreEqual(ProcessStateEnum.NotStarted, ufRuntime.State);
            Assert.AreEqual(runtime.Id, ufRuntime.Id);
            string policyNumber = collection.Get <string>("PolicyNumber");

            Assert.IsTrue(string.IsNullOrEmpty(policyNumber));
        }
        /// <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);
        }
Exemple #6
0
        protected virtual void OnTryFindFlow(IProcessDefinitionPersisnenceService service)
        {
            var processDefinition = BuildProcessdefinition();

            Assert.IsNotNull(processDefinition);
            service.Create(processDefinition, ProcessDefStatusEnum.Active, 1);
            ProcessDefinition    definition;
            ProcessDefStatusEnum stat;

            AccountData[] pdAccounts;
            Assert.IsTrue(service.TryFind(processDefinition.Id, 1, out definition, out stat, out pdAccounts));
            Assert.IsNotNull(definition);
            Md5CalcVisitor visitor = new Md5CalcVisitor();

            processDefinition.Accept(visitor);
            string md5 = visitor.CalculateMd5();

            visitor.Reset();
            definition.Accept(visitor);
            string restoredMd5 = visitor.CalculateMd5();

            Assert.AreEqual(md5, restoredMd5);
        }
Exemple #7
0
        protected virtual IProcessRuntime OnTestCreateSimplePersistentRuntime()
        {
            IProcessDefinitionPersisnenceService service = GetProcessDefinitionPersistenceService();
            var processDefinition = BuildProcessdefinition();

            service.Create(processDefinition, ProcessDefStatusEnum.Active, 1);
            Md5CalcVisitor visitor = new Md5CalcVisitor();

            processDefinition.Accept(visitor);
            string md5 = visitor.CalculateMd5();
            IProcessRuntimeService pservice   = GetProcessRuntime();
            PropertySetCollection  collection = new PropertySetCollection(new PropertySchemaSet(new PropertySchemaFactory()));
            IProcessRuntime        runtime    = pservice.Create(processDefinition, collection);

            Assert.IsNotNull(runtime);
            return(runtime);
        }
        public void DeployWorkflow(Guid guid)
        {
            List <ProcessDefinition> defs = new List <ProcessDefinition>();

            _registries.ForEach(r =>
            {
                ProcessDefinition pd;
                if (r.TryGet(guid, out pd))
                {
                    defs.Add(pd);
                }
            });
            if (defs.Count == 0)
            {
                throw new ArgumentException($"No Process Definition with ID={guid} was found.");
            }
            if (defs.Count > 1)
            {
                throw new ArgumentException($"{defs.Count} Process Definitions with ID={guid} was found.");
            }
            Md5CalcVisitor visitor = new Md5CalcVisitor();

            defs[0].Accept(visitor);
            string md5 = visitor.CalculateMd5();
            IReadOnlyList <ProcessDefinitionDigest> processes = _persistence.LisAlltWorkflows();
            var processDefinitionDigests = processes.Where(p => p.Id == guid).ToArray();
            int nextVersion = processDefinitionDigests.Length > 0?processDefinitionDigests.Max(p => p.Version) + 1:1;

            foreach (ProcessDefinitionDigest p in processDefinitionDigests)
            {
                if (p.Md5 == md5)
                {
                    throw new ArgumentException($"Process ID={defs[0].Id} MD5={md5} already deployed. Cannot deploy the same process with the same MD5.");
                }
            }
            foreach (ProcessDefinitionDigest p in processes)
            {
                _persistence.SetStatus(p.Id, p.Version, ProcessDefStatusEnum.NotActive);
            }
            _persistence.Create(defs[0], ProcessDefStatusEnum.Active, nextVersion);
        }
        /// <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));
        }