/// <summary>
        ///     Invoke global device resolution rules.
        /// </summary>
        /// <returns>True if a global device resolution rule applied.</returns>
        protected bool RunModuleRules()
        {
            Item moduleRuleItem = Context.Database.GetItem("{143624D2-7C7F-460A-B97E-068283D646B9}");
            if (moduleRuleItem == null)
                return false;

            string ruleXml = moduleRuleItem["Rule"];

            if (String.IsNullOrEmpty(ruleXml) || moduleRuleItem["Disable"] == "1")
                return false;

            // parse the rule XML
            RuleList<RuleContext> rules = new RuleList<RuleContext> {Name = moduleRuleItem.Paths.Path};
            RuleList<RuleContext> parsed = RuleFactory.ParseRules<RuleContext>(
                Context.Database,
                ruleXml);
            rules.AddRange(parsed.Rules);

            if (rules.Count < 1)
                return false;

            // invoke the rule
            RuleContext ruleContext = new RuleContext {Item = Context.Item};
            rules.Run(ruleContext);

            // rule applied
            return ruleContext.IsAborted;
        }
        public bool RunRule(Item outcomeItem)
        {
            if (outcomeItem == null)
                return false;

            string ruleXml = outcomeItem[RulesField];

            if (String.IsNullOrEmpty(ruleXml))
                return false;

            RuleList<RuleContext> rules = new RuleList<RuleContext> { Name = outcomeItem.Paths.Path };
            RuleList<RuleContext> parsed = RuleFactory.ParseRules<RuleContext>(
                Context.Database,
                ruleXml);
            rules.AddRange(parsed.Rules);

            if (rules.Count < 1)
                return false;

            RuleContext ruleContext = new RuleContext { Item = Context.Item };
            ruleContext.Parameters.Add("DefinitionItem",outcomeItem);
            
            rules.Run(ruleContext);
            return ruleContext.IsAborted;
        }  
        /// <summary>
        ///   Retrieves the rules.
        /// </summary>
        /// <param name="action"> The action. </param>
        /// <returns> </returns>
        public static RuleList RetrieveRules(Action action)
        {
            RuleList rules = new RuleList();
            try
            {
                if(action == null)
                {
                    throw new ArgumentNullException("action");
                }

                RuleBuilderFactory actionRuleFactory = new ActionRuleFactory();
                RuleBuilderFactory fieldRuleFactory = new FieldRuleFactory();
                RuleBuilderFactory propertyRuleFactory = new PropertyRuleFactory();

                RuleBuilderDirector director = new RuleBuilderDirector();
                director.Builders.Add(new ActionObjectRuleBuilder(new ActionRuleSource(actionRuleFactory, action)));
                director.Builders.Add(new ActionPropertyRuleBuilder(new PropertyRuleSource(propertyRuleFactory, action)));
                director.Builders.Add(new ActionFieldRuleBuilder(new FieldRuleSource(fieldRuleFactory, action)));

                foreach(RuleBuilderBase builder in director.Builders)
                {
                    director.DefineBuilder(builder);
                    director.CreateRules();
                    rules.AddRange(director.RetrieveRules());
                }
            }
            catch(Exception)
            {
                throw;
            }
            return rules;
        }
        /// <summary>
        /// Returns rules of a landing workflow action.
        /// </summary>
        /// <param name="processorItem">
        /// The processor item.
        /// </param>
        /// <returns>
        /// Returns RuleList of landing workflow.
        /// </returns>
        protected RuleList<WorkflowRuleContext> BuildRuleList(ProcessorItem processorItem)
        {
            Assert.ArgumentNotNull(processorItem, "processorItem");
             Item actionItem = processorItem.InnerItem;
             RuleList<WorkflowRuleContext> ruleList = new RuleList<WorkflowRuleContext>();
             if (!string.IsNullOrEmpty(actionItem["Rule"]))
             {
            ruleList.AddRange(RuleFactory.GetRules<WorkflowRuleContext>(actionItem.Fields["Rule"]).Rules);
             }

             if (!string.IsNullOrEmpty(actionItem["Rules"]))
             {
            Item[] rules = ((MultilistField)actionItem.Fields["Rules"]).GetItems();
            ruleList.AddRange(RuleFactory.GetRules<WorkflowRuleContext>(rules, "Rule").Rules);
             }

             return ruleList;
        }
        /// <summary>
        /// Returns rules of a landing workflow action.
        /// </summary>
        /// <param name="processorItem">
        /// The processor item.
        /// </param>
        /// <returns>
        /// Returns RuleList of landing workflow.
        /// </returns>
        protected RuleList <WorkflowRuleContext> BuildRuleList(ProcessorItem processorItem)
        {
            Assert.ArgumentNotNull(processorItem, "processorItem");
            Item actionItem = processorItem.InnerItem;
            RuleList <WorkflowRuleContext> ruleList = new RuleList <WorkflowRuleContext>();

            if (!string.IsNullOrEmpty(actionItem["Rule"]))
            {
                ruleList.AddRange(RuleFactory.GetRules <WorkflowRuleContext>(actionItem.Fields["Rule"]).Rules);
            }

            if (!string.IsNullOrEmpty(actionItem["Rules"]))
            {
                Item[] rules = ((MultilistField)actionItem.Fields["Rules"]).GetItems();
                ruleList.AddRange(RuleFactory.GetRules <WorkflowRuleContext>(rules, "Rule").Rules);
            }

            return(ruleList);
        }
        /// <summary>
        /// Invoke global device resolution rules.
        /// </summary>
        /// <returns>True if a global device resolution rule applied.</returns>
        protected bool InvokeGlobalRules()
        {
            Sitecore.Data.Items.Item ruleRoot = Sitecore.Context.Database.GetItem(
                "/sitecore/system/Settings/Rules/Determine Context Device/Rules");

            if (ruleRoot == null)
            {
                return(false);
            }

            // TODO: use descendants instead of children
            // for each possible global rule definition item
            foreach (Sitecore.Data.Items.Item child in ruleRoot.Children)
            {
                string ruleXml = child["Rule"];

                if (String.IsNullOrEmpty(ruleXml) || child["Disabled"] == "1")
                {
                    continue;
                }

                // parse the rule XML
                RuleList <RuleContext> rules = new RuleList <RuleContext>();
                rules.Name = child.Paths.Path;
                RuleList <RuleContext> parsed = RuleFactory.ParseRules <RuleContext>(
                    Sitecore.Context.Database,
                    ruleXml);
                rules.AddRange(parsed.Rules);

                if (rules == null || rules.Count < 1)
                {
                    continue;
                }

                // invoke the rule
                RuleContext ruleContext = new RuleContext();
                ruleContext.Item = Sitecore.Context.Item;
                rules.Run(ruleContext);

                // rule applied
                if (ruleContext.IsAborted)
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #7
0
        /// <summary>
        /// Overrides the base class implementation and adds execution of rules to determine allowed renderings.
        /// </summary>
        protected override List <Sitecore.Data.Items.Item> GetRenderings(Sitecore.Data.Items.Item placeholderItem, out bool allowedControlsSpecified)
        {
            // Get the initial list of renderings from the base implementation.
            var list = base.GetRenderings(placeholderItem, out allowedControlsSpecified);

            // Get the rules from the placeholder item.
            string rulesXml = placeholderItem[Constants.RulesFieldId];

            if (string.IsNullOrWhiteSpace(rulesXml))
            {
                return(list);
            }

            // Parse the rules.
            var parsedRules = RuleFactory.ParseRules <PlaceholderSettingsRuleContext>(placeholderItem.Database, rulesXml);

            // Construct the context.
            PlaceholderSettingsRuleContext context = new PlaceholderSettingsRuleContext();

            context.Item = placeholderItem;
            context.AllowedRenderingItems = list;
            context.DeviceId         = DeviceId;
            context.PlaceholderKey   = PlaceholderKey;
            context.ContentDatabase  = ContentDatabase;
            context.LayoutDefinition = LayoutDefinition;

            // Execute the rules.
            RuleList <PlaceholderSettingsRuleContext> rules = new RuleList <PlaceholderSettingsRuleContext>();

            rules.Name = placeholderItem.Paths.Path;
            rules.AddRange(parsedRules.Rules);
            rules.Run(context);

            // Did the rules specify if the selection tree should be displayed?
            if (context.DisplaySelectionTree.HasValue)
            {
                allowedControlsSpecified = !context.DisplaySelectionTree.Value;
            }
            // If not, only display the tree if there are no allowed renderings.
            else
            {
                allowedControlsSpecified = context.AllowedRenderingItems.Count > 0;
            }

            // Return the list.
            return(context.AllowedRenderingItems);
        }
Beispiel #8
0
        public void Deserialize(IXunitSerializationInfo info)
        {
            String val = info.GetValue <String>("Value");
            UserHabitRecordsControllerTestData_DailyNoOfCount other = JsonSerializer.Deserialize <UserHabitRecordsControllerTestData_DailyNoOfCount>(val);

            CompleteCondition = other.CompleteCondition;
            if (other.RuleList.Count > 0)
            {
                RuleList.AddRange(other.RuleList);
            }
            if (other.RecordList.Count > 0)
            {
                RecordList.AddRange(other.RecordList);
            }
            if (other.ExpectedRecordList.Count > 0)
            {
                ExpectedRecordList.AddRange(other.ExpectedRecordList);
            }
        }
        protected bool ApplyDeviceRules()
        {
            // for each device
            foreach (DeviceItem device in
                     Sitecore.Context.Database.Resources.Devices.GetAll())
            {
                // for each field of the device
                foreach (Field field in device.InnerItem.Fields)
                {
                    // ignore empty fields and fields of type other than rules.
                    if (field.TypeKey != "rules" || String.IsNullOrEmpty(field.Value))
                    {
                        continue;
                    }

                    // parse the rule definition XML in the field.
                    RuleList <SetContextDeviceRuleContext> rules =
                        new RuleList <SetContextDeviceRuleContext>();
                    rules.Name = field.Item.Paths.Path;
                    RuleList <SetContextDeviceRuleContext> parsed =
                        RuleFactory.ParseRules <SetContextDeviceRuleContext>(field.Database, field.Value);
                    rules.AddRange(parsed.Rules);

                    if (rules == null || rules.Count < 1)
                    {
                        continue;
                    }

                    // invoke the rule.
                    SetContextDeviceRuleContext ruleContext = new SetContextDeviceRuleContext(
                        device);
                    rules.Run(ruleContext);

                    // rule applied.
                    if (ruleContext.IsAborted)
                    {
                        return(true);
                    }
                }
            }

            return(false);
        }
 public UserHabitRecordsControllerTestData_MonthNoOfTimes(int dateInMonth,
                                                          List <DateTime> listRecordDates, int completeCondition,
                                                          List <UserHabitRule> ruleList,
                                                          List <UserHabitRecord> listExpectedRecords) : this()
 {
     DateInMonth       = dateInMonth;
     CompleteCondition = completeCondition;
     if (listRecordDates.Count > 0)
     {
         this.RecordDateList.AddRange(listRecordDates);
     }
     if (ruleList.Count > 0)
     {
         RuleList.AddRange(ruleList);
     }
     if (listExpectedRecords.Count > 0)
     {
         this.ExpectedRecordList.AddRange(listExpectedRecords);
     }
 }
Beispiel #11
0
 public UserHabitRecordsControllerTestData_DailyNoOfCount(
     int completeCondition,
     List <UserHabitRecord> listRecords,
     List <UserHabitRule> listRules,
     List <UserHabitRecord> listExpectedRecords) : this()
 {
     CompleteCondition = completeCondition;
     if (listRules.Count > 0)
     {
         RuleList.AddRange(listRules);
     }
     if (listRecords.Count > 0)
     {
         RecordList.AddRange(listRecords);
     }
     if (listExpectedRecords.Count > 0)
     {
         ExpectedRecordList.AddRange(listExpectedRecords);
     }
 }
Beispiel #12
0
        public bool RunRule(Item outcomeItem)
        {
            if (outcomeItem == null)
            {
                return(false);
            }

            string ruleXml = outcomeItem[RulesField];

            if (String.IsNullOrEmpty(ruleXml))
            {
                return(false);
            }

            RuleList <RuleContext> rules = new RuleList <RuleContext> {
                Name = outcomeItem.Paths.Path
            };
            RuleList <RuleContext> parsed = RuleFactory.ParseRules <RuleContext>(
                Context.Database,
                ruleXml);

            rules.AddRange(parsed.Rules);

            if (rules.Count < 1)
            {
                return(false);
            }

            RuleContext ruleContext = new RuleContext {
                Item = Context.Item
            };

            ruleContext.Parameters.Add("DefinitionItem", outcomeItem);

            rules.Run(ruleContext);
            return(ruleContext.IsAborted);
        }
        /// <summary>
        /// Overrides the base class implementation and adds execution of rules to determine allowed renderings.
        /// </summary>
        protected override List<Sitecore.Data.Items.Item> GetRenderings(Sitecore.Data.Items.Item placeholderItem, out bool allowedControlsSpecified)
        {
            // Get the initial list of renderings from the base implementation.
            var list = base.GetRenderings(placeholderItem, out allowedControlsSpecified);

            // Get the rules from the placeholder item.
            string rulesXml = placeholderItem[Constants.RulesFieldId];
            if (string.IsNullOrWhiteSpace(rulesXml)) return list;

            // Parse the rules.
            var parsedRules = RuleFactory.ParseRules<PlaceholderSettingsRuleContext>(placeholderItem.Database, rulesXml);

            // Construct the context.
            PlaceholderSettingsRuleContext context = new PlaceholderSettingsRuleContext();
            context.Item = placeholderItem;
            context.AllowedRenderingItems = list;
            context.DeviceId = DeviceId;
            context.PlaceholderKey = PlaceholderKey;
            context.ContentDatabase = ContentDatabase;
            context.LayoutDefinition = LayoutDefinition;

            // Execute the rules.
            RuleList<PlaceholderSettingsRuleContext> rules = new RuleList<PlaceholderSettingsRuleContext>();
            rules.Name = placeholderItem.Paths.Path;
            rules.AddRange(parsedRules.Rules);
            rules.Run(context);

            // Did the rules specify if the selection tree should be displayed?
            if (context.DisplaySelectionTree.HasValue)
                allowedControlsSpecified = !context.DisplaySelectionTree.Value;
            // If not, only display the tree if there are no allowed renderings.
            else
                allowedControlsSpecified = context.AllowedRenderingItems.Count > 0;

            // Return the list.
            return context.AllowedRenderingItems;
        }
 public UserHabitRecordsControllerTestData_MonthNoOfCount(
     int dateInMonth,
     List <UserHabitRecord> listRecords,
     List <UserHabitRule> ruleList,
     int completeCondition,
     int recordCount,
     List <DateTime> expectedRuleDate) : this()
 {
     DateInMonth       = dateInMonth;
     CompleteCondition = completeCondition;
     if (ruleList.Count > 0)
     {
         RuleList.AddRange(ruleList);
     }
     if (listRecords.Count > 0)
     {
         RecordList.AddRange(listRecords);
     }
     RecordCount = recordCount;
     if (expectedRuleDate.Count > 0)
     {
         ExpectedRuleDateList.AddRange(expectedRuleDate);
     }
 }