using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; using System.Data.OleDb; namespace Reservation { /// /// Search form. /// Methods created or modified: ///

Copyright (c) 2002 Alexis Grandemange
/// Mail: alexis.grandemange@pagebox.net

///
This program is free software; you can redistribute it and/or
	/// modify it under the terms of the GNU Lesser General Public
	/// License as published by the Free Software Foundation; version
	/// 2.1 of the License.
	/// This library is distributed in the hope that it will be useful,
	/// but WITHOUT ANY WARRANTY; without even the implied warranty of
	/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	/// GNU Lesser General Public License for more details.
	/// A copy of the GNU Lesser General Public License lesser.txt should be
	/// included in the distribution.
///
public class Search : System.Web.UI.Page { protected System.Web.UI.WebControls.DropDownList ddNeighbors; protected System.Web.UI.WebControls.TextBox tbAddress; protected System.Web.UI.WebControls.TextBox tbCity; protected System.Web.UI.WebControls.TextBox tbZip; protected System.Web.UI.WebControls.TextBox tbRegion; protected System.Web.UI.WebControls.TextBox tbLatitude; protected System.Web.UI.WebControls.TextBox tbLongitude; protected System.Web.UI.WebControls.CheckBox H800; protected System.Web.UI.WebControls.CheckBox H900; protected System.Web.UI.WebControls.CheckBox H1000; protected System.Web.UI.WebControls.CheckBox H1100; protected System.Web.UI.WebControls.CheckBox H1200; protected System.Web.UI.WebControls.CheckBox H1300; protected System.Web.UI.WebControls.CheckBox H1400; protected System.Web.UI.WebControls.CheckBox H1500; protected System.Web.UI.WebControls.CheckBox H1600; protected System.Web.UI.WebControls.CheckBox H1700; protected System.Web.UI.WebControls.CheckBox H1800; protected System.Web.UI.WebControls.CheckBox H830; protected System.Web.UI.WebControls.CheckBox H930; protected System.Web.UI.WebControls.CheckBox H1030; protected System.Web.UI.WebControls.CheckBox H1130; protected System.Web.UI.WebControls.CheckBox H1230; protected System.Web.UI.WebControls.CheckBox H1330; protected System.Web.UI.WebControls.CheckBox H1430; protected System.Web.UI.WebControls.CheckBox H1530; protected System.Web.UI.WebControls.CheckBox H1630; protected System.Web.UI.WebControls.CheckBox H1730; protected System.Web.UI.WebControls.CheckBox H1830; protected System.Web.UI.WebControls.Button btnList; protected System.Web.UI.WebControls.Button btnClear; protected System.Web.UI.WebControls.Calendar CalBook; protected System.Web.UI.WebControls.Label lblStatus; protected System.Web.UI.WebControls.DataGrid dgApplications; protected System.Web.UI.WebControls.DropDownList ddState; protected System.Web.UI.WebControls.DropDownList ddCountry; /// /// Id of the logged user /// private string id; /// /// True if the application data have been refreshed. /// public static bool isRefreshed = false; /// /// Last application refresh. /// public static DateTime refreshTime; /// /// Array of schedule check boxes /// CheckBox[,] cbs; /// /// Constructor. populates the array of schedule check boxes /// public Search() { Page.Init += new System.EventHandler(Page_Init); } /// /// Checks the user credentials. /// Calls setCoordFromHost. /// private void Page_Load(object sender, System.EventArgs e) { id = (string)Context.Session["userid"]; if (id == null) { Context.Session.Add("from", "Search.aspx"); Context.Response.Redirect("Login.aspx"); return; } short type = (short)Context.Session["type"]; if (type > 1) { Context.Session.Add("status", "Log-in with a customer or proxy user id"); Context.Session.Add("from", "Search.aspx"); Context.Response.Redirect("Login.aspx"); return; } if (!IsPostBack) { // We must do the initial refresh here because if we cannot do it in Global: // Global methods are invoked for Web Services. If we do the initial refresh in Global // the first page/Web service invocation will trigger the refresh. // It will invoke the Location Web service of peers. // Peer Web Service execution will trigger the invocation of their Application_BeginRequest // and therefore of this instance Location Web service. // If we lock (and we need to lock to call refresh only once) we deadlock. lock(typeof(Search)) { if (!Search.isRefreshed) { Reservation.Application ap = new Reservation.Application(Global.pageboxUrl); ap.Refresh(); Search.refreshTime = DateTime.Now; Search.isRefreshed = true; } } ArrayList al = new ArrayList(); al.Add("the address below"); al.Add("this host"); al.Add("my address"); ddNeighbors.DataSource = al; ddNeighbors.DataBind(); setCoordFromHost(); } } /// /// Populates the coordinates from the Reservation configuration. /// private void setCoordFromHost() { tbAddress.Text = Global.address; tbCity.Text = Global.city; ddState.DataSource = Global.stateAl; ddState.DataBind(); ddState.SelectedIndex = Global.stateIndex; tbZip.Text = Global.zipcode; ddCountry.DataSource = Global.countryAl; ddCountry.DataBind(); ddCountry.SelectedIndex = Global.countryIndex; tbRegion.Text = Global.region; tbLatitude.Text = Global.latitude; tbLongitude.Text = Global.longitude; } private void Page_Init(object sender, EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); } #region Web Form Designer generated code /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.btnList.Click += new System.EventHandler(this.OnList); this.btnClear.Click += new System.EventHandler(this.OnClear); this.CalBook.SelectionChanged += new System.EventHandler(this.OnCalSel); this.ddNeighbors.SelectedIndexChanged += new System.EventHandler(this.OnNeighborChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion /// /// Event fired when the user changes the entry selected on the Neighbor DropDown list. /// Populates the coordinates. /// private void OnNeighborChanged(object sender, System.EventArgs e) { if (ddNeighbors.SelectedItem.Text.Equals("this host")) setCoordFromHost(); else if (ddNeighbors.SelectedItem.Text.Equals("my address")) { OleDbConnection odc = new OleDbConnection(Global.connectionString); odc.Open(); OleDbCommand odcmd = new OleDbCommand("select * from users where id='" + id.PadRight(10, ' ') + "'", odc); OleDbDataReader odr = odcmd.ExecuteReader(); if (odr.Read()) { char[] blank = new char[] { ' ' }; tbAddress.Text = (string)odr["address"]; tbCity.Text = (string)odr["city"]; tbZip.Text = ((string)odr["zip"]).TrimEnd(blank); tbRegion.Text = ((string)odr["region"]).TrimEnd(blank); tbLatitude.Text = ((string)odr["latitude"]).TrimEnd(blank); tbLongitude.Text = ((string)odr["longitude"]).TrimEnd(blank); setDropDown((string)odr["state"], (string)odr["country"], blank); } odr.Close(); odc.Close(); } } /// /// Set the state and country drop down lists /// /// State or province code /// Country code /// For trim private void setDropDown(string state, string country, char[] blank) { string st = state.TrimEnd(blank); ddState.SelectedIndex = 0; if (st.Length > 0) { string sta = null; if (Global.statePerCode.Contains(st)) sta = (string)Global.statePerCode[st]; else { if (Global.provincePerCode.Contains(st)) sta = (string)Global.provincePerCode[st]; } for (int i = 0; i < Global.stateAl.Count; ++ i) { string s = (string)Global.stateAl[i]; if (s.Equals(sta)) { ddState.SelectedIndex = i; break; } } } string countryName = (string)Global.countryPerCode[country]; for (int i = 0; i < Global.countryAl.Count; ++ i) { string c = (string)Global.countryAl[i]; if (c.Equals(countryName)) { ddCountry.SelectedIndex = i; break; } } } /// /// Event fired when the user clicks on the Clear schedule selection button. /// Resets the schedule checkboxes. /// private void OnClear(object sender, System.EventArgs e) { H800.Checked = false; H830.Checked = false; H900.Checked = false; H930.Checked = false; H1000.Checked = false; H1030.Checked = false; H1100.Checked = false; H1130.Checked = false; H1200.Checked = false; H1230.Checked = false; H1300.Checked = false; H1330.Checked = false; H1400.Checked = false; H1430.Checked = false; H1500.Checked = false; H1530.Checked = false; H1600.Checked = false; H1630.Checked = false; H1700.Checked = false; H1730.Checked = false; H1800.Checked = false; H1830.Checked = false; } /// /// Event fired when the user clicks on the List button. /// Populates the datagrid. /// Calls addSelected. /// private void OnList(object sender, System.EventArgs e) { if (cbs == null) cbs = new CheckBox[,] { { H800, H830 }, { H900, H930 }, { H1000, H1030 }, { H1100, H1130 }, { H1200, H1230 }, { H1300, H1330 }, { H1400, H1430 }, { H1500, H1530 }, { H1600, H1630 }, { H1700, H1730 }, { H1800, H1830 }}; string st = ddState.SelectedItem.Value; string state = ""; if (!st.Equals("N/A")) { if (Global.states.Contains(st)) state = (string)Global.states[st]; else state = (string)Global.provinces[st]; } string c = ddCountry.SelectedItem.Value; string country = (string)Global.countries[c]; Hashtable h = new Hashtable(); OleDbConnection odc = new OleDbConnection(Global.connectionString); odc.Open(); string ifcountry = ""; if (country.Length > 0) ifcountry = " and country='" + country + "'"; if (tbCity.Text.Length > 0) { string ifstate = ""; if (state.Length > 0) ifstate = " and state='" + state.PadRight(3, ' ') + "'"; addSelected("select url, city, state, address, zip, country from applications where city='" + tbCity.Text + "' and url <> '" + Global.url + "'" + ifstate + ifcountry, odc, h, state, country); } if (h.Count < 5) { string zipBegin = tbZip.Text.Substring(0, 2); addSelected( "select url, city, state, address, zip, country from applications where zip like '" + zipBegin + "%'" + ifcountry, odc, h, state, country); } if (h.Count < 5) { if (state.Length == 0) { if (country.Length > 0) addSelected( "select url, city, state, address, zip, country from applications where country='" + country + "'", odc, h, state, country); } else addSelected( "select url, city, state, address, zip, country from applications where state='" + state.PadRight(3, ' ') + "'" + ifcountry, odc, h, state, country); } odc.Close(); dgApplications.DataSource = h.Values; dgApplications.DataBind(); } /// /// Builds a datagrid row. /// Calls checkAvail. /// /// SQL command /// Database connection /// ArrayList to populate private void addSelected(string cmd, OleDbConnection odc, Hashtable h, string state, string country) { OleDbCommand odcmd = new OleDbCommand(cmd, odc); OleDbDataReader odr = odcmd.ExecuteReader(); while(odr.Read()) { string url = (string)odr["url"]; if (h.Contains(url)) continue; if (checkAvail(url)) { char[] blank = new char[] { ' ' }; ApplicationEntry ae = new ApplicationEntry(); string reg = tbRegion.Text.ToLower(); if (reg.Equals("eu") || reg.Equals("europe")) { ae.mapquest = "/europe.adp?1a=" + Server.UrlEncode(tbAddress.Text) + "&1c=" + Server.UrlEncode(tbCity.Text) + "&1z=" + Server.UrlEncode(tbZip.Text) + "&1y=" + Server.UrlEncode(country) + "&2a=" + Server.UrlEncode((string)odr["address"]) + "&2c=" + Server.UrlEncode((string)odr["city"]) + "&2z=" + Server.UrlEncode(((string)odr["zip"]).TrimEnd(blank)) + "&2y=" + Server.UrlEncode((string)odr["country"]); } else { ae.mapquest = "/main.adp?1a=" + Server.UrlEncode(tbAddress.Text) + "&1c=" + Server.UrlEncode(tbCity.Text) + "&1z=" + Server.UrlEncode(tbZip.Text) + "&1s=" + Server.UrlEncode(state) + "&1y=" + Server.UrlEncode(country) + "&2a=" + Server.UrlEncode((string)odr["address"]) + "&2c=" + Server.UrlEncode((string)odr["city"]) + "&2z=" + Server.UrlEncode(((string)odr["zip"]).TrimEnd(blank)) + "&2s=" + Server.UrlEncode(((string)odr["state"]).TrimEnd(blank)) + "&2y=" + Server.UrlEncode((string)odr["country"]); } ae.lurl = url; ae.url = url.Replace("/Location.asmx", "/Book.aspx"); h.Add(url, ae); } } odr.Close(); } /// /// Calls the Location Web service. /// /// URL of the target Reservation instance /// true if the requested schedule is available private bool checkAvail(string url) { if (!H800.Checked && !H830.Checked && !H900.Checked && !H930.Checked && !H1000.Checked && !H1030.Checked && !H1100.Checked && !H1130.Checked && !H1200.Checked && !H1230.Checked && !H1300.Checked && !H1330.Checked && !H1400.Checked && !H1430.Checked && !H1500.Checked && !H1530.Checked && !H1600.Checked && !H1630.Checked && !H1700.Checked && !H1730.Checked && !H1800.Checked && !H1830.Checked) return true; LocationProxy loc = new LocationProxy(url); TimeEntry[] tes = null; try { loc.Discover(); tes = loc.Available(CalBook.SelectedDate); } catch(Exception e) { return false; } if (tes != null) { for (int i = 0; i < tes.Length; ++i) { if (cbs[tes[i].hour - 8, tes[i].minutes / 30].Checked) return true; } } return false; } private void OnCalSel(object sender, System.EventArgs e) { H800.Enabled = true; H830.Enabled = true; H900.Enabled = true; H930.Enabled = true; H1000.Enabled = true; H1030.Enabled = true; H1100.Enabled = true; H1130.Enabled = true; H1200.Enabled = true; H1230.Enabled = true; H1300.Enabled = true; H1330.Enabled = true; H1400.Enabled = true; H1430.Enabled = true; H1500.Enabled = true; H1530.Enabled = true; H1600.Enabled = true; H1630.Enabled = true; H1700.Enabled = true; H1730.Enabled = true; H1800.Enabled = true; H1830.Enabled = true; } } /// /// Row of the datagrid. /// public sealed class ApplicationEntry { /// /// URL of the Reservation candidate Book.aspx (booking page) /// public string url; /// /// URL of the Reservation candidate Location.asmx (Applications key) /// public string lurl; /// /// URL of the MapQuest geolocation service /// public string mapquest; /// /// URL of the Reservation candidate Book.aspx (booking page) /// public string Url { get { return url; } } /// /// URL of the Reservation candidate Location.asmx (Applications key) /// public string LUrl { get { return lurl; } } /// /// URL of the MapQuest geolocation service /// public string MapQuest { get { return mapquest; } } } }