ch7.catalog
Class ControllerHelper

java.lang.Object
  extended by shared.HelperBaseCh4
      extended by shared.HelperBaseCh5
          extended by shared.HelperBaseCh6
              extended by ch7.catalog.ControllerHelper

public class ControllerHelper
extends HelperBaseCh6

Uses two beans: one for the shopping cart and one for the current item

The cart will be placed in the session. The current item displayed on the page will be stored in a bean in the session, too.

This is the first application that has two beans. It will be necessary to add accessors for each of them, so the JSPs can retrieve them. It will also be necessary to copy them from the old session data.


Nested Class Summary
 
Nested classes/interfaces inherited from class shared.HelperBaseCh4
HelperBaseCh4.SessionData
 
Field Summary
 
Fields inherited from class shared.HelperBaseCh6
checked, selected
 
Fields inherited from class shared.HelperBaseCh4
logger, request, response
 
Constructor Summary
ControllerHelper(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
          Call the base class constructor to store the request and response.
 
Method Summary
 void copyFromSession(java.lang.Object sessionHelper)
          The implementation of the abstract method from the helper base.
protected  void doGet()
          GET is considered a new request, so the old data is not read and the JSP is always for the edit page.
protected  void doPost()
          Handles the HTTP POST method.
 java.lang.Object getCart()
          Accessor for the shopping cart.
 java.lang.Object getItem()
          Accessor for the current item in the page.
static void initHibernate(boolean create)
          The properties are in the hibernate.cfg.xml file.
 java.lang.String jspLocation(java.lang.String page)
          Encapsulates the location of the JSPs.
 java.lang.String methodAddCart()
          The method to call when the addCart button is clicked.
 java.lang.String methodDefault()
          The default method, it is not associated with any button.
 java.lang.String methodEmptyCart()
          The method to call when the emptyCart button is clicked.
 java.lang.String methodProcessCart()
          The method to call when the processCart button is clicked.
 java.lang.String methodViewCart()
          The method to call when the viewCart button is clicked.
 java.lang.String methodViewItem()
          The method to call when the viewItem button is clicked.
 
Methods inherited from class shared.HelperBaseCh6
addChecked, addChoice, addSelected, clearMaps, clearProperty, getChecked, getSelected, invokeGetter, setCheckedAndSelected, setCheckedAndSelected
 
Methods inherited from class shared.HelperBaseCh5
getErrors, isValid, isValidProperty, setErrors
 
Methods inherited from class shared.HelperBaseCh4
addHelperToSession, addHelperToSession, executeButtonMethod, executeButtonMethod, fillBeanFromRequest, invokeButtonMethod, writeError
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ControllerHelper

public ControllerHelper(javax.servlet.http.HttpServletRequest request,
                        javax.servlet.http.HttpServletResponse response)
Call the base class constructor to store the request and response.

Parameters:
request - Request information encapsulated in a Java object
response - Response information encapsulated in a Java object
Method Detail

initHibernate

public static void initHibernate(boolean create)
The properties are in the hibernate.cfg.xml file.

The data has already been created, so there is not need to call createTable.

Parameters:
create - true means create the table, false means do not create

getItem

public java.lang.Object getItem()
Accessor for the current item in the page. It could be null.

The accessor is necessary so that the JSPs can access the item.

Returns:
The current item in the page.

getCart

public java.lang.Object getCart()
Accessor for the shopping cart.

The accessor is necessary so that the JSPs can access the cart.

Returns:
shopping cart

copyFromSession

public void copyFromSession(java.lang.Object sessionHelper)
The implementation of the abstract method from the helper base.

The method copies item and the cart from the old helper that is still in the session to the current helper.

Specified by:
copyFromSession in class HelperBaseCh4
Parameters:
sessionHelper - The old helper that is still in the session.

jspLocation

public java.lang.String jspLocation(java.lang.String page)
Encapsulates the location of the JSPs.

Parameters:
page - The file name of the JSP.
Returns:
The full path to the JSP.

methodDefault

public java.lang.String methodDefault()
The default method, it is not associated with any button.

Place all of the items from the catalog database in a list and place it in the request, so that the JSP can access it.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

methodAddCart

public java.lang.String methodAddCart()
The method to call when the addCart button is clicked.

Use the cart bean to add the current item to the shopping cart. It is critical to reset the current item to a new item; otherwise, the current item will point into the shopping cart.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

methodEmptyCart

public java.lang.String methodEmptyCart()
The method to call when the emptyCart button is clicked.

Use the cart bean to remove all the items from the shopping cart.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

methodViewItem

public java.lang.String methodViewItem()
The method to call when the viewItem button is clicked.

Fill the item bean with the item from the request. If there is an associated item in the catalog database, set it as the current item.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

methodViewCart

public java.lang.String methodViewCart()
The method to call when the viewCart button is clicked.

The method only directs the request to the cart page

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

methodProcessCart

public java.lang.String methodProcessCart()
The method to call when the processCart button is clicked.

Loops through all the items in the cart and calculates the total number of items and the total price.

The method is adorned with a ButtonMethod annotation that associates the name of a button with the method.

Returns:
The address for the JSP.

doGet

protected void doGet()
              throws java.io.IOException,
                     javax.servlet.ServletException
GET is considered a new request, so the old data is not read and the JSP is always for the edit page.
  1. Add the helper to the session but ignore the data from the old helper in the session
  2. Always go to the edit page
  3. Forward the request and response to the next address.

Overrides:
doGet in class HelperBaseCh4
Throws:
javax.servlet.ServletException
java.io.IOException

doPost

protected void doPost()
               throws javax.servlet.ServletException,
                      java.io.IOException
Handles the HTTP POST method. POST is considered a continuation of a previous request, so read the old data from the session and calculate the next page based on the button.
  1. Add the helper to the session and copy the old helper data into the current helper. This eliminates the need for hidden fields.
  2. Call the appropriate method for the clicked button. This is done from a method in the helper base that loops throuh all the button methods, looking for the one for the clicked button. This eliminates the need for the nested if-else block.
  3. Forward the request and response to the next address.

Overrides:
doPost in class HelperBaseCh4
Throws:
javax.servlet.ServletException
java.io.IOException