type=page
status=published
title=The Duke's Bookstore Interface
next=dukes-bookstore003.html
prev=dukes-bookstore001.html
~~~~~~
The Duke's Bookstore Interface
==============================

[[GLQFD]]

[[the-dukes-bookstore-interface]]
The Duke's Bookstore Interface
------------------------------

This section provides additional detail regarding the components of the
Duke's Bookstore example and how they interact.

The following topics are addressed here:

* link:#GLQER[The Book Java Persistence API Entity]
* link:#GLQEU[Enterprise Beans Used in Duke's Bookstore]
* link:#GLQDP[Facelets Pages and Managed Beans Used in Duke's Bookstore]
* link:#GLQDX[Custom Components and Other Custom Objects Used in Duke's
Bookstore]
* link:#GLQDG[Properties Files Used in Duke's Bookstore]
* link:#GLQED[Deployment Descriptors Used in Duke's Bookstore]

[[GLQER]]

[[the-book-java-persistence-api-entity]]
The Book Java Persistence API Entity
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The `Book` entity, located in the `dukesbookstore.entity` package,
encapsulates the book data stored by Duke's Bookstore.

The `Book` entity defines attributes used in the example:

* A book ID
* The author's first name
* The author's surname
* The title
* The price
* Whether the book is on sale
* The publication year
* A description of the book
* The number of copies in the inventory

The `Book` entity also defines a simple named query, `findBooks`.

[[GLQEU]]

[[enterprise-beans-used-in-dukes-bookstore]]
Enterprise Beans Used in Duke's Bookstore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Two enterprise beans located in the `dukesbookstore.ejb` package provide
the business logic for Duke's Bookstore.

* `BookRequestBean` is a stateful session bean that contains the
business methods for the application. The methods create, retrieve, and
purchase books, and update the inventory for a book. To retrieve the
books, the `getBooks` method calls the `findBooks` named query defined
in the `Book` entity.
* `ConfigBean` is a singleton session bean used to create the books in
the catalog when the application is initially deployed. It calls the
`createBook` method defined in `BookRequestBean`.

[[GLQDP]]

[[facelets-pages-and-managed-beans-used-in-dukes-bookstore]]
Facelets Pages and Managed Beans Used in Duke's Bookstore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The Duke's Bookstore application uses Facelets and its templating
features to display the user interface. The Facelets pages interact with
a set of CDI managed beans that act as backing beans, providing the
underlying properties and methods for the user interface. The front page
also interacts with the custom components used by the application.

The application uses the following Facelets pages, which are located in
the `_tut-install_/examples/case-studies/dukes-bookstore/src/main/webapp/`
directory.

* `bookstoreTemplate.xhtml`: The template file, which specifies a header
used on every page as well as the style sheet used by all the pages. The
template also retrieves the language set in the web browser.
+
Uses the `LocaleBean` managed bean.
* `index.xhtml`: Landing page, which lays out the custom map and area
components using managed beans configured in the `faces-config.xml` file
and allows the user to select a book and advance to the
`bookstore.xhtml` page.
* `bookstore.xhtml`: Page that allows the user to obtain details on the
selected book or the featured book, to add either book to the shopping
cart, and to advance to the `bookcatalog.xhtml` page.
+
Uses the `BookstoreBean` managed bean.
* `bookdetails.xhtml`: Page that shows details on a book selected from
`bookstore.xhtml` or other pages and allows the user to add the book to
the cart and/or advance to the `bookcatalog.xhtml` page.
+
Uses the `BookDetailsBean` managed bean.
* `bookcatalog.xhtml`: Page that displays the books in the catalog and
allows the user to add books to the shopping cart, view the details for
any book, view the shopping cart, empty the shopping cart, or purchase
the books in the shopping cart.
+
Uses the `BookstoreBean`, `CatalogBean`, and `ShoppingCart` managed
beans.
* `bookshowcart.xhtml`: Page that displays the contents of the shopping
cart and allows the user to remove items, view the details for an item,
empty the shopping cart, purchase the books in the shopping cart, or
return to the catalog.
+
Uses the `ShowCartBean` and `ShoppingCart` managed beans.
* `bookcashier.xhtml`: Page that allows the user to purchase books,
specify a shipping option, subscribe to newsletters, or join the Duke
Fan Club with a purchase over a certain amount.
+
Uses the `CashierBean` and `ShoppingCart` managed beans.
* `bookreceipt.xhtml`: Page that confirms the user's purchase and allows
the user to return to the catalog page to continue shopping.
+
Uses the `CashierBean` managed bean.
* `bookordererror.xhtml`: Page rendered by `CashierBean` if the
bookstore has no more copies of a book that was ordered.

The application uses the following managed beans, which are located in
the
`_tut-install_/examples/case-studies/dukes-bookstore/src/main/java/javaeetutorial/dukesbookstore/web/managedbeans/`
directory.

