Order History

I l @ ve RuBoard

You're down to the last piece of functionality ”displaying order history. This is the code on the MyAccount page that will let a customer see his past orders and drill down to a detailed view. To start, you need to add a method to Customer.java (shown in Listing 13.10) to get back a vector of orders for display. Luckily, you already wrote the findOrder code when you created Order.java, so you are already a step ahead.

Listing 13.10 Addition to Customer.java
 import com.bfg.product.Order; . . .     public Vector getOrderHistory() {      Vector orders = new Vector();      DBConnection dbConn = null;      try          {              dbConn = TurbineDB.getConnection();              if (dbConn == null) {                  cat.error("Can't get database connection");              }              PreparedStatement pstmt =                  dbConn.prepareStatement(sql_bundle.getString("orderHistory"));              pstmt.setString(1, getEmail());              ResultSet rs = pstmt.executeQuery();              while (rs.next()) {                  orders.add(Order.findOrder(rs.getInt("ORDER_ID")));              }              rs.close();              pstmt.close();          }      catch (Exception e)          {              cat.error("Error during getOrderHistory", e);          }      finally          {              try                  {                    TurbineDB.releaseConnection(dbConn);               }           catch (Exception e)               {                   cat.error("Error during release connection", e);               }          }      return orders;     } 

Here's the query needed for this method.

 orderHistory=SELECT ORDER_ID FROM ORDERS WHERE EMAIL_ADDRESS=? 

At this point, something that you put into a SQL query string days ago comes back to bite you. The findCreditCard query string does a SQL join against the CUSTOMER table, even though it doesn't use any fields from it. This means that the order credit cards that you wrote a null into for the customer won't be returned from findCreditCard when the order is loaded back in. Ah, well, it takes just a moment's work to modify the property file:

 creditQuery=SELECT * FROM CREDIT_CARD WHERE CARD_ID=? 

Now you can add the code to the bottom of MyAccount.jsp to display theprevious orders (see Listing 13.11).

Listing 13.11 Additions to MyAccount.jsp
 <%@ page import="com.bfg.product.Order" %> <%@ page import="java.util.Vector" %> . . . <center><h2>Order History</h2></center> <% Vector history = customer.getOrderHistory(); if (history.size() > 0) { %> <TABLE WIDTH="100%">   <TR><TH ALIGN="LEFT">Order</TH><TH ALIGN="LEFT">Date</TH>     <TH ALIGN="LEFT">Amount</TH></TR> <% Iterator it = history.iterator(); while (it.hasNext()) {     Order or = (Order) it.next(); %>      <TR><TD><A HREF="viewOrder.jsp?order=<%= or.getOrderNumber() %>">           <%= or.getOrderNumber() %> </A></TD>          <TD><%= or.getOrderDate() %></TD>          <TD><%= or.getOrderTotalString() %></TD></TR> <% } %> </TABLE> <% } %> 

There's not much to this piece. It gets a list of orders and displays a summary in table form with a link to viewOrder.jsp. The results are shown in Figure 13.2.

Figure 13.2. The new and improved MyAccount page.

graphics/13fig02.jpg

Speaking of viewOrder.jsp, Listing 13.12 shows what needs to go into that page.

