using System; using System.Collections.Generic; using System.Data; using System.Data.Entity.Validation; using System.Diagnostics; using System.Net; using System.Web.Mvc; using System.Xml.Linq; using System.Linq; using Pilotfish.Afx.ComponentModel; using Pilotfish.Afx.Mvc; using Pilotfish.Afx.Services.Integration; using Pilotfish.Afx.Services.Workflows; using Pilotfish.Afx.Services.Workflows.Entities; using Pilotfish.Afx.ViewModels; using Neo.Legitimate.NewAssociationRequest.Models; using System.Text; using Neo.Legitimate.Common; namespace Neo.Legitimate.NewAssociationRequest.Controllers { public class NewAssociationRequestController : Controller { #region Constructor public NewAssociationRequestController() : base(new NewAssociationDatabase()) { } #endregion #region Console public ActionResult Console(long workflowid, long itemid) { var consoleUrl = "~/NewAssociationRequest/Console?workflowid=" + workflowid + "&itemid=" + itemid + "&t=" + DateTime.Now.Ticks; //ensure t (ticks) parameter is present to prevent caching if (Request.QueryString["t"] == null) { return Redirect(consoleUrl); } var workflowDatabase = new WorkflowsDatabase(); if (!workflowDatabase.CheckWorkflowInstanceSecurity(workflowid, itemid, Profile.UserName)) { var returnUrl = Convert.ToBase64String(Encoding.ASCII.GetBytes(consoleUrl)); return Redirect(Url.Content("~/Error/ShowSecurityError?error=WORKFLOWACCESSSECURITY&workflowid=" + workflowid + "&itemid=" + itemid + "&returnUrl=" + returnUrl)); } return View("Console", GetViewModelInternal(workflowid, itemid)); } #endregion #region Save [HttpPost] public JsonResult Save(NewAssociationRequestDetail value) { if (value.NewAssociationRequestDetailID == Database.NewID) { value.CreatedDate = DateTime.Now; value.CreatedByID = Profile.UserID; value.UpdatedDate = DateTime.Now; value.UpdatedByID = Profile.UserID; Database.Insert(value); } else { var projDb = new NewAssociationDatabase(); //don't use database as we need the values refreshed every time var existingModel = projDb.GetNewAssociationRequest(value.NewAssociationRequestDetailID, Profile.UserID); if (existingModel != null && existingModel.UpdatedDate.ToLongTimeString() != value.UpdatedDate.ToLongTimeString()) { return Json(new { Result = "CONFLICT", Message = "The record has been modified by someone else. Please try again." }); } value.UpdatedDate = DateTime.Now; value.UpdatedByID = Profile.UserID; Database.Update(value); } try { Database.Save(); } catch (DbEntityValidationException dbEx) { foreach (var validationError in dbEx.EntityValidationErrors.SelectMany(validationErrors => validationErrors.ValidationErrors)) { return Json(new { Result = "ERROR", Message = string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) }); } } catch (Exception ex) { return Json(new { Result = "ERROR", Message = string.Format("Error: {0}", ex.Message) }); } return Json(new WorkflowSaveResult { Id = value.NewAssociationRequestDetailID, WorkflowInstanceID = (value.WorkflowInstanceID ?? 0) }); } #endregion #region GetViewModelInternal private WorkflowConsoleViewModelBase GetViewModelInternal(long workflowid, long itemid, bool includeLookups = true) { var projDb = new NewAssociationDatabase(); //don't use database as we need the values refreshed every time var value = projDb.GetNewAssociationRequest(itemid, Profile.UserID); if (value == null) { throw new Exception("Error initialising object data"); } if (Profile.Roles == null) { throw new Exception("Current user does not have any roles."); } var vm = new WorkflowsDatabase().GetWorkflowConsoleViewModel(workflowid, (value.WorkflowInstanceID.HasValue ? value.WorkflowInstanceID.Value : -1), itemid, Profile.UserID, Profile.Roles, Profile.WorkflowBuddies); if (vm == null) { throw new Exception("Error initialising view model data"); } if (itemid < 0) { SetWorkflowTemplateDefaults(value, (IEnumerable)vm.WorkflowTemplateDefaults); } #region get defaults if new request if (itemid < 0) { //var integrationStore = new SqlIntegrationStore(); //try //{ // integrationStore.Open(); // var employee = integrationStore.GetData("Employees", "EmployeeNumber=" + Profile.UserID).FirstNode; // if (employee != null) // { // value.RequestedBy = employee.ToString(SaveOptions.DisableFormatting); // } //} //finally //{ // integrationStore.Close(); //} } #endregion #region load lookups var lookups = new List(); if (includeLookups) { var integrationStore = new SqlIntegrationStore(); try { integrationStore.Open(); //lookups.Add(new LookupList("NewOLIDTypes", integrationStore.GetDataEntities("IDTypes", string.Empty))); //lookups.Add(new LookupList("NewOLIDTypesIndividual", integrationStore.GetDataEntities("IDTypes", "IndvidualTypesOnly=1"))); //lookups.Add(new LookupList("NewOLModes", integrationStore.GetDataEntities("Modes", string.Empty))); //lookups.Add(new LookupList("NewOLServiceTypes", integrationStore.GetDataEntities("ServiceTypes", string.Empty))); lookups.Add(new LookupList("AssociationTypes", integrationStore.GetDataEntities("AssociationTypes", string.Empty))); lookups.Add(new LookupList("AssociationModes", integrationStore.GetDataEntities("AssociationModes", string.Empty))); lookups.Add(new LookupList("Regions", integrationStore.GetDataEntities("Regions", string.Empty))); } finally { integrationStore.Close(); } } #endregion #if DEBUG foreach (var wp in ((WorkflowInstance)vm.WorkflowInstance).WorkflowInstanceParticipants) { wp.UserID = Profile.UserID; } #endif //if (itemid > 0) //{ //} vm.Lookups = lookups; vm.Value = value; return vm; } #endregion #region GetViewModel [HttpGet] public string GetViewModel(long workflowid, long itemid) { var workflowDatabase = new WorkflowsDatabase(); if (!workflowDatabase.CheckWorkflowInstanceSecurity(workflowid, itemid, Profile.UserName)) { throw new Exception("Security error trying to access GetViewModel"); } return GetViewModelInternal(workflowid, itemid, false).ToJson(JsonSerializerOptions.FormatDateTime); } #endregion #region SetWorkflowTemplateDefaults static void SetWorkflowTemplateDefaults(NewAssociationRequestDetail value, IEnumerable defaults) { } #endregion #region JSON endpoints // All JSON endpoints require [HttpPost] to prevent JSON hijacking. // With [HttpPost], returning arrays is allowed. // See http://haacked.com/archive/2009/06/25/json-hijacking.aspx #endregion } }