333 lines
13 KiB
C#
333 lines
13 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
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
|
|
{
|
|
[Authorize]
|
|
public class NonProjectTimeCategoryController : BaseController
|
|
{
|
|
/// <summary>
|
|
/// GET: /Types/
|
|
/// </summary>
|
|
/// <returns>Empty view</returns>
|
|
[HttpGet]
|
|
[AreaSecurityAttribute(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Read)]
|
|
public ActionResult Index()
|
|
{
|
|
//TODO: There is AreaSecurityAttribute applied for current action. We do not need to check any more
|
|
if (!SecurityManager.CheckSecurityObjectPermission(Areas.RD_ResourceNPTAllocationCategory, AccessLevel.Read))
|
|
return Redirect("/");
|
|
|
|
return View();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns JSON Types list with filters and sort for jQuery DataTables
|
|
/// </summary>
|
|
[HttpPost]
|
|
[AreaSecurityAttribute(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Read)]
|
|
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel)
|
|
{
|
|
int totalRecordCount;
|
|
int searchRecordCount;
|
|
|
|
var types = GetNPCategories(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<NonProjectTimeCategoryModel> GetNPCategories(int startIndex,
|
|
int pageSize,
|
|
IEnumerable<SortedColumn> sortedColumns,
|
|
out int totalRecordCount,
|
|
out int searchRecordCount,
|
|
string searchString)
|
|
{
|
|
var query = from c in DbContext.NonProjectTimeCategories.AsNoTracking()
|
|
select new NonProjectTimeCategoryModel()
|
|
{
|
|
Id = c.Id,
|
|
Name = c.Name,
|
|
Scheduled = c.NonProjectTimes.Count
|
|
};
|
|
|
|
//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":
|
|
query = sortedColumn.Direction == SortingDirection.Ascending ? query.OrderBy(c => c.Name) : query.OrderByDescending(c => c.Name);
|
|
break;
|
|
|
|
case "Scheduled":
|
|
query = sortedColumn.Direction == SortingDirection.Ascending ? query.OrderBy(c => c.Scheduled) : query.OrderByDescending(c => c.Scheduled);
|
|
break;
|
|
}
|
|
}
|
|
|
|
totalRecordCount = DbContext.NonProjectTimeCategories.Count();
|
|
searchRecordCount = query.Count();
|
|
return query.Skip(startIndex).Take(pageSize).ToList();
|
|
}
|
|
|
|
// GET: /Type/Edit/5
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Write)]
|
|
public ActionResult Edit(Guid? id)
|
|
{
|
|
try
|
|
{
|
|
if (id.HasValue)
|
|
{
|
|
var category = (new NonProjectTimeCategoryManager(DbContext)).Load(id);
|
|
if (category == null)
|
|
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
|
|
|
|
var model = new NonProjectTimeCategoryEditModel()
|
|
{
|
|
Id = category.Id,
|
|
Name = category.Name
|
|
};
|
|
|
|
return PartialView("_editNonProjectTimeCategory", model);
|
|
}
|
|
return PartialView("_editNonProjectTimeCategory", (new NonProjectTimeCategoryEditModel()));
|
|
}
|
|
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);
|
|
}
|
|
|
|
// POST: /Type/Edit/5
|
|
// Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Write)]
|
|
public ActionResult Edit(NonProjectTimeCategoryEditModel model)
|
|
{
|
|
if (model.Id != Guid.Empty && ContentLocker.IsLock("NonProjectTimeCategories", model.Id.ToString(), User.Identity.GetUserName()))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
(new NonProjectTimeCategoryManager(null)).Save(model);
|
|
|
|
ContentLocker.RemoveLock("NonProjectTimeCategories", 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);
|
|
}
|
|
|
|
// GET: /Type/Reallocate/5
|
|
[HttpGet]
|
|
[AreaSecurity(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Write)]
|
|
public ActionResult Reallocate(Guid? id)
|
|
{
|
|
try
|
|
{
|
|
if (id.HasValue)
|
|
{
|
|
var category = new NonProjectTimeCategoryManager(DbContext).Load(id);
|
|
if (category == null)
|
|
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
|
|
|
|
var model = new NonProjectTimeCategoryReallocateModel()
|
|
{
|
|
Id = category.Id,
|
|
Name = category.Name,
|
|
CategoryId = Guid.Empty
|
|
};
|
|
|
|
return PartialView("_reallocateNonProjectTimeCategory", model);
|
|
}
|
|
return PartialView("_reallocateNonProjectTimeCategory", (new NonProjectTimeCategoryEditModel()));
|
|
}
|
|
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);
|
|
}
|
|
|
|
// POST: /Type/Reallocate/5
|
|
// Implementing Basic CRUD Functionality with the Entity Framework in ASP.NET MVC Application http://go.microsoft.com/fwlink/?LinkId=317598.
|
|
[HttpPost]
|
|
[ValidateAntiForgeryToken]
|
|
[AreaSecurity(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Write)]
|
|
public ActionResult Reallocate(NonProjectTimeCategoryReallocateModel model)
|
|
{
|
|
if (model.Id != Guid.Empty && ContentLocker.IsLock("NonProjectTimeCategories", model.Id.ToString(), User.Identity.GetUserName()))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
model.TrimStringProperties();
|
|
|
|
if (ModelState.IsValid)
|
|
{
|
|
try
|
|
{
|
|
(new NonProjectTimeCategoryManager(null)).Reallocate(model);
|
|
|
|
ContentLocker.RemoveLock("NonProjectTimeCategories", 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);
|
|
}
|
|
|
|
// POST: /Type/Delete/5
|
|
[HttpPost]
|
|
[AreaSecurity(area = Areas.RD_ResourceNPTAllocationCategory, level = AccessLevel.Write)]
|
|
public ActionResult Delete(NonProjectTimeCategoryModel model)
|
|
{
|
|
if (model == null || ContentLocker.IsLock("NonProjectTimeCategories", model.Id.ToString(), User.Identity.GetUserName()))
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
try
|
|
{
|
|
var result = DbContext.NonProjectTimeCategories
|
|
.Where(x => x.Id == model.Id)
|
|
.Select(x => new
|
|
{
|
|
category = x,
|
|
scheduled = x.NonProjectTimes.Count
|
|
}).FirstOrDefault();
|
|
if (result == null || result.category == null)
|
|
return new HttpStatusCodeResult(HttpStatusCode.NotFound);
|
|
|
|
// deleting is not allowed
|
|
if (result.scheduled > 0)
|
|
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
|
|
|
|
(new NonProjectTimeCategoryManager(DbContext)).Delete(result.category);
|
|
|
|
ContentLocker.RemoveLock("NonProjectTimeCategories", 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);
|
|
}
|
|
|
|
[HttpPost]
|
|
[ValidateJsonAntiForgeryToken]
|
|
public ActionResult GetCategories()
|
|
{
|
|
try
|
|
{
|
|
var categories = DbContext.NonProjectTimeCategories.Select(x => new { id = x.Id, name = x.Name })
|
|
.ToDictionary(x => x.id.ToString(), g => g.name);
|
|
|
|
return Json(categories);
|
|
}
|
|
catch (Exception exception) // handle any unexpected error
|
|
{
|
|
LogException(exception);
|
|
}
|
|
|
|
return new HttpStatusCodeResult(HttpStatusCode.InternalServerError);
|
|
}
|
|
}
|
|
} |