Ejemplo n.º 1
0
        public ActionResult Add(Trip tripId, int setNumber)
        {
            var repo = TubsDataService.GetRepository<PurseSeineSet>(MvcApplication.CurrentSession);

            var fset =
                repo.FilterBy(
                    s => s.Activity.Day.Trip.Id == tripId.Id && s.SetNumber == setNumber
                ).FirstOrDefault();

            // Shouldn't happen, but then again...
            if (null == fset)
                return RedirectToAction("List", new { tripId = tripId.Id });

            var vm = new LengthFrequencyViewModel();
            // Add in some properties
            vm.SetNumber = setNumber;
            vm.PageNumber = fset.SamplingHeaders.Count + 1;
            vm.PageCount = vm.PageNumber;
            vm.ActionName = "Add";
            vm.SetId = fset.Id;
            vm.TripId = tripId.Id;
            vm.TripNumber = tripId.SpcTripNumber;
            foreach (var brailNumber in Enumerable.Range(1, 30))
            {
                vm.Brails.Add(new LengthFrequencyViewModel.Brail() { Number = brailNumber });
            }

            return View("_Editor", vm);
        }
Ejemplo n.º 2
0
        public ActionResult Edit(Trip tripId, int setNumber, int pageNumber, LengthFrequencyViewModel lfvm)
        {
            var trip = tripId as PurseSeineTrip;

            if (!ModelState.IsValid)
            {
                LogModelErrors();
                if (IsApiRequest)
                    return ModelErrorsResponse();
                return View(lfvm);
            }

            var header = Mapper.Map<LengthFrequencyViewModel, LengthSamplingHeader>(lfvm);
            using (var xa = MvcApplication.CurrentSession.BeginTransaction())
            {
                // We need a PurseSeineSet repository to get the set that will be the parent for this header
                var srepo = TubsDataService.GetRepository<PurseSeineSet>(MvcApplication.CurrentSession);
                var brepo = TubsDataService.GetRepository<Brail>(MvcApplication.CurrentSession);
                var repo = TubsDataService.GetRepository<LengthSamplingHeader>(MvcApplication.CurrentSession);

                header.Set = srepo.FindById(lfvm.SetId);
                header.SetAuditTrail(User.Identity.Name, DateTime.Now);

                // Nothing is ever easy
                // If Brails was more complex, it would be worth setting it from AutoMapper
                // However, all the real work happens in an AfterMap.
                if (lfvm.BrailId != 0)
                    header.Brails = brepo.FindById(lfvm.BrailId);

                if (null == header.Brails)
                    header.Brails = new Brail();

                header.Brails.Header = header;
                header.Brails.SetAuditTrail(User.Identity.Name, DateTime.Now);

                foreach (var brail in lfvm.Brails)
                {
                    int index = brail.Number - 1;
                    if (index < 0 || index > 29)
                        continue;
                    header.Brails[index] = Mapper.Map<LengthFrequencyViewModel.Brail, BrailRecord>(brail);
                }

                // If it's not already the case, LengthSamplingHeader needs to 'own' the saving of
                // the child 'Brails' entity
                repo.Save(header);
                xa.Commit();
            }

            // Read a clean copy of the header from the database
            if (IsApiRequest)
            {
                using (var rrepo = TubsDataService.GetRepository<LengthSamplingHeader>(false))
                {
                    var xheader = rrepo.FindById(header.Id);
                    lfvm = Mapper.Map<LengthSamplingHeader, LengthFrequencyViewModel>(header);
                    lfvm.ActionName = CurrentAction;
                    return GettableJsonNetData(lfvm);
                }
            }

            // If this isn't an API request (which shouldn't really happen)
            // always push to the Edit page.
            return RedirectToAction("Edit", "Ps4", new { tripId = tripId.Id, setNumber = setNumber, pageNumber = pageNumber });
        }