Exemple #1
0
        public ActionResult Create(TripHeaderViewModel thvm)
        {
            // After much hassle with native DateTime objects, I've bowed to reality and
            // decided to transport the dates via string and do parsing here.

            // TODO:  Validate time only values via RegEx

            var departDate = thvm.DepartureDateOnly.Parse().Merge(thvm.DepartureTimeOnly);
            Logger.WarnFormat("departDate: {0}", departDate);
            if (!departDate.HasValue)
                ModelState["DepartureDateOnly"].Errors.Add("Missing or invalid departure date");

            var returnDate = thvm.ReturnDateOnly.Parse().Merge(thvm.ReturnTimeOnly);
            Logger.WarnFormat("returnDate: {0}", returnDate);
            if (!returnDate.HasValue)
                ModelState["ReturnDateOnly"].Errors.Add("Missing or invalid return date");

            if (returnDate.HasValue && departDate.HasValue && returnDate.Value.CompareTo(departDate.Value) < 0)
            {
                ModelState["ReturnDate"].Errors.Add("Return date can't be before departure date");
            }

            thvm.DepartureDate = departDate;
            thvm.ReturnDate = returnDate;

            // Check to see if the observer code/trip number combo already exists
            var repo = TubsDataService.GetRepository<Trip>(MvcApplication.CurrentSession);
            //var repo = new TubsRepository<Trip>(MvcApplication.CurrentSession);
            int existingId = (
                from t in repo.FilterBy(t => t.Observer.StaffCode == thvm.ObserverCode && t.TripNumber == thvm.TripNumber)
                select t.Id).FirstOrDefault<int>();

            // TODO:  Replace 'Flash' with toastr
            if (existingId != default(int))
            {
                string message =
                    String.Format(
                        "Trip with obstripId {0} already has this observer/trip number combination",
                        existingId);
                ModelState["ObserverCode"].Errors.Add(message);
                ModelState["TripNumber"].Errors.Add(message);
                Logger.DebugFormat("Found observer/tripNumber violation.  Existing obstripId={0}");
                // TODO Redirect to some error page that includes link to existing trip
                Flash(message);
                return View(thvm);
            }

            Trip trip = thvm.ToTrip();
            if (null == trip)
            {
                var message = String.Format("[{0}] is an unsupported gear code", thvm.GearCode);
                ModelState["GearCode"].Errors.Add(message);
            }

            // Add Vessel, Observer and Ports
            trip.FillDependentObjects(thvm, MvcApplication.CurrentSession);

            // This method fills in ModelState errors
            ValidateTripDependencies(trip, thvm);

            Logger.DebugFormat("ModelState.IsValid? {0}", ModelState.IsValid);
            if (!ModelState.IsValid)
            {
                Flash("Fix the errors below and try again");
                return View(thvm);
            }

            // Set audit trail data
            trip.SetAuditTrail(User.Identity.Name, DateTime.Now);

            try
            {
                repo.Add(trip);
                // Push to detail page for this trip
                return RedirectToAction("Details", "Trip", new { tripId = trip.Id });
            }
            catch (Exception ex)
            {
                Logger.Error("Failed to save trip header", ex);
                Flash("Failed to register trip.  Please contact technical support.");
            }

            return View(thvm);
        }
Exemple #2
0
        /// <summary>
        /// Check that the required dependent objects are present.
        /// </summary>
        /// <param name="trip"></param>
        /// <param name="thvm"></param>
        internal void ValidateTripDependencies(Trip trip, TripHeaderViewModel thvm)
        {
            // These shouldn't happen, so in addition to notifying user, drop a warning message into the application log
            if (null == trip.Observer)
            {
                var message = String.Format("Can't find observer with name [{0}] and staff code [{1}]", thvm.ObserverFullName, thvm.ObserverCode);
                Logger.Warn(message);
                ModelState["ObserverName"].Errors.Add(message);
            }

            if (null == trip.Vessel)
            {
                var message = String.Format("Can't find vessel with name [{0}]", thvm.VesselName);
                Logger.Warn(message);
                ModelState["VesselName"].Errors.Add(message);
            }

            if (null == trip.DeparturePort)
            {
                var message = String.Format("Can't find port with name [{0}] and port code [{1}]", thvm.DeparturePortName, thvm.DeparturePortCode);
                Logger.Warn(message);
                ModelState["DeparturePortName"].Errors.Add(message);
            }

            if (null == trip.ReturnPort)
            {
                var message = String.Format("Can't find port with name [{0}] and port code [{1}]", thvm.ReturnPortName, thvm.ReturnPortCode);
                Logger.Warn(message);
                ModelState["ReturnPortName"].Errors.Add(message);
            }
        }
Exemple #3
0
 public ActionResult Create()
 {
     // Drive program code and country code if this is an in-country installation
     string defaultProgramCode = WebConfigurationManager.AppSettings["DefaultProgramCode"];
     string defaultCountryCode = WebConfigurationManager.AppSettings["DefaultCountryCode"];
     var thvm = new TripHeaderViewModel()
     {
         ProgramCode = defaultProgramCode,
         CountryCode = defaultCountryCode
     };
     return View(thvm);
 }