From a234337412921fdde99b33caaf413db08460d479 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Sat, 17 Nov 2012 20:58:11 +0000 Subject: [PATCH] Many changes to postal codes - possibly quite breaking changes --- Basic/Types/BasicPostalCode.cs | 14 ++++++++------ Database/Database-PostalCodes.cs | 18 ++++++++---------- Logic/Structure/City.cs | 5 +++++ Logic/Structure/PostalCode.cs | 11 ++++++++--- Logic/Structure/PostalCodes.cs | 7 ++++++- Site5/Pages/v5/Init/Default.aspx.cs | 20 ++++++++++++++++++++ 6 files changed, 55 insertions(+), 20 deletions(-) diff --git a/Basic/Types/BasicPostalCode.cs b/Basic/Types/BasicPostalCode.cs index 74f08395..ba29d80c 100644 --- a/Basic/Types/BasicPostalCode.cs +++ b/Basic/Types/BasicPostalCode.cs @@ -7,15 +7,16 @@ namespace Activizr.Basic.Types [Serializable] public class BasicPostalCode { - public BasicPostalCode(string postalCode, string cityName, string countryCode) + public BasicPostalCode(int postalCodeId, string postalCode, int cityId, int countryId) { + this.PostalCodeId = postalCodeId; this.PostalCode = postalCode.Replace(" ", ""); - this.CityName = cityName; - this.CountryCode = countryCode; + this.CityId = cityId; + this.CountryId = countryId; } public BasicPostalCode(BasicPostalCode original) - : this(original.PostalCode, original.CityName, original.CountryCode) + : this(original.PostalCodeId, original.PostalCode, original.CityId, original.CountryId) { } @@ -25,8 +26,9 @@ namespace Activizr.Basic.Types // this should never be called from code } + public int PostalCodeId { get; private set; } public string PostalCode { get; private set; } // any whitespace removed - public string CityName { get; protected set; } - public string CountryCode { get; protected set; } + public int CityId { get; protected set; } + public int CountryId { get; protected set; } } } diff --git a/Database/Database-PostalCodes.cs b/Database/Database-PostalCodes.cs index b5466780..c0899204 100644 --- a/Database/Database-PostalCodes.cs +++ b/Database/Database-PostalCodes.cs @@ -14,18 +14,16 @@ namespace Activizr.Database #region Field reading code private const string postalCodeFieldSequence = - " PostalCodes.PostalCode,Cities.CityName,Countries.Code AS CountryCode " + - " FROM PostalCodes " + - " JOIN Cities ON (PostalCodes.CityId=Cities.CityId) " + - " JOIN Countries ON (PostalCodes.CountryId=Countries.CountryId) "; + " PostalCodeId,PostalCode,CityId,CountryId "; private BasicPostalCode ReadPostalCodeFromDataReader(DbDataReader reader) { - string postalCode = reader.GetString(0); - string cityName = reader.GetString(1); - string countryCode = reader.GetString(2); + int postalCodeId = reader.GetInt32(0); + string postalCode = reader.GetString(1); + int cityId = reader.GetInt32(2); + int countryId = reader.GetInt32(3); - return new BasicPostalCode(postalCode, cityName, countryCode); + return new BasicPostalCode(postalCodeId, postalCode, cityId, countryId); } #endregion @@ -34,7 +32,7 @@ namespace Activizr.Database #region Record reading - SELECT statements - public BasicPostalCode[] GetPostalCodesForCountry (string countryCode) + public BasicPostalCode[] GetPostalCodesForCountry (int countryId) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -42,7 +40,7 @@ namespace Activizr.Database DbCommand command = GetDbCommand( - "SELECT" + postalCodeFieldSequence + "WHERE Countries.Code='" + countryCode.Replace("'", "''") + "';", connection); + "SELECT" + postalCodeFieldSequence + "WHERE CountryId=" + countryId.ToString() + ";", connection); using (DbDataReader reader = command.ExecuteReader()) { diff --git a/Logic/Structure/City.cs b/Logic/Structure/City.cs index 851e7cfa..8b7eae7d 100644 --- a/Logic/Structure/City.cs +++ b/Logic/Structure/City.cs @@ -19,6 +19,11 @@ namespace Activizr.Logic.Structure // this instance is NOT initalized, and intended to be used only in serialization. } + public static City FromIdentity (int cityId) + { + return FromBasic(PirateDb.GetDatabaseForReading().GetCity(cityId)); + } + public Geography Geography { diff --git a/Logic/Structure/PostalCode.cs b/Logic/Structure/PostalCode.cs index 4b220119..d36f40b9 100644 --- a/Logic/Structure/PostalCode.cs +++ b/Logic/Structure/PostalCode.cs @@ -16,7 +16,7 @@ namespace Activizr.Logic.Structure } [Obsolete ("Do not call this constructor directly. It is intended only for serialization.", true)] - public PostalCode(): base (string.Empty, string.Empty, string.Empty) + public PostalCode(): base (0, string.Empty, 0, 0) { // this ctor does NOT initialize the instance. It is provided to allow for serialization. } @@ -28,12 +28,17 @@ namespace Activizr.Logic.Structure public Country Country { - get { return Country.FromCode(this.CountryCode); } + get { return Country.FromIdentity (this.CountryId); } } public City City { - get { return City.FromName(this.CityName, this.CountryCode); } + get { return City.FromIdentity(this.CityId); } + } + + public string CityName + { + get { return this.City.Name; } } } diff --git a/Logic/Structure/PostalCodes.cs b/Logic/Structure/PostalCodes.cs index 7879acea..21075659 100644 --- a/Logic/Structure/PostalCodes.cs +++ b/Logic/Structure/PostalCodes.cs @@ -24,7 +24,12 @@ namespace Activizr.Logic.Structure public static PostalCodes ForCountry (string countryCode) { - return FromArray(PirateDb.GetDatabaseForReading().GetPostalCodesForCountry (countryCode)); + return ForCountry(Country.FromCode(countryCode).Identity); + } + + public static PostalCodes ForCountry (int countryId) + { + return FromArray(PirateDb.GetDatabaseForReading().GetPostalCodesForCountry(countryId)); } } } diff --git a/Site5/Pages/v5/Init/Default.aspx.cs b/Site5/Pages/v5/Init/Default.aspx.cs index 8bc7dbee..d4347026 100644 --- a/Site5/Pages/v5/Init/Default.aspx.cs +++ b/Site5/Pages/v5/Init/Default.aspx.cs @@ -305,6 +305,7 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page Dictionary geographyIdTranslation = new Dictionary(); Dictionary cityIdTranslation = new Dictionary(); Dictionary countryIdTranslation = new Dictionary(); + Dictionary cityIdsUsedLookup = new Dictionary(); // Initialize the root geography (which becomes #1 if everything works) @@ -360,11 +361,30 @@ public partial class Pages_v5_Init_Default : System.Web.UI.Page int countryRootGeographyId = PirateDb.GetDatabaseForWriting().CreateGeography(geography.Name, rootGeographyId); geographyIdTranslation[geography.GeographyId] = countryRootGeographyId; + PirateDb.GetDatabaseForWriting().SetCountryGeographyId(countryIdTranslation[countryCode], + countryRootGeographyId); InitDatabaseThreadCreateGeographyChildren(geography.Children, countryRootGeographyId, ref geographyIdTranslation); _initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry/3); + // Get the postal codes and cities + + Activizr.Site.Automation.City[] cities = geoDataFetcher.GetCitiesForCountry(countryCode); + + _initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry/2); + + Activizr.Site.Automation.PostalCode[] postalCodes = geoDataFetcher.GetPostalCodesForCountry(countryCode); + + // Find which cities are actually used + + foreach (Activizr.Site.Automation.PostalCode postalCode in postalCodes) + { + // cityIdsUsedLookup[postalCode.CityName] = + } + + _initProgress = 10 + (int) (countryCount*initStepPerCountry + initStepPerCountry*2/3); + countryCount++; }