public ActionResult Create(DebriefingHeaderViewModel dhvm) { // Model level validations if (ModelState.IsValidField("DepartureDate") && ModelState.IsValidField("ReturnDate")) { if (dhvm.ReturnDate.CompareTo(dhvm.DepartureDate) < 0) { // This will prevent ModelState.IsValid from returning true ModelState["ReturnDate"].Errors.Add("Return date can't be before departure date"); } } if (ModelState.IsValid) { // Try and save Debriefing debrief = new Debriefing(); debrief.FillFromHeader(dhvm); // TODO Down the road, this method will only be available to authorized users // For now, add a debug value. debrief.EnteredBy = User.Identity == null ? "*****@*****.**" : User.Identity.Name; debrief.EnteredDate = DateTime.Now; // Add empty evaluations that are appropriate for version and gear code // Do it here instead of FillFromHeader because we'll need access to the database // to come up with the questions. var questions = QuestionsForDebrief(debrief); foreach (var question in questions) { debrief.AddEvaluation(new Evaluation() { QuestionNumber = question.Number }); } // Only start the transaction after we've done what we can without it. using (UnitOfWork uow = DebriefDataService.StartTransaction()) { debrief.FillDependentObjects(dhvm, uow.Session); var writeRepo = new DebriefRepository<Debriefing>(uow.Session); try { writeRepo.Add(debrief); uow.Commit(); } catch (Exception ex) { Flash("Failed to save debriefing. Please contact technical support."); LOGGER.Error("Failed to save debriefing", ex); return View(dhvm); } } return RedirectToAction("Index"); } return View(dhvm); }
/// <summary> /// The questions associated with a debriefing depend on the observer workbook version and /// the vessel's gear type. /// </summary> /// <param name="debrief"></param> /// <param name="formType"></param> /// <returns></returns> private IQueryable<Question> QuestionsForDebrief(Debriefing debrief, string formType = "") { string gearCode = DebriefingExtensions.GearCodeFromVesselType(debrief.Vessel.VesselType); return String.Empty.Equals(formType) ? questionRepo.FilterBy( q => q.Version == debrief.Version.Value && q.GearCode == gearCode) : questionRepo.FilterBy( q => q.Version == debrief.Version.Value && q.GearCode == gearCode && q.Code == formType); }