/// <summary> /// Does a high-level check of the format supplied and determines what type /// of location input was supplied. /// /// Formats are: /// 1. city - "Bronx" /// 2. city,state - "Bronx , New York" /// 3. city,state( abbreviation ) - "Bronx , NY" /// 4. city,country - "HomeTown , USA" /// 5. state - "New Jersey" /// 6. state abbreviation - "NJ" /// 7. country - "Italy" /// the actuall parsing /// </summary> /// <param name="locationData">The location to parse. Can be any combination of /// inputs, check the summary above.</param> /// <returns></returns> private LocationLookUpResult InternalParse(string locationData) { CityLookUp cityLookUp = new CityLookUp(Cities.GetAll()); StateLookUp stateLookUp = new StateLookUp(States.GetAll()); CountryLookUp countryLookUp = new CountryLookUp(Countries.GetAll()); try { bool isValidUSZipCode = IsUnitedStatesZipCode(locationData); bool containsComma = isValidUSZipCode ? false : locationData.Contains(","); // United states Zip code format if (isValidUSZipCode) { return(ParseUnitedStatesZipCode(locationData)); } // City, ( State or Country ) // Comma indicates search by city, <state> or <country> if (containsComma) { return(LocationParser.ParseCityWithStateOrCountry(cityLookUp, stateLookUp, countryLookUp, locationData)); } // Check for city, state, or country. // Start with narrowest search. // Check city. LocationLookUpResult result = LocationParser.ParseCity(cityLookUp, stateLookUp, countryLookUp, locationData); if (result != LocationLookUpResult.Empty && result.IsValid) { return(result); } // Check State - 2nd most restrictive search. result = LocationParser.ParseState(stateLookUp, countryLookUp, locationData); if (result != LocationLookUpResult.Empty && result.IsValid) { return(result); } // Check country - 3rd and final search criteria result = LocationParser.ParseCountry(countryLookUp, locationData); if (result != LocationLookUpResult.Empty && result.IsValid) { return(result); } } catch (Exception ex) { // Some error during parsing. // Log the sucker into our system. Logger.Error("Error verifying search location", ex); } return(new LocationLookUpResult(LocationLookUpType.None, false, "Unable to determine location")); }
/// <summary> /// Massage the address by setting it's cityid, stateid, countryid /// from the city, state, country name. /// </summary> /// <param name="address"></param> /// <param name="entityAction"></param> public static void Massage(Address address, EntityAction entityAction) { List <string> errors = new List <string>(); CityLookUp cityLookup = Ioc.GetObject <ICityDao>("cityDao").GetLookUp(); StatesLookUp stateLookup = Ioc.GetObject <IStateDao>("stateDao").GetLookUp(); CountryLookUp countryLookup = Ioc.GetObject <ICountryDao>("countryDao").GetLookUp(); LocationUtils.ApplyCountry(address, countryLookup, errors); LocationUtils.ApplyState(address, stateLookup, errors); LocationUtils.ApplyCity(address, cityLookup, stateLookup, countryLookup); }
/// <summary> /// Parses the city /// </summary> /// <param name="cityLookUp">The city look up component</param> /// <param name="stateLookUp">The state look up component</param> /// <param name="countryLookUp">The country look up component</param> /// <param name="cityname">The city name.</param> /// <returns></returns> public static LocationLookUpResult ParseCity(CityLookUp cityLookUp, StatesLookUp stateLookUp, CountryLookUp countryLookUp, string cityname) { City city = cityLookUp[cityname]; LocationLookUpResult result = null; // Empty city ? if (city == null) { result = new LocationLookUpResult(LocationLookUpType.City, false, "Unknown city supplied"); result.City = cityname; return(result); } // Set the city, state, country information. result = ParseStateById(LocationLookUpType.City, true, string.Empty, stateLookUp, countryLookUp, city.StateId, city.CountryId); result.City = city.Name; result.CityId = city.Id; return(result); }
/// <summary> /// Applies the city id to the address if the city is listed in the system /// and has a matching state and country id compared to what was supplied. /// </summary> /// <param name="address"></param> /// <param name="cityLookup"></param> /// <param name="stateLookup"></param> /// <param name="countryLookup"></param> public static void ApplyCity(Address address, CityLookUp cityLookup, StateLookUp stateLookup, CountryLookUp countryLookup) { address.CityId = LocationConstants.CityId_NA; // online( country id = online ) or city is null. // So don't change anything. if (address.CountryId == LocationConstants.CountryId_NA_Online) { return; } // Get the city. City city = cityLookup.FindByCountry(address.City, address.CountryId); // CASE 1: Unknown city. if (city == null) { return; } // CASE 2: Matching country and state id's with city. // Most like this is for U.S.A cities. Where user has to specify state id. if (city.CountryId == address.CountryId && city.StateId == address.StateId) { address.CityId = city.Id; address.City = city.Name; return; } // CASE 3: NON-U.S. country. // State not specified but matched the country specified with the city. if (address.StateId == LocationConstants.StateId_NA_Online && city.CountryId == address.CountryId) { address.StateId = city.StateId; address.CityId = city.Id; address.City = city.Name; } }
/// <summary> /// Parse 'city','state' | 'country'. /// e.g. Georgetown, Texas or GeorgeTown, Guyana /// </summary> /// <remarks>The area after the comma can be either the state or country. /// We store a list of valid states/regions, and countries</remarks> /// <param name="cityLookUp">The city look up component</param> /// <param name="stateLookUp">The state look up component</param> /// <param name="countryLookUp">The country look up component</param> /// <param name="smallLarge"></param> /// <returns></returns> public static LocationLookUpResult ParseCityWithStateOrCountry(CityLookUp cityLookUp, StateLookUp stateLookUp, CountryLookUp countryLookUp, string smallLarge) { // Validate. if (string.IsNullOrEmpty(smallLarge)) { return(LocationLookUpResult.Empty); } string[] tokens = smallLarge.Split(','); // Validate. Only 2 tokens. city, state or city, country. if (tokens.Length != 2) { return(new LocationLookUpResult(LocationLookUpType.CityState, false, "Invalid city,state/country.")); } // Get each token ( before comma, and after ) string smallArea = tokens[0]; string largeArea = tokens[1]; smallLarge = smallLarge.Trim(); smallArea = smallArea.Trim(); largeArea = largeArea.Trim(); // Small area has to be city. // Large area can be either state, country. City city = cityLookUp[smallArea]; bool knownCity = city != null; State state = null; Country country = null; // Valid city. if (knownCity) { // Check the user supplied state or country. // Return result if the city matches with the state or country. LocationLookUpResult result = new LocationLookUpResult(LocationLookUpType.City, false, string.Empty); if (IsCityMatchedWithStateCountry(largeArea, city, stateLookUp, countryLookUp, ref result)) { return(result); } } // Check state. state = stateLookUp[largeArea]; // Unknown city entered, but valid State. if (state != null) { country = countryLookUp[state.CountryId]; LocationLookUpResult result = Build(LocationLookUpType.CityState, true, string.Empty, country, state, null); result.City = smallArea; return(result); } // Check country. country = countryLookUp[largeArea]; // Unknown city entered, but Valid country if (country != null) { LocationLookUpResult result = Build(LocationLookUpType.CityCountry, true, string.Empty, country, null, null); result.City = smallArea; return(result); } return(new LocationLookUpResult(LocationLookUpType.CityState, false, "Unknown city/state or city/country : " + smallArea + ", " + largeArea)); }
public LocationDataMassager(CityLookUp cityLookup, StateLookUp stateLookup, CountryLookUp countryLookup) { CitiesLookup = cityLookup; StatesLookup = stateLookup; CountriesLookup = countryLookup; }