Listing 13.12 ViewOrder.jsp
 <%@ include file="/jsp/cust/AutoLogin.jsp" %> <%@ page import="com.bfg.customer.Customer" %> <%@ page import="com.bfg.customer.Address" %> <%@ page import="com.bfg.customer.CreditCard" %> <%@ page import="com.bfg.product.Order" %> <%@ page import="com.bfg.product.Product" %> <%@ page import="com.bfg.cart.CartItem" %> <%@ page import="java.util.Iterator" %> <%@ page import="java.util.Vector" %> <%@ page import="java.text.NumberFormat" %> <%@ page import="java.text.DecimalFormat" %> <jsp:useBean id="customer" class="com.bfg.customer.Customer" scope="session"/> <% if (customer.getEmail() == null) {     response.sendRedirect("Login.jsp");     return; } Order or = null; try {     NumberFormat nf = new DecimalFormat();     int ordernum = nf.parse(request.getParameter("order")).intValue();     or = Order.findOrder(ordernum); } catch (Exception ex) {     response.sendRedirect("general_error.jsp");     return; } if (!or.getCustomer().getEmail().equals(customer.getEmail())) {     response.sendRedirect("noaccess.jsp");     return; } %> <head> <title>Order <%= or.getOrderNumber() %></title> </head> <%@ include file="/jsp/includes/bfgheader.jsp" %> <h2 align="center">Order <%= or.getOrderNumber() %></h2> <table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse" graphics/ccc.gif bordercolor="#111111" width="100%">   <tr>     <th width="38%" height="23" align="left">Title</th>     <th width="11%" height="23" align="left">Price</th>     <th width="28%" height="23" align="center">Quantity</td>     <th width="23%" height="23" align="left">Total</th>   </tr>     <%     Iterator iter = or.getItems().iterator();     while (iter.hasNext()) {      CartItem item = (CartItem) iter.next();      Product prod = item.getProduct(); %>   <tr>     <td width="38%" bgcolor="#00FFFF">     <font style="font-size: 10.0pt; font-family: Times New Roman">         <A HREF="product/Product.jsp?ISBN=<%= prod.getISBN() %>">         <%= prod.getTitle() %></A>     </span><BR><BR></td>     <td width="11%" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">     <%= prod.getPriceString() %>     </span></td>     <td width="28%" bgcolor="#00FFFF">     <p align="center"><font size="2">     <%= item.getQuantity() %>     </font></td>     <td width="23%" bgcolor="#00FFFF">     <span style="font-size: 10.0pt; font-family: Times New Roman">     <%= item.getLineItemPriceString() %>     </span></td>   </tr>        <% } %>   <TR><TD COLSPAN=4><HR></TD></TR>   <TR><TD>&nbsp;</TD>   <TD>&nbsp</TD>   <TD ALIGN="center">   <span style="font-size: 10.0pt; font-family: Times New Roman">       SubTotal   </span></TD>     <TD>   <span style="font-size: 10.0pt; font-family: Times New Roman">    <%= or.getOrderSubtotalString()%>   </span></TD></TR>   <TR><TD>&nbsp;</TD>   <TD>&nbsp</TD>   <TD ALIGN="center">   <span style="font-size: 10.0pt; font-family: Times New Roman">       Shipping   </span></TD>     <TD>   <span style="font-size: 10.0pt; font-family: Times New Roman">    <%= or.getOrderShippingString()%>   </span></TD></TR>   <TR><TD>&nbsp;</TD>   <TD>&nbsp</TD>   <TD ALIGN="center">   <span style="font-size: 10.0pt; font-family: Times New Roman">       Tax   </span></TD>     <TD>   <span style="font-size: 10.0pt; font-family: Times New Roman">    <%= or.getOrderTaxString()%>   </span></TD></TR>   <TR><TD>&nbsp;</TD>   <TD>&nbsp</TD>   <TD ALIGN="center">   <span style="font-size: 10.0pt; font-family: Times New Roman">       Total   </span></TD>     <TD>   <span style="font-size: 10.0pt; font-family: Times New Roman">    <%= or.getOrderTotalString()%>   </span></TD></TR> </TABLE> <P> <B>Shipped to:</B><BR> <%= or.getAddress().getFirstName() %> <%= or.getAddress().getLastName() %><BR> <%= or.getAddress().getStreet1() %><BR> <%= or.getAddress().getStreet2() %><BR> <%= or.getAddress().getCity() %>, <%= or.getAddress().getState() %> <%= or.getAddress().getPostalCode() %><BR> <%@ include file="/jsp/includes/bfgfooter.jsp" %> 

When you are sure that they have logged in, the code needs to try to parse the order number and look it up. Did I mention how much I miss atoi in Java? Again, be really, really careful not to let the page display an order unless it belongs to the logged-in user .

From this point down, the code is essentially identical to any other place where the contents of the cart are displayed. In fact, with a few edits, it was basically cut and pasted from the shoppingcart.jsp code (in Listing 10.8). This demonstrates a benefit of using the CartItem and Product classes commonly for both the cart and the order; it means that you can use a lot of the same methods on both.

Now, when you click on an order from the MyAccount page, you get the order detail page shown in Figure 13.3.

Figure 13.3. The order detail page.

graphics/13fig03.jpg

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