"An Online Ticket Store: Part 3, Tagging the Data,"
Volume: 4 Issue: 9, p. 48

Listing 1:
<?xml version="1.0"?>
<TicketQuery>
 <DepartureCity>Smallville</DepartureCity>
 <ArrivalCity>Gotham</ArrivalCity>
 <Date>
  <BeginDate>
   <Year>1999</Year>
   <Month>12</Month>
   <Day>31</Day>
  </BeginDate>
  <EndDate>
   <Year>2000</Year>
   <Month>1</Month>
   <Day>1</Day>
  </EndDate>
 </Date>
 <NoOfSeats>2</NoOfSeats>
 <Class>First</Class>
 <SeatingPreference>Window<SeatingPreference>
 <SmokingPreference>No<SmokingPreference>
</TicketQuery>

Listing 2:
<?xml version="1.0"?>
<TicketQuote>
 <ReferenceNo>"SA2345678"</ReferenceNo>
 <Airline>SeemaAir</Airline>
 <DepartureCity>Smallville</DepartureCity>
 <ArrivalCity>Gotham</ArrivalCity>
 <Date>
  <DepartureDate>
   <Year>1999</Year>
   <Month>12</Month>
   <Day>31</Day>
  </DepartureDate>
  <DepartureTime>
   <Hour>22</Hour>
   <Minutes>00</Minutes>
  </DepartureTime>
  <ArrivalDate>
   <Year>2000</Year>
   <Month>1</Month>
   <Day>1</Day>
  </ArrivalDate>
  <ArrivalTime>
   <Hour>1</Hour>
   <Minutes>30</Minutes>
  </ArrivalTime>
 </Date>
 <NoOfSeats>2</NoOfSeats>
 <Class>First</Class>
 <SeatingPreference>Window<SeatingPreference>
 <SmokingPreference>No<SmokingPreference>
 <Price>"$1234.56"</Price>
</TicketRequest>

Listing 3:
<?xml version="1.0"?>
<BookingRequest>
 <ReferenceNo>"SA2345678"</ReferenceNo>
 <Airline>SeemaAir</Airline>
 <NameList>
  <Name>
   <LastName>"Kent"</LastName>
   <FirstName>"Clark"</FirstName>
       </Name>
  <Name>
   <LastName>"Lane"</LastName>
   <FirstName>"Lois"</FirstName>
       </Name>
 </NameList>
</BookingRequest>

Listing 4:
<?xml version="1.0"?>
<Confirmation Type="Ticket">
 <ConfirmationNo>"SA2345678"</ConfirmationNo>
</Confirmation>

Listing 5:
 <?xml version="1.0"?>
<Lease>
   <Passenger>
       <Name>
            <LastName>"Kent"</LastName>
            <FirstName>"Clark"</FirstName>
       </Name>
   </Passenger>
   <Flight>
        <ReferenceNo>"SA2345678"</ReferenceNo>
        <Airport>"Sunita International Airport"</Airport>
        <Airline>"SeemaAir"</Airline>
        <FlightNumber>"SA 123"<FlightNumber>
        <Date>
           <Year>1999</Year>
           <Month>12</Month>
           <Day>31</Day>
        </Date>
    </Flight>
    <ItemList>
        <ItemName>"CD Player"</ItemName>
        <ItemId>cd00321</ItemId>
        <Quantity>1</Quantity>
        <ItemName>"Book"</Book>
        <ItemId>bookx453</ItemId>
        <Quantity>1</Quantity>
    </ItemList>
</Lease>

Listing 6:
<?xml version="1.0"?>
<Purchase>
   <Name>
       <LastName>"Kent"</LastName>
       <FirstName>"Clark"</FirstName>
   </Name>
   <Address>
       <Street>"123 Tiny Lane"</Street>
       <City>"Smallville"</City>
       <State>"Kansas"</State>
       <Country>USA</Country>
       <ZIP>12345</ZIP>
   </Address>
   <ItemList>
        <ItemName>"CD Player"</ItemName>
        <ItemId>cd00321</ItemId>
        <Quantity>1</Quantity>
        <ItemName>"Book"</Book>
        <ItemId>bookx453</ItemId>
        <Quantity>1</Quantity>
    </ItemList>
</Purchase>
 

Listing 7: Data Object for the Online Ticket Store
import java.io.Serializable;
import java.text.ParseException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DataObjects {
}

// file TicketQuery.java
class TicketQuery implements Serializable {
 
  DateFormat df_ = DateFormat.getDateInstance();

  // Fields for a ticket query
  private long queryId_ = 0;
  private String departureCity_ = null;
  private String arrivalCity_ = null;
  private Date beginDate_ = null; // Acceptable begin date and time for flight query
  private Date endDate_ = null;   // Acceptable end date and time for flight query.
 
