public static IPoint ToGeoPoint(this Geocoding.Location location) { var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); var currentLocation = geometryFactory.CreatePoint(new NetTopologySuite.Geometries.Coordinate(location.Longitude, location.Latitude)); return((IPoint)currentLocation); }
public Truck GetTruckByLocation(Geocoding.Location location) { try { var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326); var point = geometryFactory.CreatePoint(new Coordinate(location.Longitude, location.Latitude)); return(_dbContext.Hops.OfType <Truck>().SingleOrDefault(p => p.RegionGeometry.Contains(point))); } catch (SqlException exc) { throw new DALException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } catch (Exception exc) { throw new DALException($"{exc.GetType()} Exception in {System.Reflection.MethodBase.GetCurrentMethod().Name}", exc); } }
public Parcel TrackParcel(string trackingId) { try { logger.LogInformation($"getting parcel {trackingId} from repo"); Data.Parcel dataParcel = parcelRepository.GetByTrackingId(trackingId); Parcel businessParcel = this.mapper.Map <Parcel>(dataParcel); string senderGeoString = businessParcel.Sender.ToGeoCodingString(); string recipientGeoString = businessParcel.Recipient.ToGeoCodingString(); logger.LogDebug($"converting sender geoCodingString '{senderGeoString}' to Location"); logger.LogDebug($"converting recepient geoCodingString '{recipientGeoString}' to Location"); Geocoding.Location senderAddress = geoCodingAgent.EncodeAddress(senderGeoString); Geocoding.Location recipientAddress = geoCodingAgent.EncodeAddress(recipientGeoString); Data.Hop dataSenderHop; Data.Hop dataRecipientHop; try { dataSenderHop = hopRepository.GetByCoordinates(senderAddress.Latitude, senderAddress.Longitude); dataRecipientHop = hopRepository.GetByCoordinates(recipientAddress.Latitude, recipientAddress.Longitude); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Data.Warehouse dataWarehouse; try { logger.LogDebug("load full warehouse hierarchy"); dataWarehouse = wareHouseRepository.Read(); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Hop senderHop = this.mapper.Map <Hop>(dataSenderHop); Hop recipientHop = this.mapper.Map <Hop>(dataRecipientHop); Warehouse warehouse = this.mapper.Map <Warehouse>(dataWarehouse); logger.LogDebug($"calculating route between sender {senderHop.Code} and recipeint {recipientHop.Code}"); List <HopArrival> route = routeCalculator.CalculateRoute(warehouse, senderHop.Code, recipientHop.Code, businessParcel.EntryDate); //route Datetime now List <HopArrival> soonHop = new List <HopArrival>(); List <HopArrival> pastHop = new List <HopArrival>(); foreach (HopArrival ha in route) { if (ha.DateTime < DateTime.Now) { pastHop.Add(ha); } else { soonHop.Add(ha); } } businessParcel.VisitedHops = pastHop; businessParcel.FutureHops = soonHop; return(businessParcel); } catch (DataAccessLayerException e) { throw new BusinessLayerException(); } }
public void TransferParcel_ValidParcel() { string trackingid = "ABCD12345"; float weight = 1.0f; Recipient sender = new Recipient() { Name = "Martin", City = "Wien", Country = "Österreich", PostalCode = "1100", Street = "Radnitzkygasse 16" }; Recipient recipient = new Recipient() { Name = "Simon", City = "Wien", Country = "Österreich", PostalCode = "1100", Street = "Suchenwirtplatz 10" }; Parcel p = new Parcel() { Sender = sender, Recipient = recipient, Weight = weight, TrackingId = trackingid }; // geoMock Setup Mock <IGeoCodingAgent> geoMock = new Mock <IGeoCodingAgent>(); string senderGeoString = p.Sender.ToGeoCodingString(); string recipientGeoString = p.Recipient.ToGeoCodingString(); Geocoding.Location senderLoc = new Geocoding.Location(1, 1); Geocoding.Location recipientLoc = new Geocoding.Location(2, 2); geoMock.Setup(foo => foo.EncodeAddress(recipientGeoString)).Returns(recipientLoc); geoMock.Setup(foo => foo.EncodeAddress(senderGeoString)).Returns(senderLoc); //hopMock Setup Mock <IHopRepository> hopMock = new Mock <IHopRepository>(); //var senderLoc.Latitude, senderLoc.Longitude = senderLoc.ToGeoPoint(); //var recipientLoc.Latitude, recipientLoc.Longitude = recipientLoc.ToGeoPoint(); DA.Hop senderHop = new DA.Warehouse() { Code = "ABCD", Description = "senderHop" }; DA.Hop recipientHop = new DA.Warehouse() { Code = "EFGH", Description = "recipientHop" }; hopMock.Setup(foo => foo.GetByCoordinates(senderLoc.Latitude, senderLoc.Longitude)).Returns(senderHop); hopMock.Setup(foo => foo.GetByCoordinates(recipientLoc.Latitude, recipientLoc.Longitude)).Returns(recipientHop); // wareMock Setup Mock <IWarehouseRepository> wareMock = new Mock <IWarehouseRepository>(); DA.Warehouse fullWa = new DA.Warehouse() { Code = "A1B2", Description = "fullWa" }; wareMock.Setup(foo => foo.Read()).Returns(fullWa); // routeMock Setup Mock <IRouteCalculator> routeMock = new Mock <IRouteCalculator>(); Warehouse fullWaBusiness = this.mapper.Map <Warehouse>(fullWa); Hop senderHopBusiness = this.mapper.Map <Hop>(senderHop); Hop recipientHopBusiness = this.mapper.Map <Hop>(recipientHop); List <HopArrival> arrivals = new List <HopArrival>(); arrivals.Add(new HopArrival(senderHopBusiness)); arrivals.Add(new HopArrival(recipientHopBusiness)); routeMock.Setup(foo => foo.CalculateRoute(fullWaBusiness, senderHop.Code, recipientHop.Code)).Returns(arrivals); // parcelMock Setup Mock <IParcelRepository> parcelMock = new Mock <IParcelRepository>(); parcelMock.Setup(foo => foo.Create(It.IsAny <DA.Parcel>())); ILogisticsPartnerLogic logisticsPartnerLogic = new LogisticsPartnerLogic(mapper, parcelMock.Object, wareMock.Object, hopMock.Object, geoMock.Object, routeMock.Object, NullLogger <LogisticsPartnerLogic> .Instance); string parcelID = logisticsPartnerLogic.TransferParcel(p.TrackingId, p); Assert.Equal(trackingid, parcelID); }
public void TransferParcel_ThrowsNoHopException() { string trackingid = "ABCD12345"; float weight = 1.0f; Recipient sender = new Recipient() { Name = "Martin", City = "Wien", Country = "Österreich", PostalCode = "1100", Street = "Radnitzkygasse 16" }; Recipient recipient = new Recipient() { Name = "Simon", City = "Wien", Country = "Österreich", PostalCode = "1100", Street = "Suchenwirtplatz 10" }; Parcel p = new Parcel() { Sender = sender, Recipient = recipient, Weight = weight, TrackingId = trackingid }; // geoMock Setup Mock <IGeoCodingAgent> geoMock = new Mock <IGeoCodingAgent>(); string senderGeoString = p.Sender.ToGeoCodingString(); string recipientGeoString = p.Recipient.ToGeoCodingString(); Geocoding.Location senderLoc = new Geocoding.Location(1, 1); Geocoding.Location recipientLoc = new Geocoding.Location(2, 2); geoMock.Setup(foo => foo.EncodeAddress(recipientGeoString)).Returns(recipientLoc); geoMock.Setup(foo => foo.EncodeAddress(senderGeoString)).Returns(senderLoc); //hopMock Setup Mock <IHopRepository> hopMock = new Mock <IHopRepository>(); //var senderLoc.Latitude, senderLoc.Longitude = senderLoc.ToGeoPoint(); //var recipientLoc.Latitude, recipientLoc.Longitude = recipientLoc.ToGeoPoint(); DA.Hop senderHop = new DA.Warehouse() { Code = "ABCD", Description = "senderHop" }; DA.Hop recipientHop = new DA.Warehouse() { Code = "EFGH", Description = "recipientHop" }; hopMock.Setup(foo => foo.GetByCoordinates(senderLoc.Latitude, senderLoc.Longitude)).Returns((DA.Hop)null); hopMock.Setup(foo => foo.GetByCoordinates(recipientLoc.Latitude, recipientLoc.Longitude)).Returns((DA.Hop)null); // wareMock Setup Mock <IWarehouseRepository> wareMock = new Mock <IWarehouseRepository>(); // routeMock Setup Mock <IRouteCalculator> routeMock = new Mock <IRouteCalculator>(); // parcelMock Setup Mock <IParcelRepository> parcelMock = new Mock <IParcelRepository>(); ILogisticsPartnerLogic logisticsPartnerLogic = new LogisticsPartnerLogic(mapper, parcelMock.Object, wareMock.Object, hopMock.Object, geoMock.Object, routeMock.Object, NullLogger <LogisticsPartnerLogic> .Instance); Assert.Throws <NoHopException>(() => logisticsPartnerLogic.TransferParcel(p.TrackingId, p)); }
public void ReportParcelHop(string parcelID, string code) { Data.Parcel dataParcel; try { logger.LogDebug($"getting parcel {parcelID} from repo"); dataParcel = parcelRepository.GetByTrackingId(parcelID); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Data.Hop dataHop; try { logger.LogDebug($"getting hop {code} from repo"); dataHop = hopRepository.GetByCode(code); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Parcel businessParcel = this.mapper.Map <Parcel>(dataParcel); Hop businessHop = this.mapper.Map <Hop>(dataHop); businessParcel.ReportHop(businessHop); if (code == businessParcel.FutureHops[0].Hop.Code) { businessParcel.VisitedHops.Add(businessParcel.FutureHops[0]); businessParcel.FutureHops.Remove(businessParcel.FutureHops[0]); } /* * if (code == businessParcel.FutureHops[0].Code) * { * switch (businessHop.) * { * case "Warehouse": * businessParcel.State = BL.Entities.Parcel.StateEnum.InTransportEnum; * * break; * case "Truck": * businessParcel.State = BL.Entities.Parcel.StateEnum.InTruckDeliveryEnum; * * break; * case "Transferwarehouse": * TransferToWarehouse(businessParcel, hop); * businessParcel.State = BL.Entities.Parcel.StateEnum.TransferredEnum; * * break; * default: * break; * } * * * webHookAgent.NotifyStateChanged(dalParcel); * * businessParcel.VisitedHops.Add(dalParcel.FutureHops[0]); * businessParcel.FutureHops.Remove(dalParcel.FutureHops[0]); * * parcelRepository.Update(dalParcel); * * } */ string senderGeoString = businessParcel.Sender.ToGeoCodingString(); string recipientGeoString = businessParcel.Recipient.ToGeoCodingString(); logger.LogDebug($"converting sender geoCodingString '{senderGeoString}' to Location"); logger.LogDebug($"converting recepient geoCodingString '{recipientGeoString}' to Location"); Geocoding.Location senderAddress = geoCodingAgent.EncodeAddress(senderGeoString); Geocoding.Location recipientAddress = geoCodingAgent.EncodeAddress(recipientGeoString); Data.Hop dataSenderHop; Data.Hop dataRecipientHop; try { dataSenderHop = hopRepository.GetByCoordinates(senderAddress.Latitude, senderAddress.Longitude); dataRecipientHop = hopRepository.GetByCoordinates(recipientAddress.Latitude, recipientAddress.Longitude); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } if (dataSenderHop == null || dataRecipientHop == null) { string errorMessage = "Error"; NoHopException e = new NoHopException(errorMessage); logger.LogError(e, $"No Hop found"); throw e; } Data.Warehouse dataWarehouse; try { logger.LogDebug("load full warehouse hierarchy"); dataWarehouse = wareHouseRepository.Read(); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Hop senderHop = this.mapper.Map <Hop>(dataSenderHop); Hop recipientHop = this.mapper.Map <Hop>(dataRecipientHop); Warehouse warehouse = this.mapper.Map <Warehouse>(dataWarehouse); logger.LogDebug($"calculating route betweend sender {senderHop.Code} and recipeint {recipientHop.Code}"); List <HopArrival> route = routeCalculator.CalculateRoute(warehouse, senderHop.Code, recipientHop.Code, businessParcel.EntryDate); bool checkIfOnRoute = false; foreach (HopArrival ha in route) { if (ha.Hop.Code == businessHop.Code) { checkIfOnRoute = true; break; } } if (!checkIfOnRoute) { throw new BusinessLayerException("Hop is not on TravelRoute"); } try { logger.LogInformation($"updating parcel {parcelID}"); parcelRepository.Update(this.mapper.Map <Data.Parcel>(businessParcel)); List <Data.Webhook> dataWebhooks = webhookRepository.GetByTrackingId(parcelID); List <Webhook> webhooks = new List <Webhook>(); dataWebhooks.ForEach(hook => webhooks.Add(this.mapper.Map <Webhook>(hook))); NotifyAllSubscribers(webhooks); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } }
public string TransferParcel(string parcelID, Parcel parcel) { string senderGeoString = parcel.Sender.ToGeoCodingString(); string recipientGeoString = parcel.Recipient.ToGeoCodingString(); logger.LogDebug($"converting sender geoCodingString '{senderGeoString}' to Location"); logger.LogDebug($"converting recepient geoCodingString '{recipientGeoString}' to Location"); Geocoding.Location senderAddress = geoCodingAgent.EncodeAddress(senderGeoString); Geocoding.Location recipientAddress = geoCodingAgent.EncodeAddress(recipientGeoString); Data.Hop dataSenderHop; Data.Hop dataRecipientHop; try { dataSenderHop = hopRepository.GetByCoordinates(senderAddress.Latitude, senderAddress.Longitude); dataRecipientHop = hopRepository.GetByCoordinates(recipientAddress.Latitude, recipientAddress.Longitude); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } if (dataSenderHop == null || dataRecipientHop == null) { string errorMessage = "Error"; NoHopException e = new NoHopException(errorMessage); logger.LogError(e, $"No Hop found"); throw e; } Data.Warehouse dataWarehouse; try { logger.LogDebug("load full warehouse hierarchy"); dataWarehouse = wareHouseRepository.Read(); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } Hop senderHop = this.mapper.Map <Hop>(dataSenderHop); Hop recipientHop = this.mapper.Map <Hop>(dataRecipientHop); Warehouse warehouse = this.mapper.Map <Warehouse>(dataWarehouse); logger.LogDebug($"Submiting parcel {parcelID}"); parcel.Submit(parcelID); logger.LogDebug($"calculating route betweend sender {senderHop.Code} and recipeint {recipientHop.Code}"); List <HopArrival> route = routeCalculator.CalculateRoute(warehouse, senderHop.Code, recipientHop.Code, parcel.EntryDate); logger.LogDebug($"validating parcel ({parcelID})"); ParcelValidator validator = new ParcelValidator(); ValidationResult result = validator.Validate(parcel); if (result.IsValid) { logger.LogDebug($"Parcel ({parcelID}) was valid"); Data.Parcel dataParcel = this.mapper.Map <Data.Parcel>(parcel); logger.LogInformation("Creating Parcel DB entry"); try { parcelRepository.Create(dataParcel); return(parcelID); } catch (DataAccessLayerException e) { throw new BusinessLayerException("DAL Exception", e); } } else { InvalidParcelException e = new InvalidParcelException(string.Join("", result.Errors)); logger.LogError(e, $"Parcel ({parcelID}) was invalid"); throw e; } }