Exemple #1
0
 public int Create(HopArrival h)
 {
     h.Id = h_id;
     h_id++;
     hopArrivals.Add(h);
     return(h.Id);
 }
Exemple #2
0
 public int Create(HopArrival h)
 {
     try
     {
         _db.Add(h);
         _db.SaveChanges();
         return(h.Id);
     }
     catch (SqlException ex)
     {
         _logger.LogError(ExceptionHelper.BuildSqlExceptionMessage(ex));
         throw new DalException("Could not save hopArrival to database", ex);
     }
     catch (Exception ex)
     {
         _logger.LogError("Could not save hopArrival to database", ex);
         throw new DalException("Could not save hopArrival to database", ex);
     }
 }
Exemple #3
0
 public void Update(HopArrival h)
 {
     try
     {
         var hopArrivalToUpdate = _db.HopArrivals.Single(b => b.Id == h.Id);
         if (hopArrivalToUpdate != null)
         {
             hopArrivalToUpdate = h;
             _db.SaveChanges();
         }
     }
     catch (SqlException ex)
     {
         _logger.LogError(ExceptionHelper.BuildSqlExceptionMessage(ex));
         throw new DalException("Could not update hopArrival in database", ex);
     }
     catch (Exception ex)
     {
         _logger.LogError("Could not update hopArrival in database", ex);
         throw new DalException("Could not update hopArrival in database", ex);
     }
 }
        public static void Initialize(DbContext context)
        {
            context.Database.EnsureCreated();

            // Look for any warehouses
            if (context.Warehouses.Any())
            {
                return;                   // DB has been seeded
            }

            var recipients = new Recipient[]
            {
                new Recipient {
                    FirstName = "Turanga", LastName = "Leela", Street = "Schlagergasse 1", City = "Wien", PostalCode = "A-1090"
                },
                new Recipient {
                    FirstName = "Hubert", LastName = "Farnsworth", Street = "Donatusgasse 2", City = "Linz", PostalCode = "A-4020"
                }
            };

            foreach (Recipient s in recipients)
            {
                context.Recipients.Add(s);
            }
            context.SaveChanges();

            var trucks = new Truck[]
            {
                new Truck {
                    Code = "TR01", Duration = 1m, Latitude = 48.2089816m, Longitude = 16.373213299999975m, Radius = 30m, NumberPlate = "WR-2765"
                },                                                                                                                                           //Wien Stephansplatz
                new Truck {
                    Code = "TR02", Duration = 1m, Latitude = 48.2089816m, Longitude = 16.373213299999975m, Radius = 30m, NumberPlate = "WR-2788"
                },                                                                                                                                           //Wien Stephansplatz
            };
            var trucks2 = new Truck[]
            {
                new Truck {
                    Code = "TR03", Duration = 1m, Latitude = 48.3059826m, Longitude = 14.287141199999951m, Radius = 30m, NumberPlate = "WR-2777"
                },                                                                                                                                           //Linz Hauptplatz
                new Truck {
                    Code = "TR04", Duration = 1m, Latitude = 48.85661400000001m, Longitude = 2.3522219000000177m, Radius = 30m, NumberPlate = "WR-2739"
                },                                                                                                                                                  //Paris
            };

            foreach (Truck c in trucks)
            {
                context.Trucks.Add(c);
            }
            foreach (Truck c in trucks2)
            {
                context.Trucks.Add(c);
            }
            context.SaveChanges();

            var w01 = new Warehouse {
                Code = "WH01", Description = "Superwarehouse 01", Duration = 2m, Trucks = new List <Truck>(), NextHops = new List <Warehouse>()
            };
            var w02 = new Warehouse {
                Code = "WH02", Description = "Warehouse 02", Duration = 3m, Trucks = new List <Truck>(), NextHops = new List <Warehouse>()
            };
            var w03 = new Warehouse {
                Code = "WH03", Description = "Warehouse 03", Duration = 1m, Trucks = trucks.ToList(), NextHops = new List <Warehouse>()
            };
            var w04 = new Warehouse {
                Code = "WH04", Description = "Warehouse 04", Duration = 2m, Trucks = trucks2.ToList(), NextHops = new List <Warehouse>()
            };

            w01.NextHops.Add(w02);
            w02.NextHops.Add(w03);
            w02.NextHops.Add(w04);
            var warehouses = new Warehouse[]
            {
                w01,
                w02,
                w03,
                w04
            };

            foreach (Warehouse e in warehouses)
            {
                context.Warehouses.Add(e);
            }
            context.SaveChanges();

            var trackingInformations = new TrackingInformation[]
            {
                new TrackingInformation {
                    State = TrackingInformation.StateEnum.DeliveredEnum
                },
                new TrackingInformation {
                    State = TrackingInformation.StateEnum.InTransportEnum
                }
            };

            foreach (TrackingInformation e in trackingInformations)
            {
                context.TrackingInformations.Add(e);
            }
            context.SaveChanges();

            var hopArrivals = new HopArrival[]
            {
                new HopArrival {
                    DateTime = DateTime.Parse("2017-11-09"), Code = "WH01", Status = "visited", TrackingInformationId = 1
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2017-11-10"), Code = "WH02", Status = "visited", TrackingInformationId = 1
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2017-11-11"), Code = "WH03", Status = "future", TrackingInformationId = 1
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2017-11-12"), Code = "TR01", Status = "future", TrackingInformationId = 1
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2018-10-02"), Code = "WH01", Status = "future", TrackingInformationId = 2
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2018-10-03"), Code = "WH02", Status = "future", TrackingInformationId = 2
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2018-10-04"), Code = "WH03", Status = "future", TrackingInformationId = 2
                },
                new HopArrival {
                    DateTime = DateTime.Parse("2018-10-05"), Code = "TR01", Status = "future", TrackingInformationId = 2
                },
            };

            foreach (HopArrival e in hopArrivals)
            {
                context.HopArrivals.Add(e);
            }
            context.SaveChanges();

            var parcels = new Parcel[]
            {
                new Parcel {
                    RecipientId = 1, Weight = 22, TrackingNumber = "TN000001", TrackingInformationId = 1
                },
                new Parcel {
                    RecipientId = 2, Weight = 10, TrackingNumber = "TN000002", TrackingInformationId = 2
                },
            };

            foreach (Parcel e in parcels)
            {
                context.Parcels.Add(e);
            }
            context.SaveChanges();
        }