  private int noOfSeats_ = 0;
  private String seatClass_ = null;  //  coach, business, first class
  private boolean aisle_ = false;    // window, aisle
  private boolean smoking_ = false;  // smoking/non-smoking

  /**
   * Full-arg constructor
   */
  public TicketQuery (String departureCity,
       String arrivalCity,
       String beginDate,
       String endDate,
       String noOfSeats,
       String seatClass,
       String aisle,
       String smoking) {
 
 // Generate new query ID
 queryId_ = generateQueryId();
 
 departureCity_ = departureCity;
 arrivalCity_ = arrivalCity;
 
 // The date Strings need to be parsed
 setBeginDate(beginDate);
 setEndDate(endDate);
 setNoOfSeats(noOfSeats);
 setSeatClass(seatClass);
 setAisle(aisle);
 setSmoking(smoking);
  }
 
  /**
   * The current time is used to generate the queryId. This way, we get
   * a time stamp of when the query was made.
   */
  public long generateQueryId () {
   return System.currentTimeMillis();
  }
 
  //
  // -- Setter methods are required for these fields
 
  /**
   *
   */
  public void setBeginDate (String beginDate) {
   try {
    beginDate_ = df_.parse(beginDate);
   }
   catch (ParseException pe) {
  pe.printStackTrace();
   }
  }
 
  public void setEndDate (String endDate) {
   try {
          endDate_ = df_.parse(endDate);
   }
      catch (ParseException pe) {
    pe.printStackTrace();
   }
  }
 
  public void setNoOfSeats(String noOfSeats) {
   noOfSeats_ = Integer.parseInt(noOfSeats);
  }
 
  public void setSeatClass (String seatClass) {
   seatClass_ = seatClass;
  }
 
  public void setAisle (String aisle) {
   if (aisle.equalsIgnoreCase("YES"))
    aisle_ = true;
   else
    aisle_ = false;
  }
 
  public void setSmoking(String smoking) {
   if (smoking.equalsIgnoreCase("YES"))
    smoking_ = true;
   else
    smoking_ = false;
  }

  //
  // -- Getter methods for all the fields.
  //
 
  public long getQueryId () {
   return queryId_;
  }
 
  public String getDepartureCity () {
   return departureCity_;
 
  }
 
  public String getArrivalCity () {
   return arrivalCity_;
  }
 
  public Date getBeginDate () {
   return beginDate_;
  }
 
  public Date getEndDate () {
   return endDate_;
  }
 
  public int getNoOfSeats () {
   return noOfSeats_;

  }
 
  public String getSeatClass () {
   return seatClass_;

  }
 
  public boolean isAisle () {
   return aisle_;

  }
 
  public boolean isSmoking () {
   return smoking_;

  }
}

// file TicketQuote.java
class TicketQuote implements Serializable {
 
  SimpleDateFormat sdf_ = new SimpleDateFormat();
 
  // Fields for a ticket query
  private String referenceNo_ = null;
  private String airline_ = null;
  private String departureCity_ = null;
  private String arrivalCity_ = null;

  // The Date fields include the time values also
  private Date departureDate_ = null;
  private Date arrivalDate_ = null;
 
  private int noOfSeats_ = 0;
  private String seatClass_ = null;  //  coach, business, first class
  private boolean aisle_ = false;    // window, aisle
  private boolean smoking_ = false;  // smoking/non-smoking
  private String price_ = "$0.00";

  /**
   * Full-arg constructor
   */
  public TicketQuote (String referenceNo,
       String airline,
       String departureCity,
       String arrivalCity,
       Date departureDate,
       Date arrivalDate,
       int noOfSeats,
       String seatClass,
       boolean aisle,
       boolean smoking,
       String price) {
 
 referenceNo_ = referenceNo;
 airline_ = airline;
 departureCity_ = departureCity;
 arrivalCity_ = arrivalCity;
 departureDate_ = departureDate_;
 arrivalDate_ = arrivalDate_;
 noOfSeats_ = noOfSeats;
 seatClass_ = seatClass;
 aisle_ = aisle;
 smoking_ = smoking;
 price_ = price;
  }
 
  //
  // -- Getter methods for all the fields.
  //
 
  public String getReferenceNo () {
   return referenceNo_;
  }
 
  public String getAirline () {
   return airline_;
  }
 
  public String getDepartureCity () {
   return departureCity_;
 
  }
 
  public String getArrivalCity () {
   return arrivalCity_;
  }
 
  public Date getDepartureDate () {
   return departureDate_;
  }
 
  public Date getArrivalDate () {
   return arrivalDate_;
  }
 
  public int getNoOfSeats () {
   return noOfSeats_;

  }
 
