public IList<FullItemModel> Get(string sort, bool des, ListModel model)
        {
            //should be in the controller for checking usage.
            if (model.TaskId != null)
            {
                Task task = session.Load<Task>(model.TaskId);
                FullItemModel item = new FullItemModel();
                item.FilledBy(task);
                return new List<FullItemModel> { item };
            }

            IList<FullItemModel> models = new List<FullItemModel>();

            Project current = session.Load<Project>(model.CurrentProject.TailSelectedProject.Id);
            IList<int> allProjectIds = current.GetDescendantIds();

            var tasks = _querySource
                .Get(model, allProjectIds)
                .Sort(sort, des)
                .Paged(model.Pager);

            foreach (var task in tasks)
            {
                FullItemModel item = new FullItemModel();
                item.FilledBy(task);
                models.Add(item);
            }

            return models;
        }
        public ActionResult List(ListModel model, bool setShownColoumns = false)
        {
            if (setShownColoumns)
            {
                var showColumns = model.ShowColumns.Where(s => s.Selected).ToList();
                setShownColumnsIntoCookie(showColumns);

                return RedirectToAction("List");
            }

            int projectId = getProjectInList(model);
            ViewBag.SortType = getSortTypeInList();
            ViewBag.Direction = getDirectionInList();

            fillListModel(model, projectId, ViewBag.SortType, ViewBag.Direction);

            if (!ModelState.IsValid)
            {
                //TODO: useless since this is invoked
                //ModelState.Clear();
                return View(model);
            }

            //TODO: just a workaround
            ModelState.Clear();
            return View(model);
        }
        public void SetUp()
        {
            set_Projects_Hierarchy();
            set_Tasks_With_Project();

            model = new ListModel();
            model.TimeSpan = new _TimeSpanModel();
        }
        public int GetCount(ListModel model)
        {
            Project current = session.Load<Project>(model.CurrentProject.TailSelectedProject.Id);
            IList<int> allProjectIds = current.GetDescendantIds();

            return _querySource
                .Get(model, allProjectIds)
                .Count();
        }
        public ActionResult List(int? projectId, bool setShownColoumns = false)
        {
            if (setShownColoumns)
            {
                Request.Cookies[CookieKey.ShowColumn].Value = null;
            }

            ListModel model = new ListModel { SetShownColoumns = setShownColoumns };
            model.TimeSpan = new _TimeSpanModel();
            projectId = getProjectInList(model, projectId);

            fillListModel(model, projectId.Value, TaskList.Sort_By_Created);

            return View(model);
        }
        public _SumModel GetSum(ListModel model)
        {
            _SumModel sumModel = new _SumModel
            {
                Types = new Dictionary<string, int> 
                { 
                    { "虚", 2 }, 
                    { "实", 12 }
                },
                Priorities = new Dictionary<string, int>
                { 
                    { "高", 2 },
                    { "中", 12 }, 
                    { "低", 3 } 
                },
                Difficulties = new Dictionary<string, int> 
                { 
                    { "困难", 2 }, 
                    { "普通", 11 } 
                },
                ConsumeTime = new Dictionary<string, int> 
                { 
                    { "预计", 235 }, 
                    { "实际", 386 } 
                },
                Status = new Dictionary<string, int> 
                {
                    { "发布", 1 }, 
                    { "承接", 2 }, 
                    { "质疑", 1 },
                    { "完成", 3 },
                    { "合格", 8 }, 
                    { "不合格", 1 }
                },
                OverDue = new Dictionary<string, int>
                { 
                    { "条数", 3 }, 
                    { "次数", 7 }
                },
                Qualities = new Dictionary<string, int>
                { 
                    { "良好", 2 }, 
                    { "合格", 8 }, 
                    { "不合格", 1 } 
                },
                Doubt = new Dictionary<string, int>
                { 
                    { "条数", 3 }, 
                    { "次数", 9 },
                },
                RefuseAccept = new Dictionary<string, int>
                { 
                    { "良好", 2 }, 
                    { "合格", 8 }, 
                    { "不合格", 1 } 
                },
            };

            return sumModel;
        }
 public int GetCount(ListModel model)
 {
     return getAll().Where(t => t.Project.LiteItem.Id == model.CurrentProject.TailSelectedProject.Id).ToList().Count;
 }
        public IList<FullItemModel> Get(string sort, bool des, ListModel model)
        {
            //TODO: not sure which one should be check first, projectId or taskId
            var items = getAll().Where(t => t.Project.LiteItem.Id == model.CurrentProject.TailSelectedProject.Id);
            if (model.TaskId != null)
            {
                items = getAll().Where(t => t.LiteItem.Id == model.TaskId);
                return items.ToList();
            }
            if (model.GreaterOverDue != null)
            {
                items = items.Where(t => t.OverDue > model.GreaterOverDue);
            }
            if (model.LessOverDue != null)
            {
                items = items.Where(t => t.OverDue < model.LessOverDue);
            }
            if (model.GreaterWorkPeriod != null)
            {
                items = items.Where(t => t.ActualWorkPeriod > model.GreaterWorkPeriod);
            }
            if (model.LessWorkPeriod != null)
            {
                items = items.Where(t => t.ActualWorkPeriod < model.LessWorkPeriod);
            }
            //TODO: ExpectedComplete is not DateTime
            //if (model.FromExpectComplete != null)
            //{
            //    items = items.Where(t => t.ExpectedComplete > model.FromExpectComplete);
            //}
            //if (model.ToExpectComplete != null)
            //{
            //    items = items.Where(t => t.ExpectedComplete < model.ToExpectComplete);
            //}
            if (model.TimeSpan.FromPublish != null)
            {
                items = items.Where(t => t.Created > model.TimeSpan.FromPublish);
            }
            if (model.TimeSpan.ToPublish != null)
            {
                items = items.Where(t => t.Created < model.TimeSpan.ToPublish);
            }
            if (model.TimeSpan.FromAssign != null)
            {
                items = items.Where(t => t.Assign > model.TimeSpan.FromAssign);
            }
            if (model.TimeSpan.ToAssign != null)
            {
                items = items.Where(t => t.Assign > model.TimeSpan.ToAssign);
            }
            if (model.TimeSpan.FromOwn != null)
            {
                items = items.Where(t => t.Own > model.TimeSpan.FromOwn);
            }
            if (model.TimeSpan.ToOwn != null)
            {
                items = items.Where(t => t.Own > model.TimeSpan.ToOwn);
            }
            //if (model.FromExpectComplete != null)
            //{
            //    items = items.Where(t => t.ExpectedComplete > model.FromExpectComplete);
            //}
            //if (model.ToExpectComplete != null)
            //{
            //    items = items.Where(t => t.ExpectedComplete > model.ToExpectComplete);
            //}
            if (model.TimeSpan.FromActualComplete != null)
            {
                items = items.Where(t => t.ActualComplete > model.TimeSpan.FromActualComplete);
            }
            if (model.TimeSpan.ToActualComplete != null)
            {
                items = items.Where(t => t.ActualComplete < model.TimeSpan.ToActualComplete);
            }
            if (model.TimeSpan.FromLastestUpdateTime != null)
            {
                items = items.Where(t => t.LatestUpdate > model.TimeSpan.FromLastestUpdateTime);
            }
            if (model.TimeSpan.ToLastestUpdateTime != null)
            {
                items = items.Where(t => t.LatestUpdate > model.TimeSpan.ToLastestUpdateTime);
            }

            if (model.SelectedDifficulty != null)
            {
                items = items.Where(t => t.Difficulty != null &&
                    t.Difficulty == (TaskDifficulty)model.SelectedDifficulty);
            }
            if (model.SelectedOwnerId != null)
            {
                items = items.Where(t => t.Owner != null &&
                    t.Owner.Id == model.SelectedOwnerId.Value);
            }
            if (model.SelectedPublisherId != null)
            {
                items = items.Where(t => t.Publisher != null &&
                    t.Publisher.Id == model.SelectedPublisherId.Value);
            }
            if (model.SelectedStage != null)
            {
                items = items.Where(t => t.LiteItem.CurrentStatus != null &&
                    t.LiteItem.CurrentStatus.Stage == model.SelectedStage.Value);
            }
            if (model.SelectedQuality != null)
            {
                items = items.Where(t => t.Quality != null &&
                    t.Quality == (TaskQuality)model.SelectedQuality);
            }

            items = items.Sort(sort, des);

            return Paged(items, model.Pager)
                .ToList();
        }
 //TODO: jsut a walkaround, need refactor: see http://task.zyfei.net/Task/Edit/2386
 public static IQueryable<Task> Get(this IQueryable<Task> source, ListModel model)
 {
     return source.Get(model, new List<int> { model.CurrentProject.TailSelectedProject.Id });
 }
        //TODO: should make use of InFilter()
        public static IQueryable<Task> Get(this IQueryable<Task> source, ListModel model, IList<int> projectIds)
        {
            IQueryable<Task> items = source.GetInProject(projectIds);

            if (model.GreaterOverDue.HasValue)
            {
                items = items.Where(t => t.OverDue > model.GreaterOverDue);
            }
            if (model.LessOverDue.HasValue)
            {
                items = items.Where(t => t.OverDue < model.LessOverDue);
            }
            if (model.GreaterWorkPeriod.HasValue)
            {
                items = items.Where(t => t.ActualWorkPeriod > model.GreaterWorkPeriod);
            }
            if (model.LessWorkPeriod.HasValue)
            {
                items = items.Where(t => t.ActualWorkPeriod < model.LessWorkPeriod);
            }

            #region TimeSpan related, should be abstracted

            if (model.TimeSpan.FromPublish.HasValue)
            {
                items = items.Where(t => t.CreateTime > model.TimeSpan.FromPublish);
            }
            if (model.TimeSpan.ToPublish.HasValue)
            {
                items = items.Where(t => t.CreateTime < model.TimeSpan.ToPublish);
            }
            if (model.TimeSpan.FromAssign.HasValue)
            {
                items = items.Where(t => t.AssignTime > model.TimeSpan.FromAssign);
            }
            if (model.TimeSpan.ToAssign.HasValue)
            {
                items = items.Where(t => t.AssignTime < model.TimeSpan.ToAssign);
            }
            if (model.TimeSpan.FromOwn.HasValue)
            {
                items = items.Where(t => t.OwnTime > model.TimeSpan.FromOwn);
            }
            if (model.TimeSpan.ToOwn.HasValue)
            {
                items = items.Where(t => t.OwnTime < model.TimeSpan.ToOwn);
            }
            if (model.TimeSpan.FromExpectComplete.HasValue)
            {
                items = items.Where(t => t.ExpectCompleteTime > model.TimeSpan.FromExpectComplete);
            }
            if (model.TimeSpan.ToExpectComplete.HasValue)
            {
                items = items.Where(t => t.ExpectCompleteTime < model.TimeSpan.ToExpectComplete);
            }
            if (model.TimeSpan.FromActualComplete.HasValue)
            {
                items = items.Where(t => t.ActualCompleteTime > model.TimeSpan.FromActualComplete);
            }
            if (model.TimeSpan.ToActualComplete.HasValue)
            {
                items = items.Where(t => t.ActualCompleteTime < model.TimeSpan.ToActualComplete);
            }
            if (model.TimeSpan.FromLastestUpdateTime.HasValue)
            {
                items = items.Where(t => t.LatestUpdateTime > model.TimeSpan.FromLastestUpdateTime);
            }
            if (model.TimeSpan.ToLastestUpdateTime.HasValue)
            {
                items = items.Where(t => t.LatestUpdateTime < model.TimeSpan.ToLastestUpdateTime);
            }

            #endregion

            if (model.SelectedPriority.HasValue)
            {
                items = items.Where(t => t.Priority == (TaskPriority)model.SelectedPriority);
            }
            if (model.SelectedDifficulty.HasValue)
            {
                items = items.Where(t => t.Difficulty == model.SelectedDifficulty);
            }
            if (model.SelectedOwnerId.HasValue)
            {
                items = items.Where(t => t.Owner.Id == model.SelectedOwnerId);
            }
            if (model.SelectedAccepterId.HasValue)
            {
                items = items.Where(t => t.Accepter.Id == model.SelectedAccepterId);
            }
            if (model.SelectedPublisherId.HasValue)
            {
                items = items.Where(t => t.Publisher.Id == model.SelectedPublisherId);
            }
            if (model.SelectedStages != null)
            {
                items = items.Where(t => model.SelectedStages.Contains((int)t.CurrentStatus));
            }
            else
            {
                if (model.SelectedStage.HasValue)
                {
                    items = items.Where(t => t.CurrentStatus == (Status)model.SelectedStage);
                }
            }
            if (model.SelectedQuality.HasValue)
            {
                items = items.Where(t => t.Quality == model.SelectedQuality);
            }
            if (model.SelectedNodeType.HasValue)
            {
                if (model.SelectedNodeType == NodeType.Leaf)
                {
                    items = items.Where(t => t.Parent != null && t.Children.Count == 0);
                }
                else if (model.SelectedNodeType == NodeType.Branch)
                {
                    items = items.Where(t => t.Parent != null && t.Children.Count > 0);
                }
                else if (model.SelectedNodeType == NodeType.Root)
                {
                    items = items.Where(t => t.Parent == null);
                }
            }

            return items;
        }
        private int getProjectInList(ListModel model)
        {
            int projectId = model.CurrentProject.TailSelectedProject.Id;
            model.CurrentProject = _projectService.GetDropdownlistLink(userHelper.CurrentUserId.Value, projectId);

            return projectId;
        }
        private int? getProjectInList(ListModel model, int? projectId)
        {
            if (projectId == null)
            {
                model.CurrentProject = _projectService.GetDropdownlistLink(userHelper.CurrentUserId.Value);
                projectId = model.CurrentProject.TailSelectedProject.Id;
            }
            else
            {
                model.CurrentProject = _projectService.GetDropdownlistLink(userHelper.CurrentUserId.Value, projectId.Value);
            }

            return projectId;
        }
        public _SumModel GetSum(ListModel model)
        {
            _SumModel sumModel = new _SumModel();

            Project current = session.Load<Project>(model.CurrentProject.TailSelectedProject.Id);
            IList<int> allProjectIds = current.GetDescendantIds();
            var allTasks = _querySource
                .Get(model, allProjectIds);

            sumModel.Types = new Dictionary<string, int>();
            sumModel.Types.Add("虚", allTasks.Count(t => t.IsVirtual));
            sumModel.Types.Add("实", allTasks.Count(t => !t.IsVirtual));

            sumModel.Publish = allTasks.GroupBy(t => t.Publisher).ToDictionary(t => t.Key.Name, t => t.Count());
            sumModel.Own = allTasks.GroupBy(t => t.Owner).ToDictionary(t => getName(t.Key), t => t.Count());
            sumModel.Accept = allTasks.GroupBy(t => t.Accepter).ToDictionary(t => t.Key.Name, t => t.Count());

            sumModel.Priorities = allTasks.GroupBy(t => t.Priority).ToDictionary(t => getDescription(t.Key), t => t.Count());
            sumModel.Difficulties = allTasks.GroupBy(t => t.Difficulty).ToDictionary(t => getDescription(t.Key), t => t.Count());

            sumModel.ConsumeTime = new Dictionary<string, int>();
            sumModel.ConsumeTime.Add("预计", allTasks.Sum(t => t.ExpectWorkPeriod) ?? 0);
            sumModel.ConsumeTime.Add("实际", allTasks.Sum(t => t.ActualWorkPeriod) ?? 0);

            sumModel.Status = allTasks.GroupBy(t => t.CurrentStatus).ToDictionary(t => t.Key.GetEnumDescription(), t => t.Count());

            sumModel.OverDue = new Dictionary<string, int>();
            sumModel.OverDue.Add("条数", allTasks.Where(t => t.OverDue > 0).Count());
            sumModel.OverDue.Add("时间", allTasks.Sum(t => t.OverDue) ?? 0);

            sumModel.Qualities = allTasks.GroupBy(t => t.Quality).ToDictionary(t => getDescription(t.Key), t => t.Count());

            sumModel.Doubt = new Dictionary<string, int>();
            sumModel.Doubt.Add("条数", allTasks.Count(t => t.Histroy.Count(h => h.Status == Status.Doubt) > 0));
            sumModel.Doubt.Add("次数", _historyItemQuery.Get(allTasks, Status.Doubt).Count());

            sumModel.RefuseAccept = new Dictionary<string, int>();
            sumModel.RefuseAccept.Add("条数", allTasks.Count(t => t.Histroy.Count(h => h.Status == Status.RefuseAccept) > 0));
            sumModel.RefuseAccept.Add("次数", _historyItemQuery.Get(allTasks, Status.RefuseAccept).Count());

            return sumModel;
        }
 public ActionResult _Sum(ListModel model)
 {
     //get selected many status 
     setStatusWithCookie(model);
     _SumModel result = _taskService.GetSum(model);
     return PartialView("~/Views/Task/List/_Sum.cshtml", result);
 }
 private void getShownColumnsFromCookie(ListModel model)
 {
     if (Request.Cookies[CookieKey.ShowColumn] != null &&
         !string.IsNullOrEmpty(Request.Cookies[CookieKey.ShowColumn].Value))
     {
         string strShowColumns = Request.Cookies[CookieKey.ShowColumn].Value;
         model.ShowColumns = JsonConvert.DeserializeObject<IList<SelectListItem>>(strShowColumns);
     }
     else
     {
         model.ShowColumns = typeof(ListColumn).GetSelectListItems().ToList();
         model.ShowColumns.ToList().ForEach(s => s.Selected = true);
     }
 }
        private void setStatusWithCookie(ListModel model)
        {
            if (Request.Cookies[CookieKey.PreferStatus] != null)
            {
                string PreferStatus = Server.UrlDecode(Request.Cookies[CookieKey.PreferStatus].Value);
                string[] statuses = PreferStatus.Split(",".ToCharArray());

                //there will always be one empty value after splitting, so need statuses.Length - 1
                //e.g: [1, 23,] => [1], [23], [""]
                for (int i = 0; i < statuses.Length - 1; i++)
                {
                    model.SelectedStages = model.SelectedStages ?? new List<int>();
                    model.SelectedStages.Add(int.Parse(statuses[i]));
                }
            }
        }
        private void fillListModel(ListModel model, int projectId, string sort, bool des = true)
        {
            getShownColumnsFromCookie(model);
            setStatusWithCookie(model);
            model.CanOwn = _authService.GetTokens(userHelper.CurrentUserId.Value, projectId)
                .Contains(Token.Owner);

            model.AllPriorities = _projectConfigService.GetPriorities(projectId);
            model.AllDifficulties = _projectConfigService.GetDifficulties(projectId);
            model.AllQualities = _projectConfigService.GetQualities(projectId);
            model.AllStatus = _projectConfigService.GetStatus(projectId);

            model.Owners = _userService.GetAllOwners(projectId);
            model.Accepters = _userService.GetAllAccepters(projectId);
            model.Publishers = _userService.GetAllPublishers(projectId);

            model.NodeTypes = getNodeTypes();

            int sumOfItems = _taskService.GetCount(model);
            int pageSize = 20;
            int rowSize = 20;
            model.PageIndex = model.PageIndex ?? 1;

            //to avoid customer provide bigger page index than the actual have
            if ((model.PageIndex - 1) * pageSize >= sumOfItems)
            {
                model.PageIndex = sumOfItems / (pageSize + 1) + 1;
            }

            model.Pager = new PagerModel { SumOfItems = sumOfItems, PageIndex = model.PageIndex.Value, PageSize = pageSize, RowSize = rowSize };

            model.Items = _taskService.Get(sort, des, model);
        }