private void BindGridView(bool clearAllFilters = false) { using (var aryaDc = new AryaDbDataContext()) { var currUser = (from u in aryaDc.Users where u.EmailAddress == Email select u).FirstOrDefault(); if (currUser == null) { FormsAuthentication.RedirectToLoginPage(); return; } using (var taskDc = new AryaServicesDbDataContext()) { var tasks = Enumerable.Empty <AryaTask>().AsQueryable(); if (currUser.IsAdmin) { tasks = taskDc.AryaTasks; } else { //Import Admin Tasks tasks = tasks.Union(GetTasks(aryaDc, taskDc, currUser, Group.ImportAdminGroup, new[] { typeof(ImportWorker).Name }, false)); //Import User Tasks tasks = tasks.Union(GetTasks(aryaDc, taskDc, currUser, Group.ImportUserGroup, new[] { typeof(ImportWorker).Name }, true)); var exportJobTypes = ExportWorkerBase.GetExports().Select(e => e.GetType().Name).ToList(); //Export Admin Tasks tasks = tasks.Union(GetTasks(aryaDc, taskDc, currUser, Group.ExportAdminGroup, exportJobTypes.Union(typeof(CustomExportWorker).Name), false)); //Standard Export User Tasks tasks = tasks.Union(GetTasks(aryaDc, taskDc, currUser, Group.StandardExportUserGroup, exportJobTypes, true)); //Custom Export User Tasks tasks = tasks.Union(GetTasks(aryaDc, taskDc, currUser, Group.CustomExportUserGroup, new[] { typeof(CustomExportWorker).Name }, true)); } if (!clearAllFilters) { Guid selectedProject, selectedUser; if (Guid.TryParse(DdlProjects.SelectedValue, out selectedProject)) { tasks = tasks.Where(task => task.ProjectID == selectedProject); } if (Guid.TryParse(DdlSubmittedBy.SelectedValue, out selectedUser)) { tasks = tasks.Where(task => task.SubmittedBy == selectedUser); } var selectedJobType = DdlJobType.SelectedValue; if (!string.IsNullOrWhiteSpace(selectedJobType) && selectedJobType != "ALL") { tasks = tasks.Where(task => task.JobType == DdlJobType.SelectedValue); } var selectedStatus = DdlStatus.SelectedValue; if (!string.IsNullOrWhiteSpace(selectedStatus) && selectedStatus != "ALL") { tasks = tasks.Where(task => task.Status == DdlStatus.SelectedValue); } } var allTasks = tasks.OrderByDescending(task => task.SubmittedOn).Take(100).ToList(); var distinctUserProjects = allTasks.Select(t => t.SubmittedBy + "|" + t.ProjectID).Distinct().ToList(); var userProjects = (from up in aryaDc.UserProjects //from task in tasks.ToList() //where task.SubmittedBy == up.UserID && task.ProjectID == up.ProjectID where distinctUserProjects.Contains(up.UserID + "|" + up.ProjectID) select new { up.User, up.Project }).ToList(); var visibleTasks = from task in allTasks from up in userProjects where up.Project.ID == task.ProjectID && up.User.ID == task.SubmittedBy let project = up.Project let user = up.User let state = WorkerBase.GetFriendlyWorkerState( (WorkerState)Enum.Parse(typeof(WorkerState), task.Status)) orderby task.SubmittedOn descending select new { task.ID, task.ProjectID, ProjectName = project.DisplayName, Type = task.DisplayJobType, task.JobType, task.Description, task.Status, FriendlyStatus = state, user.FullName, task.SubmittedBy, task.SubmittedOn }; if (sortExpression != string.Empty) { visibleTasks = LinqExtensions.ApplyOrder(visibleTasks, sortExpression, sortOrder == "asc" ? "OrderBy" : "OrderByDescending"); } var visibleTaskList = visibleTasks.Distinct().ToList(); GvStatus.DataSource = visibleTaskList; GvStatus.DataBind(); if (visibleTaskList.Count == 0) { return; } if (!Page.IsPostBack) { var projects = userProjects.Select( up => new DropDownListItem { Text = up.Project.DisplayName, Value = up.Project.ID.ToString() }) .Distinct(new KeyEqualityComparer <DropDownListItem>(item => item.Value)) .OrderBy(item => item.Text) .ToList(); Session["projects"] = projects; var users = userProjects.Select( up => new DropDownListItem { Text = up.User.FullName, Value = up.User.ID.ToString() }) .Distinct(new KeyEqualityComparer <DropDownListItem>(item => item.Value)) .OrderBy(item => item.Text) .ToList(); Session["users"] = users; var jobTypes = allTasks.Select( task => new DropDownListItem { Text = task.DisplayJobType, Value = task.JobType }) .Distinct(new KeyEqualityComparer <DropDownListItem>(item => item.Value)) .OrderBy(item => item.Text) .ToList(); Session["jobTypes"] = jobTypes; var statuses = allTasks.Select(task => new DropDownListItem { Text = task.Status, Value = task.Status }) .Distinct(new KeyEqualityComparer <DropDownListItem>(item => item.Value)) .OrderBy(item => item.Text) .ToList(); Session["statuses"] = statuses; } var prjs = Session["projects"] as List <DropDownListItem> ?? new List <DropDownListItem>(); var visibleProjects = prjs.Where(prj => visibleTaskList.Any(task => task.ProjectID.ToString() == prj.Value)).ToList(); visibleProjects.Insert(visibleProjects.Count > 1 ? 0 : 1, new DropDownListItem { Text = "ALL", Value = "ALL" }); visibleProjects.Add(new DropDownListItem { Text = "-----", Value = "ALL" }); visibleProjects.AddRange(prjs.Where(prj => !visibleProjects.Contains(prj))); DdlProjects.DataSource = visibleProjects; DdlProjects.DataBind(); var usrs = Session["users"] as List <DropDownListItem> ?? new List <DropDownListItem>(); var visibleusers = usrs.Where(usr => visibleTaskList.Any(task => task.SubmittedBy.ToString() == usr.Value)) .ToList(); visibleusers.Insert(visibleusers.Count > 1 ? 0 : 1, new DropDownListItem { Text = "ALL", Value = "ALL" }); visibleusers.Add(new DropDownListItem { Text = "-----", Value = "ALL" }); visibleusers.AddRange(usrs.Where(usr => !visibleusers.Contains(usr))); DdlSubmittedBy.DataSource = visibleusers; DdlSubmittedBy.DataBind(); var jbts = Session["jobTypes"] as List <DropDownListItem> ?? new List <DropDownListItem>(); var visiblejobTypes = jbts.Where(jbt => visibleTaskList.Any(task => task.JobType == jbt.Value)).ToList(); visiblejobTypes.Insert(visiblejobTypes.Count > 1 ? 0 : 1, new DropDownListItem { Text = "ALL", Value = "ALL" }); visiblejobTypes.Add(new DropDownListItem { Text = "-----", Value = "ALL" }); visiblejobTypes.AddRange(jbts.Where(jbt => !visiblejobTypes.Contains(jbt))); DdlJobType.DataSource = visiblejobTypes; DdlJobType.DataBind(); var stus = Session["statuses"] as List <DropDownListItem> ?? new List <DropDownListItem>(); var visiblestatuses = stus.Where(stu => visibleTaskList.Any(task => task.Status == stu.Value)).ToList(); visiblestatuses.Insert(visiblestatuses.Count > 1 ? 0 : 1, new DropDownListItem { Text = "ALL", Value = "ALL" }); visiblestatuses.Add(new DropDownListItem { Text = "-----", Value = "ALL" }); visiblestatuses.AddRange(stus.Where(stu => !visiblestatuses.Contains(stu))); DdlStatus.DataSource = visiblestatuses; DdlStatus.DataBind(); } } }
private void FetchExportTypes() { ddlExportType.Items.Clear(); ddlExportType.Items.Add(new ListItem("---Select an Export Type---", "---Select an Export Type---")); Guid selectedProjectId; if (!Guid.TryParse(ddlModel.SelectedValue, out selectedProjectId)) { return; } using (var dc = new AryaDbDataContext()) { var standardExports = new List <ListItem>(); var customExports = new List <ListItem>(); var globalExports = new List <ListItem>(); var groups = (from up in dc.UserProjects where up.ProjectID == selectedProjectId && up.User.EmailAddress == ((SiteMaster)Page.Master).Email select new { up.User.IsAdmin, up.GroupID }).ToList(); // get standard exports if (groups.Any(g => g.IsAdmin) || groups.Any(g => g.GroupID == Group.ExportAdminGroup) || groups.Any(g => g.GroupID == Group.StandardExportUserGroup)) { standardExports = (from export in ExportWorkerBase.GetExports() let type = export.GetType() let displayName = type.GetCustomAttributes(typeof(DisplayNameAttribute), true) .Select(att => ((DisplayNameAttribute)att).DisplayName) .FirstOrDefault() ?? type.Name.Remove(0, 15).Spacify() select new ListItem(displayName, type.FullName)).ToList(); standardExports = standardExports.OrderBy(li => li.Text.ToLower()).ToList(); } // get custom exports if (groups.Any(g => g.IsAdmin) || groups.Any(g => g.GroupID == Group.ExportAdminGroup) || groups.Any(g => g.GroupID == Group.CustomExportUserGroup)) { var customExportPath = CustomExportPath(selectedProjectId); if (!Directory.Exists(customExportPath)) { Directory.CreateDirectory(customExportPath); } var exports = Directory.EnumerateFiles(customExportPath, "*.xml"); customExports = (from export in exports select new ListItem(Path.GetFileNameWithoutExtension(export).Spacify(), export)).ToList(); if (!groups.Any(g => g.IsAdmin || g.GroupID == Group.ExportAdminGroup)) { customExports = customExports.Where(li => !li.Text.ToLower().StartsWith("test")).ToList(); } customExports = customExports.OrderBy(li => li.Text.ToLower()).ToList(); } // get global exports if (groups.Any(g => g.IsAdmin) || groups.Any(g => g.GroupID == Group.ExportAdminGroup) || groups.Any(g => g.GroupID == Group.CustomExportUserGroup)) { var customExportPath = CustomExportPath(Guid.Empty); if (!Directory.Exists(customExportPath)) { Directory.CreateDirectory(customExportPath); } var exports = Directory.EnumerateFiles(customExportPath, "*.xml"); globalExports = (from export in exports select new ListItem(Path.GetFileNameWithoutExtension(export).Spacify(), export)).ToList(); if (!groups.Any(g => g.IsAdmin || g.GroupID == Group.ExportAdminGroup)) { globalExports = globalExports.Where(li => !li.Text.ToLower().StartsWith("test")).ToList(); } globalExports = globalExports.OrderBy(li => li.Text.ToLower()).ToList(); } var allExportTypes = standardExports.Union(customExports).Union(globalExports).ToArray(); ddlExportType.Items.AddRange(allExportTypes); lblExportType.Visible = ddlExportType.Visible = true; } }