using System; using System.Text; using System.Data; using System.Linq; using System.Web.Mvc; using System.Data.Entity.Validation; using System.Collections.Generic; using Neo.Afx.Mvc; using Neo.Afx.ViewModels; using Neo.Afx.ComponentModel; using Neo.Afx.Services.Integration; using Neo.Afx.Services.Workflows; using Neo.Afx.Services.Workflows.Entities; using Neo.Afx.Services.Forms.Entities; using Neo.LegitimateLicences.Common; using Neo.LegitimateLicences.Data.Models; using Neo.LegitimateLicences.AccreditationCancelRequest.Models; using Neo.LegitimateLicences.Data; using Neo.Afx.Services.Audits; using Neo.LegitimateLicences.Common.Helpers; using Neo.LegitimateLicences.AccreditationCancelRequest.Models.Database; using AccreditationCancelRequestDetail = Neo.LegitimateLicences.AccreditationCancelRequest.Models.Database.AccreditationCancelDetail; using Neo.Afx.Common; namespace Neo.LegitimateLicences.AccreditationCancelRequest { public partial class AccreditationCancelRequestController : Controller { #region Save [HttpPost] public JsonNetResult Save(AccreditationCancelRequestDetail value) { var changes = new List(); var auditDb = new AuditsDatabase(); if (value.AccreditationCancelDetailID == Database.NewID) { value.CreatedDate = DateTime.Now; value.CreatedByID = Profile.UserID; value.UpdatedDate = DateTime.Now; value.UpdatedByID = Profile.UserID; Database.Insert(value); } else { var accreditationCanceDb = new AccreditationCancelDatabase(); //don't use database as we need the values refreshed every time var existingModel = accreditationCanceDb.GetAccreditationCancelForm(value.AccreditationCancelDetailID, Profile.UserID); if (existingModel != null && existingModel.UpdatedDate.ToLongTimeString() != value.UpdatedDate.ToLongTimeString()) { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = "The record has been modified by someone else. Please try again." } }; } value.UpdatedDate = DateTime.Now; value.UpdatedByID = Profile.UserID; Database.Update(value); changes.AddRange(auditDb.GetEntityDifferences(existingModel, value, "AccreditationCancelDetailID")); } try { Database.Save(); WorkflowsDatabase workflowsDatabase = new WorkflowsDatabase(); workflowsDatabase.ExecutePostFormSave(value.WorkflowInstanceID.Value, Profile.UserID); if (changes.Count > 0) { auditDb.WriteEntries((short)AuditSourceEnum.Application, (short)AuditActionEnum.Updated, changes, (short)EntityEnum.AccreditationCancelDetails, value.AccreditationCancelDetailID, Profile.UserID); } } catch (Exception ex) { return new JsonNetResult() { Data = new JsonBaseResult() { Success = false, ErrorCode = "ERROR", Error = string.Format("Error: {0}", new LegitimateDatabase().GetInnermostMessage(ex)) } }; } var formDb = new AccreditationCancelDatabase(); var formData = formDb.GetAccreditationCancelForm(value.AccreditationCancelDetailID, Profile.UserID); return new JsonNetResult() { Data = new JsonBaseResult() { Result = formData, Success = true } }; } #endregion } }