Coffee Break Server

The Coffee Break server uses servlets, JSP pages, and JavaBeans components to dynamically construct HTML pages for consumption by a web browser client. The JSP pages use the template tag library discussed in A Template Tag Library to achieve a common look and feel among the HTML pages, and many of the JSTL custom tags discussed in Chapter 14.

The Coffee Break server implementation is organized along the Model-View-Controller design pattern. The Dispatcher servlet is the controller. It examines the request URL, creates and initializes model JavaBeans components, and dispatches requests to view JSP pages. The JavaBeans components contain the business logic for the application; they call the web services and perform computations on the data returned from the services. The JSP pages format the data stored in the JavaBeans components. The mapping between JavaBeans components and pages is summarized in Table 35-1.

Table 35-1 Model and View Components 
Function
JSP Page
JavaBeans Component
Update order data
orderForm
ShoppingCart
Update delivery and billing data
checkoutForm
CheckoutFormBean
Display order confirmation
checkoutAck
OrderConfirmations

JSP Pages

orderForm

orderForm displays the current contents of the shopping cart. The first time the page is requested, the quantities of all the coffees are 0 (zero). Each time the customer changes the coffee amounts and clicks the Update button, the request is posted back to orderForm. The Dispatcher servlet updates the values in the shopping cart, which are then redisplayed by orderForm. When the order is complete, the customer proceeds to the checkoutForm page by clicking the Checkout link.

checkoutForm

checkoutForm is used to collect delivery and billing information from the customer. When the Submit button is clicked, the request is posted to the checkoutAck page. However, the request is first handled by the Dispatcher, which invokes the validate method of checkoutFormBean. If the validation does not succeed, the requested page is reset to checkoutForm, with error notifications in each invalid field. If the validation succeeds, checkoutFormBean submits suborders to each supplier and stores the result in the request-scoped OrderConfirmations JavaBeans component, and control is passed to checkoutAck.

checkoutAck

checkoutAck simply displays the contents of the OrderConfirmations JavaBeans component, which is a list of the suborders that constitute an order and the ship dates of each suborder.

JavaBeans Components

RetailPriceList

RetailPriceList is a list of retail price items. A retail price item contains a coffee name, a wholesale price per pound, a retail price per pound, and a supplier. This data is used for two purposes: it contains the price list presented to the end user and is used by CheckoutFormBean when it constructs the suborders dispatched to coffee suppliers.

RetailPriceList first performs a JAXR lookup to determine the JAX-RPC service endpoints. It then queries each JAX-RPC service for a coffee price list. Finally it queries the SAAJ service for a price list. The two price lists are combined and a retail price per pound is determined by adding a markup of 35% to the wholesale prices.

Discovering the JAX-RPC Service

Instantiated by RetailPriceList, JAXRQueryByName connects to the registry and searches for coffee suppliers registered with the name JAXRPCCoffeeSupplier in the executeQuery method. The method returns a collection of organizations that contain services. Each service is accessible via a service binding or URL. RetailPriceList makes a JAX-RPC call to each URL.

ShoppingCart

ShoppingCart is a list of shopping cart items. A ShoppingCartItem contains a retail price item, the number of pounds of that item, and the total price for that item.

OrderConfirmations

OrderConfirmations is a list of order confirmation objects. An OrderConfirmation contains order and confirmation objects, as discussed in Service Interface.

CheckoutFormBean

CheckoutFormBean checks the completeness of information entered into checkoutForm. If the information is incomplete, the bean populates error messages, and Dispatcher redisplays checkoutForm with the error messages. If the information is complete, order requests are constructed from the shopping cart and the information supplied to checkoutForm, and these orders are sent to each supplier. As each confirmation is received, an order confirmation is created and added to OrderConfirmations.

if (allOk) {
  String orderId = CCNumber;

  AddressBean address = 
    new AddressBean(street, city, state, zip);
  CustomerBean customer = 
    new CustomerBean(firstName, lastName,
      "(" + areaCode + ") " + phoneNumber, email);

  for (Iterator d = rpl.getSuppliers().iterator();
      d.hasNext(); ) {
    String supplier = (String)d.next();
    logger.info(supplier);
    ArrayList lis = new ArrayList();
    BigDecimal price = new BigDecimal("0.00");
    BigDecimal total = new BigDecimal("0.00");
    for (Iterator c = cart.getItems().iterator(); 
        c.hasNext(); ) {
      ShoppingCartItem sci = (ShoppingCartItem) c.next();
      if ((sci.getItem().getSupplier()).
          equals(supplier) &&
          sci.getPounds().floatValue() > 0) {
        price = sci.getItem().getWholesalePricePerPound().
          multiply(sci.getPounds());
        total = total.add(price);
        LineItemBean li = new LineItemBean(
          sci.getItem().getCoffeeName(), sci.getPounds(), 
          sci.getItem().getWholesalePricePerPound());
        lis.add(li);
      }
    }

    if (!lis.isEmpty()) {
      OrderBean order = new OrderBean(address, customer, 
        orderId, lis, total);

      String SAAJOrderURL = 
        URLHelper.getSaajURL() + "/orderCoffee";
      if (supplier.equals(SAAJOrderURL)) {
        OrderRequest or = new OrderRequest(SAAJOrderURL);
        confirmation = or.placeOrder(order);
      } else {
        OrderCaller ocaller = new OrderCaller(supplier);
        confirmation = ocaller.placeOrder(order);
      }
      OrderConfirmation oc = 
        new OrderConfirmation(order, confirmation);
      ocs.add(oc);
    }
  }
} 

RetailPriceListServlet

RetailPriceListServlet responds to requests to reload the price list via the URL /loadPriceList. It simply creates a new RetailPriceList and a new ShoppingCart.

Because this servlet would be used by administrators of the Coffee Break server, it is a protected web resource. To load the price list, a user must authenticate (using basic authentication), and the authenticated user must be in the admin role.