Ejemplo n.º 1
0
        /// <summary>
        /// Gets the business categories that exist within the businesses at the said state, city, and zipcode
        /// </summary>
        /// <param name="business">Selected business</param>
        /// <returns>List of categories</returns>
        public static List <BaseInfo> BusinessCategories(List <BaseInfo> businesses)
        {
            if (businesses.Count > 0)
            {
                if (businesses[0] is Business business)
                {
                    Select data = new Select("businesscategories");
                    data.AddColumns("categoryname, Count(categoryname) as total");
                    data.AddInnerJoin("business", "businessid");
                    data.SetGroupBy("categoryname");
                    data.AddFilter($"state = '{business.State}'");
                    data.AddFilter($"city = '{business.City}'");
                    data.AddFilter($"zipcode = '{business.Zipcode}'");

                    OrFilter orfilter = new OrFilter();
                    foreach (Business b in businesses)
                    {
                        orfilter.Add($"business.businessid = '{b.Businessid}'");
                    }
                    data.AddOrFilter(orfilter);

                    return(CreateList(typeof(BusinessCategory), RunQuery(data)));
                }
            }
            return(new List <BaseInfo>());
        }
Ejemplo n.º 2
0
        private static TestFilter FromXml(TNode node)
        {
            switch (node.Name)
            {
            case "filter":
            case "and":
                var andFilter = new AndFilter();
                foreach (var childNode in node.ChildNodes)
                {
                    andFilter.Add(FromXml(childNode));
                }
                return(andFilter);

            case "or":
                var orFilter = new OrFilter();
                foreach (var childNode in node.ChildNodes)
                {
                    orFilter.Add(FromXml(childNode));
                }
                return(orFilter);

            case "not":
                return(new NotFilter(FromXml(node.FirstChild)));

            case "id":
                var idFilter = new IdFilter();
                if (node.Value != null)
                {
                    foreach (string id in node.Value.Split(COMMA))
                    {
                        idFilter.Add(id);
                    }
                }
                return(idFilter);

            case "tests":
                var testFilter = new SimpleNameFilter();
                foreach (var childNode in node.SelectNodes("test"))
                {
                    testFilter.Add(childNode.Value);
                }
                return(testFilter);

            case "cat":
                var catFilter = new CategoryFilter();
                if (node.Value != null)
                {
                    foreach (string cat in node.Value.Split(COMMA))
                    {
                        catFilter.AddCategory(cat);
                    }
                }
                return(catFilter);

            default:
                throw new ArgumentException("Invalid filter element: " + node.Name, "xmlNode");
            }
        }
Ejemplo n.º 3
0
        private static TestFilter FromXml(XmlNode xmlNode)
        {
            switch (xmlNode.Name)
            {
            case "filter":
            case "and":
                var andFilter = new AndFilter();
                foreach (XmlNode childNode in xmlNode.ChildNodes)
                {
                    andFilter.Add(FromXml(childNode));
                }
                return(andFilter);

            case "or":
                var orFilter = new OrFilter();
                foreach (System.Xml.XmlNode childNode in xmlNode.ChildNodes)
                {
                    orFilter.Add(FromXml(childNode));
                }
                return(orFilter);

            case "not":
                return(new NotFilter(FromXml(xmlNode.FirstChild)));

            case "id":
                var idFilter = new IdFilter();
                foreach (string id in xmlNode.InnerText.Split(COMMA))
                {
                    idFilter.Add(int.Parse(id));
                }
                return(idFilter);

            case "tests":
                var testFilter = new SimpleNameFilter();
                foreach (XmlNode childNode in xmlNode.SelectNodes("test"))
                {
                    testFilter.Add(childNode.InnerText);
                }
                return(testFilter);

            case "cat":
                var catFilter = new CategoryFilter();
                foreach (string cat in xmlNode.InnerText.Split(COMMA))
                {
                    catFilter.AddCategory(cat);
                }
                return(catFilter);

            default:
                throw new ArgumentException("Invalid filter element: " + xmlNode.Name, "xmlNode");
            }
        }