Exemple #5
0
        public bool StaffReportHop(string trackingId, string code)
        {
            try
            {
                //Get Data from DB and check if exists
                var parcelDal = _sqlRepoParcel.GetByTrackingID(trackingId);
                var hopDal    = _sqlRepoHop.GetByCode(code);
                if (parcelDal == null || hopDal == null || parcelDal.FutureHops.Count == 1 || parcelDal.FutureHops[0].Code != code)
                {
                    return(false);
                }
                //Validation
                var parcelBL = _mapper.Map <Parcel>(parcelDal);
                var hopBL    = new HopArrival()
                {
                    Code     = code,
                    DateTime = DateTime.Now
                };
                var validatorParcel     = new ParcelValidation();
                var validatorHopArrival = new HopArrivalValidation();
                var checkParcel         = validatorParcel.Validate(parcelBL);
                var checkHop            = validatorHopArrival.Validate(hopBL);
                //check if Parcel and Hop is Valid and the next Hop is the same as the incoming Hop
                if (!checkParcel.IsValid && !checkHop.IsValid)
                {
                    return(false);
                }

                //Change State
                var hopArrival = _mapper.Map <DAL.HopArrival>(hopBL);
                if (hopDal.HopType == "Truck")
                {
                    parcelDal.State = DAL.Parcel.StateEnum.InTruckDeliveryEnum;
                }
                if (hopDal.HopType == "Transferwarehouse")
                {
                    DAL.Transferwarehouse post = (DAL.Transferwarehouse)hopDal;

                    string url = $"{post.LogisticsPartnerUrl}/parcel/{trackingId}";
                    HttpResponseMessage msg = SendParcelToPartner(url, parcelBL);

                    if (msg.StatusCode != HttpStatusCode.OK)
                    {
                        throw new Exception($"Could not POST Parcel to Partner {post.LogisticsPartnerUrl}");
                    }
                    parcelDal.State = DAL.Parcel.StateEnum.DeliveredEnum;
                }
                if (hopDal.HopType == "Warehouse")
                {
                    parcelDal.State = DAL.Parcel.StateEnum.InTransportEnum;
                }
                // Update Visited/Future Hops
                parcelDal.FutureHops.RemoveAt(0);
                parcelDal.VisitedHops.Add(hopArrival);
                _sqlRepoParcel.Update(parcelDal);

                //Contact Webhook Subscriber
                List <DAL.Webhook> contactList = _webrep.GetWebhooksByTrackingID(trackingId);
                var webhookparcel = _mapper.Map <Parcel>(parcelDal);
                foreach (var hook in contactList)
                {
                    HttpResponseMessage msg = SendWebhookResponse(hook.Url, webhookparcel);
                }
                return(true);
            }
            catch (DAL.DALException exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
        }
Exemple #6
0
        public DTO.NewParcelInfo SubmitParcelIntoBL(Parcel parcel)
        {
            try
            {
                var validator   = new ParcelValidation();
                var checkParcel = validator.Validate(parcel);

                if (!checkParcel.IsValid)
                {
                    throw new BLException("BL: Parcel Validation failed");
                }

                //check if parcel.trackingId is set -> only happens if it is from a Logistic Partner
                if (parcel.TrackingId == null || _sqlRepoParcel.GetByTrackingID(parcel.TrackingId) != null)
                {
                    //generate new trackingId until it is a unique
                    do
                    {
                        parcel.TrackingId = GenerateTrackingId();
                    } while (_sqlRepoParcel.GetByTrackingID(parcel.TrackingId) != null);
                }
                //Set State to InTransport
                parcel.State = Parcel.StateEnum.InTransportEnum;

                //Encode Sender and Receipient into a Location

                Location recLocation  = _agent.EncodeGeocodeAsync(GenerateAddress(parcel.Receipient));
                Location sendLocation = _agent.EncodeGeocodeAsync(GenerateAddress(parcel.Sender));

                //Find responsible Truck for Sender - START
                //currently only for austria -> NO Transferwarehouse

                var        senderTruck = _mapper.Map <Truck>(_sqlRepoHop.GetTruckByLocation(sendLocation));
                HopArrival firstHop    = new HopArrival()
                {
                    Code     = senderTruck.Code,
                    DateTime = DateTime.Now
                };
                parcel.VisitedHops.Add(firstHop);

                //Find responsible Truck for Recipient - FINISH
                var recTruck = _mapper.Map <Truck>(_sqlRepoHop.GetTruckByLocation(recLocation));

                //Calculate Route

                var rcptHopList   = GetRouteToRoot(recTruck);
                var senderHopList = GetRouteToRoot(senderTruck);

                //var intersect = rcptHopList.Intersect(senderHopList,EqualityComparerFactory.Create<WarehouseNextHops>((a) => a.HopACode.GetHashCode(), (a, b) =))
                bool found        = false;
                Hop  intersection = null;
                foreach (var senderStep in senderHopList)
                {
                    if (found == false)
                    {
                        foreach (var step in rcptHopList)
                        {
                            if (senderStep.Code == step.Code)
                            {
                                intersection = senderStep;
                                found        = true;
                                break;
                            }
                        }
                    }
                }
                //Get path to intersection
                var rcptJourney   = rcptHopList.TakeWhile(p => p.Code != intersection.Code).ToList();
                var senderJourney = senderHopList.TakeWhile(p => p.Code != intersection.Code).ToList();
                senderJourney.Add(intersection);
                //reverse rcptJourney
                rcptJourney.Reverse();
                senderJourney.AddRange(rcptJourney);
                senderJourney.Add(recTruck);
                foreach (var step in senderJourney)
                {
                    HopArrival futureHop = new HopArrival()
                    {
                        Code     = step.Code,
                        DateTime = DateTime.Now
                    };
                    parcel.FutureHops.Add(futureHop);
                }


                var parcelDAL = _mapper.Map <DAL.Parcel>(parcel);
                _sqlRepoParcel.Create(parcelDAL);

                var NPInfoMapped         = _mapper.Map <DTO.NewParcelInfo>(parcel);
                DTO.NewParcelInfo NPInfo = (DTO.NewParcelInfo)NPInfoMapped;

                return(NPInfo);
            }
            catch (DAL.DALException exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
            catch (Exception exc)
            {
                _logger.LogError(exc.ToString());
                throw new BLException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc);
            }
        }
Exemple #7
0
        public void Update(HopArrival h)
        {
            HopArrival h2 = hopArrivals.Find(item => item.Id == h.Id);

            h = h2;
        }
Exemple #8
0
        public void Delete(int id)
        {
            HopArrival h = hopArrivals.SingleOrDefault(item => item.Id == id);

            hopArrivals.Remove(h);
        }