Exemplo n.º 1
0
        private void AssignNodeRoot(JobSearchFilterReceiver root, string input)
        {
            //sample input: A_B_C_D
            List <string> itemIDs = input.Split(new string[] { "_" }, StringSplitOptions.RemoveEmptyEntries).ToList();

            if (itemIDs.Count() == 0)
            {
                return;
            }

            if (itemIDs.Count() == 1)
            {
                if (root.values == null)
                {
                    root.values.Add(new JobSearchFilterReceiverItem {
                        ItemID = itemIDs[0]
                    });
                }
                else
                {
                    bool hasMatchingItem = root.values.Where(c => c.ItemID.Equals(itemIDs[0])).Any();
                    if (!hasMatchingItem)
                    {
                        root.values.Add(new JobSearchFilterReceiverItem {
                            ItemID = itemIDs[0]
                        });
                    }
                }
                return;
            }
            else
            {
                JobSearchFilterReceiverItem nextLevelItem = null;
                if (root.values == null)
                {
                    nextLevelItem = new JobSearchFilterReceiverItem {
                        ItemID = itemIDs[0]
                    };
                    root.values.Add(nextLevelItem);
                }
                else
                {
                    JobSearchFilterReceiverItem matchingItem = root.values.Where(c => c.ItemID.Equals(itemIDs[0])).FirstOrDefault();
                    if (matchingItem == null)
                    {
                        nextLevelItem = new JobSearchFilterReceiverItem {
                            ItemID = itemIDs[0]
                        };
                        root.values.Add(nextLevelItem);
                    }
                    else
                    {
                        nextLevelItem = matchingItem;
                    }
                }
                if (nextLevelItem != null)
                {
                    AssignNodeRootItem(nextLevelItem, String.Join("_", itemIDs.Skip(1)));
                }
            }
        }
Exemplo n.º 2
0
        public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
        {
            HttpRequestBase request  = controllerContext.HttpContext.Request;
            string          keywords = request.Params["Keywords"];

            int page = 1;

            if (request.Params["Page"] != null)
            {
                page = Int32.Parse(request.Params["Page"]);
            }

            string sortBy = request.Params["SortBy"];

            JobSearchSalaryFilterReceiver salary = null;

            if (request.Params["Salary.TargetValue"] != null && request.Params["Salary.LowerRange"] != null && request.Params["Salary.UpperRange"] != null)
            {
                string salaryTargetValue = request.Params["Salary.TargetValue"];
                int    salaryLowerRange  = 0;
                Int32.TryParse(request.Params["Salary.LowerRange"], out salaryLowerRange);
                int salaryUpperRange = 0;
                Int32.TryParse(request.Params["Salary.UpperRange"], out salaryUpperRange);
                salary = new JobSearchSalaryFilterReceiver()
                {
                    RootName = "Salary", TargetValue = salaryTargetValue, LowerRange = salaryLowerRange, UpperRange = salaryUpperRange
                };
            }

            /*
             * Input samples
             * Filters[0].rootId = 0
             * Filters[0].values = 1, 1_2, 1_2_3, 1_2_4, 1_3_18
             * Filters[1].rootId = A
             * Filters[1].values = B, B_C, B_C_D, B_C_E
             */

            Dictionary <string, FilterNode> tracker = new Dictionary <string, FilterNode>();

            List <string> filterKeys = request.QueryString.AllKeys.Where(c => c.ToUpper().Contains("FILTERS")).OrderBy(c => c).ToList();
            var           reqParams  = request.QueryString;

            if (filterKeys == null || filterKeys.Count <= 0)
            {
                filterKeys = request.Params.AllKeys.Where(c => c.ToUpper().Contains("FILTER")).OrderBy(c => c).ToList();
                reqParams  = request.Params;
            }

            foreach (string k in filterKeys)
            {
                string thisFilterKey = ExtractFilterNodeID(k);
                if (thisFilterKey != null)
                {
                    if (tracker.Keys.Contains(thisFilterKey))
                    {
                        FilterNode thisNode = tracker[thisFilterKey];
                        if (k.ToUpper().Contains("ROOTID"))
                        {
                            thisNode.RootId = reqParams[k];
                        }
                        else if (k.ToUpper().Contains("VALUES"))
                        {
                            thisNode.Values = reqParams[k].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        }
                    }
                    else
                    {
                        FilterNode newNode = new FilterNode();
                        if (k.ToUpper().Contains("ROOTID"))
                        {
                            newNode.RootId = reqParams[k];
                        }
                        else if (k.ToUpper().Contains("VALUES"))
                        {
                            newNode.Values = reqParams[k].Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries).ToList();
                        }

                        tracker.Add(thisFilterKey, newNode);
                    }
                }
            }

            /*
             * Output Sample
             * tracker["Filter[0]"] = FilterNode { RootId ="0", Values = ["1","1_2","1_2_3" ]}
             * tracker["Filter[1]"] = FilterNode { RootId = "A", Values = ["B","B_C","B_C_E" ]}
             */

            //process tracker into target model
            JobSearchResultsFilterModel targetModel = new JobSearchResultsFilterModel()
            {
                Keywords = keywords, Page = page, Filters = new List <JobSearchFilterReceiver>(), Salary = salary, SortBy = sortBy
            };

            foreach (string trackerKey in tracker.Keys)
            {
                FilterNode thisFilterNode        = tracker[trackerKey];
                JobSearchFilterReceiver receiver = new JobSearchFilterReceiver {
                    rootId = thisFilterNode.RootId, values = new List <JobSearchFilterReceiverItem>()
                };
                if (thisFilterNode.Values != null && thisFilterNode.Values.Count() > 0)
                {
                    foreach (string value in thisFilterNode.Values)
                    {
                        AssignNodeRoot(receiver, value);
                    }
                }

                targetModel.Filters.Add(receiver);
            }

            return(targetModel);
        }