Exemplo n.º 1
0
        /// <summary>
        /// Populates a rule condition
        /// </summary>
        /// <param name="ConditionNode"></param>
        /// <returns></returns>
        private RuleCondition PopulateCondition(XmlNode ConditionNode)
        {
            RuleCondition condition = new RuleCondition();

            System.Xml.XPath.XPathNavigator nav = ConditionNode.CreateNavigator();

            condition.Expression  = nav.SelectSingleNode(TranslateExpressionCache.GetXpathExpression("Expression")).InnerXml;
            condition.ID          = ConditionNode.Attributes["id"] == null ? "" : ConditionNode.Attributes["id"].Value.ToString();
            condition.Name        = ConditionNode.Attributes["name"] == null ? "" : ConditionNode.Attributes["name"].Value.ToString();
            condition.Destination = nav.SelectSingleNode(TranslateExpressionCache.GetXpathExpression("Destination")).InnerXml;
            XmlNode DestinationNode = ConditionNode.SelectSingleNode("Destination");

            if (DestinationNode.Attributes != null && DestinationNode.Attributes["format"] != null)
            {
                condition.format = DestinationNode.Attributes["format"].Value;
                condition.type   = Type.GetType(String.Format(DestinationNode.Attributes["type"].Value), true, true);
            }

            //Populate attributes for true values
            XmlNode ConditionTrueNode = ConditionNode.SelectSingleNode("True");

            if (ConditionTrueNode != null)
            {
                condition.TrueValue.Value               = ConditionTrueNode.InnerXml;
                condition.TrueValue.ExpressionSource    = ConditionTrueNode.Attributes["ExpressionSource"] == null ? "" : ConditionTrueNode.Attributes["ExpressionSource"].Value;
                condition.TrueValue.isExpression        = ConditionTrueNode.Attributes["isExpression"] == null ? false : Convert.ToBoolean(ConditionTrueNode.Attributes["isExpression"].Value);
                condition.TrueValue.UseDefault          = ConditionTrueNode.Attributes["UseDefault"] == null ? false : Convert.ToBoolean(ConditionTrueNode.Attributes["UseDefault"].Value);
                condition.TrueValue.ContunueProcessing  = ConditionTrueNode.Attributes["ContinueProcessing"] == null ? true : Convert.ToBoolean(ConditionTrueNode.Attributes["ContinueProcessing"].Value);
                condition.TrueValue.Actionid            = ConditionTrueNode.Attributes["Actionid"] == null ? "" : ConditionTrueNode.Attributes["Actionid"].Value;
                condition.TrueValue.ContinueTransaction = ConditionTrueNode.Attributes["ContinueTransaction"] == null ? true : Convert.ToBoolean(ConditionTrueNode.Attributes["ContinueTransaction"].Value);
            }
            //populate attributes for false values
            XmlNode ConditionFalseNode = ConditionNode.SelectSingleNode("False");

            if (ConditionFalseNode != null)
            {
                condition.FalseValue.Value               = ConditionFalseNode.InnerXml;
                condition.FalseValue.ExpressionSource    = ConditionFalseNode.Attributes["ExpressionSource"] == null ? "" : ConditionFalseNode.Attributes["ExpressionSource"].Value;
                condition.FalseValue.isExpression        = ConditionFalseNode.Attributes["isExpression"] == null ? false : Convert.ToBoolean(ConditionFalseNode.Attributes["isExpression"].Value);
                condition.FalseValue.UseDefault          = ConditionFalseNode.Attributes["UseDefault"] == null ? false : Convert.ToBoolean(ConditionFalseNode.Attributes["UseDefault"].Value);;
                condition.FalseValue.ContunueProcessing  = ConditionFalseNode.Attributes["ContinueProcessing"] == null ? true : Convert.ToBoolean(ConditionFalseNode.Attributes["ContinueProcessing"].Value);
                condition.FalseValue.Actionid            = ConditionFalseNode.Attributes["Actionid"] == null ? "" : ConditionFalseNode.Attributes["Actionid"].Value;
                condition.FalseValue.ContinueTransaction = ConditionFalseNode.Attributes["ContinueTransaction"] == null ? true : Convert.ToBoolean(ConditionFalseNode.Attributes["ContinueTransaction"].Value);
            }

            if (condition.TrueValue != null && condition.FalseValue != null)
            {
                return(condition);
            }
            else
            {
                return(null);
            }
        }
