Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Lab Exercises Online Bookstore Application

Carol McDonald

Sample Application
Review process of developing application from specification to design to implementation. 1. Scenarios or Use Cases 2. Partitioning of functionality into modules 3. Assignment of functionality to tiers 4. Model View Controller Design Pattern 5. Business Logic Classes

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Use Case Scenarios

C reate/Update Custom e r Account

Browse Catalog

Customer Login to Customer Account add items to shopping cart <<extend>>

<<includes>> check out order items Add Order to Warehouse System (external)

<<includes>>

Send Receipt

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Divide the Application Into Modules Based on Functionality


Account Catalog Shopping Cart Order Inventory Mailer

: Customer

login

getProducts

addItem

checkOut

createOrder

updateInventory

sendOrderConfirmation

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Divide the Application Into Modules Based on Functionality


Account

Customer Account Module: tracks Customer Account information Product Catalog Module: provides search for products, and product details Shopping Cart Module: allows user to save selected items for the session. Order Processing Module: performs order Processing when user buys the items in the cart. 1999 Sun Microsystems, Inc., All rights reserved. Copyright

Catalog

Cart

Order

Divide the Application Into Modules Based on Functionality


Inventory

Inventory Module: maintains information on the number of each type of product in stock. Messaging Module: sends confirmation receipt messages.

Mail

Control

Control Module: control interactions between user (browse, add items, check out) and business objects. (Servlets)
Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Interrelationship of the modules


User interface
Control

browse

Catalog

new/existing account add/remove items checkout


Account

Inventory

Cart

Mail

Order

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Divide Application into Objects


Divide Application Objects into 3 categories:

View, Boundary, or Presentation classes: present the interface to the user Control classes: accept user requests and control the business objects to fulfill the request. Model classes: represent the application business data and rules.
Entity, Domain classes: usually abstractions of real world entities. Long lived, nouns. Session classes: business process, rules, workflow.
Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Model View Controller Design Pattern


Entity EJB and Session EJB

Model
State query Notify Change Event View selection

State change

View

Controller
Servlets, or Controller classes Session EJB

User event JSP JavaBeans components

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The model is the heart of an application. It represents the application data and the business rules that govern access and modification of this data. The model serves as a software approximation to a real world process. Thus we can use simple real world modeling techniques when defining the model. The model abstraction provides: The ability for the view to query the model about its state. Notification to the view when the model changes. The ability for the controller to access application functionality encapsulated by the model. A view renders the contents of a model. It accesses data from the model and specifies how that data should be presented. When the model changes, it notifies the view. It is the views responsibility to maintain consistency in its presentation after a model change. When the view is rendered in a Web browser, it is generated as part of a response to a user request. In this scenario the treatment of model changes applies to parts of the model that might be cached in the Web server tier. 9

MVC
Presentation Logic Layout Application Logic Business Data Access
data model
Entity Bean Manages data

control
Servlet Receives request Validates input Calls session bean Call JSP

Process /service
Session Bean Validates request Executes process Enforces transactions

HTML layout
JSP

model
Entity Bean

Formats HTML Manages data Responds to client Copyright 1999 Sun Microsystems, Inc., All rights reserved.

10

Model: Business Objects: Business Data


Often called domain classes since they deal with abstractions of real-world entities. Often Nouns. Entity Beans: Object view of business entity stored in persistent storage. Provides object wrapper for accessing and manipulating the data. Persistent , long lived.

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

There is state associated both with the application user interface and with the application or business logic. In general, an application must maintain the follow-ing state: The user identity - Typically, the user account module maintains the user iden-tify, which includes the users login ID and certain security credentials. The search cursor and catalog position - The catalog module maintains the cur-sors position within the current search and within the catalog hierarchy. The items in the shopping cart - The shopping cart module maintains the list of items placed in the shopping cart. Order information - When the user commits the order, the shopping cart passes this information the order informationbilling address, shipping address, and payment methodto the order management module, which eventually stores it to a database.

11

