Section 6.19. Iteration 3Buy a Car


6.19. Iteration 3Buy a Car

In this Iteration, we're going to enable a user to buy a car from the JAW Motors web site. We'll add a new page to enable the user to buy a car. When the user presses the "Submit" button on the new "Buy Car" page, the business logic changes the car's status to "Sold" and inserts a new row into the ACCOUNTING table.

We'll take the following steps:

  • Upgrade the web site:

    • Add a "Buy Car" link to the Car Inventory page (carList.jsp).

    • Add a "Buy Car" action to the Controller Servlet.

  • Modify the Persistence Tier:

    • Create an ACCOUNTING table.

    • Write a HibernateAccountingDTO.

    • Develop a HibernateAccountingDAO.

  • Change the Session Bean:

    • Add a new buyCar( ) method to the InventoryFacadeBean that encapsulates a Container-Managed Transaction that involves both the CAR and ACCOUNTING tables.

6.19.1. Upgrade the Web Site: Adding a "Buy Car" Link

We've added a "Buy Car" link to the JAW Motors Car Inventory page (carList.jsp) as shown in Figure 6-2.

Figure 6-2. JAW Motors Inventory Page


When the user clicks on the "Buy" link, the Controller routes them to the Buy Car page as depicted in Figure 6-3 so they can buy the car.

The user enters her price in the form and presses the "Save" button. The Controller Servlet then takes the user back to the Car Inventory page. The purchased car is no longer available, so it won't show up on the Car Inventory page.

Figure 6-3. JAW Motors Buy Car Page


Now that the web pages are done, we have to add actions to the Controller Servlet for buying a car. Example 6-27 shows the changes.

Example 6-27. ControllerServlet.java
 public class ControllerServlet extends HttpServlet {     ...     private static final String VIEW_BUY_CAR_FORM_ACTION = "viewBuyCarForm";     private static final String BUY_CAR_ACTION = "buyCar";     ...     protected void processRequest(HttpServletRequest request,                                   HttpServletResponse response)     throws ServletException, IOException {         ...         else if (VIEW_BUY_CAR_FORM_ACTION.equals(actionName))         {             int id = Integer.parseInt(request.getParameter("id"));             request.setAttribute("car", inventory.findCar(id));             destinationPage = "/buyCarForm.jsp";         }         else if (BUY_CAR_ACTION.equals(actionName))         {             int carId = Integer.parseInt(request.getParameter("id"));             double price;             // Use $5000.00 as the default price if the user enters bad data.             try             {                 price = Double.parseDouble(request.getParameter("price"));             }             catch (NumberFormatException nfe)             {                 price = 5000.00;             }             System.out.println("carId = [" + carId + "], price = [" + price + "]");             // mark the car as sold             inventory.buyCar(carId, price);             // prepare the list             request.setAttribute("carList", inventory.listAvailableCars(  ));             destinationPage = "/carList.jsp";         }         ...     }     ... } 

As shown in the web pages, pressing the "Buy" link on the Car Inventory page invokes the viewBuyCarForm action, and the Controller Servlet routes the user to the "Buy Car" Form. Pressing the "Submit" button from the form invokes the buyCar action, and the Controller Servlet captures the user's price and calls the InventoryFacadeBean's buyCar( ) method to change the car's status to "Sold" and insert a new row into the ACCOUNTING table to record the sale. The Controller then gets the current list of available (unsold) cars and routes the user to the Car Inventory page.

Now that we've upgraded the web site, let's start modifying the Persistence Tier by adding the new ACCOUNTING table.

6.19.2. Creating the ACCOUNTING Table

The new ACCOUNTING table keeps track of the cars we've sold. Example 6-28 is the SQL in ch06-c/sql/build.xml that creates the table:

Example 6-28. sql/build.xml
 DROP TABLE IF EXISTS ACCOUNTING; CREATE TABLE ACCOUNTING (     ID BIGINT identity,     CAR_ID BIGINT,     PRICE DOUBLE,     SALE_DATE DATE ); 

Due to the CAR_ID column, the ACCOUNTING table depends on the CAR table, but we're not going to complicate things by adding foreign key constraints.

We've created the ACCOUNTING table, so let's add the corresponding AccountingDTO.



JBoss at Work. A Practical Guide
JBoss at Work: A Practical Guide
ISBN: 0596007345
EAN: 2147483647
Year: 2004
Pages: 197

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