Exemplo n.º 2
0
        private void ExecuteConditions(RuleCondition condition, XmlDocument NewDocTemplate, XPathNavigator nav, List <string> ActionsToExecute, ref bool ContinueProcessing)
        {
            bool Continue = true;

            ExecuteConditions(condition, NewDocTemplate, nav, ActionsToExecute, ref ContinueProcessing, ref Continue);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Loads a translate rule
        /// </summary>
        public void LoadRule()
        {
            XmlNode RuleNode = _RulesDoc.SelectSingleNode(String.Format("RuleEngine/Rules/Rule[@id='{0}']", _RuleID));

            _RuleDescription     = RuleNode.Attributes["desc"].Value;
            _DestinationSavePath = RuleNode.Attributes["DestinationSavePath"].Value;
            _ProcessAsGroup      = Convert.ToBoolean(RuleNode.Attributes["ProcessAsGroup"].Value);
            _MapExact            = Convert.ToBoolean(RuleNode.Attributes["ExactMapping"] == null ? "false" : RuleNode.Attributes["ExactMapping"].Value);
            XmlNodeList MappingList = RuleNode.SelectNodes("Mappings/*");

            //add all mapings
            for (int x = 0, count = MappingList.Count; x < count; x++)
            {
                RuleMapping map = new RuleMapping();
                map.SourcePath      = MappingList[x].SelectSingleNode("Source").InnerXml;
                map.DestinationPath = MappingList[x].SelectSingleNode("Destination").InnerXml;

                if (MappingList[x].Attributes != null && MappingList[x].Attributes["format"] != null)
                {
                    map.Format     = MappingList[x].Attributes["format"].Value;
                    map.FormatType = Type.GetType(String.Format(MappingList[x].Attributes["type"].Value), true, true);
                }
                _RuleMappings.Add(map);
            }

            //Add all conditions now
            XmlNodeList ConditionList = RuleNode.SelectNodes("Conditions/*");

            for (int x = 0, count = ConditionList.Count; x < count; x++)
            {
                RuleCondition condition = PopulateCondition(ConditionList[x]);

                if (condition != null)
                {
                    _RuleConditions.Add(condition);
                }
            }

            //Load All Actions
            XmlNodeList ActionList = RuleNode.SelectNodes("Actions/Action/*");

            for (int x = 0, count = ActionList.Count; x < count; x++)
            {
                RuleAction action      = new RuleAction();
                XmlNode    CurrentNode = ActionList[x];

                if (CurrentNode.Name == "CreateLineItem")
                {
                    action.ID           = CurrentNode.SelectSingleNode("@id").Value;
                    action.ItemToCreate = CurrentNode.SelectSingleNode("@ItemToCreate").Value;

                    if (CurrentNode.HasChildNodes)
                    {
                        foreach (XmlNode node in ActionList[x].ChildNodes)
                        {
                            if (node.Name == "Attribute")
                            {
                                RuleAttribute att = new RuleAttribute();
                                //att.ID = node.SelectSingleNode("@id").Value;
                                att.ID    = node.Attributes["id"].Value;
                                att.Value = node.InnerXml;
                                att.UseBuiltInIdentity = node.Attributes["useIdentity"] != null?Convert.ToBoolean(node.Attributes["useIdentity"].Value) : false;

                                att.IsExpression = node.Attributes["isExpression"] != null?Convert.ToBoolean(node.Attributes["isExpression"].Value) : false;

                                att.ExpressionSrouce = node.Attributes["ExpressionSource"] != null ? node.Attributes["ExpressionSource"].Value : "";

                                if (node.Attributes != null && node.Attributes["format"] != null)
                                {
                                    att.format     = node.Attributes["format"].Value;
                                    att.formatType = Type.GetType(String.Format(node.Attributes["type"].Value), true, true);
                                }
                                action.Attributes.Add(att);
                            }
                            if (node.Name == "Conditions")
                            {
                                XmlNodeList conditionList = node.SelectNodes("Condition");
                                foreach (XmlNode cNode in conditionList)
                                {
                                    RuleCondition AttributeCondition = PopulateCondition(cNode);
                                    if (AttributeCondition != null)
                                    {
                                        action.Conditions.Add(AttributeCondition);
                                    }
                                }
                            }
                        }
                    }
                    _RuleActions.Add(action);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Condition Executeion isolated here so we can reuse this code for conditions that are acciciated to multiple elements
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="NewDocTemplate"></param>
        /// <param name="nav"></param>
        /// <param name="ActionsToExecute"></param>
        /// <param name="ContinueProcessing"></param>
        /// <param name="ImportNode"></param>
        private void ExecuteConditions(RuleCondition condition, XmlDocument NewDocTemplate, XPathNavigator nav, List <string> ActionsToExecute, ref bool ContinueProcessing, ref bool ContinueTransaction)
        {
            //Temporary addition to allow for special characters in the xml template.  this should be cleaned up.
            string newCondition = condition.Expression;

            //newCondition = newCondition.Replace("&amp", "&");
            //newCondition = newCondition.Replace("&gt;", ">");
            //newCondition = newCondition.Replace("&lt;", "<");

            bool ExpressionResult;

            //Evaluate our expression
            //if (newCondition.Contains("number(@taxes) + (sum(./../L36/@rettax) * -1)"))
            //Console.WriteLine("");
            //if (condition.Contains("string(./../Customer/@Order_Number)"))
            //Console.WriteLine("");

            //if (Convert.ToBoolean(nav.Evaluate("count(preceding-sibling::L12) > 1")))
            //Console.WriteLine();

            //ExpressionResult = Convert.ToBoolean(nav.Evaluate(newCondition));
            ExpressionResult = Convert.ToBoolean(nav.Evaluate(GetXpathExpression(newCondition)));

            RuleValue ResultConditionValue;

            if (ExpressionResult)
            {
                ResultConditionValue = condition.TrueValue;
            }
            else
            {
                ResultConditionValue = condition.FalseValue;
            }

            //if (ResultConditionValue.Value == "number(@taxes) + (sum(./../L36/@rettax) * -1) - sum(./../L38/@tax_amount)")
            //  Console.WriteLine();

            //RuleValue ResultConditionValue = condition.TrueValue;
            if (ResultConditionValue != null && ResultConditionValue.Value != null)
            {
                //condition could be left blank if we only want to execute the action assiciated with the result
                if (!String.IsNullOrEmpty(condition.Destination))
                {
                    //check to see if we want to only use the default value instead of the one assigned
                    if (!ResultConditionValue.UseDefault)
                    {
                        //Check if the result is an expression.
                        if (!ResultConditionValue.isExpression)
                        {
                            NewDocTemplate.SelectSingleNode(condition.Destination).Value = FormatValue(condition.format, condition.type, ResultConditionValue.Value);
                            ContinueProcessing = ResultConditionValue.ContunueProcessing;
                            ImportNode         = true;
                        }
                        else
                        {
                            if (ResultConditionValue.ExpressionSource == ExpressionSource.SourceFile.ToString())
                            {
                                NewDocTemplate.SelectSingleNode(condition.Destination).Value = FormatValue(condition.format, condition.type, nav.Evaluate(GetXpathExpression(ResultConditionValue.Value)).ToString());
                                ImportNode = true;
                            }
                            if (ResultConditionValue.ExpressionSource == ExpressionSource.DestinationFile.ToString())
                            {
                                XPathNavigator destinationNav = NewDocTemplate.DocumentElement.CreateNavigator();
                                NewDocTemplate.SelectSingleNode(condition.Destination).Value = FormatValue(condition.format, condition.type, destinationNav.Evaluate((ResultConditionValue.Value)).ToString());
                                ImportNode = true;
                            }
                        }
                    }
                }
                //Check to see if we should continue processing our conditions
                ContinueProcessing = ResultConditionValue.ContunueProcessing;
                //Check to see if we should continue processing the current transaction.
                ContinueTransaction = ResultConditionValue.ContinueTransaction.Value;

                //Assign any actions that are needed to be performed for later processing
                if (!String.IsNullOrEmpty(ResultConditionValue.Actionid))
                {
                    ActionsToExecute.Add(ResultConditionValue.Actionid);
                }
            }
        }