public int Handle(WoeIdByCoordinates query)
        {
            if (query == null)
            {
                throw new ArgumentNullException("query");
            }

            int?      woeId             = null;
            var       retries           = 0;
            const int retryLimit        = 6;
            Result    placeFinderResult = null;
            // ReSharper disable PossibleInvalidOperationException
            var latitude  = query.Coordinates.Latitude.Value;
            var longitude = query.Coordinates.Longitude.Value;

            // ReSharper restore PossibleInvalidOperationException

            while (!woeId.HasValue && retries++ < retryLimit)
            {
                placeFinderResult = _placeFinder.Find(
                    new PlaceByCoordinates(latitude, longitude)).FirstOrDefault();
                if (placeFinderResult != null)
                {
                    woeId = placeFinderResult.WoeId;
                }
                if (!woeId.HasValue)
                {
                    latitude  += 0.00001;
                    longitude += 0.00001;
                }
            }

            if (!woeId.HasValue && placeFinderResult != null)
            {
                var freeformText = string.Format("{0} {1}", placeFinderResult.CityName, placeFinderResult.CountryName);
                var result       = _placeFinder.Find(new PlaceByFreeformText(freeformText)).FirstOrDefault();
                if (result != null)
                {
                    woeId = result.WoeId;
                }
            }

            if (woeId.HasValue)
            {
                return(woeId.Value);
            }
            return(GeoPlanetPlace.EarthWoeId);
        }
Exemple #2
0
        //[Authorize(Users = "*****@*****.**")]
        public virtual PartialViewResult FindPlaces(double latitude, double longitude)
        {
            var results = _placeFinder.Find(new PlaceByCoordinates(latitude, longitude))
                          .Where(y => y.WoeId != null).ToList();

            if (results.Count == 1)
            {
                var woeId = results.Single().WoeId;
                if (woeId != null)
                {
                    //var place = _placeFactory.FromWoeId(woeId.Value);
                    var place = _queryProcessor.Execute(
                        new GetPlaceByWoeIdQuery
                    {
                        WoeId = woeId.Value,
                    });
                    var places = place.Ancestors.OrderByDescending(n => n.Separation)
                                 .Select(n => n.Ancestor).ToList();
                    places.Add(place);
                    var models = Mapper.Map <Collection <EstablishmentForm.LocationForm.EstablishmentPlaceForm> >(places);
                    return(PartialView(GetEditorTemplateViewName(Area, SharedName,
                                                                 MVC.Establishments.Shared.Views.EditorTemplates.EstablishmentPlacesForm), models));
                }
            }
            return(null);
        }
        private void Build(int retryCount)
        {
            _queryProcessor = ServiceProviderLocator.Current.GetService<IProcessQueries>();
            _updateEstablishment = ServiceProviderLocator.Current.GetService<IHandleCommands<UpdateEstablishment>>();
            _unitOfWork = ServiceProviderLocator.Current.GetService<IUnitOfWork>();
            _placeFinder = ServiceProviderLocator.Current.GetService<IConsumePlaceFinder>();
            try
            {
                var establishment = _queryProcessor.Execute(new GetEstablishmentByIdQuery(_establishmentId));
                if (!establishment.Location.Center.HasValue) return;

                var latitude = establishment.Location.Center.Latitude;
                var longitude = establishment.Location.Center.Longitude;
                if (!latitude.HasValue || !longitude.HasValue) return;

                var result = _placeFinder.Find(new PlaceByCoordinates(latitude.Value, longitude.Value)).SingleOrDefault();
                if (result == null) return;
                if (!result.WoeId.HasValue)
                {
                    throw new NotSupportedException(string.Format(
                        "Could not find WOE ID for coordinates {0},{1}", latitude, longitude));
                }
                //var place = _placeFactory.FromWoeId(result.WoeId.Value);
                var place = _queryProcessor.Execute(
                    new GetPlaceByWoeIdQuery
                    {
                        WoeId = result.WoeId.Value,
                    });
                var places = place.Ancestors.OrderByDescending(n => n.Separation).Select(a => a.Ancestor).ToList();
                places.Add(place);
                var command = new UpdateEstablishment
                {
                    Id = establishment.RevisionId,
                    PlaceIds = places.Select(p => p.RevisionId).ToArray(),
                };
                _updateEstablishment.Handle(command);
                //_establishment.Location.Places.Clear();
                //_establishment.Location.Places = places;
                //_objectCommander.Update(_establishment, true);
                //_entities.Update(_establishment);
                _unitOfWork.SaveChanges();
            }
            catch (Exception ex)
            {
                var exceptionLogger = ServiceProviderLocator.Current.GetService<ILogExceptions>();
                exceptionLogger.LogException(ex);

                if (ex is NotSupportedException)
                {
                    retryCount = 3;
                }

                if (retryCount < 2)
                {
                    Build(++retryCount);
                }
            }
        }
 public ResultSet Find(PlaceByCoordinates request)
 {
     return(_client.Find(request, _consumerKey, _consumerSecret));
 }
Exemple #5
0
        private void Build(int retryCount)
        {
            _queryProcessor      = ServiceProviderLocator.Current.GetService <IProcessQueries>();
            _updateEstablishment = ServiceProviderLocator.Current.GetService <IHandleCommands <UpdateEstablishment> >();
            _unitOfWork          = ServiceProviderLocator.Current.GetService <IUnitOfWork>();
            _placeFinder         = ServiceProviderLocator.Current.GetService <IConsumePlaceFinder>();
            try
            {
                var establishment = _queryProcessor.Execute(new GetEstablishmentByIdQuery(_establishmentId));
                if (!establishment.Location.Center.HasValue)
                {
                    return;
                }

                var latitude  = establishment.Location.Center.Latitude;
                var longitude = establishment.Location.Center.Longitude;
                if (!latitude.HasValue || !longitude.HasValue)
                {
                    return;
                }

                var result = _placeFinder.Find(new PlaceByCoordinates(latitude.Value, longitude.Value)).SingleOrDefault();
                if (result == null)
                {
                    return;
                }
                if (!result.WoeId.HasValue)
                {
                    throw new NotSupportedException(string.Format(
                                                        "Could not find WOE ID for coordinates {0},{1}", latitude, longitude));
                }
                //var place = _placeFactory.FromWoeId(result.WoeId.Value);
                var place = _queryProcessor.Execute(
                    new GetPlaceByWoeIdQuery
                {
                    WoeId = result.WoeId.Value,
                });
                var places = place.Ancestors.OrderByDescending(n => n.Separation).Select(a => a.Ancestor).ToList();
                places.Add(place);
                var command = new UpdateEstablishment
                {
                    Id       = establishment.RevisionId,
                    PlaceIds = places.Select(p => p.RevisionId).ToArray(),
                };
                _updateEstablishment.Handle(command);
                //_establishment.Location.Places.Clear();
                //_establishment.Location.Places = places;
                //_objectCommander.Update(_establishment, true);
                //_entities.Update(_establishment);
                _unitOfWork.SaveChanges();
            }
            catch (Exception ex)
            {
                var exceptionLogger = ServiceProviderLocator.Current.GetService <ILogExceptions>();
                exceptionLogger.LogException(ex);

                if (ex is NotSupportedException)
                {
                    retryCount = 3;
                }

                if (retryCount < 2)
                {
                    Build(++retryCount);
                }
            }
        }