public void Initialize()
 {
     _orderedList = new[] { "key1", "key2", "key3" };
     _keyValueComparerUsingOrderedList = new KeyValueComparerUsingOrderedList(_orderedList);
     Assert.IsInstanceOf(typeof(KeyValueComparerUsingOrderedList), _keyValueComparerUsingOrderedList);
 }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the current list of queries that have been marked as enabled
        /// </summary>
        /// <param name="tenantName"></param>
        /// <param name="agencyId"></param>
        /// <param name="agencyName"></param>
        /// <returns></returns>
        public List <QueryCategoryReference> GetEnabledQueries(string tenantName, Guid agencyId, string agencyName)
        {
            //Get the categories from the Message Switch
            var orderedCategories = GetQueryCategories().Select(x => x.Name).ToList();

            //Comparer for category dictionaries
            var comparer = new KeyValueComparerUsingOrderedList(orderedCategories);

            //Get enabled queries for the agency and order the categories for each
            var queries = GetConfigurations(tenantName, agencyId, agencyName).Where(qc => qc.IsEnabled).Select(x => new QueryConfiguration()
            {
                QueryId          = x.QueryId,
                QueryName        = x.QueryName,
                QueryDescription = x.QueryDescription,
                QueryCategories  =
                    x.QueryCategories.OrderBy(c => c, comparer)
                    .ToDictionary(kv => kv.Key, kv => kv.Value)
            }).ToList();

            //Create list of queries grouped by their categories
            var qcrList = new List <QueryGrouping>();

            queries.ForEach(q =>
            {
                var match = qcrList.SingleOrDefault(qc => q.QueryCategories.EqualsDictionary(qc.Categories));
                if (match != null)
                {
                    match.Queries.Add(new QueryReference {
                        Id = q.QueryId, Name = q.QueryName
                    });
                }
                else
                {
                    qcrList.Add(new QueryGrouping
                    {
                        Categories = q.QueryCategories,
                        Queries    = new List <QueryReference> {
                            new QueryReference {
                                Id = q.QueryId, Name = q.QueryName
                            }
                        }
                    });
                }
            });

            //Order the list by the categories
            qcrList =
                qcrList.OrderBy(
                    q =>
                    String.Format("({0})",
                                  string.Join("", q.Categories.OrderBy(c => c, new KeyValueComparerUsingOrderedList(orderedCategories)).Select(x => x.Value)))).ToList();

            //Generate a list of QueryReference objects
            var list = new List <QueryCategoryReference>();

            foreach (var q in qcrList)
            {
                var i       = 0;
                var curList = list;
                foreach (var c in q.Categories)
                {
                    var key   = c.Key;
                    var value = c.Value;

                    //Search the current list for a matching category name and value
                    var cat = curList.SingleOrDefault(x => x.Type.Equals(key) && x.Value.Equals(value));
                    if (cat == null)
                    {
                        cat = new QueryCategoryReference(c.Key, c.Value);
                        curList.Add(cat);
                    }

                    //If on the last category, add the queries to the list for this category
                    if (i == orderedCategories.Count - 1)
                    {
                        cat.Queries.AddRange(q.Queries.Select(x => new QueryReference {
                            Id = x.Id, Name = x.Name
                        }));
                    }
                    //Otherwise, set the new list to search
                    else
                    {
                        curList = cat.Categories;
                    }
                    ++i;
                }
            }

            return(list);
        }