using Neo.Afx.Security;
using Neo.Afx.Services.Integration;
using Neo.Afx.ViewModels;
using Neo.LegitimateLicences.ApplicationRequest.Models.Database;
using Neo.LegitimateLicences.Common;
using Neo.LegitimateLicences.Common.Helpers;
using Neo.LegitimateLicences.Data.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Neo.Afx.Services.Audits;
using System.Data.Entity.Validation;
using Neo.Afx.Services.Workflows.Entities;
using Neo.Afx.Common;
namespace Neo.LegitimateLicences.ApplicationRequest
{
public partial class ApplicationRequestController
{
#region Referrals
///
/// Return View of all Referrals
///
///
public ActionResult Referrals()
{
var isNew = WebHelper.ItemID == -1;
var itemId = WebHelper.ItemID;
var taskId = WebHelper.TaskID;
#region Get active user task
Vw_WorkflowInstanceTask activeUserTask = null;
if (taskId != -1)
{
activeUserTask = WorkflowHelper.GetActiveUserUserTaskByID((long)EntityEnum.ApplicationRequestDetails, itemId, taskId, Profile.UserID);
}
if (activeUserTask == null)
{
activeUserTask = WorkflowHelper.GetActiveUserTask(Profile.UserID, EntityEnum.ApplicationRequestDetails, itemId);
}
#endregion
var model = new ApplicationRequestFormModel
{
UserSecurables = (Profile.Securables != null) ? Profile.Securables.ToList() : new List(),
Ui = new WorkflowConsoleUIProperties()
{
canSave = (activeUserTask != null && activeUserTask.WorkflowTemplateStepNumber == 11)
}
};
var applicationDatabase = new ApplicationDatabase();
var applicationReferral = applicationDatabase.GetApplicationReferrals(itemId, Profile.UserID);
applicationReferral.Referrals = applicationReferral.Referrals.Where(a => !a.IsCancelled).ToList();
model.Data = applicationReferral;
model.ApplicationView = applicationDatabase.GetApplicationView(itemId, Profile.UserID);
model.Lookups = GetLookupsForFormGetLookupsFormView();
model.WorkflowInstance = WorkflowHelper.GetWorkflowInstanceView(WebHelper.EntityID, WebHelper.ItemID, Profile.UserID);
return View("Referrals", model);
}
#endregion
#region Referral
///
/// Dialog to capture Referral details
///
///
///
///
public ActionResult Referral(long applicationDetailID, long applicationReferralDetailID)
{
var applicationDatabase = new ApplicationDatabase();
var model = new ApplicationReferalModel
{
UserSecurables = (Profile.Securables != null) ? Profile.Securables.ToList() : new List(),
ApplicationReferral = applicationDatabase.GetApplicationReferral(applicationReferralDetailID, applicationDetailID, Profile.UserID),
ApplicationDetailID = applicationDetailID
};
model.Lookups = GetLookupsForReferral();
return View("Referrals.Referral", model);
}
#endregion
#region GetLookupsForReferral
///
/// Look list uniquie for Referral(
///
///
public List GetLookupsForReferral()
{
var lookups = new List();
var integrationStore = new SqlIntegrationStore();
try
{
integrationStore.Open();
lookups.Add(new LookupList("ReferralTypes", integrationStore.GetDataEntities("ReferralTypes", string.Empty).OrderBy(a => a.Title)));
}
finally
{
integrationStore.Close();
}
return lookups;
}
#endregion
#region SaveApplicationReferral
///
/// Save Application Referral
///
///
///
///
[HttpPost]
public JsonNetResult SaveApplicationReferral(long applicationDetailID, ApplicationReferral value)
{
var changes = new List();
var auditDb = new AuditsDatabase();
var appDb = new ApplicationDatabase(); //don't use database context as we need the values refreshed every time
try
{
#region Check if can update
var model = GetModel();
if (!model.CanEditReferralsDetails)
{
throw new Exception("Security error");
}
#endregion
var existingModel = appDb.GetApplicationReferral(value.ApplicationReferralDetailID, applicationDetailID, Profile.UserID);
if(value.ApplicationReferralDetailID > 0)
{
Database.Update(value);
changes.AddRange(auditDb.GetEntityDifferences(existingModel, value, "ApplicationReferralDetailID"));
}
else
{
Database.Insert(value);
changes.AddRange(auditDb.GetEntityDifferences(existingModel, value, "ApplicationReferralDetailID"));
}
Database.Save();
if (changes.Count > 0)
{
auditDb.WriteEntries(2 /*Application*/, 2 /*Updated*/, changes, (short)EntityEnum.ApplicationtDetails, applicationDetailID, Profile.UserID);
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationError in dbEx.EntityValidationErrors.SelectMany(validationErrors => validationErrors.ValidationErrors))
{
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = false,
ErrorCode = "ERROR",
Error = string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage)
}
};
}
}
catch (Exception ex)
{
#region Error Handling
#region Error Additional Data
Dictionary additionalData = new Dictionary();
additionalData.Add("ApplicationDetailID", value.ApplicationDetailID);
#endregion
string error = ErrorHelper.ProcessError(ex, "ApplicationRequest", "Save", additionalData);
#endregion
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = false,
ErrorCode = "ERROR",
Error = error
}
};
}
#region Clean data on return
value.ReferralType = appDb.GetReferralType(value.ReferralTypeID);
#endregion
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = true,
Result = value
}
};
}
#endregion
#region DeleteApplicationReferral
///
/// Cancel Application Referral
///
///
///
///
[HttpPost]
public JsonNetResult DeleteApplicationReferral(long applicationDetailID, ApplicationReferral value)
{
var changes = new List();
var auditDb = new AuditsDatabase();
try
{
#region Check if can save
var model = GetModel();
if (!model.CanEditReferralsDetails)
{
throw new Exception("Security error");
}
#endregion
var projDb = new ApplicationDatabase(); //don't use database context as we need the values refreshed every time
var existingModel = projDb.GetApplicationReferral(value.ApplicationReferralDetailID, applicationDetailID, Profile.UserID);
if (value.ApplicationReferralDetailID > 0)
{
value.IsCancelled = true;
Database.Update(value);
changes.AddRange(auditDb.GetEntityDifferences(existingModel, value, "ApplicationReferralDetailID"));
}
Database.Save();
if (changes.Count > 0)
{
auditDb.WriteEntries(2 /*Application*/, 2 /*Updated*/, changes, (short)EntityEnum.ApplicationtDetails, applicationDetailID, Profile.UserID);
}
}
catch (DbEntityValidationException dbEx)
{
foreach (var validationError in dbEx.EntityValidationErrors.SelectMany(validationErrors => validationErrors.ValidationErrors))
{
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = false,
ErrorCode = "ERROR",
Error = string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage)
}
};
}
}
catch (Exception ex)
{
#region Error Handling
#region Error Additional Data
Dictionary additionalData = new Dictionary();
additionalData.Add("ApplicationDetailID", value.ApplicationDetailID);
#endregion
string error = ErrorHelper.ProcessError(ex, "ApplicationRequest", "Save", additionalData);
#endregion
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = false,
ErrorCode = "ERROR",
Error = error
}
};
}
return new JsonNetResult()
{
Data = new JsonBaseResult()
{
Success = true
}
};
}
#endregion
}
}