/// <summary>
        /// Gets the element conditions.
        /// </summary>
        /// <param name="expressionPlace">The expression place.</param>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        public override ConditionElementCollection GetElementConditions(string expressionPlace, FilterExpressionNode node)
        {
            ConditionElementCollection retVal = new ConditionElementCollection();

            if (node != null)
            {
                ElementDefs elementDef = FindElementDef(node.Key);
                if (elementDef == null)
                {
                    throw new ArgumentException("element  " + node.Key + " not registered");
                }
                //Default Decimal conditions
                var conditions = elementDef.Conditions ?? ConditionDecimalDefs;
                if (elementDef.HasChildren && node.Method != null)
                {
                    ElementDefs methodDef = elementDef.GetMethodDef(node.Method.Key);
                    if (methodDef == null)
                    {
                        throw new ArgumentException("method  " + node.Method.Key + " not registered");
                    }
                    conditions = methodDef.Conditions;
                }
                //Add stored condition elements in Tag  property in definition
                foreach (ElementDefs conditionDef in conditions)
                {
                    retVal.Add(conditionDef.Tag as ConditionElement);
                }
            }
            return(retVal);
        }
        /// <summary>
        /// Converts the node to string expression.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        protected override string ConvertNodeToStringExpression(FilterExpressionNode node)
        {
            string retVal = string.Empty;

            if (node.NodeType == FilterExpressionNodeType.OrBlock)
            {
                return(ConvertNodesToStringExpression(node.ChildNodes, "||"));
            }
            if (node.NodeType == FilterExpressionNodeType.AndBlock)
            {
                return(ConvertNodesToStringExpression(node.ChildNodes, "&&"));
            }
            if (node != null && node.Key == FilterExpressionItem.chooseBlock)
            {
                return(retVal);
            }

            ElementDefs elementDef = FindElementDef(node.Key);

            if (elementDef == null)
            {
                throw new ArgumentException("element  " + node.Key + " not registered");
            }

            //is Method Block
            if (elementDef.Methods != null)
            {
                //Get selected methods
                ElementDefs methodDef = elementDef.GetMethodDef(node.Method.Key);
                if (methodDef == null)
                {
                    throw new ArgumentException("method " + node.Method.Key + " not registered");
                }
                //Get methods conditions
                ElementDefs conditionDef = methodDef.GetConditionDefByName(node.Condition.Key);
                if (conditionDef == null)
                {
                    throw new ArgumentException("condition  " + node.Condition.Key + " not registered");
                }
                string collectionName            = elementDef.Name;
                string conditionOp               = conditionDef.Name;
                MethodElementParams methodParams = node.Method.Params;

                string rightStatement = GetEscapedAndCleanedExpressionNodeRightValue(node);

                string predicateExpression = "1 == 1";
                string leftStatement       = null;

                if (node.ChildNodes != null && node.ChildNodes.Count != 0)
                {
                    predicateExpression = ConvertNodesToStringExpression(node.ChildNodes);
                    //Parser bug. Nested double quotes
                    predicateExpression = predicateExpression.Replace('\"', '\'');
                }

                if (methodDef == MethodSumDef)
                {
                    FilterExpressionNode filterNode       = methodParams[0] as FilterExpressionNode;
                    ElementDefs          methodElementDef = FindElementDef(filterNode.Key);
                    if (methodElementDef == null)
                    {
                        throw new ArgumentException("element method" + filterNode.Key + " not registered");
                    }
                    leftStatement = methodElementDef.Name.Replace("this", string.Empty).Trim(new char[] { '[', ']', '\'', '.' });
                    retVal        = String.Format(@"({0}.{1}({2},""{3}"",""{4}"")) {5} {6}", ContextClass, methodDef.Name,
                                                  collectionName, leftStatement, predicateExpression, conditionOp, rightStatement);
                }
                else if (methodDef == MethodCountDef)
                {
                    retVal = String.Format(@"{0}.{1}({2},""{3}"")  {4} {5}", ContextClass, methodDef.Name,
                                           collectionName, predicateExpression, conditionOp, rightStatement);
                }
                else
                {
                    retVal = String.Format(@"{0}.{1}({2},""{3}"")", ContextClass, methodDef.Name,
                                           collectionName, predicateExpression);
                }
            }
            else
            {
                //call base impl
                retVal = base.ConvertNodeToStringExpression(node);
            }

            return(retVal);
        }
        /// <summary>
        /// Gets the new elements.
        /// </summary>
        /// <param name="expressionPlace">The expression place.</param>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override FilterExpressionNodeCollection GetNewElements(string expressionPlace, FilterExpressionNode parent)
        {
            List <FilterExpressionNode> retVal           = new List <FilterExpressionNode>();
            FilterExpressionNode        parentMethodNode = null;
            FilterExpressionNode        parentNext       = parent;

            //set default group
            string currentPromotionGroup = PromotionGroup.GetPromotionGroup(PromotionGroup.PromotionGroupKey.Order).Key;
            //expressionPlace
            string realExpressionPlace = string.Empty;

            //Get current promotion target group from ExpressionPlace {promotionGroup : custom controls data }
            string[] expressionPlaceParts = expressionPlace.Split(':');
            if (expressionPlaceParts.Length != 0)
            {
                currentPromotionGroup = expressionPlaceParts[0];
            }
            if (expressionPlaceParts.Length > 1)
            {
                realExpressionPlace = expressionPlaceParts[1];
            }

            IEnumerable <FilterExpressionNode> resultColl;

            _filterElementsMap.TryGetValue(currentPromotionGroup, out resultColl);
            //detected presents parent block type MethodBlock
            while (parentNext != null && parentMethodNode == null)
            {
                if (parentNext.NodeType == FilterExpressionNodeType.MethodBlock)
                {
                    parentMethodNode = parentNext;
                }
                parentNext = parentNext.ParentNode;
            }

            if (parent == null || parentMethodNode == null)
            {
                if (resultColl != null)
                {
                    //Add all registered element
                    retVal.AddRange(resultColl);
                }
            }
            else
            {
                if (resultColl != null)
                {
                    //Collections support
                    FilterExpressionNode parentNode             = FilterNodeFindRecursively(resultColl, x => x.Key == parentMethodNode.Key);
                    IEnumerable <FilterExpressionNode> childEls = null;
                    if (parentNode != null)
                    {
                        //gets all child element for current parrent node
                        childEls = parentNode.ChildNodes;
                    }
                    //is call from custom cotrol for get elements for binding
                    if (realExpressionPlace == "Sum")
                    {
                        ElementDefs parentElementDef = FindElementDef(parent.Key);
                        ElementDefs methodDef        = parentElementDef.GetMethodDef(parent.Method.Key);
                        //For method SUM return only Decimal type elemnts
                        if (methodDef == MethodSumDef)
                        {
                            var result = new List <FilterExpressionNode>();
                            foreach (FilterExpressionNode node in childEls)
                            {
                                ElementDefs nodeDef = FindElementDef(node.Key);
                                if (nodeDef != null && nodeDef.Conditions == ConditionDecimalDefs)
                                {
                                    result.Add(node);
                                }
                            }
                            childEls = result;
                        }
                        else
                        {
                            childEls = new FilterExpressionNode[] { };
                        }
                    }

                    retVal.AddRange(childEls);
                }
            }

            // Sort retVal By Name
            retVal.Sort(delegate(FilterExpressionNode x, FilterExpressionNode y)
                        { return(x.Name.CompareTo(y.Name)); });

            return(new FilterExpressionNodeCollection(retVal.Select(x => x.Clone(true)).ToArray()));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Converts the node to string expression.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        protected override string ConvertNodeToStringExpression(FilterExpressionNode node)
        {
            string expression = String.Empty;

            decimal             quantity     = 0;
            decimal             rewardAmount = 0;
            var                 skuList      = new string[] { };
            MethodElementParams methodParams = null;
            //Find registered element definition by key
            ElementDefs elementDef = FindElementDef(node.Key);

            if (elementDef == null)
            {
                throw new ArgumentException("element " + node.Key + " not registered");
            }
            if (elementDef.Methods != null)
            {
                skuList      = node.Value != null ? ((Dictionary <string, string>)node.Value).Keys.ToArray() : skuList;
                skuList      = EntryId2LineItemCode(skuList).ToArray();
                methodParams = node.Method.Params;
            }
            else
            {
                rewardAmount = Convert.ToDecimal(node.Value);
            }
            //Find registered method definition by key
            ElementDefs methodDef = null;

            if (elementDef.Methods != null)
            {
                methodDef = elementDef.GetMethodDef(node.Method.Key);
            }

            string[] namePart = elementDef.Name.Split(':');

            string rewardAmountType = PromotionRewardAmountType.Value;
            string rewardType       = PromotionRewardType.WholeOrder;

            if (namePart.Count() == 2)
            {
                rewardAmountType = namePart[0];
                rewardType       = namePart[1];
            }

            //gift
            if (methodDef == MethodGiftDefs)
            {
                if (skuList.Count() != 0)
                {
                    quantity = Convert.ToDecimal(methodParams[0]);
                    var skuPattern = skuList.Select(x => String.Format("\"{0}\"", x));
                    expression = String.Format("this.AddFreeGiftToCart(\"{0}\", {1})",
                                               quantity.ToString(CultureInfo.InvariantCulture),
                                               String.Join(",", skuPattern.ToArray()));
                }
            }             //with quantity, amount, sku list
            else if (methodDef == MethodEachEntryDefs)
            {
                quantity     = Convert.ToDecimal(methodParams[1]);
                rewardAmount = Convert.ToDecimal(methodParams[0]);
                if (skuList.Count() != 0)
                {
                    var entryPattern = "this.CreatePromotionEntriesSetFromTarget(\"{0}\", {1})";
                    expression = String.Format("AddPromotionItemRecord(this.CreatePromotionReward(\"{0}\", \"{1}\", \"{2}\"), {3})",
                                               rewardType, rewardAmount.ToString(CultureInfo.InvariantCulture), rewardAmountType,
                                               String.Join(",", skuList.Select(x => String.Format(entryPattern, x, quantity)).ToArray()));
                }
            }             //with amount, sku list
            else if (methodDef == MethodAllEntryDefs || methodDef == MethodAllEntryPercentDefs)
            {
                rewardAmount = Convert.ToDecimal(methodParams[0]);
                if (skuList.Count() != 0)
                {
                    var entryPattern = "this.CreatePromotionEntriesSetFromTarget(\"{0}\")";
                    expression = String.Format("AddPromotionItemRecord(this.CreatePromotionReward(\"{0}\", \"{1}\", \"{2}\"), {3})",
                                               rewardType, rewardAmount.ToString(CultureInfo.InvariantCulture), rewardAmountType,
                                               String.Join(",", skuList.Select(x => String.Format(entryPattern, x)).ToArray()));
                }
            }             // only amount
            else
            {
                var pattern = "this.AddPromotionItemRecord(this.CreatePromotionReward(\"{0}\", \"{1}\", \"{2}\")," +
                              "this.PromotionContext.TargetEntriesSet)";
                expression = String.Format(pattern, rewardType, rewardAmount.ToString(CultureInfo.InvariantCulture), rewardAmountType);
            }

            return(expression);
        }