  public String getSeatClass () {
   return seatClass_;

  }
 
  public boolean isAisle () {
   return aisle_;

  }
 
  public boolean isSmoking () {
   return smoking_;

  }
 
  public String getPrice () {
   return price_;
  }
}

// PLEASE GO TO www.sys-con.com/java FOR THE FOLLOWING CODE

class Name {}
class BookingRequest {}
class Confirmation {}
class Address {}
class Item {}
class Lease {}
class Purchase {}

Listing 8: TicketBrokerServlet.java
import java.io.*;
import java.util.*;
import java.rmi.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class TicketBrokerServlet extends HttpServlet
{
  protected HttpServletRequest request_ = null;
  protected HttpServletResponse response_ = null;
  protected StringBuffer result_ = null;
 
  XMLProcessor xp_ = null;
 
  PrintWriter toClient_ = null;
 
  // TicketClientManager clientManager_ = null;
 
 
  public void doPost (HttpServletRequest request,
                      HttpServletResponse response)
    throws ServletException, IOException {
 try {

      request_ = request;
      response_ = response;

   // Get a reference to the output stream
      toClient_ = new PrintWriter(response_.getOutputStream());

      if (toClient_ == null) {
        response_.sendError(1, "Null toClient_ stream");
   }
 
      // etc.
 
   result_ = new StringBuffer();
   processParameters();
 
   toClient_.println(result_.toString());
 }
 catch (Exception e) {
   e.printStackTrace();
 }
  }
 
  private void processParameters () {
 // Create a new XMLProcessor to convert XML to Java objects.
 xp_ = new XMLProcessor();
 
 try {
   xp_.initParser(request_.getInputStream());
 }
 catch (IOException ioe) {
   ioe.printStackTrace();
 }
 
 String queryType = xp_.getQueryType();
 if (queryType.equals("TicketQuery")) {
  TicketQuery ticketQuery = xp_.processTicketQuery();
  processQuery(ticketQuery);
 }
 else if (queryType.equals("BookingRequest")) {
  // BookingRequest bookingRequest = xp_.processTicketBooking();
  // processBooking(bookingRequest);
 }
  }

  /**
   *
   */
  private void processQuery (TicketQuery query) {
 Vector quotes = null;
 
 // !! Stubbed code. Typically, this will be replaced by a call
 // !! to the appropriate client via the TicketClientManager.
 
 // etc.
 
 TicketQuote ticketQuote = new TicketQuote("SA123456", "SeemaAir",
             "SmallVille", "Gotham",
             new Date("1999.12.31"),
             new Date("2000.1.1"),
             2, "First", false, false, "$1234.56");
 
 String xmlQuote = xp_.processTicketQuote(ticketQuote);
 
 result_.append(xmlQuote);
  }
 
  /**
   * Process a booking request.
   */
  private void processBooking (BookingRequest bookingRequest) {
 // etc.
  }
 

  /**
   * Return best quote from the quotes obtained from the Application
   * Services tier
   */
  private TicketQuote getBestQuote (Vector quotes)
  {
   // etc.
   return null;
  }
 
}

Listing 9: XML processor for the Online Ticket Store

import com.ibm.xml.parser.*;
import org.w3c.dom.*;
import java.io.*;
import java.util.*;
import Order;
import Promise;
import AtomicDate;
import AtomicOrder;

public class XMLProcessor {
  Document doc_;
  TXElement root_;
 
  /**
   * Initialize the XML parser and document
   */
  public void initParser(InputStream input)
  {
    Parser p = new Parser("XMLParser");
    doc_ = p.readStream(input);
    root_ = (TXElement) doc_.getDocumentElement();
  }
 
  public String getQueryType () {
   return root_.getName();
  }

  public TicketQuery processTicketQuery ()
  {
    String departureCity = processSingleTag("DepartureCity", root_);
    String arrivalCity =  processSingleTag("ArrivalCity", root_);
 String noOfSeats = processSingleTag("NoOfSeats", root_);
 String seatClass = processSingleTag("Class", root_);
 String seatingPref = processSingleTag("SeatingPreference", root_);
 String smokingPref = processSingleTag("SmokingPreference", root_);
 
 
 TXElement txe = null;
 
 NodeIterator iterator1 = root_.getElementsByTagName("Date");
 
 // There is only one Date element
 TXElement dateElement = (TXElement)iterator1.toFirstNode();
 
 NodeIterator iterator2 = dateElement.getElementsByTagName("BeginDate");
 
 TXElement beginDateElement = (TXElement)iterator2.toFirstNode();
 
 String beginYear = processSingleTag("Year", beginDateElement);
 String beginMonth = processSingleTag("Month", beginDateElement);
 String beginDay = processSingleTag("Day", beginDateElement);
 
 String beginDate = beginYear + "." + beginMonth + "." + beginDay;
 
 iterator2 = dateElement.getElementsByTagName("EndDate");
 
 TXElement endDateElement = (TXElement)iterator2.toFirstNode();
 
 String endYear = processSingleTag("Year", endDateElement);
 String endMonth = processSingleTag("Month", endDateElement);
 String endDay = processSingleTag("Day", endDateElement);
 
    String endDate = endYear + "." + endMonth + "." + endDay;
 
    return new TicketQuery(departureCity, arrivalCity, beginDate, endDate,
         noOfSeats, seatClass, seatingPref, smokingPref);
 
  }
 
