using System; using System.Collections.Generic; using System.Data.Entity; using System.Linq; using System.Net; using System.Web; using System.Web.Mvc; using EnVisage.Code; using EnVisage.Code.BLL; using EnVisage.Code.Cache; using EnVisage.Models; using jQuery.DataTables.Mvc; using EnVisage.App_Start; using Microsoft.AspNet.Identity; using EnVisage.Code.HtmlHelpers; namespace EnVisage.Controllers { [Authorize] public class ViewController : BaseController { /// /// GET: /View/ /// /// Empty view [HttpGet] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Read)] public ActionResult Index() { if (!HtmlHelpers.CheckSecurityObjectPermission(null, Areas.Views, AccessLevel.Read)) return Redirect("/"); return View(); } /// /// Returns JSON View list with filters and sort for jQuery DataTables /// [HttpPost] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Read)] public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel) { int totalRecordCount; int searchRecordCount; var views = GetViews(startIndex: jQueryDataTablesModel.iDisplayStart, pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(), totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch); return this.DataTablesJson(items: views, totalRecords: totalRecordCount, totalDisplayRecords: searchRecordCount, sEcho: jQueryDataTablesModel.sEcho); } private IEnumerable GetViews(int startIndex, int pageSize, IEnumerable sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString) { var query = from c in DbContext.Views select new ViewListModel() { Id = c.Id, Name = c.Name, TeamCount = c.Team2View.Count(), Companies = c.Company2View.Select(x => x.Company.Name).ToList() }; //filter if (!string.IsNullOrWhiteSpace(searchString)) { query = query.Where(c => c.Name.ToLower().Contains(searchString.ToLower())); } //sort foreach (var sortedColumn in sortedColumns) { switch (sortedColumn.PropertyName) { case "Id": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Id); else query = query.OrderByDescending(c => c.Id); break; //case "GLNumber": // if (sortedColumn.Direction == SortingDirection.Ascending) // query = query.OrderBy(c => c.GLNumber); // else // query = query.OrderByDescending(c => c.GLNumber); // break; //case "ExpenditureCount": // if (sortedColumn.Direction == SortingDirection.Ascending) // query = query.OrderBy(c => c.ExpenditureCount); // else // query = query.OrderByDescending(c => c.ExpenditureCount); // break; default: if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Name); else query = query.OrderByDescending(c => c.Name); break; } } totalRecordCount = DbContext.Views.Count(); searchRecordCount = query.Count(); return query.Skip(startIndex).Take(pageSize).ToList(); } // GET: /View/Edit/5 [HttpGet] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Write)] public ActionResult Edit(Guid? id) { var model = new ViewModel(); try { var manager = new ViewManager(DbContext); model = (ViewModel)manager.LoadWithChildCollections(id) ?? new ViewModel(); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return View(model); } // POST: /View/Edit/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Write)] public ActionResult Edit(ViewModel model) { if (ContentLocker.IsLock("View", model.Id.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); model.TrimStringProperties(); if (ModelState.IsValid) { try { var manager = new ViewManager(DbContext); manager.Save(model); DbContext.SaveChanges(); ContentLocker.RemoveLock("View", model.Id.ToString(), User.Identity.Name); return RedirectToAction("Index"); } catch (BLLException blEx) // handle any system specific error { // display error message if required if (blEx.DisplayError) ModelState.AddModelError(string.Empty, blEx.Message); else // if display not requried then display modal form with general error message { LogException(blEx); SetErrorScript(); } } catch (Exception exception) // handle any unexpected error { LogException(exception); SetErrorScript(); } } // return empty model with validation messages (if any) return View(model); } // GET: /View/Delete/5 [HttpGet] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Write)] public ActionResult Delete(Guid? id) { if (id == null || id == Guid.Empty) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var model = new ViewModel(); try { var manager = new ViewManager(DbContext); model = (ViewModel)manager.Load(id); if (model == null) return HttpNotFound(); if (model.TeamCount > 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "View has Teams assigned and could not be deleted"); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return View(model); } // POST: /View/Delete/5 [HttpPost] [ValidateAntiForgeryToken] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Write)] public ActionResult Delete(ViewModel model) { if (ContentLocker.IsLock("View", model.Id.ToString(), User.Identity.Name)) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var manager = new ViewManager(DbContext); var dbObj = manager.Load(model.Id, false); if (dbObj == null) return HttpNotFound(); if (dbObj.Team2View.Count > 0) return new HttpStatusCodeResult(HttpStatusCode.BadRequest, "View has Teams assigned and could not be deleted"); DbContext.Company2View.RemoveRange(DbContext.Company2View.Where(c2s => c2s.ViewId == dbObj.Id)); DbContext.View2CostCenter.RemoveRange(DbContext.View2CostCenter.Where(d2c => d2c.ViewId == dbObj.Id)); var us = DbContext.User2View.Where(u2d => u2d.ViewId == dbObj.Id).ToList(); DbContext.User2View.RemoveRange(us); DbContext.Team2View.RemoveRange(DbContext.Team2View.Where(tv => tv.ViewId == dbObj.Id)); DbContext.Views.Remove(dbObj); DbContext.SaveChanges(); ContentLocker.RemoveLock("View", dbObj.Id.ToString(), User.Identity.Name); return RedirectToAction("Index"); } [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Read)] public ActionResult Board() { var model = new ViewBoardModel(); model.Views = new ViewManager(DbContext).GetViewsByOwner(Guid.Parse(User.Identity.GetUserId())).ToList(); model.Teams = new ViewManager(DbContext).GetTeams(model.Views.Select(x => x.Id).FirstOrDefault()).ToList(); var user = new UsersCache().Value.FirstOrDefault(x => x.Id == new Guid(HttpContext.User.Identity.GetUserId())); if (user != null) ViewBag.IsUOMHours = !user.PreferredResourceAllocation; return View(model); } [HttpPost] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Read)] public ActionResult GetTeams(Guid? viewId) { return PartialView("~/Views/View/_resourcesList.cshtml", new ViewManager(DbContext).GetTeams(viewId).ToList()); } // GET: /View/Details/5 [HttpGet] [AreaSecurityAttribute(area = Areas.Views, level = AccessLevel.Read)] public ActionResult Details(Guid? id) { if (id == null || id == Guid.Empty) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); var model = new ViewModel(); try { var manager = new ViewManager(DbContext); model = (ViewModel)manager.Load(id) ?? new ViewModel(); if (model.Id == Guid.Empty) return HttpNotFound(); } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return PartialView("_details", model); } } }