public QueryResultDisplay SearchForms(QueryDisplay query)
        {
            var textSearch = query.Parameters.FirstOrDefault(x => x.FieldName == "textSearch").IfNotNull(x => x.Value);
            var isPublic = query.Parameters.FirstOrDefault(x => x.FieldName == "isPublic").IfNotNull(x => x.Value);
            var groups = query.Parameters.FirstOrDefault(x => x.FieldName == "groups").IfNotNull(x => x.Value);
            var tags = query.Parameters.FirstOrDefault(x => x.FieldName == "tags").IfNotNull(x => x.Value);
            if (query.ItemsPerPage < 1)
            {
                query.ItemsPerPage = _itemsPerPage;
            }

            bool isPublicParsed;
            var searchModel = new SearchFormModel
            {
                SortColumn = query.SortBy,
                SortDirection = query.SortDirection,

                TextSearch = textSearch,
                IsPublic = !string.IsNullOrEmpty(isPublic) && Boolean.TryParse(isPublic, out isPublicParsed) ? (bool?)isPublicParsed : null,
                Groups = !string.IsNullOrEmpty(groups) ? groups.ToDelimitedList() : new List<string>(),
                Tags = !string.IsNullOrEmpty(tags) ? tags.ToDelimitedList() : new List<string>()
            };

            var forms = _formService.SearchAll(searchModel);
            var totalItems = forms.Count();
            var pagedForms = forms
                .Skip(query.CurrentPage * query.ItemsPerPage)
                .Take(query.ItemsPerPage)
                .Select(Mapper.Map<IContent, ContentItemBasic<ContentPropertyBasic, IContent>>)
                .Select(Mapper.Map<ContentItemBasic<ContentPropertyBasic, IContent>, FormListItemDisplay>).ToList();
            var result = QueryResultDisplay.Build(pagedForms, totalItems, query.CurrentPage, query.ItemsPerPage);
            return result;
        }
        public QueryResultDisplay SearchFormGroups(QueryDisplay query)
        {
            var states = query.Parameters.FirstOrDefault(x => x.FieldName == "states").IfNotNull(x => x.Value);
            var categories = query.Parameters.FirstOrDefault(x => x.FieldName == "categories").IfNotNull(x => x.Value);
            var productTypes = query.Parameters.FirstOrDefault(x => x.FieldName == "productTypes").IfNotNull(x => x.Value);
            var productNames = query.Parameters.FirstOrDefault(x => x.FieldName == "productNames").IfNotNull(x => x.Value);
            var memberSituations = query.Parameters.FirstOrDefault(x => x.FieldName == "memberSituations").IfNotNull(x => x.Value);
            if (query.ItemsPerPage < 1)
            {
                query.ItemsPerPage = _itemsPerPage;
            }

            var searchModel = new SearchFormGroupModel
            {
                SortDirection = query.SortDirection,

                States = !string.IsNullOrEmpty(states) ? states.ToDelimitedList() : new List<string>(),
                Categories = !string.IsNullOrEmpty(categories) ? categories.ToDelimitedList() : new List<string>(),
                ProductTypes = !string.IsNullOrEmpty(productTypes) ? productTypes.ToDelimitedList() : new List<string>(),
                ProductNames = !string.IsNullOrEmpty(productNames) ? productNames.ToDelimitedList() : new List<string>(),
                MemberSituations = !string.IsNullOrEmpty(memberSituations) ? memberSituations.ToDelimitedList() : new List<string>()
            };

            var forms = _formGroupService.SearchAll(searchModel);
            var totalItems = forms.Count();
            var pagedForms = forms
                .Skip(query.CurrentPage * query.ItemsPerPage)
                .Take(query.ItemsPerPage)
                .Select(Mapper.Map<IContent, ContentItemBasic<ContentPropertyBasic, IContent>>)
                .ToList();
            var result = QueryResultDisplay.Build(pagedForms, totalItems, query.CurrentPage, query.ItemsPerPage);
            return result;
        }
        public QueryResultDisplay SearchAgents(QueryDisplay query)
        {
            var agentNumber = query.Parameters.FirstOrDefault(x => x.FieldName == "agentnumber").IfNotNull(x => x.Value);
            var firstName = query.Parameters.FirstOrDefault(x => x.FieldName == "firstname").IfNotNull(x => x.Value);
            var lastName = query.Parameters.FirstOrDefault(x => x.FieldName == "lastname").IfNotNull(x => x.Value);
            var emailAddress = query.Parameters.FirstOrDefault(x => x.FieldName == "emailaddress").IfNotNull(x => x.Value);
            var ssn = query.Parameters.FirstOrDefault(x => x.FieldName == "ssn").IfNotNull(x => x.Value);
            var agencyName = query.Parameters.FirstOrDefault(x => x.FieldName == "agencyname").IfNotNull(x => x.Value);
            var location = query.Parameters.FirstOrDefault(x => x.FieldName == "location").IfNotNull(x => x.Value);
            if (query.ItemsPerPage < 1)
            {
                query.ItemsPerPage = _itemsPerPage;
            }

            var agents = _agentService.Search(agentNumber, firstName, lastName, ssn, emailAddress, agencyName, location,
                query.CurrentPage, query.ItemsPerPage, query.SortBy, query.SortDirection);
            var result = QueryResultDisplay.Build<GetAgentsReturnModel, AgentListItemDisplay>(agents, query.CurrentPage,
                query.ItemsPerPage);
            var ms = Services.MemberService;
            var allAgentMembershipUsers = System.Web.Security.Roles.GetUsersInRole("agents");
            var allAgentUmbracoMembers = allAgentMembershipUsers.Select(x => ms.GetByUsername(x));
            var agentUmbracoMembersWithNumbers = allAgentUmbracoMembers.Where(x => !String.IsNullOrEmpty(x.GetValue<string>("agentNumber")));
            var agentUmbracoMembersByNumbers = new Dictionary<string, Umbraco.Core.Models.IMember>();
            foreach (var agentUmbracoMemberWithNumber in agentUmbracoMembersWithNumbers)
            {
                agentUmbracoMembersByNumbers[agentUmbracoMemberWithNumber.GetValue<string>("agentNumber")] = agentUmbracoMemberWithNumber;
            }
            var tempItems = new List<AgentListItemDisplay>();
            foreach (AgentListItemDisplay agent in result.Items)
            {
                Umbraco.Core.Models.IMember agentUmbracoMember;
                if (agentUmbracoMembersByNumbers.TryGetValue(agent.AgentNumber, out agentUmbracoMember))
                {
                    agent.IsMember = true;
                    agent.UmbracoUserName = agentUmbracoMember.Username;
                }
                else
                {
                    agent.IsMember = false;
                    agent.UmbracoUserName = null;
                }
                tempItems.Add(agent);
            }
            result.Items = tempItems;
            return result;
        }
        public QueryResultDisplay SearchMembers(QueryDisplay query)
        {
            var firstName = query.Parameters.FirstOrDefault(x => x.FieldName == "firstname").IfNotNull(x => x.Value);
            var lastName = query.Parameters.FirstOrDefault(x => x.FieldName == "lastname").IfNotNull(x => x.Value);
            var emailAddress =
                query.Parameters.FirstOrDefault(x => x.FieldName == "emailaddress").IfNotNull(x => x.Value);
            var ssn = query.Parameters.FirstOrDefault(x => x.FieldName == "ssn").IfNotNull(x => x.Value);
            var arbor = query.Parameters.FirstOrDefault(x => x.FieldName == "arbor").IfNotNull(x => x.Value);
            var groupName = query.Parameters.FirstOrDefault(x => x.FieldName == "group").IfNotNull(x => x.Value);

            var searcher = ExamineManager.Instance.SearchProviderCollection["InternalMemberSearcher"];
            var searchCriteria = searcher.CreateSearchCriteria(UmbracoExamine.IndexTypes.Content);
            var group = MemberGroup.GetByName(groupName);
            var luceneString = "_memberGroups:" + group.Id;

            if (query.ItemsPerPage < 1)
            {
                query.ItemsPerPage = _itemsPerPage;
            }

            ////
            if (!String.IsNullOrWhiteSpace(firstName))
            {
                luceneString += " AND memberFirstName:" + firstName;
            }
            if (!String.IsNullOrWhiteSpace(lastName))
            {
                luceneString += " AND memberLastName:" + lastName;
            }
            if (!String.IsNullOrWhiteSpace(emailAddress))
            {
                luceneString += " AND legacyEmail:" + emailAddress;
            }
            if (!String.IsNullOrWhiteSpace(ssn))
            {
                luceneString += " AND socSecNum:" + ssn;
            }

            if (!String.IsNullOrWhiteSpace(arbor))
            {
                luceneString += " AND arbors:" + arbor;
            }
            ////

            var luceneQuery = searchCriteria.RawQuery(luceneString);
            var searchResults = searcher.Search(luceneQuery);
            int membersCount = searchResults.Count();
            var luceneMembers = searchResults
                .Skip(query.CurrentPage*query.ItemsPerPage)
                .Take(query.ItemsPerPage)
                .Select(x => new MemberListItemDisplay
                    {
                        MemberNumber = x["id"],
                        MemberName = (x.Fields.ContainsKey("memberFirstName") ? x["memberFirstName"] : String.Empty) + " " + (x.Fields.ContainsKey("memberLastName") ? x["memberLastName"] : String.Empty),
                        LastLogin = DateTime.Parse(x["umbracoMemberLastLogin"]),
                        IsLocked = String.Compare(x["umbracoMemberLockedOut"], "0") != 0,
                        IsMember = true,
                        UserName = x["nodeName"]
                    }
                );

            /*
            IEnumerable<umbraco.cms.businesslogic.member.Member> members;

            if (query.Parameters.All(x => String.IsNullOrWhiteSpace(x.Value)))
            {
                var rightLimit = query.ItemsPerPage * ++query.CurrentPage;
                var leftLimit = rightLimit - query.ItemsPerPage;
                members = uQuery.GetMembersByXPath("//*[position() >= " + leftLimit + " and position() < " + rightLimit + "]");

                if (HttpContext.Current.Cache.Get("membersCount") == null)
                {
                    membersCount = uQuery.GetMembersByXPath("//*").Count();
                    HttpContext.Current.Cache.Insert("membersCount", membersCount);
                }
                else
                {
                    membersCount = (int) HttpContext.Current.Cache.Get("membersCount");
                }
            }
            else
            {
                string xpathRequest = "//*[";
                if (!String.IsNullOrWhiteSpace(firstName))
                {
                    xpathRequest += "contains(memberFirstName, '" + firstName + "') and ";
                }
                if (!String.IsNullOrWhiteSpace(lastName))
                {
                    xpathRequest += "contains(memberLastName, '" + lastName + "') and ";
                }
                if (!String.IsNullOrWhiteSpace(emailAddress))
                {
                    xpathRequest += "contains(legacyEmail, '" + emailAddress + "') and ";
                }
                if (!String.IsNullOrWhiteSpace(ssn))
                {
                    xpathRequest += "contains(socSecNum, '" + emailAddress + "') and ";
                }
                if (!String.IsNullOrWhiteSpace(arbor) && arbor != "null")
                {
                    xpathRequest += "contains(arbors, '" + arbor + "') and ";
                }
                xpathRequest = xpathRequest.TrimEnd(" and ");
                xpathRequest += "]";
                members = uQuery.GetMembersByXPath(xpathRequest).Skip(query.CurrentPage*query.ItemsPerPage).Take(query.ItemsPerPage);
                membersCount = uQuery.GetMembersByXPath(xpathRequest).Count();
            }
            */
            var result = QueryResultDisplay.Build<MemberListItemDisplay, MemberListItemDisplay>(luceneMembers, query.CurrentPage,
                query.ItemsPerPage, membersCount);

            return result;
        }