Beispiel #1
0
		public JsonNetResult GetSearchBingEnterprises( int deviceId, List<string> excludeBingIds, string sort, string text )
		{
			var hotel = ConnectCmsRepository.HotelRepository.GetHotelFromDevice( deviceId );

			EnterpriseSearchSortTypes sortType = ( EnterpriseSearchSortTypes )int.Parse( sort );

			List<EnterpriseModel> enterprises = new List<EnterpriseModel>();

			if( hotel != null )
			{
				var location = hotel.Location;

				if( location != null )
					// TODO: JD: Get culture
					enterprises = ConnectCmsRepository.BingRepository.GetSearchBingEnterprises( location.Latitude, location.Longitude, hotel.RadiusInKilometers, text, sortType, "en-US", excludeBingIds );
			}

			return JsonNet( enterprises );
		}
Beispiel #2
0
        public JsonNetResult GetSearchEnterprises(int deviceId, int?categoryId, string text, string sort)
        {
            var hotel = ConnectCmsRepository.HotelRepository.GetHotelFromDevice(deviceId);

            EnterpriseSearchSortTypes sortType = ( EnterpriseSearchSortTypes )int.Parse(sort);

            List <EnterpriseModel> enterprises = new List <EnterpriseModel>();

            if (hotel != null)
            {
                var location = hotel.Location;

                if (location != null)
                {
                    enterprises = ConnectCmsRepository.EnterpriseRepository.GetSearchEnterprises(deviceId, location.Latitude, location.Longitude, hotel.RadiusInMeters, categoryId, text, sortType);
                }
            }

            return(JsonNet(enterprises ?? new List <EnterpriseModel>()));
        }
