Beispiel #1
0
        /// <summary>
        /// Returns total number of times current promotion has been used by current customer. The results are cached.
        /// </summary>
        /// <value>The customer used count.</value>
        public int GetCustomerUsageCount(Guid customerId)
        {
            DataTable usage = null;

            // Load customer specific
            string cacheKey     = MarketingCache.CreateCacheKey("MarketingHelper-customer", customerId.ToString());
            object cachedObject = MarketingCache.Get(cacheKey);

            if (cachedObject != null)
            {
                usage = (DataTable)cachedObject;
            }
            else
            {
                usage = PromotionManager.GetPromotionUsageStatistics(customerId);
                MarketingCache.Insert(cacheKey, usage, MarketingConfiguration.Instance.CacheConfig.PromotionCollectionTimeout);
            }

            System.Data.DataRow[] rows = usage.Select(String.Format("PromotionId = {0}", DataRow.PromotionId));
            if (rows != null && rows.Length > 0)
            {
                return((int)rows[0]["TotalUsed"]);
            }

            return(0);
        }
Beispiel #2
0
        /// <summary>
        /// Evals the specified expression against the context passed in the dictionary.
        /// </summary>
        /// <param name="key">The key. Must be a unique key identifying the current expression. It might be used for caching purpose by the engine.</param>
        /// <param name="expr">The expr.</param>
        /// <param name="context">The context, which consists of object that will be accessible during expression evaluation.</param>
        /// <returns></returns>
        public ValidationResult Eval(string key, string expr, IDictionary <string, object> context)
        {
            string cacheKey = MarketingCache.CreateCacheKey("Expression-engine", key);

            // check cache first
            object cachedObject = MarketingCache.Get(cacheKey);

            RuleEngine ruleEngine = null;

            // Return true if empty
            if (String.IsNullOrEmpty(expr))
            {
                return(new ValidationResult(true, String.Empty));
            }

            if (cachedObject != null)
            {
                ruleEngine = (RuleEngine)cachedObject;
            }
            else
            {
                lock (MarketingCache.GetLock(cacheKey))
                {
                    ruleEngine = (RuleEngine)MarketingCache.Get(cacheKey);
                    if (ruleEngine == null)
                    {
                        // Try to deserialize rules
                        RuleSet ruleSet = DeserializeRuleSet(expr);

                        // If empty, return false
                        if (ruleSet == null)
                        {
                            return(new ValidationResult(false, String.Empty));
                        }

                        ruleEngine = new RuleEngine(ruleSet, new RuleValidation(typeof(RulesContext), null));

                        // Save in cache
                        MarketingCache.Insert(cacheKey, ruleEngine, MarketingConfiguration.Instance.CacheConfig.ExpressionCollectionTimeout);
                    }
                }
            }

            // Initialize context object
            RulesContext rulesContext = new RulesContext(context);

            if (ruleEngine == null)
            {
                throw new NullReferenceException("RuleEngine object can not be null");
            }

            try
            {
                ruleEngine.Execute(rulesContext);
            }
            catch (RuleSetValidationException ex)
            {
                string errorMessage = String.Empty;
                foreach (ValidationError error in ex.Errors)
                {
                    errorMessage += "\n\r" + error.ToString();
                }
                throw new ApplicationException(errorMessage, ex);
            }
            catch (RuleEvaluationException ex)
            {
                //If runtime error then skip promotion
                System.Diagnostics.Trace.WriteLine(String.Format("evaluation error {0} ... skip promotion", ex.Message));
            }
            catch (TargetInvocationException ex)
            {
                //If runtime error then skip promotion
                System.Diagnostics.Trace.WriteLine(String.Format("evaluation error {0} ... skip promotion", ex.Message));
            }


            // Return result
            return(rulesContext.ValidationResult);
        }