JavaScript Editor Free JavaScript Editor     JavaScript Debugger 




Main Page

Previous Page
Next Page

8.4. A Problem Revisited

Now that we have covered some of the necessary background material for using XML, SOAP, and XMLHTTP, let's apply it to the e-commerce site. As you might recall, the objective of the site is to provide materials for the often-overlooked market of mad scientists, alchemists, and sorcerers. In Chapter 5, we created pages using a primitive ancestor of Ajax; now let's give it a shot using the real thing. This doesn't mean that it is entirely necessary to completely abandon hidden frames. If you decide that you need them, then by all means, use them; we abandon hidden frames from here on, however.

In addition, we change server-side languages from PHP to C#. The reason for this change isn't that PHP can't be used to develop web services; it is actually the fact that I'm more comfortable using C# for developing web services. To those of you who question the presence of C# in an open source book, I have one word for you: Mono.

No, not the Mono that everybody came down with in high school, college, or, in my case, Bell Labsthe Mono that is the open source implementation of the .NET Framework. You haven't lived until you've seen a C# application running under Linux. It doesn't feel wrong; it feels more like when Lieutenant Commander Worf said: "Assimilate this!" in Star Trek First Contact.

Listing 8-18 contains the web service that will handle the server-side requirements for the remainder of this chapter.

Listing 8-18. A Web Service

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml;
using MySql.Data.MySqlClient;
using MySql.Data.Types;
namespace AJAX
{
  /// <summary>
  /// Summary description for msas.
  /// </summary>
  public class msas : System.Web.Services.WebService
  {
    const string CONNECTION_STRING =
      "Persist Security
Info=False;database=ajax;server=localhost;username=root;password=wyvern";

    public msas()
    {
      InitializeComponent();
    }

    #region Component Designer generated code

    //Required by the Web Services Designer
    private IContainer components = null;

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
    }

    /// <summary>
    /// Clean up any resources being used.
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if(disposing && components != null)
      {
        components.Dispose();
      }
      base.Dispose(disposing);
    }

    #endregion

    [WebMethod]
    public XmlDocument getState(string state_abbreviation)
    {
      MySqlConnection connection =
      new MySqlConnection(CONNECTION_STRING);
      MySqlDataAdapter adapter = new MySqlDataAdapter();
      DataSet dataSet = new DataSet();
      XmlDocument xml = new XmlDocument();
      string query = "CALL stateSelect(NULL)";
      if(state_abbreviation.Length != 0)
        query = "CALL stateSelect('" + state_abbreviation + "')";

      adapter.SelectCommand =
      new MySqlCommand(query, connection);
      adapter.Fill(dataSet);
      xml.LoadXml(dataSet.GetXml());

      connection.Close();

      return(xml);
    }

    [WebMethod]
    public XmlDocument getXML(string name)
    {
      XmlDocument xml = new XmlDocument();

      try
      {
        xml.Load(Server.MapPath(name));
      }
      catch(Exception e)
      {
        StringWriter writer = new StringWriter();
        Server.UrlEncode(name, writer);
        String encodedName = writer.ToString();
        XmlNode node =
        xml.CreateNode(XmlNodeType.CDATA,"detail","");

        node.Value = encodedName;

        throw(new
SoapException(e.Message,SoapException.ClientFaultCode,"",node));
      }

      return(xml);
    }

    [WebMethod]
    public XmlDocument getItems(string guild_item_id,string guild_id)
    {
      MySqlConnection connection =
      new MySqlConnection(CONNECTION_STRING);
      MySqlDataAdapter adapter = new MySqlDataAdapter();
      DataSet dataSet = new DataSet();
      XmlDocument xml = new XmlDocument();
      string query;

      if(guild_item_id.Length == 0)
        if(guild_id.Length == 0)
          query = "CALL itemSelect(NULL,NULL)";
        else
          query = "CALL itemSelect(NULL," + guild_id + ")";
      else
        if(guild_id.Length == 0)
          query = "CALL itemSelect(" + guild_item_id + ",NULL)";
        else
          query = "CALL itemSelect(" + guild_item_id + "," + guild_id +
")";
      adapter.SelectCommand =
      new MySqlCommand(query, connection);
      adapter.Fill(dataSet);
      xml.LoadXml(dataSet.GetXml());

      connection.Close();

      return(xml);
    }
  }
}

I'd like to point out that the web service shown handles several different jobs. First, if necessary, it performs database queries against a MySQL database. Immediately following the queries, it builds the XHTML required to display the page; finally, it creates a node that contains a line of JavaScript. All this is then incorporated into a single XML document, which is then sent to the client. Although this might seem a wee bit strange, there is a method to my madness. As with the hidden frames example, there will be a single HTML document that also has several different jobs to perform.


Previous Page
Next Page

R7


JavaScript Editor Free JavaScript Editor     JavaScript Debugger


©