/// <summary>
        /// Saves scheme to a store
        /// </summary>
        /// <param name="scheme">Not parsed scheme of the process</param>
        /// <exception cref="SchemeAlredyExistsException"></exception>
        public void SaveScheme(SchemeDefinition <XElement> scheme)
        {
            var db = _connector.GetDatabase();


            var tran = db.CreateTransaction();

            if (!scheme.IsObsolete) //there is only one current scheme can exists
            {
                if (!scheme.RootSchemeId.HasValue)
                {
                    var key  = GetKeyForCurrentScheme(scheme.SchemeCode);
                    var hash = scheme.DefiningParameters;
                    tran.AddCondition(Condition.HashNotExists(key, hash));
                    tran.HashSetAsync(key, hash, string.Format("{0:N}", scheme.Id));
                }
            }

            var newProcessScheme = new WorkflowProcessScheme
            {
                DefiningParameters = scheme.DefiningParameters,
                Scheme             = scheme.Scheme.ToString(),
                SchemeCode         = scheme.SchemeCode,
                RootSchemeCode     = scheme.RootSchemeCode,
                RootSchemeId       = scheme.RootSchemeId,
                AllowedActivities  = scheme.AllowedActivities,
                StartingTransition = scheme.StartingTransition
            };

            var newSchemeValue = JsonConvert.SerializeObject(newProcessScheme);
            var newSchemeKey   = GetKeyForProcessScheme(scheme.Id);



            tran.AddCondition(Condition.KeyNotExists(newSchemeKey));

            tran.StringSetAsync(newSchemeKey, newSchemeValue);

            if (scheme.RootSchemeId.HasValue)
            {
                tran.HashSetAsync(GetKeySchemeHierarchy(scheme.RootSchemeId.Value), scheme.SchemeCode, scheme.Id.ToString("N"));
            }

            var result = tran.Execute();

            if (!result)
            {
                throw SchemeAlredyExistsException.Create(scheme.SchemeCode, SchemeLocation.WorkflowProcessScheme, scheme.DefiningParameters);
            }
        }
 private SchemeDefinition <XElement> ConvertToSchemeDefinition(Guid schemeId, bool isObsolete, WorkflowProcessScheme workflowProcessScheme)
 {
     return(new SchemeDefinition <XElement>(schemeId, workflowProcessScheme.RootSchemeId,
                                            workflowProcessScheme.SchemeCode, workflowProcessScheme.RootSchemeCode,
                                            XElement.Parse(workflowProcessScheme.Scheme), isObsolete, false,
                                            workflowProcessScheme.AllowedActivities, workflowProcessScheme.StartingTransition,
                                            workflowProcessScheme.DefiningParameters));
 }