Example #1
0
 public void LegacyGen3ViewModelToEntity()
 {
     Mapper.AssertConfigurationIsValid();
     var vm = new Gen3ViewModel();
     vm.Incidents = new List<Gen3ViewModel.Incident>(20);
     for (int i = 0; i < 20; i++)
     {
         vm.Incidents.Add(new Gen3ViewModel.Incident() { Answer = "NO", QuestionCode = Char.ToString((char)(97 + i)) });
     }
     vm.Incidents[6].Answer = "YES";
     var entity = Mapper.Map<Gen3ViewModel, TripMonitor>(vm);
     Assert.NotNull(entity);
     Assert.True(entity.Question7.HasValue);
     Assert.True(entity.Question7.Value);
 }
Example #2
0
        public ActionResult Edit(Trip tripId, Gen3ViewModel vm)
        {
            if (null == vm)
            {
                return ViewActionImpl(tripId);
            }

            // Complex validation here
            if (null != vm.Notes)
            {
                foreach (var note in vm.Notes)
                {
                    if (null != note && note.Date.HasValue && !note._destroy)
                    {
                        if (!tripId.IsDuringTrip(note.Date.Value))
                        {
                            ModelState.AddModelError("Date", "Date doesn't fall between departure and return dates");
                        }
                    }
                }
            }

            // Simple validation here
            if (!ModelState.IsValid)
            {
                if (IsApiRequest)
                    return ModelErrorsResponse();

                return View(CurrentAction, vm);
            }

            using (var xa = MvcApplication.CurrentSession.BeginTransaction())
            {
                var deletedNoteIds = vm.Notes.Where(n => null != n && n._destroy).Select(n => n.Id);

                if (Spc.Ofp.Tubs.DAL.Common.WorkbookVersion.v2009 == tripId.Version.Value)
                {
                    // MockTripMonitor is a disposable container for the two lists of entities
                    // we _do_ care about
                    var tm = Mapper.Map<Gen3ViewModel, MockTripMonitor>(vm);

                    var arepo = TubsDataService.GetRepository<Gen3Answer>(MvcApplication.CurrentSession);
                    foreach (var answer in tm.Answers)
                    {
                        answer.Trip = tripId;
                        answer.SetAuditTrail(User.Identity.Name, DateTime.Now);
                        if (!answer.IsNew())
                        {
                            AuditHelper.BackfillTrail<Gen3Answer>(answer.Id, answer, arepo);
                        }

                        arepo.Save(answer);

                    }

                    var drepo = TubsDataService.GetRepository<Gen3Detail>(MvcApplication.CurrentSession);
                    foreach (var detail in tm.Details)
                    {
                        if (deletedNoteIds.Contains(detail.Id))
                        {
                            continue;
                        }

                        detail.Trip = tripId;
                        detail.SetAuditTrail(User.Identity.Name, DateTime.Now);
                        if (!detail.IsNew())
                        {
                            AuditHelper.BackfillTrail<Gen3Detail>(detail.Id, detail, drepo);
                        }

                        drepo.Save(detail);
                    }

                    deletedNoteIds.ToList().ForEach(id => drepo.DeleteById(id));

                }
                else
                {
                    var tm = Mapper.Map<Gen3ViewModel, TripMonitor>(vm);
                    tm.Trip = tripId;
                    tm.SetAuditTrail(User.Identity.Name, DateTime.Now);
                    var repo = TubsDataService.GetRepository<TripMonitor>(MvcApplication.CurrentSession);

                    if (!tm.IsNew())
                    {
                        AuditHelper.BackfillTrail<TripMonitor>(tm.Id, tm, repo);
                    }

                    var drepo = TubsDataService.GetRepository<TripMonitorDetail>(MvcApplication.CurrentSession);
                    foreach (var detail in tm.Details)
                    {
                        if (deletedNoteIds.Contains(detail.Id))
                        {
                            continue;
                        }

                        detail.Header = tm;
                        detail.SetAuditTrail(User.Identity.Name, DateTime.Now);
                        if (!detail.IsNew())
                        {
                            AuditHelper.BackfillTrail<TripMonitorDetail>(detail.Id, detail, drepo);
                        }
                    }

                    deletedNoteIds.ToList().ForEach(id => drepo.DeleteById(id));
                    repo.Save(tm);
                }

                xa.Commit();
            }

            if (IsApiRequest)
            {
                using (var trepo = TubsDataService.GetRepository<Trip>(false))
                {
                    var trip = trepo.FindById(tripId.Id);
                    vm = LoadViewModel(trip);
                }

                return GettableJsonNetData(vm);
            }

            // If this isn't an API request (which shouldn't really happen)
            // always push to the Edit page.  It's been saved, so an Add is counter-productive
            return RedirectToAction("Edit", "Gen3", new { tripId = tripId.Id });
        }
Example #3
0
 public void Gen3ViewModelToEntity()
 {
     // Arrange
     Mapper.AssertConfigurationIsValid();
     var vm = new Gen3ViewModel();
     vm.Incidents = new List<Gen3ViewModel.Incident>(3);
     vm.Incidents.Add(new Gen3ViewModel.Incident() { Answer = "YES", QuestionCode = "RS-A", JournalPage = 1 });
     vm.Incidents.Add(new Gen3ViewModel.Incident() { Answer = "YES", QuestionCode = "RS-C", JournalPage = 5 });
     vm.Incidents.Add(new Gen3ViewModel.Incident() { Answer = "YES", QuestionCode = "NR-A", JournalPage = 3 });
     vm.Notes = new List<Gen3ViewModel.Note>(2);
     vm.Notes.Add(new Gen3ViewModel.Note() { Date = DateTime.Now, Comments = "Xyzzy" });
     vm.Notes.Add(new Gen3ViewModel.Note() { Date = DateTime.Now, Comments = "Plover" });
     // Act
     var entity = Mapper.Map<Gen3ViewModel, MockTripMonitor>(vm);
     // Assert
     Assert.NotNull(entity);
     Assert.NotNull(entity.Answers);
     Assert.AreEqual(3, entity.Answers.Count);
     Assert.NotNull(entity.Details);
     Assert.AreEqual(2, entity.Details.Count);
 }
Example #4
0
        internal ActionResult ViewActionImpl(Trip tripId)
        {
            string formatString =
                IsEdit ?
                    "{0}: GEN-3 Edit" :
                    "{0}: GEN-3";

            ViewBag.Title = String.Format(formatString, tripId.SpcTripNumber);

            // Build ViewModel from the appropriate Trip sub entity/entities
            var vm = LoadViewModel(tripId);

            // v2007 workbooks without a TripMonitor result in a null view model
            // object.  This should fix the issue with the least amount of fuss.
            if (null == vm)
            {
                vm = new Gen3ViewModel();
                vm.TripId = tripId.Id;
                vm.VersionNumber = 2007;
                vm.TripNumber = tripId.SpcTripNumber;
            }

            // Ensure that, for v2009 workbooks, the ViewModel has 31 possible questions
            // in the same display order as the printed workbook
            vm.PrepareIncidents();

            if (IsApiRequest)
                return GettableJsonNetData(vm);

            // Min/max dates in the ViewBag are only useful outside of API calls
            AddMinMaxDates(tripId);
            return View(CurrentAction, vm);
        }