The Author Object

I l @ ve RuBoard

The Author Object

The Product object makes use of the Author object because a product has a reference to the authors of the book. So, you need to implement the Author object now as well. The Author object, by the way, is extremely simple, as Listing 9.7 shows.

Listing 9.7 Author.Java
 package com.bfg.product; import java.util.Vector; import java.util.HashMap; public class Author {     protected static HashMap authors = new HashMap();     protected Vector pBooks = new Vector();     protected String pName;     public Vector getBooks() {      return pBooks;     }     public void setBooks(Vector Books) {      pBooks = Books;     }     public String getName() {      return pName;     }     public void setName(String Name) {      pName = Name;     }         public static Author findAuthor(String name) {     if (authors.get(name) != null) {         return ((Author) authors.get(name));     }     Author author = new Author();     author.setName(name);     authors.put(name, author);     return author;     } } 

All that the class has is a property to store the author's name and another property that stores a list of the books that the author has written. This will come in useful if you add functionality that lets you click on an author's name and retrieve a list of books that the author has written. As with the products, you will have the class cache the authors.

The getAuthorString method is a JSP helper function. The authors of a book need to be displayed in a lot of places. Because they are stored as a vector of objects, it's easier to set up one place where the JSP can go to get a print-friendly version of the author list, rather than having to replicate the code in the JSP every time it is needed.

Similarly, getPriceString returns a formatted string with a dollar sign ($) and two decimal places on the right; otherwise , $49.90 would show up as 49.9 on the page. getPubDateString returns the publication date in the form that you want ”just asking for the Date object gets you somewhere along the lines of 2001-10-02 .

findProduct works a lot like findCustomer , except that, because it is caching, it will need to check whether the book is in the cache first. If it is not, it must be read out of the database. The SQL queries from the SQLQueries.properties file are shown in Listing 9.8.

Listing 9.8 SQLQueries.properties
 findQuery=SELECT * FROM PRODUCT WHERE ISBN = ? authorQuery=SELECT AUTHOR_NAME FROM AUTHOR,PRODUCT,PRODUCT_AUTHOR_XREF PAX \                    WHERE AUTHOR.AUTHOR_ID = PAX.AUTHOR_ID AND \                    PAX.PRODUCT_ISBN = PRODUCT.ISBN AND \                PRODUCT.ISBN = ? 

If the method finds a matching product, it looks for authors for that product. It goes through adding authors to the product and adding products to the authors. Finally, it releases the database connection and returns the product.

Now you can add a unit test for Product to the Ant script with the following additions to the test target (see Listing 9.9).

Listing 9.9 Additions to build.xml
 <java classname="com.bfg.product.Product" fork="yes">   <classpath>   <pathelement path="${java.class.path} "/>     <fileset dir="c:\tomcat\lib">       <include name="**/*.jar"/>     </fileset>     <pathelement path="src"/>     <pathelement path="props"/>   </classpath> </java> 

When you run the script, you can see that the unit test worked:

 C:\CARTAPP\bfg>ant test Buildfile: build.xml init: compile:     [javac] Compiling 1 source file dist:       [jar] Building jar: C:\tomcat\webapps\bfg\WEB-INF\lib\bfgclasses.jar test:      [java] Turbine: init      [java] Turbine: Turbine: init() Ready to Rumble!      [java] Good Test: Create Customer      [java] Good Test: Create Duplicate Customer      [java] Good Test: Find Real Customer      [java] Good Test: Find Fake Customer      [java] Good Test: Delete Customer      [java] Turbine: init      [java] Turbine: Turbine: init() Ready to Rumble!      [java] Good Test: Find Real Product      [java] Author string is: Stefan Haustein; Michael Kroll BUILD SUCCESSFUL Total time: 3 seconds 

Now you can write the JSP to display a product (see Listing 9.10) that is again based on the wireframe.

Listing 9.10 Product.jsp
 <%@ page import="com.bfg.product.Product" %> <% Product prod = null; if (((request.getParameter("ISBN") != null) &&      (prod = Product.findProduct(request.getParameter("ISBN"))) != null)) { %> <head> <title><%= prod.getTitle() %></title> </head> <%@ include file="/jsp/includes/bfgheader.jsp" %> <h2 align="center"><span style="font-family: Times New Roman"> <%= prod.getTitle() %> </span></h2> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" graphics/ccc.gif bordercolor="#111111" width="639" id="AutoNumber3">  <tr>    <td width="128">     <img border="0" src="/bfg/jsp/images/products/<%= prod.getISBN() %>.jpg"          align="left" width="125" height="155">    </td>    <td valign="top" width="511">      <p align="center">      <span style="font-size: 10.0pt; font-family: Times New Roman">         By <%= prod.getAuthorString() %>      </span></p>         <p align="center"><font size="2" face="Times New Roman">Publication         date: </font>         <span style="font-size: 10.0pt; font-family: Times New Roman">          <%= prod.getPubDateString() %>, <%= prod.getPriceString() %></span></td>    </tr> </table> <p><span style="font-size: 10.0pt"><%= prod.getDescription() %></span></p>     <p><font size="2"><a href="buyit.jsp?ISBN=<%= prod.getISBN() %>">[Buy It!] </a></font></p> <p align="center">&nbsp;</td> <%@ include file="/jsp/includes/bfgfooter.jsp" %> <% }  else {%> <head> <title>ISBN Not Found</title> </head> <%@ include file="/jsp/includes/bfgheader.jsp" %> <H1>The requested ISBN was not found.</H1> If you believe you have reached this page in error, please contact <A HREF="mailto: info@bfgbooks.com">info@bfgbooks.com</A> <% }  %> 

The ISBN is passed in as a parameter, and the first thing that the JSP does ”even before displaying the title ”is to look up the ISBN. If it can't find the ISBN, you have it skip to the end and put up a helpful error message (see Figure 9.2).

Figure 9.2. A bad ISBN.

graphics/09fig02.jpg

On the other hand, if the customer requested a good ISBN, your beautiful product display page appears (see Figure 9.3).

Figure 9.3. The product page in action.

graphics/09fig03.jpg

As you can see, the code extracts various attributes of the book and puts them in place of the boilerplate text that the wireframe had used.

I l @ ve RuBoard


MySQL and JSP Web Applications. Data-Driven Programming Using Tomcat and MySQL
MySQL and JSP Web Applications: Data-Driven Programming Using Tomcat and MySQL
ISBN: 0672323095
EAN: 2147483647
Year: 2002
Pages: 203
Authors: James Turner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net