using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Net; using System.Web.Mvc; using EnVisage.App_Start; using EnVisage.Code; using EnVisage.Code.BLL; using EnVisage.Models; using jQuery.DataTables.Mvc; namespace EnVisage.Controllers { public class UDFController : BaseController { // GET: Notification public ActionResult Index() { return View(); } [HttpPost] #region Index page public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel) { int totalRecordCount; int searchRecordCount; var types = GetUDFS(startIndex: jQueryDataTablesModel.iDisplayStart, pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(), totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch); return this.DataTablesJson(items: types, totalRecords: totalRecordCount, totalDisplayRecords: searchRecordCount, sEcho: jQueryDataTablesModel.sEcho); } private IEnumerable GetUDFS(int startIndex, int pageSize, IEnumerable sortedColumns, out int totalRecordCount, out int searchRecordCount, string searchString) { var udfMan = new UDFManager(this.DbContext); var query = udfMan.UdfModelBasicQuery; //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 "Name": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Name); else query = query.OrderByDescending(c => c.Name); break; //case "Description": // if (sortedColumn.Direction == SortingDirection.Ascending) // query = query.OrderBy(c => c.Description); // else // query = query.OrderByDescending(c => c.Description); // break; case "Label": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.Label); else query = query.OrderByDescending(c => c.Label); break; case "Area": if (sortedColumn.Direction == SortingDirection.Ascending) query = query.OrderBy(c => c.DomainId); else query = query.OrderByDescending(c => c.DomainId); break; } } totalRecordCount = DbContext.UserDefinedFields.Count(); searchRecordCount = query.Count(); return query.Skip(startIndex).Take(pageSize).ToList(); } #endregion [HttpGet] public ActionResult Edit(Guid? id) { try { if (id.HasValue && id.Value != Guid.Empty) { var udfMan= new UDFManager(this.DbContext); var udf = (UDFModel)udfMan.Load(id); if (udf== null) return new HttpStatusCodeResult(HttpStatusCode.NotFound); return PartialView("_edit", udf); } else { return PartialView("_edit", (new UDFModel())); } } catch (BLLException blEx) { if (blEx.DisplayError) SetErrorScript(message: blEx.Message); else { LogException(blEx); SetErrorScript(); } } catch (Exception exception) { LogException(exception); SetErrorScript(); } return new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } [HttpPost] [ValidateAntiForgeryToken] public JsonResult Edit(UDFModel model) { if (model.Id != Guid.Empty && ContentLocker.IsLock("UDF", model.Id.ToString(), User.Identity.GetUserName())) { return Json( new { Success = false, StatusMessage = "Field not created due to errors", Id = Guid.Empty }); } model.TrimStringProperties(); if (ModelState.IsValid) { try { model.ModifiedById = Guid.Parse(this.HttpContext.User.Identity.GetUserId()); if (model.Id == null || model.Id == Guid.Empty) model.CreatorId = model.ModifiedById; var udf = (new UDFManager(null)).Save(model); this.DbContext.SaveChanges(); ContentLocker.RemoveLock("UDF", model.Id.ToString(), User.Identity.GetUserName()); return Json( new { Success = true, StatusMessage = "Field created ", }); } 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 Json( new { Success = false, StatusMessage = "Field not created due to errors", Id = Guid.Empty }); } [HttpPost] public ActionResult Delete(UDFModel model) { if (model == null || ContentLocker.IsLock("UDF", model.Id.ToString(), User.Identity.GetUserName())) return new HttpStatusCodeResult(HttpStatusCode.BadRequest); try { var udfMan=new UDFManager(this.DbContext); udfMan.Delete(model.Id); this.DbContext.SaveChanges(); ContentLocker.RemoveLock("UDF", model.Id.ToString(), User.Identity.GetUserName()); return new HttpStatusCodeResult(HttpStatusCode.OK); } 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 new HttpStatusCodeResult(HttpStatusCode.InternalServerError); } } }