public ActionResult Save(FormViewModel Model) { // builds new form data model from submitted form, keeps documents and workflow from old tblForm, and throws away the old values. FormsDataContext FormsDB = new FormsDataContext(); tblForm ExistingForm = FormsDB.tblForms.Where(e => e.ID == Model.ID).SingleOrDefault(); if (ExistingForm != null) { Model.ReloadExistingDocuments(ExistingForm); } if (ModelState.IsValid) { if (CustomRequestValidation(Model)) { tblForm Form = null; // Create form DB object from Model try { Form = new tblForm(Model); } catch (Exception ex) { TempData[ORSPARouting.Models.DataConstants.ErrorKey] = "An error occurred when attempting to save the form. The values were not saved. The error has been logged."; TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; SecureAccess.Utilities.Logging.LogException(Logging.LogType.Audit, "RequestController -> Save() : An error occurred when attempting to build a tblForm object from a Model.", ex); Model.Project = FormsDB.tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("Edit", Model)); } // Save form data to DB Form.SubmitterUsername = Current.User.Username; Form.Submitter = Current.User.ENumber; Form.IsSubmitted = false; FormsDB.tblForms.InsertOnSubmit(Form); FormsDB.SubmitChanges(); if (ExistingForm != null) { // set Workflow from old form Form.WorkflowInstanceID = ExistingForm.WorkflowInstanceID; Form.IsSubmitted = ExistingForm.IsSubmitted; Form.SubmittedDate = ExistingForm.SubmittedDate; // set Documents from old form foreach (tblDocument Doc in ExistingForm.tblDocuments.ToList()) { Doc.tblForm = Form; } // delete old form data FormsDB.tblFields.DeleteAllOnSubmit(ExistingForm.tblSections.SelectMany(e => e.tblFields)); FormsDB.tblSections.DeleteAllOnSubmit(ExistingForm.tblSections); FormsDB.tblForms.DeleteOnSubmit(ExistingForm); } // save changes before new document attachment FormsDB.SubmitChanges(); #region Save Documents from input... if (Model.Documents != null && Model.Documents.Files != null) { // save uploaded files List <tblDocument> DocumentsToSave = new List <tblDocument>(); foreach (FileBundle bundle in Model.Documents.Files) { if (bundle.File != null && bundle.File.ContentLength > 0) { // Save as unique path name Form##_Filename. ex.(Form13_ConflictofInterest.doc) var fileName = String.Format("Attachment_{0}_{1}", Path.GetFileName(bundle.File.FileName), DateTime.Now.ToString("yyyyMMddHmmss")); var path = Path.Combine(Server.MapPath("~/uploads"), fileName); bundle.File.SaveAs(path); tblDocument document = new tblDocument(); document.FormID = Form.ID; document.FriendlyFilename = Path.GetFileName(bundle.File.FileName); document.ActualFilename = fileName; document.Description = bundle.Description; document.Type = bundle.Type; DocumentsToSave.Add(document); } } if (DocumentsToSave.Count > 0) { FormsDB.tblDocuments.InsertAllOnSubmit(DocumentsToSave); FormsDB.SubmitChanges(); } } #endregion // Don't Create Workflow // log SecureAccess.Utilities.Logging.Log(Logging.LogType.Audit, string.Format("User {0} successfully saved a request. FormID: {1}; FormType: {2}", Current.User.Username, Form.ID, Form.FormType)); this.ShowPageMessage(String.Format("Request successfully saved: {0}", DateTime.Now.ToLongTimeString())); return(RedirectToAction("Edit", new { @ID = Form.ID })); } // end custom validation IF block else { TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; Model.Project = (new FormsDataContext()).tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("Edit", Model)); } } else { TempData[ORSPARouting.Models.DataConstants.ErrorKey] = ORSPARouting.Models.DataConstants.ModelStateInvalidError; TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; Model.Project = FormsDB.tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("Edit", Model)); } // all paths above should return a value }
public ActionResult Create(FormViewModel Model) { FormsDataContext FormsDB = new FormsDataContext(); tblForm ExistingForm = FormsDB.tblForms.Where(e => e.ID == Model.ID).SingleOrDefault(); if (ExistingForm != null) { Model.ReloadExistingDocuments(ExistingForm); } if (ModelState.IsValid) { if (CustomRequestValidation(Model)) { tblForm Form = null; // Create form DB object from Model try { Form = new tblForm(Model); } catch (Exception ex) { TempData[ORSPARouting.Models.DataConstants.ErrorKey] = "An error occurred when attempting to save the form. The values were not saved. The error has been logged."; TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; SecureAccess.Utilities.Logging.LogException(Logging.LogType.Audit, "RequestController -> Create() : An error occurred when attempting to build a tblForm object from a Model.", ex); Model.Project = FormsDB.tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("New", Model)); } // Save form to DB Form.SubmitterUsername = Current.User.Username; Form.Submitter = Current.User.ENumber; Form.IsSubmitted = false; // only mark submitted when everything goes ok Form.SubmittedDate = DateTime.Now; FormsDB.tblForms.InsertOnSubmit(Form); FormsDB.SubmitChanges(); #region Handle Existing Form (either submitting a rejected or unsubmitted form) if (ExistingForm != null) { // set Workflow from old form Form.WorkflowInstanceID = ExistingForm.WorkflowInstanceID; // set Documents from old form foreach (tblDocument Doc in ExistingForm.tblDocuments.ToList()) { Doc.tblForm = Form; } // delete old form data FormsDB.tblFields.DeleteAllOnSubmit(ExistingForm.tblSections.SelectMany(e => e.tblFields)); FormsDB.tblSections.DeleteAllOnSubmit(ExistingForm.tblSections); FormsDB.tblForms.DeleteOnSubmit(ExistingForm); } #endregion // save changes before new document attachment FormsDB.SubmitChanges(); #region Save Documents... // save uploaded files if (Model.Documents != null && Model.Documents.Files != null) { List <tblDocument> DocumentsToSave = new List <tblDocument>(); foreach (FileBundle bundle in Model.Documents.Files) { if (bundle.File != null && bundle.File.ContentLength > 0) { // Save as unique path name Form##_Filename. ex.(Form13_ConflictofInterest.doc) var fileName = String.Format("Form{0}_{1}", Form.ID, Path.GetFileName(bundle.File.FileName)); var path = Path.Combine(Server.MapPath("~/uploads"), fileName); bundle.File.SaveAs(path); tblDocument document = new tblDocument(); document.FormID = Form.ID; document.FriendlyFilename = Path.GetFileName(bundle.File.FileName); document.ActualFilename = fileName; document.Description = bundle.Description; document.Type = bundle.Type; DocumentsToSave.Add(document); } } if (DocumentsToSave.Count > 0) { FormsDB.tblDocuments.InsertAllOnSubmit(DocumentsToSave); FormsDB.SubmitChanges(); } } #endregion #region Handle Workflow // We're submitting request. If there was no existing form to get a workflow from or there was an existing form, but there is no WorkflowInstanceID, or the workflow exists and was rejected, create a workflow if (ExistingForm == null || (ExistingForm != null && !Form.WorkflowInstanceID.HasValue) || (ExistingForm != null && Form.WorkflowInstanceID.HasValue && Workflows.GetWorkflow(Form.WorkflowInstanceID.Value).Rejected)) { try { #region Create Workflow... // Create Workflow, save instance id to Form tblProject Project = FormsDB.tblProjects.Where(e => e.ID == Model.ProjectID).SingleOrDefault(); List <WorkflowPersonnel> KeyPersonnel = Project.tblProjectPersonnels.Where(e => e.IsPrincipalInvestigator == false).Select(e => new WorkflowPersonnel() { ENumber = Banner.GetUser(e.Username).ENumber, OrganizationCode = e.DepartmentCollege }).ToList <WorkflowPersonnel>(); tblProjectPersonnel PIData = Project.tblProjectPersonnels.Where(e => e.IsPrincipalInvestigator).SingleOrDefault(); WorkflowPersonnel PI = new WorkflowPersonnel() { ENumber = Banner.GetUser(PIData.Username).ENumber, OrganizationCode = PIData.DepartmentCollege }; Form.WorkflowInstanceID = Workflows.CreateWorkflow(PI, KeyPersonnel); FormsDB.SubmitChanges(); #endregion } catch (Exception ex) { // on workflow create exception, set form as unsubmitted so it won't get stuck in limbo or lost. Form.IsSubmitted = false; FormsDB.SubmitChanges(); throw ex; } } #endregion // everything went ok, so mark form as submitted. Form.IsSubmitted = true; FormsDB.SubmitChanges(); SecureAccess.Utilities.Logging.Log(Logging.LogType.Audit, string.Format("User {0} successfully submitted a request. FormID: {1}; FormType: {2}", Current.User.Username, Form.ID, Form.FormType)); } else { TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; Model.Project = (new FormsDataContext()).tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("New", Model)); } } else { TempData[ORSPARouting.Models.DataConstants.ErrorKey] = ORSPARouting.Models.DataConstants.ModelStateInvalidError; TempData[ORSPARouting.Models.DataConstants.ReattachDocumentsNoticeKey] = ORSPARouting.Models.DataConstants.ReattachDocumentsNotice; Model.Project = (new FormsDataContext()).tblProjects.Where(e => e.ID == Model.ProjectID).FirstOrDefault(); return(View("New", Model)); } return(RedirectToAction("Status", "Projects")); }