Example #1
0
        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();
            }
        }
Example #2
0
        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;
            }
        }
Example #3
0
        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);
            }
        }
Example #4
0
 public List <BusLineRoute> CalculateRoute(string fromStopName, string toStopName)
 {
     return(routeCalculator.CalculateRoute(fromStopName, toStopName));
 }