Ejemplo n.º 1
0
        public Tuple<bool, string> CopyTrip(int sourceId)
        {
            bool success = false;
            string message = String.Empty;
            Tubs.Entities.Trip dest = null;

            var source = new Repository<Observer.Entities.Trip>(_sourceSession).FindBy(sourceId);
            Logger.DebugFormat("Found trip with Id {0}? {1}", sourceId, null != source);
            // Can't copy a trip you can't find
            if (null == source)
                return Tuple.Create(false, String.Format("Trip with Id {0} not found in source system", sourceId));

            // Temporary check
            var tripType = source.GetType();
            Logger.DebugFormat("Trip type: {0}", tripType);
            /*
            if (!(typeof(Observer.Entities.LongLineTrip) == tripType) || (typeof(Observer.Entities.PurseSeineTrip) == tripType))
                return Tuple.Create(false, String.Format("Trip of type {0} not yet supported", tripType));
            */
            try
            {
                dest = Mapper.Map<Observer.Entities.Trip, Tubs.Entities.Trip>(source);
                Logger.Debug("Map operation completed");
            }
            catch (Exception ex)
            {
                Logger.Error("Map operation failed with Exception", ex);
                Logger.ErrorFormat("Source: {0}", ex.Source);
                Logger.ErrorFormat("TargetSite: {0}", ex.TargetSite);
                Logger.ErrorFormat("Stack Trace: {0}", ex.StackTrace);
                Logger.ErrorFormat("Inner Exception: {0}", ex.InnerException);
                Logger.ErrorFormat("Data: {0}", ex.Data);
                return Tuple.Create(false, ex.Message ?? "Unknown mapping error");
            }

            Logger.DebugFormat("Map operation was successful? {0}", null != dest);
            if (null == dest)
                return Tuple.Create(false, "Unknown mapping error:  Map produced null target entity");

            // Quick validation
            if (null == dest.Vessel || null == dest.DeparturePort || null == dest.ReturnPort || null == dest.Observer)
            {
                string msg = String.Format(
                    "Has Vessel? {0}\nHas Departure Port? {1}\nHas Return Port {2}\nHas Observer {3}",
                    null != dest.Vessel,
                    null != dest.DeparturePort,
                    null != dest.ReturnPort,
                    null != dest.Observer
                );
                Logger.Debug(msg);
                return Tuple.Create(false, msg);
            }

            using (var session = Tubs.TubsDataService.GetSession())
            using (var xa = session.BeginTransaction())
            using (var statusRepo = new Tubs.TubsRepository<Tubs.Entities.ImportStatus>(session))
            {
                message = "Unknown error during save";
                try
                {
                    Tubs.TubsDataService.SaveFullTrip(dest);

                    var status = new Tubs.Entities.ImportStatus()
                    {
                        SourceName = "FoxPro Observer",
                        SourceId = sourceId.ToString(),
                        TripId = dest.Id,
                        EnteredBy = "TubsTripProcessor",
                        EnteredDate = DateTime.Now,
                        StatusCode = "S"
                    };
                    statusRepo.Add(status);
                    xa.Commit();
                    success = true;
                    message = String.Format("Trip saved with obstrip_id {0}", dest.Id);
                }
                catch (Exception ex)
                {
                    Logger.Error("Failed during trip save", ex);
                    message = ex.Message;
                }
            }

            return Tuple.Create(success, message);
        }
Ejemplo n.º 2
0
 private IList<TripViewModel> RemoveExisting(List<TripViewModel> trips)
 {
     using (var repo = new Tubs.TubsRepository<Tubs.Entities.TripHeader>(Tubs.TubsDataService.GetSession()))
     {
         var tripNumbers =
             from trip in repo.All().ToList()
             select trip.SpcTripNumber;
         HashSet<string> tubsTripNumbers = new HashSet<string>(tripNumbers);
         trips.RemoveAll(x => tubsTripNumbers.Contains(x.TripNumber));
     }
     return trips;
 }