Beispiel #3
0
        public List <EnterpriseModel> GetSearchBingEnterprises(float?hotelLatitude, float?hotelLongitude, float radiusInKilometers, string text, EnterpriseSearchSortTypes sortType, string culture, IEnumerable <string> excludeBingIds = null)
        {
            List <EnterpriseModel> enterprises = new List <EnterpriseModel>();

            if (hotelLatitude.HasValue && hotelLongitude.HasValue)
            {
                var bingAppId       = GetBingAppId();
                var enUsCultureInfo = new CultureInfo("en-US");
                var latitude        = hotelLatitude.Value;
                var longitude       = hotelLongitude.Value;

                var searchRequest = new Bing.Search.SearchRequest()
                {
                    Credentials = new Bing.Search.Credentials()
                    {
                        ApplicationId = bingAppId
                    },
                    Culture       = FormatCulture(culture),
                    SearchOptions = new Bing.Search.SearchOptions()
                    {
                        Count       = 20,
                        ListingType = Bing.Search.ListingType.Business,
                        Radius      = radiusInKilometers,
                        SortOrder   = Bing.Search.SortOrder.Popularity
                    },
                    StructuredQuery = new Bing.Search.StructuredSearchQuery()
                    {
                        Location = latitude.ToString(enUsCultureInfo) + ", " + longitude.ToString(enUsCultureInfo),
                        Keyword  = text
                    },
                    UserProfile = new Bing.Search.UserProfile()
                    {
                        CurrentLocation = new Bing.Search.UserLocation()
                        {
                            Latitude  = latitude,
                            Longitude = longitude
                        }
                    }
                };

                var searchServiceService = new Bing.Search.SearchServiceClient("BasicHttpBinding_ISearchService");
                var searchResponse       = searchServiceService.Search(searchRequest);

                if (searchResponse != null &&
                    searchResponse.ResultSets != null && searchResponse.ResultSets.Count() > 0 &&
                    searchResponse.ResultSets[0].Results != null && searchResponse.ResultSets[0].Results.Count() > 0)
                {
                    Bing.Search.Address address;
                    Bing.Search.BusinessSearchResult businessSearchResult;
                    EnterpriseLocationModel          enterpriseLocation;
                    EnterpriseModel             enterprise;
                    Bing.Search.GeocodeLocation geocodeLocation;

                    foreach (Bing.Search.SearchResultBase searchResultBase in searchResponse.ResultSets[0].Results)
                    {
                        if (searchResultBase is Bing.Search.BusinessSearchResult &&
                            searchResultBase.LocationData != null &&
                            searchResultBase.LocationData.Locations != null && searchResultBase.LocationData.Locations.Count > 0)
                        {
                            businessSearchResult = searchResultBase as Bing.Search.BusinessSearchResult;

                            if (excludeBingIds == null || !excludeBingIds.Contains(businessSearchResult.Id))
                            {
                                enterprise = new EnterpriseModel()
                                {
                                    EnterpriseLocations = new List <EnterpriseLocationModel>(),
                                    Name       = businessSearchResult.Name,
                                    WebsiteUrl = businessSearchResult.Website != null?businessSearchResult.Website.ToString() : null
                                };

                                enterpriseLocation = new EnterpriseLocationModel()
                                {
                                    BingId               = businessSearchResult.Id,
                                    Blacklisted          = false,
                                    DistanceInKilometers = businessSearchResult.Distance,
                                    LocalHours           = businessSearchResult.AdditionalProperties.ContainsKey("HoursOfOperation") ? businessSearchResult.AdditionalProperties["HoursOfOperation"].ToString().ToLower() : null,
                                    Phone = businessSearchResult.PhoneNumber
                                };

                                geocodeLocation = searchResultBase.LocationData.Locations.FirstOrDefault();

                                address = businessSearchResult.Address;

                                if (address != null)
                                {
                                    enterpriseLocation.Location = new LocationModel()
                                    {
                                        Address1       = address.AddressLine,
                                        City           = address.PostalTown ?? address.Locality,
                                        ISOCountryCode = address.CountryRegion,
                                        PostalCode     = address.PostalCode,
                                        ISOStateCode   = address.AdminDistrict
                                    };

                                    if (geocodeLocation.Latitude != 0 && geocodeLocation.Longitude != 0)
                                    {
                                        enterpriseLocation.Location.Latitude = ( float )geocodeLocation.Latitude;
                                    }
                                    enterpriseLocation.Location.Longitude = ( float )geocodeLocation.Longitude;

                                    enterpriseLocation.PhoneISOCountryCode = enterpriseLocation.Location.ISOCountryCode;
                                }

                                // TODO: JD: Get culture
                                enterpriseLocation.TranslatedHours.Add("EN", enterpriseLocation.LocalHours);

                                ((List <EnterpriseLocationModel>)enterprise.EnterpriseLocations).Add(enterpriseLocation);

                                enterprises.Add(enterprise);
                            }
                        }
                    }
                }

                if (enterprises.Any())
                {
                    switch (sortType)
                    {
                    case EnterpriseSearchSortTypes.ALAPHABETICAL:
                        enterprises = enterprises.OrderBy(e => e.Name).ThenBy(e => e.EnterpriseLocations.Min(el => el.DistanceInMeters)).ToList();

                        break;

                    case EnterpriseSearchSortTypes.NEAREST:
                        enterprises = enterprises.OrderBy(e => e.EnterpriseLocations.Min(el => el.DistanceInMeters)).ToList();

                        break;
                    }
                }
            }

            return(enterprises);
        }
