コード例 #1
0
        /// <summary>
        /// Checks that the state belongs to the country.
        /// </summary>
        /// <param name="stateLookUp"></param>
        /// <param name="countryLookUp"></param>
        /// <param name="stateId"></param>
        /// <param name="countryId"></param>
        /// <returns></returns>
        public static bool IsValidStateCountryRelation(StateLookUp stateLookUp, CountryLookUp countryLookUp, int stateId, int countryId)
        {
            // indicates online.
            bool isUSA = countryId == LocationConstants.CountryId_USA;

            // If country id is online, disregard check.
            if (countryId == LocationConstants.CountryId_NA_Online)
            {
                return(true);
            }

            // Do not have to select a state for Non-USA countries.
            if (stateId == LocationConstants.StateId_NA_Online && !isUSA)
            {
                return(true);
            }

            // Must select a state for USA.
            if (stateId == LocationConstants.StateId_NA_Online && isUSA)
            {
                return(false);
            }

            State   state   = stateLookUp[stateId];
            Country country = countryLookUp[countryId];

            // Check combination
            if (state.CountryId == country.Id)
            {
                return(true);
            }

            return(false);
        }
コード例 #2
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);
        }
コード例 #3
0
        /// <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="stateLookup"></param>
        /// <param name="errors">list of errors to populate if any validation fails.</param>
        public static bool ApplyState(Address address, StateLookUp stateLookup, IList <string> errors)
        {
            if (address.StateId == LocationConstants.StateId_NA_Online)
            {
                return(true);
            }
            if (address.StateId > 0)
            {
                return(true);
            }

            // Can't determine state by name if it's not supplied.
            if (string.IsNullOrEmpty(address.State))
            {
                return(false);
            }

            int initialErrorCount = errors.Count;

            // Check the state.
            bool  isCountryIdEmpty = (address.CountryId <= 0);
            State state            = isCountryIdEmpty ? stateLookup[address.State] : stateLookup.FindByCountry(address.State, address.CountryId);

            if (ValidationUtils.Validate(state == null, errors, "Invalid state supplied."))
            {
                address.StateId   = state.Id;
                address.StateAbbr = state.Abbreviation;
                address.State     = state.Name;
            }

            return(initialErrorCount == errors.Count);
        }
コード例 #4
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"));
        }
コード例 #5
0
 /// <summary>
 /// Initialize
 /// </summary>
 /// <param name="statesLookup"></param>
 /// <param name="countryLookup"></param>
 /// <param name="address"></param>
 /// <param name="isOnline"></param>
 public void Init(StateLookUp statesLookup, CountryLookUp countryLookup, Address address, bool isOnline)
 {
     _isOnline      = isOnline;
     _stateId       = address.StateId;
     _state         = address.State;
     _countryId     = address.CountryId;
     _country       = address.Country;
     _city          = address.City;
     _zip           = address.Zip;
     _stateLookup   = statesLookup;
     _countryLookup = countryLookup;
 }
コード例 #6
0
 /// <summary>
 /// Initialize
 /// </summary>
 /// <param name="statesLookup"></param>
 /// <param name="countryLookup"></param>
 /// <param name="countryId"></param>
 /// <param name="stateId"></param>
 /// <param name="city"></param>
 /// <param name="zip"></param>
 /// <param name="isOnline"></param>
 public LocationValidator(StateLookUp statesLookup, CountryLookUp countryLookup,
                          int countryId, int stateId, string city, string zip, bool isOnline)
 {
     _isOnline      = isOnline;
     _stateId       = stateId;
     _state         = string.Empty;
     _countryId     = countryId;
     _country       = string.Empty;
     _city          = city;
     _zip           = zip;
     _stateLookup   = statesLookup;
     _countryLookup = countryLookup;
 }
コード例 #7
0
        private static LocationLookUpResult ParseStateById(LocationLookUpType lookupType, bool isValid, string message,
                                                           StateLookUp stateLookUp, CountryLookUp countryLookUp, int stateId, int countryId)
        {
            State state = stateLookUp[stateId];

            Country country = null;

            if (state != null)
            {
                country = countryLookUp[state.CountryId];
            }
            else
            {
                country = countryLookUp[countryId];
            }

            return(Build(lookupType, isValid, message, country, state, null));
        }
コード例 #8
0
        /// <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(StateLookUp stateLookUp, CountryLookUp countryLookUp, string stateFullNameOrAbbr)
        {
            State state = stateLookUp[stateFullNameOrAbbr];
            LocationLookUpResult result = null;

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

            // Build up the location data.
            result = ParseStateById(LocationLookUpType.State, true, string.Empty, stateLookUp, countryLookUp, state.Id, state.CountryId);
            return(result);
        }
コード例 #9
0
        /// <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, StateLookUp stateLookUp, CountryLookUp countryLookUp, string cityname)
        {
            City city = cityLookUp[cityname];
            LocationLookUpResult result = null;

            // Empty city ?
            if (city == null)
            {
                return new LocationLookUpResult(LocationLookUpType.City, false, "Unknown city supplied")
                       {
                           City = cityname
                       }
            }
            ;


            // 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);
        }
コード例 #10
0
        /// <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;
            }
        }
コード例 #11
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));
        }
コード例 #12
0
 /// <summary>
 /// Initalize using the Address object.
 /// </summary>
 /// <param name="statesLookup"></param>
 /// <param name="countryLookup"></param>
 /// <param name="address"></param>
 /// <param name="isOnline"></param>
 public LocationValidator(StateLookUp statesLookup, CountryLookUp countryLookup, Address address, bool isOnline)
 {
     Init(statesLookup, countryLookup, address, isOnline);
 }
コード例 #13
0
 public LocationDataMassager(CityLookUp cityLookup, StateLookUp stateLookup, CountryLookUp countryLookup)
 {
     CitiesLookup    = cityLookup;
     StatesLookup    = stateLookup;
     CountriesLookup = countryLookup;
 }