From b242c09f54405402325edf05d3e5d2ccd6f29ab2 Mon Sep 17 00:00:00 2001 From: Rick Falkvinge Date: Mon, 5 Nov 2012 10:59:26 +0000 Subject: [PATCH] Implemented webmethods to extract geography data from an installation --- Database/Database-PostalCodes.cs | 2 +- Database/Database-PostalCodesCities.cs | 52 ++++++++++++++++++++++++-- Logic/Logic.csproj | 2 + Logic/Structure/Cities.cs | 4 ++ Logic/Structure/City.cs | 6 +++ Logic/Structure/PostalCode.cs | 34 +++++++++++++++++ Logic/Structure/PostalCodes.cs | 30 +++++++++++++++ Site5/Automation/GetGeographyData.asmx | 10 +++++ 8 files changed, 136 insertions(+), 4 deletions(-) create mode 100644 Logic/Structure/PostalCode.cs create mode 100644 Logic/Structure/PostalCodes.cs diff --git a/Database/Database-PostalCodes.cs b/Database/Database-PostalCodes.cs index 4f3a4e29..7cb6304d 100644 --- a/Database/Database-PostalCodes.cs +++ b/Database/Database-PostalCodes.cs @@ -34,7 +34,7 @@ namespace Activizr.Database #region Record reading - SELECT statements - public BasicPostalCode[] GetPostalCodes(string countryCode) + public BasicPostalCode[] GetPostalCodesForCountry (string countryCode) { using (DbConnection connection = GetMySqlDbConnection()) { diff --git a/Database/Database-PostalCodesCities.cs b/Database/Database-PostalCodesCities.cs index 50c906b9..a2757dce 100644 --- a/Database/Database-PostalCodesCities.cs +++ b/Database/Database-PostalCodesCities.cs @@ -138,7 +138,7 @@ namespace Activizr.Database } - public BasicCity GetCityByName (string cityName, int countryId) + public BasicCity GetCityByName(string cityName, int countryId) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -158,8 +158,29 @@ namespace Activizr.Database } } } - - public BasicCity[] GetCitiesByName (string cityName, int countryId) + + public BasicCity GetCityByName(string cityName, string countryCode) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SELECT " + cityFieldSequence + " WHERE CityName='" + cityName.Replace("'", "''") + "' AND CountryCode='" + countryCode.ToUpperInvariant().Replace("'", "''") + "'", + connection); + + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + return ReadCityFromDataReader(reader); + } + + throw new ArgumentException("No such CityName: " + cityName); + } + } + } + + public BasicCity[] GetCitiesByName(string cityName, int countryId) { using (DbConnection connection = GetMySqlDbConnection()) { @@ -184,5 +205,30 @@ namespace Activizr.Database } } + public BasicCity[] GetCitiesByCountry(string countryCode) + { + using (DbConnection connection = GetMySqlDbConnection()) + { + connection.Open(); + + DbCommand command = GetDbCommand("SELECT " + cityFieldSequence + " WHERE CountryCode='" + countryCode.ToUpperInvariant().Replace("'", "''") + "'", + connection); + List result = new List(); + using (DbDataReader reader = command.ExecuteReader()) + { + if (reader.Read()) + { + result.Add(ReadCityFromDataReader(reader)); + while (reader.Read()) + { + result.Add(ReadCityFromDataReader(reader)); + } + return result.ToArray(); + } + throw new ArgumentException("Cities not in place yet for country: " + countryCode); + } + } + } + } } \ No newline at end of file diff --git a/Logic/Logic.csproj b/Logic/Logic.csproj index 278526cd..36f7c3c5 100644 --- a/Logic/Logic.csproj +++ b/Logic/Logic.csproj @@ -196,6 +196,8 @@ + + diff --git a/Logic/Structure/Cities.cs b/Logic/Structure/Cities.cs index 1fc0e03f..23da1424 100644 --- a/Logic/Structure/Cities.cs +++ b/Logic/Structure/Cities.cs @@ -40,5 +40,9 @@ namespace Activizr.Logic.Structure return FromArray(PirateDb.GetDatabase().GetCitiesByName(cityName, countryId)); } + public static Cities ForCountry (string countryCode) + { + return FromArray(PirateDb.GetDatabase().GetCitiesByCountry (countryCode)); + } } } \ No newline at end of file diff --git a/Logic/Structure/City.cs b/Logic/Structure/City.cs index 9a2f6a4a..76c18296 100644 --- a/Logic/Structure/City.cs +++ b/Logic/Structure/City.cs @@ -1,8 +1,10 @@ +using System; using Activizr.Basic.Types; using Activizr.Database; namespace Activizr.Logic.Structure { + [Serializable] public class City : BasicCity { private City (BasicCity basic) @@ -26,5 +28,9 @@ namespace Activizr.Logic.Structure return FromBasic(PirateDb.GetDatabase().GetCityByName(cityName, countryId)); } + public static City FromName (string cityName, string countryCode) + { + return FromBasic(PirateDb.GetDatabase().GetCityByName(cityName, countryCode)); + } } } \ No newline at end of file diff --git a/Logic/Structure/PostalCode.cs b/Logic/Structure/PostalCode.cs new file mode 100644 index 00000000..28f9d3d5 --- /dev/null +++ b/Logic/Structure/PostalCode.cs @@ -0,0 +1,34 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Activizr.Basic.Types; + +namespace Activizr.Logic.Structure +{ + [Serializable] + public class PostalCode: BasicPostalCode + { + public PostalCode (BasicPostalCode basic): + base (basic) + { + // constructor + } + + static public PostalCode FromBasic (BasicPostalCode basic) + { + return new PostalCode(basic); + } + + public Country Country + { + get { return Country.FromCode(this.CountryCode); } + } + + public City City + { + get { return City.FromName(this.CityName, this.CountryCode); } + } + + } +} diff --git a/Logic/Structure/PostalCodes.cs b/Logic/Structure/PostalCodes.cs new file mode 100644 index 00000000..2482c19d --- /dev/null +++ b/Logic/Structure/PostalCodes.cs @@ -0,0 +1,30 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using Activizr.Basic.Types; +using Activizr.Database; + +namespace Activizr.Logic.Structure +{ + [Serializable] + public class PostalCodes: List + { + public static PostalCodes FromArray(BasicPostalCode[] array) + { + var result = new PostalCodes { Capacity = (array.Length * 11 / 10) }; + + foreach (BasicPostalCode basic in array) + { + result.Add(PostalCode.FromBasic(basic)); + } + + return result; + } + + public static PostalCodes ForCountry (string countryCode) + { + return FromArray(PirateDb.GetDatabase().GetPostalCodesForCountry (countryCode)); + } + } +} diff --git a/Site5/Automation/GetGeographyData.asmx b/Site5/Automation/GetGeographyData.asmx index b5abe7e8..497e32a4 100644 --- a/Site5/Automation/GetGeographyData.asmx +++ b/Site5/Automation/GetGeographyData.asmx @@ -23,6 +23,16 @@ public class GetGeographyData : System.Web.Services.WebService { return Countries.GetInUse(); } + [WebMethod] + public Cities GetCitiesForCountry (string countryCode) + { + return Cities.ForCountry(countryCode); + } + [WebMethod] + public PostalCodes GetPostalCodesForCountry (string countryCode) + { + return PostalCodes.ForCountry(countryCode); + } } \ No newline at end of file