private List <Data.Goal> SortList(List <Data.Goal> dbGoalList, SortableProperty sortColumn = null)
        {
            if (sortColumn == null)
            {
                dbGoalList = dbGoalList.OrderBy(t => t.Title).ToList();
            }
            else
            {
                switch (sortColumn.Name)
                {
                case "StatusId":
                    dbGoalList = dbGoalList.OrderBy(g => g.Status.Description).ToList();
                    break;

                case "CategoryId":
                    dbGoalList = dbGoalList.OrderBy(g => _appInfo.CategoryList.Where(c => c.CategoryID == g.CategoryID).FirstOrDefault().Description).ToList();
                    break;

                case "CreatedDate":
                    dbGoalList = dbGoalList.OrderBy(t => t.CreatedDate).ToList();
                    break;

                case "CompletedDate":
                    dbGoalList = dbGoalList.OrderBy(t => t.CompletedDate).ToList();
                    break;

                default:
                    dbGoalList = dbGoalList.OrderBy(t => t.Title).ToList();
                    break;
                }
            }

            return(dbGoalList);
        }
        /// <summary>
        /// Returns a shallow-copied list of all tasks in the repository.
        /// </summary>
        public List <Task> GetTasks(string filterTerm = "", SortableProperty sortColumn = null, int?pageNumber = null)
        {
            if (sortColumn == null)
            {
                sortColumn = new SortableProperty()
                {
                    Description = "Title", Name = "Title"
                }
            }
            ;

            QueryCacheItem   allTasksCacheItem = _appInfo.GlobalQueryCache.GetCacheItem(Constants.AllTasksCacheItem);
            List <Data.Task> allTasksList;

            // retrieve the query from cache if available
            // this will avoid retrieving all records when only a page is needed
            if (allTasksCacheItem == null)
            {
                // get the unordered, unfiltered list for caching
                IQueryable <Data.Task> allTasks = GetAllTasksQuery(filterTerm);
                allTasksList = GetOrderedList(allTasks, sortColumn);
                _appInfo.GlobalQueryCache.AddCacheItem(Constants.AllTasksCacheItem, allTasksList);
            }
            else
            {
                allTasksList = (List <Data.Task>)allTasksCacheItem.Value;
            }

            // now do the ordering and filtering
            if (!string.IsNullOrEmpty(filterTerm))
            {
                allTasksList = (from t in allTasksList
                                where t.Title.ToLower().Contains(filterTerm.ToLower())
                                select t).ToList();
            }

            if (sortColumn != null)
            {
                allTasksList = SortList(allTasksList, sortColumn);
            }

            if (pageNumber.HasValue)
            {
                allTasksList = allTasksList.Skip(Constants.RecordsPerPage * (pageNumber.Value - 1))
                               .Take(Constants.RecordsPerPage).ToList();
            }

            List <Task> tasks = new List <Task>();

            foreach (Data.Task dbTask in allTasksList)
            {
                tasks.Add(Task.CreateTask(dbTask));
            }

            return(tasks);
        }
        /// <summary>
        /// Returns a shallow-copied list of all goals in the repository.
        /// </summary>
        public List <Goal> GetGoals(string filterTerm = "", SortableProperty sortColumn = null, int?pageNumber = null)
        {
            QueryCacheItem   allGoalsCacheItem = _appInfo.GlobalQueryCache.GetCacheItem(Constants.AllGoalsCacheItem);
            List <Data.Goal> dbGoalsList;

            // retrieve the query from cache if available
            // this will avoid retrieving all records when only a page is needed
            if (allGoalsCacheItem == null)
            {
                // get the unordered, unfiltered list for caching
                IQueryable <Data.Goal> allGoals = GetAllGoalsQuery();
                dbGoalsList = GetOrderedList(allGoals);
                _appInfo.GlobalQueryCache.AddCacheItem(Constants.AllGoalsCacheItem, dbGoalsList);
            }
            else
            {
                dbGoalsList = (List <Data.Goal>)allGoalsCacheItem.Value;
            }

            // now do the ordering and filtering
            if (!string.IsNullOrEmpty(filterTerm))
            {
                dbGoalsList = (from g in dbGoalsList
                               where g.Title.ToLower().Contains(filterTerm.ToLower())
                               select g).ToList();
            }

            if (sortColumn != null)
            {
                dbGoalsList = SortList(dbGoalsList, sortColumn);
            }

            // do the paging
            if (pageNumber.HasValue)
            {
                dbGoalsList = dbGoalsList.Skip(Constants.RecordsPerPage * (pageNumber.Value - 1))
                              .Take(Constants.RecordsPerPage).ToList();
            }

            // create system goal objects from db goal objects
            List <Goal> goals = new List <Goal>();

            foreach (Data.Goal dbGoal in dbGoalsList)
            {
                goals.Add(Goal.CreateGoal(dbGoal));
            }

            return(goals);
        }
        /// <summary>
        /// Returns a shallow-copied list of all projects in the repository.
        /// </summary>
        public List <Project> GetProjects(string filterTerm = "", SortableProperty sortColumn = null, int?pageNumber = null)
        {
            QueryCacheItem      allProjectsCacheItem = _appInfo.GlobalQueryCache.GetCacheItem(Constants.AllProjectsCacheItem);
            List <Data.Project> allProjectsList;

            // retrieve the query from cache if available
            // this will avoid retrieving all records when only a page is needed
            if (allProjectsCacheItem == null)
            {
                // get the unordered, unfiltered list for caching
                IQueryable <Data.Project> allProjects = GetAllProjectsQuery();
                allProjectsList = GetOrderedList(allProjects);
                _appInfo.GlobalQueryCache.AddCacheItem(Constants.AllProjectsCacheItem, allProjectsList);
            }
            else
            {
                allProjectsList = (List <Data.Project>)allProjectsCacheItem.Value;
            }

            // now do the ordering and filtering
            if (!string.IsNullOrEmpty(filterTerm))
            {
                allProjectsList = (from p in allProjectsList
                                   where p.Title.ToLower().Contains(filterTerm.ToLower())
                                   select p).ToList();
            }

            if (sortColumn != null)
            {
                allProjectsList = SortList(allProjectsList, sortColumn);
            }

            if (pageNumber.HasValue)
            {
                allProjectsList = allProjectsList.Skip(Constants.RecordsPerPage * (pageNumber.Value - 1))
                                  .Take(Constants.RecordsPerPage).ToList();
            }

            List <Project> projects = new List <Project>();

            foreach (Data.Project dbProject in allProjectsList)
            {
                projects.Add(Project.CreateProject(dbProject));
            }

            return(projects);
        }
        private List <Data.Task> GetOrderedList(IQueryable <Data.Task> tasksQuery, SortableProperty sortColumn = null)
        {
            List <Data.Task> dbTaskList;

            if (sortColumn == null)
            {
                dbTaskList = tasksQuery.OrderBy(t => t.SortOrder).ToList();
            }
            else
            {
                switch (sortColumn.Name)
                {
                case "StatusId":
                    dbTaskList = tasksQuery.OrderBy(t => t.Status.Description).ToList();
                    break;

                case "PriorityId":
                    dbTaskList = tasksQuery.OrderByDescending(t => t.PriorityID).ToList();
                    break;

                case "ProjectTitle":
                    dbTaskList = tasksQuery.OrderBy(t => t.Projects.FirstOrDefault() == null ? "" : t.Projects.FirstOrDefault().Title).ToList();
                    break;

                case "CreatedDate":
                    dbTaskList = tasksQuery.OrderBy(t => t.CreatedDate).ToList();
                    break;

                case "CompletedDate":
                    dbTaskList = tasksQuery.OrderBy(t => t.CompletedDate).ToList();
                    break;

                case "Title":
                    dbTaskList = tasksQuery.OrderBy(t => t.Title).ToList();
                    break;

                default:
                    dbTaskList = tasksQuery.OrderBy(t => t.SortOrder).ToList();
                    break;
                }
            }

            return(dbTaskList);
        }
        private List <Data.Project> SortList(List <Data.Project> dbProjectList, SortableProperty sortColumn = null)
        {
            if (sortColumn == null)
            {
                dbProjectList = dbProjectList.OrderBy(t => t.Title).ToList();
            }
            else
            {
                switch (sortColumn.Name)
                {
                case "StatusId":
                    dbProjectList = dbProjectList.OrderBy(p => p.Status.Description).ToList();
                    break;

                case "EstimatedCost":
                    dbProjectList = dbProjectList.OrderBy(t => t.EstimatedCost).ToList();
                    break;

                case "GoalTitle":
                    dbProjectList = dbProjectList.OrderBy(t => t.Goals.FirstOrDefault() == null ? "" : t.Goals.FirstOrDefault().Title).ToList();
                    break;

                case "CreatedDate":
                    dbProjectList = dbProjectList.OrderBy(t => t.CreatedDate).ToList();
                    break;

                case "CompletedDate":
                    dbProjectList = dbProjectList.OrderBy(t => t.CompletedDate).ToList();
                    break;

                default:
                    dbProjectList = dbProjectList.OrderBy(t => t.Title).ToList();
                    break;
                }
            }

            return(dbProjectList);
        }