Online BookStore Database Schema


1 Customer email password name address 0-n Orders orderid email date ShipAddress Total price
1

0..n

Product ISBN title author description

OrderLineItem orderId linenumber ISBN quantity Unit price

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The application maintains accounts and tracks orders for products. Thus, there are three areas for which data must be maintained: product, account, and order information. The product, category, and item tables represent the businesss product catalog. Each item has an associated entry in the inventory table that represents the inventory for that product. The account table maintains account information, one record per customer, with information such as customer name, password, and customer address. Finally, there is an orders table with one record per order, which keeps information about the order, including ship-to address, bill-to address, total price of the order, and payment (credit card name, expiration date, type) information. The orders table is linked to lineitem and orderstatus tables. Each item in an order is stored in a separate lineitem record, which contains the quantity ordered and price and a separate orderstatus record, which contains a reference to the item and the status of the order.

12

UML for Entity Objects

Customer
email

0..n

Order
orderId email

0..n

Product
ISBN

0..n

OrderLineItem
orderId lineNumber ISBN

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

13

Customer Entity EJB


C u s t o m e rE J B
e m a il a d d res s nam e p a s s w o rd c r e d i t C a rd N u m b e r s e t C u s to m e r D e ta i l s ( ) g e t C u s to m e r D e ta i l s ( ) lo g in ( ) e e e e s u e e e e j b C r e a te ( ) j b P o s tC r e a t e ( ) jb F in d B yN a m e () jb R e m o ve ( ) e tE n tityC o n te xt() n s e tE n tityC o n te x t ( ) jb A c tiva te ( ) jb P a s s iva te ( ) jb L o a d () j b S to r e ( )

Customer email password name address

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

14

Order Entity EJB


orders orderid emailId date ShipAddress Total price
OrderEJB
orde rId orderLineItem s email s ta tu s to talPrice orde rDate getOrderDeta i l s () ejbCreate() ejb R e m ove() s e tEntityContext() ejb L o a d () ejb S tore() ejbFind ByPrim a ryK e y() ejb Activate() ejb P a s s ivate()

lineitem orderId linenumber itemId quantity Unit price

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

15

Value Objects
CustomerEJB
email address name password creditCardNumber setCustomerDetails() getCustomerDetails() login() ejbCreate() ejbPostCreate() ejbFindByName() ejbRemove() setEntityContext() unsetEntityContext() ejbActivate() ejbPassivate() ejbLoad() ejbStore()

CustomerDetails
email address name password cred itCardNum ber getEmail() g e tA d d r e s s ( ) getName() getPassword() g e tC reditCardNumber()

Value objects are used to encapsulate a serializable read only version of an entire remote object. This allows to retrieve the value of all the details of a remote object This App has details objects named XXXDetails (where XXX takes the values Book, Customer, and Order) for each enterprise bean.
Copyright 1999 Sun Microsystems, Inc., All rights reserved.

Value Objects A value object is a business object that can be passed by value. The pass-byvalue semantics can be achieved by implementing the object as a serializable Java object. A business concept should be implemented as a value object when it is fine-grained, dependent, and immutable. There are two types of value objects: dependent objects and details objects. An object is a dependent object of another object if its life cycle is completely managed by that object and if it can only be accessed indirectly through that object. Dependent objects have no set methods. Therefore, dependent objects can only be modified by creating a new containing object. Examples of dependent objects in the sample application are Address and CreditCard. A value object can also be used to encapsulate a serializable version of an entire remote object. Such objects are used to allow a client to retrieve the value of all the details of a remote object in one call. The sample application contains a details object named XXXModel (where XXX takes the values Catalog, Inventory, Account, Cart, and Order) for each enterprise bean.

16

Model: Business Objects: Business Rules, Process, Workflow


Stateless Session Beans: Reusable service objects May provide high performance Need to operate on multiple rows at a time Provides procedural view of data

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

17

Stateless Services: Catalog Session EJB


