EnVisageOnline/Main/Source/EnVisage/Controllers/ScenarioGroupController.cs

198 lines
5.9 KiB
C#

using System;
using System.Collections.Generic;
using System.Net;
using System.Web.Mvc;
using EnVisage.Code;
using EnVisage.Code.BLL;
using EnVisage.Models;
using jQuery.DataTables.Mvc;
using EnVisage.App_Start;
namespace EnVisage.Controllers
{
[Authorize]
public class ScenarioGroupController : BaseController
{
#region Actions
/// <summary>
/// GET: /UnitOfMeasure/
/// </summary>
/// <returns>Empty view</returns>
[HttpGet]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Read)]
public ActionResult Index()
{
if (!SecurityManager.CheckSecurityObjectPermission(Areas.Scenarios, AccessLevel.Read))
return Redirect("/");
return View();
}
/// <summary>
/// Returns JSON ScenarioGroup list with filters and sort for jQuery DataTables
/// </summary>
[HttpPost]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Read)]
public JsonResult Index(JQueryDataTablesModel jQueryDataTablesModel)
{
int totalRecordCount;
int searchRecordCount;
IEnumerable<ScenarioGroupModel> groups;
using (var manager = new ScenarioGroupManager(null))
{
groups = manager.GetGroups(startIndex: jQueryDataTablesModel.iDisplayStart,
pageSize: jQueryDataTablesModel.iDisplayLength, sortedColumns: jQueryDataTablesModel.GetSortedColumns(),
totalRecordCount: out totalRecordCount, searchRecordCount: out searchRecordCount, searchString: jQueryDataTablesModel.sSearch);
}
return this.DataTablesJson(items: groups,
totalRecords: totalRecordCount,
totalDisplayRecords: searchRecordCount,
sEcho: jQueryDataTablesModel.sEcho);
}
// GET: /ScenarioGroup/Edit/5
[HttpGet]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Write)]
public ActionResult Edit(Guid? id)
{
var model = new ScenarioGroupModel();
try
{
using (var manager = new ScenarioGroupManager(DbContext))
{
model = (ScenarioGroupModel)manager.Load(id) ?? new ScenarioGroupModel();
}
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
return View(model);
}
// POST: /ScenarioGroup/Edit/5
// Чтобы защититься от атак чрезмерной передачи данных, включите определенные свойства, для которых следует установить привязку. Дополнительные
// сведения см. в статье http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Write)]
public ActionResult Edit(ScenarioGroupModel model)
{
if (ContentLocker.IsLock("ScenarioGroup", model.Id.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
model.TrimStringProperties();
if (ModelState.IsValid)
{
try
{
using (var manager = new ScenarioGroupManager(DbContext))
{
manager.Save(model);
}
DbContext.SaveChanges();
ContentLocker.RemoveLock("ScenarioGroup", model.Id.ToString(), User.Identity.GetUserName());
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: /ScenarioGroup/Delete/5
[HttpGet]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Write)]
public ActionResult Delete(Guid? id)
{
if (id == null || id == Guid.Empty)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new ScenarioGroupModel();
try
{
using (var manager = new ScenarioGroupManager(DbContext))
{
model = (ScenarioGroupModel)manager.Load(id);
}
if (model == null)
return HttpNotFound();
}
catch (BLLException blEx)
{
if (blEx.DisplayError)
SetErrorScript(message: blEx.Message);
else
{
LogException(blEx);
SetErrorScript();
}
}
catch (Exception exception)
{
LogException(exception);
SetErrorScript();
}
return View(model);
}
// POST: /ScenarioGroup/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
[AreaSecurity(area = Areas.Scenarios, level = AccessLevel.Write)]
public ActionResult Delete(ScenarioGroupModel model)
{
if (ContentLocker.IsLock("ScenarioGroup", model.Id.ToString(), User.Identity.GetUserName()))
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
SystemAttribute dbObj;
using (var manager = new ScenarioGroupManager(null))
{
dbObj = manager.Load(model.Id, false);
if (dbObj == null)
return HttpNotFound();
manager.Delete(model.Id);
}
ContentLocker.RemoveLock("ScenarioGroup", dbObj.Id.ToString(), User.Identity.GetUserName());
return RedirectToAction("Index");
}
#endregion
}
}