Beispiel #4
0
        public List <EnterpriseModel> GetSearchEnterprises(int deviceId, float?hotelLatitude, float?hotelLongitude, float radiusInMeters,
                                                           int?categoryId, string text, EnterpriseSearchSortTypes sortType)
        {
            List <EnterpriseModel> enterprises = null;

            var device = RootRepository.DeviceRepository.GetDevice(deviceId);

            var hotelPoint = GeographyUtils.GetPointFromLatitudeAndLongitude(hotelLatitude, hotelLongitude);

            if (hotelPoint != null)
            {
                RootRepository.SecurityRepository.AssertDevicePermissions(deviceId);

                if (text != null)
                {
                    text = text.Trim();
                }

                var query = (from e in ProxylessContext.Enterprises
                             .Include(x => x.EnterpriseLocations.Select(el => el.BlackListEnterpriseLocationMaps))
                             .Include(x => x.EnterpriseLocations.Select(el => el.HotelBestOfEnterpriseLocationMaps))
                             .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.Image))
                             .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.CategoryMap.Image))
                             .Include(x => x.EnterpriseCategoryMaps.Select(ecm => ecm.CategoryMap.HotelCategoryMaps.Select(hcm => hcm.Image)))
                             .Include(
                                 x =>
                                 x.EnterpriseCategoryMaps.Select(
                                     ecm => ecm.HotelBestOfEnterpriseMaps.Select(hboem => hboem.HotelBestOfEnterpriseLocationMaps)))
                             .Include(x => x.BlackListEnterpriseMaps)
                             where
                             e.IsActive &&
                             e.EnterpriseLocations.Any(x => x.IsActive && x.Latitude != null && x.Longitude != null)
                             select new
                {
                    CMImage = e.EnterpriseCategoryMaps.FirstOrDefault().CategoryMap.Image,
                    ECMImage = e.EnterpriseCategoryMaps.FirstOrDefault().Image,
                    Enterprise = new EnterpriseModel
                    {
                        Blacklisted = e.BlackListEnterpriseMaps.Any(x => x.FKDevice == deviceId),
                        EnterpriseLocations = e.EnterpriseLocations.Where(x => x.IsActive && x.Latitude != null && x.Longitude != null).Select(el => new EnterpriseLocationModel
                        {
                            BingId = el.BingId,
                            Blacklisted = el.BlackListEnterpriseLocationMaps.Any(x => x.FKHotel == device.FKHotel),
                            DistanceInMeters = el.Coordinates.Distance(hotelPoint),
                            Location = new LocationModel
                            {
                                Address1 = el.Address1,
                                Address2 = el.Address2,
                                City = el.City,
                                ISOCountryCode = el.Country,
                                Latitude = el.Latitude,
                                Longitude = el.Longitude,
                                PostalCode = el.PostalCode,
                                ISOStateCode = el.State
                            },
                            Phone = el.Phone,
                            PKID = el.PKID,
                            Recommended = el.HotelBestOfEnterpriseLocationMaps.Any(x => x.HotelBestOfEnterpriseMap.FKDevice == deviceId)
                        }),
                        Name = e.Name,
                        PKID = e.PKID,
                        Recommended =
                            e.EnterpriseCategoryMaps.Any(x => x.HotelBestOfEnterpriseMaps.Any(hboem => hboem.FKDevice == deviceId))
                    },
                    e.EnterpriseCategoryMaps,
                    HCMImage =
                        e.EnterpriseCategoryMaps.FirstOrDefault()
                        .CategoryMap.HotelCategoryMaps.FirstOrDefault(x => x.FKDevice == deviceId)
                        .Image,
                });

                //var query = ( from e in ProxylessContext.Enterprises.Where( e2 => e2.IsActive )
                //			  let el = e.EnterpriseLocations.Where( el => el.IsActive && el.Coordinates.Distance( hotelPoint ) < radiusInMeters )
                //			  let ecm = e.EnterpriseCategoryMaps.Where( ecm2 => ecm2.IsActive )
                //			  let fecm = ecm.FirstOrDefault()
                //			  let cm = ecm.Where( ecm2 => ecm2.CategoryMap.IsActive ).Select( ecm2 => ecm2.CategoryMap )
                //			  let fcm = cm.FirstOrDefault()
                //			  let hcm = cm.SelectMany( cm2 => cm2.HotelCategoryMaps.Where( hcm2 => hcm2.IsActive && hcm2.FKDevice == deviceId ) )
                //			  let fhcm = hcm.OrderBy( hcm2 => hcm2.Ordinal ).FirstOrDefault()
                //			  let hboem = ecm.SelectMany( ecm2 => ecm2.HotelBestOfEnterpriseMaps.Where( hboem2 => hboem2.IsActive && hboem2.FKDevice == deviceId ).OrderBy( hboem2 => hboem2.Ordinal ) )
                //			  let hboelm = hboem.SelectMany( hboem2 => hboem2.HotelBestOfEnterpriseLocationMaps )
                //			  let blem = e.BlackListEnterpriseMaps.Where( blem2 => blem2.IsActive && blem2.FKDevice == deviceId )
                //			  let blelm = el.SelectMany( el2 => el2.BlackListEnterpriseLocationMaps.Where( blelm2 => blelm2.IsActive == true && blelm2.Hotel.Devices.Any( d => d.PKID == deviceId ) ) )
                //			  where el.Any()
                //			  select new
                //			  {
                //				  CMImage = fcm.Image,
                //				  ECMImage = fecm.Image,
                //				  Enterprise = new EnterpriseModel()
                //				  {
                //					  Blacklisted = blem.Any(),
                //					  EnterpriseLocations = ( from el2 in el
                //											  select new EnterpriseLocationModel()
                //											  {
                //												  BingId = el2.BingId,
                //												  Blacklisted = blelm.Select( blelm2 => blelm2.FKEnterpriseLocation ).Contains( el2.PKID ),
                //												  DistanceInMiles = ProxylessContext.CalculateDistance( el2.Latitude, el2.Longitude, hotelGeocode.Latitude.Value, hotelGeocode.Longitude.Value ),
                //												  Location = new LocationModel()
                //												  {
                //													  Address1 = el2.Address1,
                //													  Address2 = el2.Address2,
                //													  City = el2.City,
                //													  Country = new CountryModel
                //													  {
                //														  ISOCountryCode = el2.Country
                //													  },
                //													  Geocode = new GeocodeModel()
                //													  {
                //														  Latitude = el2.Latitude,
                //														  Longitude = el2.Longitude
                //													  },
                //													  PostalCode = el2.PostalCode,
                //													  State = new StateModel
                //													  {
                //														  ISOStateCode = el2.State,
                //													  }
                //												  },
                //												  Phone = el2.Phone,
                //												  PKID = el2.PKID,
                //												  Recommended = hboelm.Any( hboelm2 => hboelm2.FKEnterpriseLocation == el2.PKID )
                //											  } ),
                //					  Name = e.Name,
                //					  PKID = e.PKID,
                //					  Recommended = hboem.Any() && !hboelm.Any()
                //				  },
                //				  EnterpriseCategoryMaps = ecm,
                //				  HCMImage = fhcm.Image,
                //			  } );

                if (categoryId.HasValue)
                {
                    query = (from q in query
                             where q.EnterpriseCategoryMaps.Any(ecm2 => ecm2.CategoryMap.FKChildCategory == categoryId.Value)
                             select q);
                }

                if (!string.IsNullOrWhiteSpace(text))
                {
                    var ft = ProxylessContext.GetEnterprisesByFreeText(text).Select(gebft => gebft.Key);
                    query = query.Where(x => ft.Contains(x.Enterprise.PKID));
                }

                switch (sortType)
                {
                case EnterpriseSearchSortTypes.ALAPHABETICAL:
                    query = (from q in query
                             orderby q.Enterprise.Name
                             select q);

                    break;

                case EnterpriseSearchSortTypes.NEAREST:
                    query = (from q in query
                             orderby q.Enterprise.EnterpriseLocations.Min(el => el.DistanceInMeters),
                             q.Enterprise.Name
                             select q);

                    break;
                }

                var list = Rp.ExecuteAction(() => query.Take(20)).ToList();

                if (list != null)
                {
                    list.ForEach(e =>
                    {
                        if (e.Enterprise != null)
                        {
                            if (e.CMImage != null)
                            {
                                e.Enterprise.ListItemImage = new ImageModel(e.CMImage);
                            }

                            if (e.HCMImage != null)
                            {
                                e.Enterprise.ListItemImage = new ImageModel(e.HCMImage);
                            }

                            if (e.ECMImage != null)
                            {
                                e.Enterprise.ListItemImage = new ImageModel(e.ECMImage);
                            }

                            if (enterprises == null)
                            {
                                enterprises = new List <EnterpriseModel>();
                            }

                            enterprises.Add(e.Enterprise);
                        }
                    });
                }
            }

            return(enterprises);
        }