protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(ListName))
            {
                throw new ApplicationException("List Name was not supplied to ListViewSettings user control");
            }

            listSettings = userSettings.GetSettingsForList(ListName);
        }
        /// <summary>
        /// Applies the sorts from a specific user's list view settings.
        /// </summary>
        /// <param name="ticketQuery">The ticket query.</param>
        /// <param name="listViewSettings">The list view settings to sort by.</param>
        /// <returns></returns>
        private static IQueryable<Ticket> ApplySorts(this IQueryable<Ticket> ticketQuery, ListViewSettings listViewSettings)
        {
            IQueryable<Ticket> newQuery = ticketQuery;

            //sorts have to be applied in reverse order as the last applied is the first in the SQL query generated
            for (int x = listViewSettings.SortColumns.Count() - 1; x >= 0; x--)
            {
                ListViewSortColumn column = listViewSettings.SortColumns[x];
                bool isDescending = (column.SortDirection == ColumnSortDirection.Descending);
                newQuery = newQuery.ApplySort(column.ColumnName, isDescending);
            }
            return newQuery;
        }
        protected void Page_Load()
        {
            userSettings = ListViewSettingsCollection.GetSettingsForUser();

            string listName = Page.Request.QueryString["list"];
            if (string.IsNullOrEmpty(listName))
            {
                int minOrder = userSettings.Settings.Min(us => us.ListViewDisplayOrder);
                listSetting = userSettings.Settings.SingleOrDefault(us => us.ListViewDisplayOrder == minOrder);
                listName = listSetting.ListViewName;
            }
            listSetting = userSettings.Settings.SingleOrDefault(us => us.ListViewName == listName);
            Page.Title = string.Format("Система заявок: {0}", listSetting.ListViewDisplayName);
            ListViewSettingsEditorControl.ListName = listName;
            ListViewControl.ListName = listName;
        }
        /// <summary>
        /// Applies the filters, sorts, and pages for a specific user's list view settings.
        /// </summary>
        /// <param name="ticketQuery">The ticket query.</param>
        /// <param name="listViewSettings">The list view settings containing the filters and sorts to apply.</param>
        /// <returns></returns>
        public static IQueryable<Ticket> ApplyListViewSettings(this IQueryable<Ticket> ticketQuery, ListViewSettings listViewSettings, int startRowIndex)
        {
            IQueryable<Ticket> newQuery = ticketQuery;

            //TODO: Need to modify list search to store the where clause in list settings instead of managing the query directly
            if (listViewSettings.ListViewName != "search")
            {
                newQuery = newQuery.ApplyFilters(listViewSettings);
            }
            if (newQuery != null)
            {
                newQuery = newQuery.ApplySorts(listViewSettings);
            }
            else
            {
                newQuery = ticketQuery.ApplySorts(listViewSettings);
            }

            return newQuery;
        }
        /// <summary>
        /// Gets the settings for a particular list view.
        /// </summary>
        /// <param name="listName">Name of the list view whose settings you wish to retrieve.</param>
        /// <returns></returns>
        public ListViewSettings GetSettingsForList(string listName)
        {
            ListViewSettings setting = null;

            foreach (ListViewSettings s in Settings)
            {
                if (s.ListViewName == listName)
                {
                    setting = s;
                    break;
                }
            }

            if (setting == null)
            {
                var m = this.Settings.Max(st => st.ListViewDisplayOrder);
                setting = new ListViewSettings(listName, listName, m + 1, false);
                _settings.Add(setting);//adds this setting to the collection for future use
            }
            return(setting);
        }
 /// <summary>
 /// Applies the filters for a specific user's list view settings.
 /// </summary>
 /// <param name="ticketQuery">The ticket query.</param>
 /// <param name="listViewSettings">The list view settings to filder by.</param>
 /// <returns></returns>
 private static IQueryable<Ticket> ApplyFilters(this IQueryable<Ticket> ticketQuery, ListViewSettings listViewSettings)
 {
     IQueryable<Ticket> newQuery = ticketQuery;
     foreach (ListViewFilterColumn column in listViewSettings.FilterColumns)
     {
         newQuery = newQuery.ApplyFilter(column.ColumnName, column.ColumnValue, column.EqualityComparison);
     }
     return newQuery;
 }
        /// <summary>
        /// Creates a new [empty] settings instance.
        /// </summary>
        /// <returns></returns>
        public static ListViewSettingsCollection CreateNewSettings(ListViewSettingsCollection settingsCollection)
        {
            string user = HttpContext.Current.User.Identity.GetFormattedUserName();
           

            List<string> disableStatusColumn = new List<string>();
            disableStatusColumn.Add("CurrentStatus");

            List<string> disableAssignedColumn = new List<string>();
            disableAssignedColumn.Add("AssignedTo");

            List<string> disableOwnerColumn = new List<string>();
            disableOwnerColumn.Add("Owner");


            if (SecurityManager.IsStaff)
            {
                //unassigned
                List<ListViewSortColumn> unassignedSortColumns = new List<ListViewSortColumn>();
                List<ListViewFilterColumn> unassignedFilterColumns = new List<ListViewFilterColumn>();
                unassignedSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
                unassignedFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
                unassignedFilterColumns.Add(new ListViewFilterColumn("AssignedTo", null, null));
                ListViewSettings unassigned = new ListViewSettings("unassigned", "Unassigned Tickets", 0, 20, unassignedSortColumns, unassignedFilterColumns, disableAssignedColumn);
                settingsCollection.Settings.Add(unassigned);

                //assigned to me
                List<ListViewSortColumn> assignedtomeSortColumns = new List<ListViewSortColumn>();
                List<ListViewFilterColumn> assignedtomeFilterColumns = new List<ListViewFilterColumn>();
                assignedtomeSortColumns.Add(new ListViewSortColumn("CurrentStatus", ColumnSortDirection.Ascending));
                assignedtomeSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
                assignedtomeFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
                assignedtomeFilterColumns.Add(new ListViewFilterColumn("AssignedTo", true, user));
                ListViewSettings assignedtome = new ListViewSettings("assignedtome", "Tickets Assigned To Me", 1, 20, assignedtomeSortColumns, assignedtomeFilterColumns, disableAssignedColumn);
                settingsCollection.Settings.Add(assignedtome);
            }

            int disOrder = (SecurityManager.IsStaff) ? 2 : 0; //if staff display order starts at 2 for the remaining built-in lists, otherwise starts at 0
            //my tickets
            List<ListViewSortColumn> myticketsSortColumns = new List<ListViewSortColumn>();
            List<ListViewFilterColumn> myticketsFilterColumns = new List<ListViewFilterColumn>();
            myticketsSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            myticketsFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
            myticketsFilterColumns.Add(new ListViewFilterColumn("Owner", true, user));
            ListViewSettings mytickets = new ListViewSettings("mytickets", "All My Tickets", disOrder, 20, myticketsSortColumns, myticketsFilterColumns, disableOwnerColumn);
            settingsCollection.Settings.Add(mytickets);


            //open tickets
            List<ListViewSortColumn> openSortColumns = new List<ListViewSortColumn>();
            List<ListViewFilterColumn> openFilterColumns = new List<ListViewFilterColumn>();
            openSortColumns.Add(new ListViewSortColumn("CurrentStatus", ColumnSortDirection.Ascending));
            openSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            openFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
            ListViewSettings opentickets = new ListViewSettings("opentickets", "All Open Tickets", disOrder + 1, 20, openSortColumns, openFilterColumns, disableStatusColumn);
            settingsCollection.Settings.Add(opentickets);


            //history
            List<ListViewSortColumn> historyticketsSortColumns = new List<ListViewSortColumn>();
            List<ListViewFilterColumn> historyticketsFilterColumns = new List<ListViewFilterColumn>();
            historyticketsSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            historyticketsFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", true, "closed"));
            historyticketsFilterColumns.Add(new ListViewFilterColumn("Owner", true, user));

            if (SecurityManager.IsStaff)
            {
                historyticketsFilterColumns.Add(new ListViewFilterColumn("AssignedTo", true, user));

            }
            ListViewSettings historytickets = new ListViewSettings("historytickets", "Ticket History", disOrder + 2, 20, historyticketsSortColumns, historyticketsFilterColumns, disableStatusColumn);
            settingsCollection.Settings.Add(historytickets);
            
            return settingsCollection;
        }
        /// <summary>
        /// Gets the settings for a particular list view.
        /// </summary>
        /// <param name="listName">Name of the list view whose settings you wish to retrieve.</param>
        /// <returns></returns>
        public ListViewSettings GetSettingsForList(string listName)
        {
            ListViewSettings setting = null;
            foreach (ListViewSettings s in Settings)
            {
                if (s.ListViewName == listName)
                {
                    setting = s;
                    break;
                }
            }

            if (setting == null)
            {
                var m = this.Settings.Max(st => st.ListViewDisplayOrder);
                setting = new ListViewSettings(listName, listName, m + 1, false);
                _settings.Add(setting);//adds this setting to the collection for future use
            }
            return setting;
        }
        /// <summary>
        /// Creates a new [empty] settings instance.
        /// </summary>
        /// <returns></returns>
        public static ListViewSettingsCollection CreateNewSettings(ListViewSettingsCollection settingsCollection)
        {
            string user = HttpContext.Current.User.Identity.GetFormattedUserName();


            List <string> disableStatusColumn = new List <string>();

            disableStatusColumn.Add("CurrentStatus");

            List <string> disableAssignedColumn = new List <string>();

            disableAssignedColumn.Add("AssignedTo");

            List <string> disableOwnerColumn = new List <string>();

            disableOwnerColumn.Add("Owner");


            if (SecurityManager.IsStaff)
            {
                //unassigned
                List <ListViewSortColumn>   unassignedSortColumns   = new List <ListViewSortColumn>();
                List <ListViewFilterColumn> unassignedFilterColumns = new List <ListViewFilterColumn>();
                unassignedSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
                unassignedFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
                unassignedFilterColumns.Add(new ListViewFilterColumn("AssignedTo", null, null));
                ListViewSettings unassigned = new ListViewSettings("unassigned", "Unassigned Tickets", 0, 20, unassignedSortColumns, unassignedFilterColumns, disableAssignedColumn);
                settingsCollection.Settings.Add(unassigned);

                //assigned to me
                List <ListViewSortColumn>   assignedtomeSortColumns   = new List <ListViewSortColumn>();
                List <ListViewFilterColumn> assignedtomeFilterColumns = new List <ListViewFilterColumn>();
                assignedtomeSortColumns.Add(new ListViewSortColumn("CurrentStatus", ColumnSortDirection.Ascending));
                assignedtomeSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
                assignedtomeFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
                assignedtomeFilterColumns.Add(new ListViewFilterColumn("AssignedTo", true, user));
                ListViewSettings assignedtome = new ListViewSettings("assignedtome", "Tickets Assigned To Me", 1, 20, assignedtomeSortColumns, assignedtomeFilterColumns, disableAssignedColumn);
                settingsCollection.Settings.Add(assignedtome);
            }

            int disOrder = (SecurityManager.IsStaff) ? 2 : 0; //if staff display order starts at 2 for the remaining built-in lists, otherwise starts at 0
            //my tickets
            List <ListViewSortColumn>   myticketsSortColumns   = new List <ListViewSortColumn>();
            List <ListViewFilterColumn> myticketsFilterColumns = new List <ListViewFilterColumn>();

            myticketsSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            myticketsFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
            myticketsFilterColumns.Add(new ListViewFilterColumn("Owner", true, user));
            ListViewSettings mytickets = new ListViewSettings("mytickets", "All My Tickets", disOrder, 20, myticketsSortColumns, myticketsFilterColumns, disableOwnerColumn);

            settingsCollection.Settings.Add(mytickets);


            //open tickets
            List <ListViewSortColumn>   openSortColumns   = new List <ListViewSortColumn>();
            List <ListViewFilterColumn> openFilterColumns = new List <ListViewFilterColumn>();

            openSortColumns.Add(new ListViewSortColumn("CurrentStatus", ColumnSortDirection.Ascending));
            openSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            openFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", false, "closed"));
            ListViewSettings opentickets = new ListViewSettings("opentickets", "All Open Tickets", disOrder + 1, 20, openSortColumns, openFilterColumns, disableStatusColumn);

            settingsCollection.Settings.Add(opentickets);


            //history
            List <ListViewSortColumn>   historyticketsSortColumns   = new List <ListViewSortColumn>();
            List <ListViewFilterColumn> historyticketsFilterColumns = new List <ListViewFilterColumn>();

            historyticketsSortColumns.Add(new ListViewSortColumn("LastUpdateDate", ColumnSortDirection.Descending));
            historyticketsFilterColumns.Add(new ListViewFilterColumn("CurrentStatus", true, "closed"));
            historyticketsFilterColumns.Add(new ListViewFilterColumn("Owner", true, user));

            if (SecurityManager.IsStaff)
            {
                historyticketsFilterColumns.Add(new ListViewFilterColumn("AssignedTo", true, user));
            }
            ListViewSettings historytickets = new ListViewSettings("historytickets", "Ticket History", disOrder + 2, 20, historyticketsSortColumns, historyticketsFilterColumns, disableStatusColumn);

            settingsCollection.Settings.Add(historytickets);

            return(settingsCollection);
        }