Ejemplo n.º 4
0
        private TestFilter GetExpression()
        {
            TestFilter term = GetTerm();

            if (token != "|")
            {
                return(term);
            }
            OrFilter orFilter = new OrFilter(term);

            while (token == "|")
            {
                GetToken();
                orFilter.Add(GetTerm());
            }
            return(orFilter);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Create filter by arguments.
        /// </summary>
        public Filter Create()
        {
            BinaryCompoundFilter filter = new AndFilter();

            foreach (KeyValuePair <string, ValueObject> argument in arguments)
            {
                // Get filter factory.
                FilterFactory factory;
                if ((argument.Value == null) || !FilterFactories.TryGetValue(argument.Key, out factory))
                {
                    continue;
                }
                Logger.Trace("{0}: {1}", argument.Key, factory);
                // Check argument type.
                List <string> values = new List <string>();
                if (!argument.Value.IsList)
                {
                    values.Add(argument.Value.ToString());
                }
                else
                {
                    values.AddRange(argument.Value
                                    .AsList
                                    .Cast <object>()
                                    .Select(value => value.ToString()));
                }
                // Add all.
                if (values.Count != 0)
                {
                    CompoundFilter innerFilter = new OrFilter();
                    foreach (string value in values)
                    {
                        innerFilter.Add(factory(GetParameterName, value));
                    }
                    filter.Add(innerFilter);
                }
            }
            return(filter);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Reinitialize the Attributes list with the given businesses remaining
        /// </summary>
        /// <param name="businesses">List of businesses remaining</param>
        /// <returns>List of attributes found</returns>
        public static List <BaseInfo> BusinessAttributes(List <BaseInfo> businesses)
        {
            List <string> qualifiedAttributes = new List <string>(MainData.GetQualifiedAttributes().Keys);

            OrFilter orfilter = new OrFilter();

            foreach (string atr in qualifiedAttributes)
            {
                orfilter.Add($"attributename like '{atr}'");
            }
            if (businesses.Count > 0)
            {
                if (businesses[0] is Business business)
                {
                    Select data = new Select("businessattributes");
                    data.AddColumns("attributename, Count(attributename) as total");
                    data.AddInnerJoin("business", "businessid");
                    data.SetGroupBy("attributename");
                    data.AddFilter($"state = '{business.State}'");
                    data.AddFilter($"city = '{business.City}'");
                    data.AddFilter($"zipcode = '{business.Zipcode}'");
                    data.AddFilter($"value <> 'False'");
                    data.AddFilter($"value <> 'no'");
                    data.AddOrFilter(orfilter);

                    OrFilter orfilter2 = new OrFilter();
                    foreach (Business b in businesses)
                    {
                        orfilter2.Add($"business.businessid = '{b.Businessid}'");
                    }
                    data.AddOrFilter(orfilter2);

                    return(CreateList(typeof(BusinessAttribute), RunQuery(data)));
                }
            }
            return(new List <BaseInfo>());
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Create a TestFilter from it's TNode representation
        /// </summary>
        public static TestFilter FromXml(TNode node)
        {
            bool isRegex = node.Attributes["re"] == "1";

            switch (node.Name)
            {
            case "filter":
            case "and":
                var andFilter = new AndFilter();
                foreach (var childNode in node.ChildNodes)
                {
                    andFilter.Add(FromXml(childNode));
                }
                return(andFilter);

            case "or":
                var orFilter = new OrFilter();
                foreach (var childNode in node.ChildNodes)
                {
                    orFilter.Add(FromXml(childNode));
                }
                return(orFilter);

            case "not":
                return(new NotFilter(FromXml(node.FirstChild)));

            case "id":
                return(new IdFilter(node.Value));

            case "test":
                return(new FullNameFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "name":
                return(new TestNameFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "method":
                return(new MethodNameFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "class":
                return(new ClassNameFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "namespace":
                return(new NamespaceFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "cat":
                return(new CategoryFilter(node.Value)
                {
                    IsRegex = isRegex
                });

            case "prop":
                string name = node.Attributes["name"];
                if (name != null)
                {
                    return new PropertyFilter(name, node.Value)
                           {
                               IsRegex = isRegex
                           }
                }
                ;
                break;
            }

            throw new ArgumentException("Invalid filter element: " + node.Name, "xmlNode");
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Query which price ranges exist among the current list of businesses
        /// </summary>
        /// <param name="businesses">List of businesses on the businessGrid</param>
        /// <returns>bool of prices and meals that exist</returns>
        public static (bool[] priceArray, Dictionary <string, bool> mealsList) BusinessPriceMealsList(List <BaseInfo> businesses)
        {
            return(Prices(), Meals());

            // Find what priceranges exist
            bool[] Prices()
            {
                bool[] prices     = new bool[4];
                Select priceQuery = new Select("businessattributes");

                priceQuery.AddColumns("distinct value");
                priceQuery.AddFilter($"lower(attributename) = 'restaurantspricerange2'");
                priceQuery.AddOrFilter(AddBusinessOrFilter());

                DataTable priceResult = RunQuery(priceQuery);

                foreach (DataRow row in priceResult.Rows)
                {
                    object[] element = row.ItemArray;
                    if (int.TryParse(element[0].ToString(), out int index))
                    {
                        prices[index - 1] = true;
                    }
                }

                return(prices);
            }

            // Find which meals are offered
            Dictionary <string, bool> Meals()
            {
                Dictionary <string, bool> mealFilters = new Dictionary <string, bool>
                {
                    { "breakfast", false },
                    { "brunch", false },
                    { "dessert", false },
                    { "dinner", false },
                    { "latenight", false },
                    { "lunch", false }
                };
                Select mealsQuery = new Select("businessattributes");

                mealsQuery.AddColumns("distinct attributename, value");
                mealsQuery.AddFilter($"lower(value) = 'true'");
                OrFilter orfilter = new OrFilter();

                foreach (string meal in mealFilters.Keys)
                {
                    orfilter.Add($"lower(attributename) = '{meal}'");
                }
                mealsQuery.AddOrFilter(AddBusinessOrFilter());
                mealsQuery.AddOrFilter(orfilter);
                DataTable mealsResult = RunQuery(mealsQuery);

                foreach (DataRow row in mealsResult.Rows)
                {
                    object[] element = row.ItemArray;
                    if (element.Length > 0)
                    {
                        if (element[1].ToString() == "True")
                        {
                            mealFilters[element[0].ToString()] = true;
                        }
                    }
                }

                return(mealFilters);
            }

            // Creates a big OrFilter for this query
            OrFilter AddBusinessOrFilter()
            {
                OrFilter returnorfilter = new OrFilter();

                foreach (Business business in businesses)
                {
                    returnorfilter.Add($"businessid = '{business.Businessid}'");
                }
                return(returnorfilter);
            }
        }