  /**
   * Process a single tag from an XML document
   */
 
  public String processSingleTag(String tag, Element startNode) {
    TXElement e;
    Node n;
    String textValue = null;
    NodeIterator iter = startNode.getElementsByTagName(tag);

    int length = iter.getLength();

    if (length != 1) {
      System.out.println("Houston we have a problem with tag: " + tag);
    }
 else {
      n = iter.toFirstNode();
      e = (TXElement) n;
      textValue = e.getText();
    }

    return textValue;
  }
 
  /**
   * Process a list of tags from an XML document
   */

  public Vector processListTag(String tag, Element startNode)
  {
    TXElement e, cElement, cTemp;
    Node n;
    Vector list = null;
    String text;

 System.out.println("tag = " + tag + ", startNode = " + startNode);
    NodeIterator iter = startNode.getElementsByTagName(tag);

    int length = iter.getLength();

    if (length != 0)
    {
      list = new Vector();
    }

    for (int i = 0; i < length; i++)
    {
      n = iter.moveTo(i);
      e = (TXElement) n;
      list.addElement(e);
    }
    return list;
  }

  /**
   * Create an XML document from the TicketQuote object
   */
  public String processTicketQuote (TicketQuote quote)
  {
    TXDocument doc = new TXDocument();
    TXElement root = new TXElement("TicketQuote");
    doc.insert(root, 0);
    doc.setVersion("1.0");

    TXElement child, grandChild, greatGrandChild;
    TXText text;

    child = new TXElement("ReferenceNo");
    text = new TXText((quote.getReferenceNo()).toString());
    child.addTextElement(text);
    root.addElement(child);
 
    child = new TXElement("Airline");
    text = new TXText((quote.getAirline()).toString());
    child.addTextElement(text);
    root.addElement(child);

    child = new TXElement("DepartureCity");
    text = new TXText((quote.getDepartureCity()).toString());
    child.addTextElement(text);
    root.addElement(child);

    child = new TXElement("ArrivalCity");
    text = new TXText((quote.getArrivalCity()).toString());
    child.addTextElement(text);
    root.addElement(child);

    child = new TXElement("Date");
    root.addElement(child);
 
 grandChild = new TXElement("DepartureDate");
    child.addElement(grandChild);

 Date date = quote.getDepartureDate();
 
 greatGrandChild = new TXElement("Year");
 text = new TXText(String.valueOf(date.getYear()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 greatGrandChild = new TXElement("Month");
 text = new TXText(String.valueOf(date.getMonth()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 greatGrandChild = new TXElement("Day");
 text = new TXText(String.valueOf(date.getDay()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 grandChild = new TXElement("ArrivalDate");
    child.addElement(grandChild);

 date = quote.getArrivalDate();
 
 greatGrandChild = new TXElement("Year");
 text = new TXText(String.valueOf(date.getYear()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 greatGrandChild = new TXElement("Month");
 text = new TXText(String.valueOf(date.getMonth()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 greatGrandChild = new TXElement("Day");
 text = new TXText(String.valueOf(date.getDay()));
 greatGrandChild.addTextElement(text);
    grandChild.addElement(greatGrandChild);
 
 child = new TXElement("NoOfSeats");
 text = new TXText(String.valueOf(quote.getNoOfSeats()));
    child.addTextElement(text);
    root.addElement(child);

 child = new TXElement("Class");
    text = new TXText((quote.getSeatClass()).toString());
    child.addTextElement(text);
    root.addElement(child);

 child = new TXElement("SeatingPreference");
 text = new TXText(String.valueOf(quote.isAisle()));
    child.addTextElement(text);
    root.addElement(child);

 child = new TXElement("SmokingPreference");
 text = new TXText(String.valueOf(quote.isSmoking()));
    child.addTextElement(text);
    root.addElement(child);

 child = new TXElement("Price");
    text = new TXText((quote.getPrice()).toString());
    child.addTextElement(text);
    root.addElement(child);

 StringWriter dString = new StringWriter();
    doc.printWithFormat(new PrintWriter(dString));
 return dString.toString();
  }
}