* `AbstractBean`: Contains utility methods called by other managed
beans.
* `BookDetailsBean`: Backing bean for the `bookdetails.xhtml` page.
Specifies the name `details`.
* `BookstoreBean`: Backing bean for the `bookstore.xhtml` page.
Specifies the name `store`.
* `CashierBean`: Backing bean for the `bookcashier.xhtml` and
`bookreceipt.xhtml` pages.
* `CatalogBean`: Backing bean for the `bookcatalog.xhtml` page.
Specifies the name `catalog`.
* `LocaleBean`: Managed bean that retrieves the current locale; used on
each page.
* `ShoppingCart`: Backing bean used by the `bookcashier.xhtml`,
`bookcatalog.xhtml`, and `bookshowcart.xhtml` pages. Specifies the name
`cart`.
* `ShoppingCartItem`: Contains methods called by `ShoppingCart`,
`CatalogBean`, and `ShowCartBean`.
* `ShowCartBean`: Backing bean for the `bookshowcart.xhtml` page.
Specifies the name `showcart`.

[[GLQDX]]

[[custom-components-and-other-custom-objects-used-in-dukes-bookstore]]
Custom Components and Other Custom Objects Used in Duke's Bookstore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The map and area custom components for Duke's Bookstore, along with
associated renderer, listener, and model classes, are defined in the
following packages in the
`_tut-install_/examples/case-studies/dukes-bookstore/src/main/java/javaeetutorial/dukesbookstore/`
directory.

* `components`: Contains the `MapComponent` and `AreaComponent` classes.
See link:jsf-custom005.html#BNAVU[Creating Custom Component Classes].
* `listeners`: Contains the `AreaSelectedEvent` class, along with other
listener classes. See link:jsf-custom008.html#BNAWD[Handling Events for
Custom Components].
* `model`: Contains the `ImageArea` class. See
link:jsf-custom003.html#GLPBO[Configuring Model Data] for more
information.
* `renderers`: Contains the `MapRenderer` and `AreaRenderer` classes.
See link:jsf-custom006.html#BNAWA[Delegating Rendering to a Renderer].

The
`_tut-install_/examples/case-studies/dukes-bookstore/src/java/dukesbookstore/`
directory also contains a custom converter and other custom listeners
not specifically tied to the custom components.

* `converters`: Contains the `CreditCardConverter` class. See
link:jsf-custom011.html#BNAUS[Creating and Using a Custom Converter].
* `listeners`: Contains the `LinkBookChangeListener`,
`MapBookChangeListener`, and `NameChanged` classes. See
link:jsf-custom007.html#BNAUT[Implementing an Event Listener].

[[GLQDG]]

[[properties-files-used-in-dukes-bookstore]]
Properties Files Used in Duke's Bookstore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The strings used in the Duke's Bookstore application are encapsulated
into resource bundles to allow the display of localized strings in
multiple locales. The properties files, located in the
`_tut-install_/examples/case-studies/dukes-bookstore/src/main/java/javaeetutorial/dukesbookstore/web/messages/`
directory, consist of a default file containing English strings and
three additional files for other locales. The files are as follows:

* `Messages.properties`: Default file, containing English strings
* `Messages_de.properties`: File containing German strings
* `Messages_es.properties`: File containing Spanish strings
* `Messages_fr.properties`: File containing French strings

The language setting in the user's web browser determines which locale
is used. The `html` tag in `bookstoreTemplate.xhtml` retrieves the
language setting from the `language` property of `LocaleBean`:

[source,oac_no_warn]
----
<html lang="#{localeBean.language}"
...
----

For more information about resource bundles, see
link:webi18n.html#BNAXU[Chapter 22, "Internationalizing and Localizing
Web Applications."]

The resource bundle is configured as follows in the `faces-config.xml`
file:

[source,oac_no_warn]
----
<application>
    <resource-bundle>
        <base-name>
            javaeetutorial.dukesbookstore.web.messages.Messages
        </base-name>
        <var>bundle</var>
    </resource-bundle>
    <locale-config>
        <default-locale>en</default-locale>
        <supported-locale>de</supported-locale>
        <supported-locale>es</supported-locale>
        <supported-locale>fr</supported-locale>
    </locale-config>
</application>
----

This configuration means that in the Facelets pages, messages are
retrieved using the prefix `bundle` with the key found in the
`Messages_`locale`.properties` file, as in the following example from
the `index.xhtml` page:

[source,oac_no_warn]
----
<h:outputText style="font-weight:bold"
              value="#{bundle.ChooseBook}" />
----

In `Messages.properties`, the key string is defined as follows:

[source,oac_no_warn]
----
ChooseBook=Choose a Book from our Catalog
----

[[GLQED]]

[[deployment-descriptors-used-in-dukes-bookstore]]
Deployment Descriptors Used in Duke's Bookstore
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The following deployment descriptors are used in Duke's Bookstore:

* `src/main/resources/META-INF/persistence.xml`: The Java Persistence
API configuration file
* `src/main/webapp/WEB-INF/bookstore.taglib.xml`: The tag library
descriptor file for the custom components
* `src/main/webapp/WEB-INF/faces-config.xml`: The JavaServer Faces
configuration file, which configures the managed beans for the map
component as well as the resource bundles for the application
* `src/main/webapp/WEB-INF/web.xml`: The web application configuration
file
