Esempio n. 1
0
        public void LongLineSampleRoundTrip([Values(26)] int setId)
        {
            Mapper.AssertConfigurationIsValid();
            using (var repo = TubsDataService.GetRepository<LongLineSet>(false))
            {
                var fset = repo.FindById(setId);
                Assert.IsNotNull(fset);
                // Convert to intermediate object
                var header = new LongLineCatchHeader();
                header.FishingSet = fset;
                header.Samples = fset.CatchList;

                var vm = Mapper.Map<LongLineCatchHeader, LongLineSampleViewModel>(header);
                Assert.NotNull(vm);

                var entity = Mapper.Map<LongLineSampleViewModel, LongLineCatchHeader>(vm);
                Assert.IsNotNull(entity);
                Assert.AreEqual(fset.CatchList.Count, entity.Samples.Count);
                for (int i = 0; i < fset.CatchList.Count; i++)
                {
                    var catch_ori = fset.CatchList[i];
                    Assert.IsNotNull(catch_ori);
                    var catch_rt = entity.Samples[i];
                    Assert.IsNotNull(catch_rt);

                    Assert.AreEqual(catch_ori.Id, catch_rt.Id);
                    Assert.AreEqual(catch_ori.SpeciesCode, catch_rt.SpeciesCode);
                    Assert.AreEqual(catch_ori.FateCode, catch_rt.FateCode);
                    Assert.AreEqual(catch_ori.Date, catch_rt.Date);
                }

            }
        }
        public ActionResult Edit(Trip tripId, int setNumber, LongLineSampleViewModel vm)
        {
            var trip = tripId as LongLineTrip;
            // TODO Validation
            // Possible validations:
            // 1)  Any sample date less than start of haul
            // 2)  Length out of range (really needs to be on client side too)

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

            var header = Mapper.Map<LongLineSampleViewModel, LongLineCatchHeader>(vm);

            using (var xa = MvcApplication.CurrentSession.BeginTransaction())
            {
                // We need a LongLineSet repository to get the set that will be the parent for all the catch
                IRepository<LongLineSet> srepo = TubsDataService.GetRepository<LongLineSet>(MvcApplication.CurrentSession);
                IRepository<LongLineCatch> crepo = TubsDataService.GetRepository<LongLineCatch>(MvcApplication.CurrentSession);

                vm.DeletedCatch.ToList().ForEach(id => crepo.DeleteById(id));

                var fset = srepo.FindById(vm.SetId);

                int index = 1;
                foreach (var sample in header.Samples.OrderBy(s => s.Date))
                {
                    sample.SampleNumber = index;
                    sample.FishingSet = fset;
                    sample.SetAuditTrail(User.Identity.Name, DateTime.Now);
                    crepo.Save(sample);
                    ++index;
                }

                // MeasuringInstrument is recorded on the LongLineSet entity
                // Save here, after samples, to avoid transient object problems
                fset.MeasuringInstrument = vm.MeasuringInstrument.MeasuringInstrumentFromString();
                srepo.Save(fset);

                xa.Commit();

            }

            if (IsApiRequest)
            {
                using (var repo = TubsDataService.GetRepository<LongLineSet>(false))
                {
                    var updatedSet = repo.FindById(vm.SetId);
                    var xheader = new LongLineCatchHeader();
                    xheader.SetId = updatedSet.Id;
                    xheader.FishingSet = updatedSet;
                    xheader.Samples = updatedSet.CatchList;

                    var svm = Mapper.Map<LongLineCatchHeader, LongLineSampleViewModel>(xheader);
                    // Set some properties that AutoMapper can't manage for us
                    svm.ActionName = CurrentAction;
                    svm.HasNext = vm.HasNext;
                    svm.HasPrevious = setNumber > 1;
                    svm.SetCount = vm.SetCount;

                    return GettableJsonNetData(svm);
                }
            }

            // 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
            // (besides, redirecting to Add with the current dayNumber will redirect to Edit anyways...)
            return RedirectToAction("Edit", "LongLineSampling", new { tripId = tripId.Id, setNumber = setNumber });
        }
Esempio n. 3
0
        public void LongLineSampleToViewModel([Values(25)] int setId)
        {
            Mapper.AssertConfigurationIsValid();
            using (var repo = TubsDataService.GetRepository<LongLineSet>(false))
            {
                var fset = repo.FindById(setId);
                Assert.IsNotNull(fset);
                // Convert to intermediate object
                var header = new LongLineCatchHeader();
                header.FishingSet = fset;
                header.Samples = fset.CatchList;

                var vm = Mapper.Map<LongLineCatchHeader, LongLineSampleViewModel>(header);
                Assert.NotNull(vm);

                Assert.AreEqual(fset.CatchList.Count, vm.Details.Count);
                Assert.True(fset.MeasuringInstrument.HasValue);
                Assert.AreEqual(MeasuringInstrument.C, fset.MeasuringInstrument);
                Assert.AreEqual("Aluminum Caliper", vm.MeasuringInstrument);

            }
        }
        internal override ActionResult ViewActionImpl(Trip tripId, int setNumber)
        {
            // This should never happen, but a little defensive coding goes a long way
            var trip = tripId as LongLineTrip;
            var sets =
                TubsDataService.GetRepository<LongLineSet>(MvcApplication.CurrentSession)
                    .FilterBy(s => s.Trip.Id == trip.Id);

            // If I was just a _bit_ smarter, I could probably figure out how to extract the
            // following 10 lines into something that can be used between both PS and LL
            // data.

            // This LINQ method results in a trip to the database
            // (select count(1) from ... where obstrip_id = ?)
            // It's also worth mentioning that if an Add/Edit has
            // a 'bad' setNumber param, we'll be running this query twice
            // I expect SQL Server to be able to handle that...
            int maxSets = sets.Count();
            var checkpoint = NeedsRedirect(trip.Id, setNumber, maxSets);
            if (checkpoint.Item1)
                return new RedirectToRouteResult(checkpoint.Item2);

            // One minor point.  If the user passes in a completely crazy setNumber for Index
            // we'll re-interpret based on intent.
            if (IsIndex)
            {
                if (setNumber < 1) { setNumber = 1; }
                if (setNumber > maxSets) { setNumber = maxSets; }
            }

            // Based on NeedsRedirect, we should be okay -- the
            // setNumber should be perfect for the action
            var fset = (
                from s in sets
                where s.SetNumber == setNumber
                select s).FirstOrDefault();

            // This is where it gets tricky.  LL doesn't have a "header" object for all bio samples
            // in a set -- it's just a list of entities hanging off the set.  AutoMapper doesn't work
            // very well in that situation.
            var header = new LongLineCatchHeader();
            header.SetId = fset.Id;
            header.FishingSet = fset;
            header.Samples = fset.CatchList;

            var svm = Mapper.Map<LongLineCatchHeader, LongLineSampleViewModel>(header);
            // Set some properties that AutoMapper can't manage for us
            svm.ActionName = CurrentAction;
            svm.HasNext = setNumber < maxSets;
            svm.HasPrevious = setNumber > 1;
            svm.SetCount = maxSets;

            if (IsApiRequest)
                return GettableJsonNetData(svm);

            return View(CurrentAction, svm);
        }