Ejemplo n.º 1
0
        /// <summary>
        /// Builds up the LocationLookupResult based on the location components passed in.
        /// City, state, country can be null.
        /// </summary>
        /// <param name="lookupType">The location lookup type</param>
        /// <param name="isValid">Whether or not the location was valid.</param>
        /// <param name="message">Error message if not valid.</param>
        /// <param name="country">Optional - The country to be used for setting the id and name of the country.</param>
        /// <param name="state">Optional - The state to be used for setting the id and name of the state.</param>
        /// <param name="city">Optional - The city to be used for setting the id and name of the city</param>
        /// <returns></returns>
        private static LocationLookUpResult Build(LocationLookUpType lookupType, bool isValid, string message, Country country, State state, City city)
        {
            LocationLookUpResult result = new LocationLookUpResult(lookupType, isValid, message);

            Build(country, state, city, result);
            return(result);
        }
Ejemplo n.º 2
0
        /// <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>
        /// Parses the state.
        /// </summary>
        /// <param name="stateLookUp">The state look up component</param>
        /// <param name="countryLookUp">The country look up component</param>
        /// <param name="stateFullNameOrAbbr"></param>
        /// <returns></returns>
        public static LocationLookUpResult ParseState(StatesLookUp stateLookUp, CountryLookUp countryLookUp, string stateFullNameOrAbbr)
        {
            State state = stateLookUp[stateFullNameOrAbbr];
            LocationLookUpResult result = null;

            // Empty state return state.
            if (state == null)
            {
                result       = new LocationLookUpResult(LocationLookUpType.State, false, "Unknown state supplied");
                result.State = stateFullNameOrAbbr;
                return(result);
            }

            // Build up the location data.
            result = ParseStateById(LocationLookUpType.State, true, string.Empty, stateLookUp, countryLookUp, state.Id, state.CountryId);
            return(result);
        }
        /// <summary>
        /// Parse the country.
        /// </summary>
        /// <param name="countryLookUp">The country lookup component</param>
        /// <param name="countryText">The text representing a country</param>
        /// <returns></returns>
        public static LocationLookUpResult ParseCountry(CountryLookUp countryLookUp, string countryText)
        {
            Country country             = countryLookUp[countryText];
            LocationLookUpResult result = null;

            if (country == null)
            {
                result         = new LocationLookUpResult(LocationLookUpType.Country, false, "Unknown country supplied.");
                result.Country = countryText;
                return(result);
            }

            // Set the city, state, country information.
            result           = new LocationLookUpResult(LocationLookUpType.Country, true, string.Empty);
            result.CountryId = country.Id;
            result.Country   = country.Name;
            return(result);
        }
Ejemplo n.º 5
0
 private static void Build(Country country, State state, City city, LocationLookUpResult result)
 {
     if (city != null)
     {
         result.City   = city.Name;
         result.CityId = city.Id;
     }
     if (state != null)
     {
         result.State     = state.Name;
         result.StateId   = state.Id;
         result.StateAbbr = state.Abbreviation;
     }
     if (country != null)
     {
         result.CountryId = country.Id;
         result.Country   = country.Name;
     }
 }
        /// <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);
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Parse the zipcode representing a United States Zipcode.
        /// This must be a 5 digit zipcode.
        /// e.g. "10465"
        /// </summary>
        /// <param name="zip"></param>
        /// <returns></returns>
        private LocationLookUpResult ParseUnitedStatesZipCode(string zip)
        {
            LocationLookUpResult result;

            if (zip.Length == 5)
            {
                for (int ndx = 0; ndx < zip.Length; ndx++)
                {
                    if (!Char.IsDigit(zip, ndx))
                    {
                        result           = new LocationLookUpResult(LocationLookUpType.Zip, false, "U.S. Zip code format is not valid.");
                        result.Zip       = zip;
                        result.CountryId = LocationConstants.CountryId_USA;
                        return(result);
                    }
                }
            }

            result           = new LocationLookUpResult(LocationLookUpType.Zip, true, string.Empty);
            result.Zip       = zip;
            result.CountryId = LocationConstants.CountryId_USA;
            return(result);
        }
Ejemplo n.º 8
0
 private static void Build(LocationLookUpResult result, LocationLookUpType lookupType, bool isValid, string message, Country country, State state, City city)
 {
     result.Init(lookupType, isValid, message);
     Build(country, state, city, result);
 }
Ejemplo n.º 9
0
        /// <summary>
        /// Given a user entered text contain a city and (state or country) combination,
        /// this method checks that the state or country entered by the user matches
        /// the state or country of the city that was matched.
        /// </summary>
        /// <param name="stateOrCountryTrimmed">The state or country entered by the user.</param>
        /// <param name="city">The city that was found in our system, entered by the user.</param>
        /// <param name="stateLookUp">The state lookup component.</param>
        /// <param name="countryLookUp">The country lookup component.</param>
        /// <param name="lookUpResult"></param>
        /// <returns></returns>
        private static bool IsCityMatchedWithStateCountry(string stateOrCountryTrimmed,
                                                          City city, StateLookUp stateLookUp, CountryLookUp countryLookUp, ref LocationLookUpResult lookUpResult)
        {
            // Require that the city is not null.
            if (city == null)
            {
                return(false);
            }

            State   state   = stateLookUp[stateOrCountryTrimmed];
            Country country = countryLookUp[stateOrCountryTrimmed];

            // Matched state of city and state input.
            // This now turns into a simple city based search.
            if (state != null && city.StateId == state.Id)
            {
                country = countryLookUp[state.CountryId];
                Build(lookUpResult, LocationLookUpType.City, true, string.Empty, country, state, city);
                return(true);
            }

            // Matched country of city and country input
            // This now turns into a simple city based search.
            if (country != null && city.CountryId == country.Id)
            {
                state = stateLookUp[city.StateId];
                Build(lookUpResult, LocationLookUpType.City, true, string.Empty, country, state, city);
                return(true);
            }

            return(false);
        }
Ejemplo n.º 10
0
        /// <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));
        }
 /// <summary>
 /// Static constructor to build the null object "Empty"
 /// </summary>
 static LocationLookUpResult()
 {
     _empty = new LocationLookUpResult(LocationLookUpType.All, true, string.Empty);
 }