product ISBN title author description
CatalogEJB
getBooks() getBookDetails() findBooksBySubject() ejbCreate() s e tSessionContext() e j b R e m o ve() ejbActivate() ejbPassivate()

A Catalog object represents different products and provides browsing and searching services to its clients. Both of the primary functions of the catalog, browsing and searching, are generic services which are not tied to any particular client. Also, the catalog object reads multiple rows in the database at the same time and provides a shared view of the data.
Copyright 1999 Sun Microsystems, Inc., All rights reserved.

A Catalog object represents different categories and products and provides browsing and searching services to its clients. Both of the primary functions of the catalog, browsing and searching, are generic services which are not tied to any particular client. Also, the catalog object operates on multiple rows in the database at the same time and provides a shared view of the data. The sample application uses stateless session beans for objects containing more than one database row. In particular, because stateless session beans provide high performance, stateless session beans are a good choice to provide a high-performance cache of data for operations that access multiple rows. In the sample application, the Catalog stateless session bean functions as a cache that is built up over time.

18

Model: Business Objects: Business Rules, Process, Workflow


Stateful Session Beans
Maintain client specific state Non-persistent Workflow, process management

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

19

Stateful Session EJB


ShoppingCartEJB
cart numberOfItems addItem () deleteItem () clear() getItem s () getItem () getNumberOfItems() getTotal() ejbCreate() s e tSessionContext() ejbRemove() ejbActivate() ejbPassivate()

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

20

Controller Objects
Responsible for coordinating the Model and View. 1. Receives user requests, and translate them into application business events. 2. Invokes methods on the model to cause desired state changes. 3. Selects the screen shown in response to the request.
request
Customer

controller
display

event

model

view
Copyright 1999 Sun Microsystems, Inc., All rights reserved.

he Controller The sample application must reflect the state of a users interaction with the applica-tion and the current values of persistent data in the user interface. According to the MVC pattern, this functionality is assumed by the controller. In the sample applica-tion, the controller is split between the Web tier and the EJB tier. In this section we will discuss the implementation of the controller for the shopping interaction in the sample application. The controller is responsible for coordinating the model and view. The view depends on the controller for view selection. The model depends on the controller for making state changes to the model. The controller must accept user gestures from the view, translate them into a business events based on the behavior of the application, and process these events. The processing of an event involves invok-ing methods of the model to cause the desired state changes. Finally, the controller selects the screen shown in response to the request that was processed. Since the controller must coordinate both the view and the data, it straddles 21

Example Solution: EJBs


Clients Web Server Application Server
EJB Container Catalog Servlets Servlets Shopping Cart Customer Account

DBMS

Order Entry

EJB Server

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

When using a distributed component model platform for providing a software solution, the goal of the application designer is to identify various components comprising a system. Our recommendation is to partition the system based on functionality, isolate the functional units of work, and identify the components that make up the function. For example, in this application we divided the application into following functional units: Customer Account Management, Order Management, Product Inventory Management, Catalog Management. Each functional unit can then be broken down in to various sub-components. For example, Customer Account management has a Web component to display and gather account information from the client, a business logic component to process the account information in accordance with some business rules, and a database component to store the persistent account information. The different components of an application need to follow certain design rules when communicating with each other.

22

Example Scenario continued


Servlets
add to cart register or login show products

Shopping cart
purchase

catalog customer Order

Copyright 1999 Sun Microsystems, Inc., All rights reserved.

23

Example Scenario continued

Servlet Login/Register
H T T P (S)

Enterprise JavaBean Customer

J D B C

Browse

Catalog

Purchase

Shopping Cart

Order

Enterprise Application Server


Server Configuration

JNDI Directory Server


Copyright 1999 Sun Microsystems, Inc., All rights reserved.

The Client interface is provided by servlets which generate HTML pages displayed in the browser. The servlet calls methods in the appropriate enterprise beans. The session bean ShoppingCart also acts as the client for the entity bean Order. The entity beans are stored in database tables - Order, Customer, (Product) .

24

You might also like