using System.Web.Mvc;
using Neo.Afx.Security;
using Neo.Afx.Mvc;
using Neo.Afx.Services.Audits;
using System;
using System.Collections.Generic;
using Neo.Legitimate.Common;
namespace Neo.LegitimateLicences.Common
{
///
/// A controller with a database.
///
[Authorize]
public abstract class ControllerBase : Controller
{
protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
var auditDb = new AuditsDatabase();
int entityID = -1;
var itemIDProperty = "";
long? itemID = null;
List skippedMethods = new List();
skippedMethods.Add("GetViewModel");
skippedMethods.Add("SearchVerifiedVehicles");
skippedMethods.Add("SearchVerifiedMembers");
skippedMethods.Add("SearchVerifiedRoutes");
skippedMethods.Add("SearchVerifiedAssociations");
skippedMethods.Add("SearchVerifiedOperatingLicences");
string originController = filterContext.RouteData.Values["controller"].ToString();
string originAction = filterContext.RouteData.Values["action"].ToString();
if (!skippedMethods.Contains(originAction))
{
#region EntityID ItemID Breakdown
switch (originController)
{
case "ApplicationRequest":
entityID = 1000;
itemIDProperty = "itemid";
break;
case "LicenceIssueRequest":
entityID = 1014;
itemIDProperty = "itemid";
break;
case "NewAssociationRequest":
entityID = 1004;
itemIDProperty = "itemid";
break;
case "NewMemberRequest":
entityID = 1005;
itemIDProperty = "itemid";
break;
case "NewRouteRequest":
entityID = 1002;
itemIDProperty = "itemid";
break;
case "NewVehicleRequest":
entityID = 1016;
itemIDProperty = "itemid";
break;
case "AdjudicationConsole":
entityID = 1007;
itemIDProperty = "AdjudicationDetailID";
break;
case "AssociationConsole":
entityID = 1008;
itemIDProperty = "AssociationID";
break;
case "GazetteConsole":
entityID = 1006;
itemIDProperty = "gazetteDetailID";
break;
case "InspectionConsole":
entityID = 1014;
itemIDProperty = "licenceIssueDetailID";
break;
case "IntegrationConsole":
entityID = 1000;
itemIDProperty = "appDetailID";
break;
case "MemberConsole":
entityID = 1009;
itemIDProperty = "MemberID";
break;
case "OperatingLicenceConsole":
entityID = 1011;
itemIDProperty = "licenceID";
break;
case "RouteConsole":
entityID = 1010;
itemIDProperty = "RouteID";
break;
case "VehicleConsole":
entityID = 1008;
itemIDProperty = "VehicleID";
break;
case "VerificationConsole":
entityID = 1000;
itemIDProperty = "applicationRequestDetailID";
break;
case "UserConsole":
entityID = 1;
itemIDProperty = "UserID";
break;
default:
break;
}
#endregion
#region Parameters
var parameters = "";
foreach (string p in filterContext.HttpContext.Request.QueryString)
{
var value = filterContext.HttpContext.Request.QueryString[p];
if (!string.IsNullOrEmpty(itemIDProperty) && !string.IsNullOrEmpty(value))
{
if (p.ToLower() == itemIDProperty.ToLower())
{
itemID = Convert.ToInt32(value);
}
}
if (value != null)
{
var t = value.GetType();
if (t.IsPrimitive || t == typeof(String) || t == typeof(Boolean) || t == typeof(Int64) || t == typeof(Int32) || t == typeof(Int16) || t == typeof(Double) || t == typeof(DateTime))
{
parameters += p + ":" + value + ";";
}
}
}
#endregion
var calledMethod = "";
if (!string.IsNullOrEmpty(parameters))
{
calledMethod = string.Format("{0} -> {1} -> {2}", originController, originAction, parameters);
}
else
{
calledMethod = string.Format("{0} -> {1}", originController, originAction);
}
auditDb.WriteEntry(Profile.UserID, "Application", "Page view", calledMethod, false, false, entityID, itemID);
}
base.OnResultExecuted(filterContext);
}
///
/// Gets the user profile.
///
public new UserProfile Profile
{
get
{
return (UserProfile)base.Profile;
}
}
public UserSettings UserSettings
{
get
{
return new UserSettings(Profile.Settings);
}
}
///
/// Creates a object that serializes the specified object to JavaScript Object Notation (JSON).
///
/// The JavaScript object graph to serialize.
///
/// The JSON result object that serializes the specified object to JSON format. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed.
///
protected new JsonResult Json(object data)
{
return new JsonResultEx
{
Data = data
};
}
///
/// Creates a object that serializes the specified object to JavaScript Object Notation (JSON).
///
/// The JavaScript object graph to serialize.
/// The serialization options
///
/// The JSON result object that serializes the specified object to JSON format. The result object that is prepared by this method is written to the response by the ASP.NET MVC framework when the object is executed.
///
protected JsonResult Json(object data, JsonSerializerOptions options)
{
return new JsonResultEx(options)
{
Data = data
};
}
///
/// Sets the current page.
///
/// The page.
protected void SetCurrentPage(dynamic page)
{
ViewData.Add("CurrentPage", page);
}
}
}