 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture.

Slides:



Advertisements
Similar presentations
12 Copyright © 2005, Oracle. All rights reserved. Implementing Business Tasks with Session EJBs.
Advertisements

Technische universität dortmund Service Computing Service Computing Prof. Dr. Ramin Yahyapour IT & Medien Centrum 12. November 2009.
EJB Entity Beans. Entity Beans Data versus logic Used to represent an instance rather than a collection of data (depending on underlying storage) Represents.
Development and Deployment Roles Venugopal Pakanati.
Enterprise JavaBeans 2.0 Wu Lu (Cont’d). EJB2.0 Query Language EJB QL is a declarative language similar in many respects to SQL Is intended to be portable.
Copyright W. Howden1 Lecture 19: Intro to O/O Components.
Writing Enterprise Applications with J2EE (Third lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
ASP.NET Programming with C# and SQL Server First Edition
Chapter 10 EJB Concepts of EJB Three Components in Creating an EJB Starting/Stopping J2EE Server and Deployment Tool Installation and Configuration of.
EJB. Component Characteristics An enterprise Bean typically contains business logic that operates on the enterprise’s data. An enterprise Bean’s instances.
Brad Rippe Fullerton College. What you need to get started? JDK 1.3 standard for compilation J2EE - SDK1.2.1 App Server - An EJB Container/Web Container.
1 Session Bean Chuyên đề Lập trình Java & J2EE Chương 14 Biên soạn: Th.S Nguyễn văn Lành.
J2EE Part 2: Enterprise JavaBeans CSCI 4300 Images and code samples from jGuru EJB tutorial,
Enterprise Java Beans - (EJB)
 2002 Prentice Hall. All rights reserved. Chapter 18 Enterprise Java Case Study: Presentation and Controller Logic Outline 18.1 Introduction 18.2 XMLServlet.
Writing Enterprise Applications with J2EE (Second lesson) Alessio Bechini June 2002 (based on material by Monica Pawlan)
 2002 Prentice Hall. All rights reserved. Chapter 21 Application Servers Outline 21.1 Introduction 21.2 J2EE Specification and Benefits 21.3 Commercial.
Distributed Object-Oriented Programming (3) – Enterprise JavaBeans SNU iDB Lab. Taewhi Lee May 14th, 2007.
Entity Beans BMP Celsina Bignoli
Middleware Technology (J2EE/EJB) Entity Bean. 2 Introduction to Entity Beans Persistence Concepts Entity beans are persistent objects that can be stored.
Lecture 8 Advanced Topics in Enterprise JavaBeans.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Container-Managed Persistence (CMP) Entity Beans Lesson 3A / Slide 1 of 42J2EE Server Components Objectives In this lesson, you will learn to: Identify.
Enterprise JavaBeans Umer Farooq CS6704: Design Patterns & Component FrameworksFebruary 25, 2002.
Distributed Systems 1 Master of Information System Management Distributed Systems Persistence.
The Triad of Beans I Oleh: Dini Addiati ( ) Fahrurrozi Rahman ( Y) Irfan Hilmy ( ) Salman Azis A ( ) Aziiz Surahman.
Presented By Pradeep K Sahu. What will be the Contents of the Seminar ? What is EJB ? EJB Architecture Types of EJB Session Entity Why EJB ? Writing a.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
EJB Framework.  As we know, EJB is the center of the J2EE architecture that provides a sturdy framework for building enterprise applications. The major.
Enterprise Java Bean Computer Networks Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
 2002 Prentice Hall. All rights reserved. Chapter 17: Enterprise Java Case Study: Architectural Overview Outline 17.1 Introduction 17.2 Deitel Bookstore.
Session Beans -) stateless -) stateful. Session Beans A session bean represents a single client inside the J2EE server. To access an application that.
A TUTORIAL TO USING EJBs by SHREERAM IYER 09/17/2001.
Enterprise JavaBeans Understanding EJB Components Version 0.1 Kamal Wickramanayake
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Outline 19.1 Test-Driving the Shipping Hub Application.
© jGuru.com Enterprise JavaBeans Fundamentals.
Enterprise Java v041102EJB QL1 EJB Query Language Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
Creating competitive advantage Copyright © 2003 Enterprise Java Beans Presenter: Wickramanayake HMKSK Version:0.1 Last Updated:
Enterprise Java v041109Container Managed Relationships1 Container Managed Relationships (CMR) Source: “Enterprise JavaBeans, 3rd Edition”, Richard Monson-Haefel.
EJB Overview: Constraint Types and Enforcement in JBoss Elissa Newman Fluid Meeting 6/3/04.
Entity Beans & Persistence Chris Alexander CS 486 Spring 2001.
Enterprise Java Bean Technology Briefing Markus Hebach.
Enterprise JavaBeans Session Beans. Session beans are a type of Enterprise JavaBean component designed to implement business logic responsible for managing.
Session Beans Objectives Introduction to Session Beans Local and Remote Session Beans Stateless and Stateful Session Beans Session Bean Lifecycle Accessing.
Java Programming: Advanced Topics 1 Enterprise JavaBeans Chapter 14.
Enterprise Java Beans N.V.RAJASEKHAR REDDY. Definition of EJB EJBs are the components that are the set of classes and interfaces deployed within a container.
Copyright © 2002 ProsoftTraining. All rights reserved. Enterprise JavaBeans.
Enterprise Java Beans Ye Zhou CS6704 Presentation Virginia Tech.
13 Copyright © 2004, Oracle. All rights reserved. Managing Persistent Data in the Business Tier Entity EJBs.
CSCI 6962: Server-side Design and Programming Shopping Carts and Databases.
D Copyright © 2004, Oracle. All rights reserved. BMP Entity EJBs J2EE Connector Architecture.
Enterprise JavaBeans. Lesson 1: Introduction to Server-Side Component Software.
Middleware Technology (J2EE/EJB) EJB Fundamentals.
Advanced Java Session 7 New York University School of Continuing and Professional Studies.
Enterprise JavaBeans™ Trademark of Sun Microsystems, Inc.
Session Beans & Business Logic CS 486 Sagar Deshpande.
14 Copyright © 2004, Oracle. All rights reserved. Achieving State Management in the Business Tier.
17 Copyright © 2004, Oracle. All rights reserved. Integrating J2EE Components.
JAVA & J2EE UNIT – 8 ENTERPRISE JAVA BEAN - EJB 28-Jun-16 Prepared by: SANTHIYA.M & GANGA V. C. Department: Computer Science & Engineering Course code:
1 EJB 3.0: There’s Something Different About You Martin Jäkle, TSBU Middleware.
©NIIT Session Beans Lesson 1B/ Slide 1 of 37J2EE Server Components Objectives In this lesson, you will learn to: Describe the characteristics of session.
Entity Bean Chuyên đề Lập trình Java & J2EE Chương 15
Chapter 14: Session EJBs and Distributed Transactions
Structure of Enterprise Java Beans
Objectives In this lesson, you will learn to:
Understanding and Designing with EJB
Entity Beans B.Ramamurthy 2/17/2019 BR.
Knowledge Byte In this section, you will learn about:
Understanding and Designing with EJB
Objectives In this lesson, you will learn about:
Presentation transcript:

 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 Outline 19.1 Introduction 19.2 EJB Architecture 19.3 ShoppingCart Implementation ShoppingCart Remote Interface ShoppingCartEJB Implementation ShoppingCartHome Interface 19.4 Product Implementation Product Remote Interface ProductEJB Implementation ProductHome Interface ProductModel 19.5 Order Implementation Order Remote Interface OrderEJB Implementation OrderHome Interface OrderModel

 2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part OrderProduct Implementation OrderProduct Remote Interface OrderProductEJB Implementation OrderProductHome Interface OrderProductPK Primary-Key Class OrderProductModel

 2002 Prentice Hall. All rights reserved Introduction Chapter presents –EJB business logic for shopping-cart e-commerce model –entity EJBs that provide object-based interface to catalog You will understand –use of EJBs in e-commerce application –more advanced EJB topics custom primary-key classes many-to-many relationships

 2002 Prentice Hall. All rights reserved EJB Architecture EJBs implement business logic of case study. Server controller logic –communicates with EJB business logic to process requests retrieve data from database Entity EJBs alleviate network congestion using models to transmit EJB data. –models are Serializable classes containing all data for given EJB. each entity bean provides get method that returns model

 2002 Prentice Hall. All rights reserved EJB Architecture (cont.) Fig. 19.1Communication between GetProductServlet and Product EJB.

 2002 Prentice Hall. All rights reserved ShoppingCart Implementation Stateful session EJB ShoppingCart –used by customers to gather products as they browse store –implements business logic for managing each shopping cart –consists of remote interface EJB implementation home interface –is stateful so it will persist throughout user’s session

 2002 Prentice Hall. All rights reserved ShoppingCart Remote Interface Remote interface ShoppingCart –defines business logic methods available Each method must declare exceptions thrown.

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCart remote interface for adding, removing and updating Product s, checking out and calculating Order ’s total cost. Line 20 Lines Lines Lines // ShoppingCart.java 2 // ShoppingCart is the remote interface for stateful session 3 // EJB ShoppingCart. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.rmi.RemoteException; 8 import java.util.ArrayList; 9 10 // Java extension packages 11 import javax.ejb.EJBObject; // Deitel packages 14 import com.deitel.advjhtp1.bookstore.model.*; 15 import com.deitel.advjhtp1.bookstore.exceptions.*; public interface ShoppingCart extends EJBObject { // get contents of ShoppingCart 20 public Collection getContents() throws RemoteException; // add Product with given ISBN to ShoppingCart 23 public void addProduct( String isbn ) 24 throws RemoteException, ProductNotFoundException; // remove Product with given ISBN from ShoppingCart 27 public void removeProduct( String isbn ) 28 throws RemoteException, ProductNotFoundException; // change quantity of Product in ShoppingCart with 31 // given ISBN to given quantity 32 public void setProductQuantity( String isbn, int quantity ) 33 throws RemoteException, ProductNotFoundException, 34 IllegalArgumentException; 35 returns an Collection of Product s in ShoppingCart takes ISBN of product to add to ShoopingCart removes Product with given ISBN from ShoppingCart updates quantity of Product with given ISNB in ShoppingCart

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCart remote interface for adding, removing and updating Product s, checking out and calculating Order ’s total cost. Lines Line // checkout ShoppingCart (i.e., create new Order) 37 public Order checkout( String userID ) 38 throws RemoteException, ProductNotFoundException; // get total cost for Products in ShoppingCart 41 public double getTotal() throws RemoteException; 42 } places Order for Product s in ShoppingCart returns total cost of Product s in ShoppingCart

 2002 Prentice Hall. All rights reserved ShoppingCartEJB Implementation ShoppingCartEJB –ShoppingCart remote interface implementation –contains Collection of OrderProductModel s. represents an item in ShoppingCart –each contains Product and quantity in ShoppingCart

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 24 Lines Lines // ShoppingCartEJB.java 2 // Stateful session EJB ShoppingCart represents a Customer's 3 // shopping cart. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.util.*; 8 import java.rmi.RemoteException; 9 import java.text.DateFormat; // Java extension packages 12 import javax.ejb.*; 13 import javax.naming.*; 14 import javax.rmi.PortableRemoteObject; // Deitel packages 17 import com.deitel.advjhtp1.bookstore.model.*; 18 import com.deitel.advjhtp1.bookstore.exceptions.*; public class ShoppingCartEJB implements SessionBean { 21 private SessionContext sessionContext; // OrderProductModels (Products & quantities) in ShoppingCart 24 private Collection orderProductModels; // create new ShoppingCart 27 public void ejbCreate() 28 { 29 orderProductModels = new ArrayList(); 30 } // get contents of ShoppingCart 33 public Collection getContents() 34 { 35 return orderProductModels; contain OrderProductModel sinitializes Collection returns contents of ShoppingCart

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines Lines } // add Product with given ISBN to ShoppingCart 39 public void addProduct( String isbn ) 40 throws ProductNotFoundException, EJBException 41 { 42 // check if Product with given ISBN is already 43 // in ShoppingCart 44 Iterator iterator = orderProductModels.iterator(); while ( iterator.hasNext() ) { 47 OrderProductModel orderProductModel = 48 ( OrderProductModel ) iterator.next(); ProductModel productModel = 51 orderProductModel.getProductModel(); // if Product is in ShoppingCart, increment quantity 54 if ( productModel.getISBN().equals( isbn ) ) { orderProductModel.setQuantity( 57 orderProductModel.getQuantity() + 1 ); return; 60 } } // end while // if Product is not in ShoppingCart, find Product with 65 // given ISBN and add OrderProductModel to ShoppingCart 66 try { 67 InitialContext context = new InitialContext(); Object object = context.lookup( 70 "java:comp/env/ejb/Product" ); if isbn found, increase corresponding quantity number determine if product already in ShoppingCart

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 77 Lines Line 87 Line 88 Line 91 Lines ProductHome productHome = ( ProductHome ) 73 PortableRemoteObject.narrow( object, 74 ProductHome.class ); // find Product with given ISBN 77 Product product = productHome.findByPrimaryKey( isbn ); // get ProductModel 80 ProductModel productModel = product.getProductModel(); // create OrderProductModel for ProductModel and set 83 // its quantity 84 OrderProductModel orderProductModel = 85 new OrderProductModel(); orderProductModel.setProductModel( productModel ); 88 orderProductModel.setQuantity( 1 ); // add OrderProductModel to ShoppingCart 91 orderProductModels.add( orderProductModel ); } // end try // handle exception when finding Product record 96 catch ( FinderException finderException ) { 97 finderException.printStackTrace(); throw new ProductNotFoundException( "The Product " "with ISBN " + isbn + " was not found." ); 101 } // handle exception when invoking Product EJB methods 104 catch ( Exception exception ) { 105 throw new EJBException( exception ); locate Product EJB with given ISBN create OrderProductModel to store Product in ShoppingCart add ProductModel to OrderProductModel set quantity to 1 add OrderProductModel to Collection if findByPrimaryKey of interface ProductHome does not find Product with given primary key

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines Lines Lines } } // end method addProduct // remove Product with given ISBN from ShoppingCart 111 public void removeProduct( String isbn ) 112 throws ProductNotFoundException 113 { 114 Iterator iterator = orderProductModels.iterator(); while ( iterator.hasNext() ) { // get next OrderProduct in ShoppingCart 119 OrderProductModel orderProductModel = 120 ( OrderProductModel ) iterator.next(); ProductModel productModel = 123 orderProductModel.getProductModel(); // remove Product with given ISBN from ShoppingCart 126 if ( productModel.getISBN().equals( isbn ) ) { 127 orderProductModels.remove( orderProductModel ); return; 130 } } // end while // throw exception if Product not found in ShoppingCart 135 throw new ProductNotFoundException( "The Product " "with ISBN " + isbn + " was not found in your " "ShoppingCart." ); } // end method removeProduct 140 iterate through OrderProductModel s if ProductModel found, remove thrown when product not found

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines Lines // set quantity of Product in ShoppingCart 142 public void setProductQuantity( String isbn, 143 int productQuantity ) throws ProductNotFoundException 144 { 145 // throw IllegalArgumentException if uantity not valid 146 if ( productQuantity < 0 ) 147 throw new IllegalArgumentException( 148 "Quantity cannot be less than zero." ); // remove Product if productQuantity less than if ( productQuantity == 0 ) { 152 removeProduct( isbn ); 153 return; 154 } Iterator iterator = orderProductModels.iterator(); while ( iterator.hasNext() ) { // get next OrderProduct in ShoppingCart 161 OrderProductModel orderProductModel = 162 ( OrderProductModel ) iterator.next(); ProductModel productModel = 165 orderProductModel.getProductModel(); // set quantity for Product with given ISBN 168 if ( productModel.getISBN().equals( isbn ) ) { 169 orderProductModel.setQuantity( productQuantity ); 170 return; 171 } } // end while 174 removes product if product quantity is 0 updates product quantity if product found

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines Lines // throw exception if Product not found in ShoppingCart 176 throw new ProductNotFoundException( "The Product " "with ISBN " + isbn + " was not found in your " "ShoppingCart." ); } // end method setProductQuantity // checkout of store (i.e., create new Order) 183 public Order checkout( String userID ) 184 throws ProductNotFoundException, EJBException 185 { 186 // throw exception if ShoppingCart is empty 187 if ( orderProductModels.isEmpty() ) 188 throw new ProductNotFoundException( "There were " "no Products found in your ShoppingCart." ); // create OrderModel for Order details 192 OrderModel orderModel = new OrderModel(); // set OrderModel's date to today's Date 195 orderModel.setOrderDate( new Date() ); // set list of OrderProduct in OrderModel 198 orderModel.setOrderProductModels( orderProductModels ); // set OrderModel's shipped flag to false 201 orderModel.setShipped( false ); // use OrderHome interface to create new Order 204 try { 205 InitialContext context = new InitialContext(); // look up Order EJB 208 Object object = context.lookup( 209 "java:comp/env/ejb/Order" ); thrown if product not found create OrderModel to represent Order details

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 217 Line 220 Line OrderHome orderHome = ( OrderHome ) 212 PortableRemoteObject.narrow( object, 213 OrderHome.class ); // create new Order using OrderModel and 216 // Customer's userID 217 Order order = orderHome.create( orderModel, userID ); // empty ShoppingCart for further shopping 220 orderProductModels = new ArrayList(); // return Order EJB that was created 223 return order; } // end try // handle exception when looking up Order EJB 228 catch ( Exception exception ) { 229 throw new EJBException( exception ); 230 } } // end method checkout // get total cost for Products in ShoppingCart 235 public double getTotal() 236 { 237 double total = 0.0; 238 Iterator iterator = orderProductModels.iterator(); // calculate Order's total cost 241 while ( iterator.hasNext() ) { 242 create new Order empty ShoppingCart by creating new Collection of OrderProductModel s return remote reference to newly created Order

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines // get next OrderProduct in ShoppingCart 244 OrderProductModel orderProductModel = 245 ( OrderProductModel ) iterator.next(); ProductModel productModel = 248 orderProductModel.getProductModel(); // add OrderProduct extended price to total 251 total += ( productModel.getPrice() * 252 orderProductModel.getQuantity() ); 253 } return total; } // end method getTotal // set SessionContext 260 public void setSessionContext( SessionContext context ) 261 { 262 sessionContext = context; 263 } // activate ShoppingCart EJB instance 266 public void ejbActivate() {} // passivate ShoppingCart EJB instance 269 public void ejbPassivate() {} // remove ShoppingCart EJB instance 272 public void ejbRemove() {} 273 } calculate total cost of items in ShoppingCart

 2002 Prentice Hall. All rights reserved ShoppingCartHome Interface Interface ShoppingCartHome –defines single create method creates new ShoppingCart EJB instances EJB container provides implementation During deployment –use settings in following figure tables –set Transaction Type to Required for business methods

 2002 Prentice Hall. All rights reserved. Outline Fig ShoppingCartHome interface for creating ShopppingCart EJB instances. Lines // ShoppingCartHome.java 2 // ShoppingCartHome is the home interface for stateful session 3 // EJB ShoppingCart. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.rmi.RemoteException; 8 9 // Java extension packages 10 import javax.ejb.*; public interface ShoppingCartHome extends EJBHome { // create new ShoppingCart EJB 15 public ShoppingCart create() 16 throws RemoteException, CreateException; 17 } implementation provided by EJB container

 2002 Prentice Hall. All rights reserved ShoppingCartHome Interface (cont.)

 2002 Prentice Hall. All rights reserved ShoppingCartHome Interface (cont.)

 2002 Prentice Hall. All rights reserved Product Implementation Entity EJB Product –uses container-managed persistence –implements methods that select, insert, update and delete database data Deployer provides information of Product EJB mapping to database tables.

 2002 Prentice Hall. All rights reserved Product Remote Interface Declares method getProductmodel –returns ProductModel containing Product ’s details

 2002 Prentice Hall. All rights reserved. Outline Fig Product remote interface for modifying details of Product EJB instances. Lines // Product.java 2 // Product is the remote interface for entity EJB Product. 3 package com.deitel.advjhtp1.bookstore.ejb; 4 5 // Java core packages 6 import java.rmi.RemoteException; 7 8 // Java extension packages 9 import javax.ejb.*; // Deitel packages 12 import com.deitel.advjhtp1.bookstore.model.*; public interface Product extends EJBObject { // get Product details as ProductModel 17 public ProductModel getProductModel() 18 throws RemoteException; 19 } enables ProductModel retrieval

 2002 Prentice Hall. All rights reserved ProductEJB Implementation ProductEJB –Product remote interface implementation –uses container-managed persistence

 2002 Prentice Hall. All rights reserved. Outline Fig ProductEJB implementation of Product remote interface. Line Lines Line 30 Lines // ProductEJB.java 2 // Entity EJB Product represents a Product, including the 3 // ISBN, publisher, author, title, price number of pages 4 // and cover image. 5 package com.deitel.advjhtp1.bookstore.ejb; 6 7 // Java extension packages 8 import javax.ejb.*; 9 10 // Deitel packages 11 import com.deitel.advjhtp1.bookstore.model.*; 12 import com.deitel.advjhtp1.bookstore.*; public class ProductEJB implements EntityBean { 15 private EntityContext entityContext; // container-managed fields 18 public String ISBN; 19 public String publisher; 20 public String author; 21 public String title; 22 public double price; 23 public int pages; 24 public String image; // get Product details as ProductModel 27 public ProductModel getProductModel() 28 { 29 // construct new ProductModel 30 ProductModel productModel = new ProductModel(); // initialize ProductModel with data from Product 33 productModel.setISBN( ISBN ); 34 productModel.setPublisher( publisher ); 35 productModel.setAuthor( author ); container synchronizes access to database-mapped public members creates ProductModel containing Product ’s details create ProductModel populate ProductModel

 2002 Prentice Hall. All rights reserved. Outline Fig ProductEJB implementation of Product remote interface. Lines Line productModel.setTitle( title ); 37 productModel.setPrice( price ); 38 productModel.setPages( pages ); 39 productModel.setImage( image ); return productModel; } // end method getProductModel // set Product details using ProductModel 46 private void setProductModel( ProductModel productModel ) 47 { 48 // populate Product's data members with data in 49 // provided ProductModel 50 ISBN = productModel.getISBN(); 51 publisher = productModel.getPublisher(); 52 author = productModel.getAuthor(); 53 title = productModel.getTitle(); 54 price = productModel.getPrice(); 55 pages = productModel.getPages(); 56 image = productModel.getImage(); } // end method setProductModel // create instance of Product EJB using given ProductModel 61 public String ejbCreate( ProductModel productModel ) 62 { 63 setProductModel( productModel ); 64 return null; 65 } // perform any necessary post-creation tasks 68 public void ejbPostCreate( ProductModel productmodel ) {} 69 set Product ’s details initialize EJB with provided ProductModel

 2002 Prentice Hall. All rights reserved. Outline Fig ProductEJB implementation of Product remote interface. 70 // set EntityContext 71 public void setEntityContext( EntityContext context ) 72 { 73 entityContext = context; 74 } // unset EntityContext 77 public void unsetEntityContext() 78 { 79 entityContext = null; 80 } // activate Product EJB instance 83 public void ejbActivate() 84 { 85 ISBN = ( String ) entityContext.getPrimaryKey(); 86 } // passivate Product EJB instance 89 public void ejbPassivate() 90 { 91 ISBN = null; 92 } // remove Product EJB instance 95 public void ejbRemove() {} // store Product EJB data in database 98 public void ejbStore() {} // load Product EJB data from database 101 public void ejbLoad() {} 102 }

 2002 Prentice Hall. All rights reserved ProductHome Interface Interface ProductHome –creates new ProductEJB instances –declares finder methods for finding existing Product s.

 2002 Prentice Hall. All rights reserved. Outline Fig ProductHome interface for finding and creating Product EJB instances. Lines Lines Lines Lines // ProductHome.java 2 // ProductHome is the home interface for entity EJB Product. 3 package com.deitel.advjhtp1.bookstore.ejb; 4 5 // Java core packages 6 import java.rmi.RemoteException; 7 import java.util.Collection; 8 9 // Java extension packages 10 import javax.ejb.*; // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; public interface ProductHome extends EJBHome { // create Product EJB using given ProductModel 18 public Product create( ProductModel productModel ) 19 throws RemoteException, CreateException; // find Product with given ISBN 22 public Product findByPrimaryKey( String isbn ) 23 throws RemoteException, FinderException; // find all Products 26 public Collection findAllProducts() 27 throws RemoteException, FinderException; // find Products with given title 30 public Collection findByTitle( String title ) 31 throws RemoteException, FinderException; 32 } interface for creating ProductEJB instance takes the ISBN as String argument. Container implements method using deployment SQL queries returns Collection of all Product s in database. Container implements method using deployment SQL queries. returns Collection of matching Product s. Container implements method using deployment SQL queries.

 2002 Prentice Hall. All rights reserved ProductModel Implements interface Serializable –instances may be serialized over RMI-IIOP.

 2002 Prentice Hall. All rights reserved. Outline Fig ProductModel class for serializing Product data. Line 15 Lines // ProductModel.java 2 // ProductModel represents a Product in the Deitel Bookstore, 3 // including ISBN, author, title and a picture of the cover. 4 package com.deitel.advjhtp1.bookstore.model; 5 6 // Java core packages 7 import java.io.*; 8 import java.util.*; 9 import java.text.*; // third-party packages 12 import org.w3c.dom.*; public class ProductModel implements Serializable, 15 XMLGenerator { // ProductModel properties 18 private String ISBN; 19 private String publisher; 20 private String author; 21 private String title; 22 private double price; 23 private int pages; 24 private String image; 25 private int quantity; // set ISBN 28 public void setISBN( String productISBN ) 29 { 30 ISBN = productISBN; 31 } 32 private members accessible through get and set methods implements XMLGenerator

 2002 Prentice Hall. All rights reserved. Outline Fig ProductModel class for serializing Product data. 33 // get ISBN 34 public String getISBN() 35 { 36 return ISBN; 37 } // set publisher 40 public void setPublisher( String productPublisher ) 41 { 42 publisher = productPublisher; 43 } // get publisher 46 public String getPublisher() 47 { 48 return publisher; 49 } // set author 52 public void setAuthor( String productAuthor ) 53 { 54 author = productAuthor; 55 } // get author 58 public String getAuthor() 59 { 60 return author; 61 } // set title 64 public void setTitle( String productTitle ) 65 { 66 title = productTitle; 67 }

 2002 Prentice Hall. All rights reserved. Outline Fig ProductModel class for serializing Product data // get title 70 public String getTitle() 71 { 72 return title; 73 } // set price 76 public void setPrice( double productPrice ) 77 { 78 price = productPrice; 79 } // get price 82 public double getPrice() 83 { 84 return price; 85 } // set number of pages 88 public void setPages( int pageCount ) 89 { 90 pages = pageCount; 91 } // get number of pages 94 public int getPages() 95 { 96 return pages; 97 } // set URL of cover image 100 public void setImage( String productImage ) 101 { 102 image = productImage;

 2002 Prentice Hall. All rights reserved. Outline Fig ProductModel class for serializing Product data. Lines } // get URL of cover image 106 public String getImage() 107 { 108 return image; 109 } // get XML representation of Product 112 public Element getXML( Document document ) 113 { 114 // create product Element 115 Element product = document.createElement( "product" ); // create ISBN Element 118 Element temp = document.createElement( "ISBN" ); 119 temp.appendChild( 120 document.createTextNode( getISBN() ) ); 121 product.appendChild( temp ); // create publisher Element 124 temp = document.createElement( "publisher" ); 125 temp.appendChild( 126 document.createTextNode( getPublisher() ) ); 127 product.appendChild( temp ); // create author Element 130 temp = document.createElement( "author" ); 131 temp.appendChild( 132 document.createTextNode( getAuthor() ) ); 133 product.appendChild( temp ); 134 generates XML Element for data contained in ProductModel. Does not modify Document.

 2002 Prentice Hall. All rights reserved. Outline Fig ProductModel class for serializing Product data. Line // create title Element 136 temp = document.createElement( "title" ); 137 temp.appendChild( 138 document.createTextNode( getTitle() ) ); 139 product.appendChild( temp ); NumberFormat priceFormatter = 142 NumberFormat.getCurrencyInstance( Locale.US ); // create price Element 145 temp = document.createElement( "price" ); 146 temp.appendChild( document.createTextNode( 147 priceFormatter.format( getPrice() ) ) ); 148 product.appendChild( temp ); // create pages Element 151 temp = document.createElement( "pages" ); 152 temp.appendChild( document.createTextNode( 153 String.valueOf( getPages() ) ) ); 154 product.appendChild( temp ); // create image Element 157 temp = document.createElement( "image" ); 158 temp.appendChild( 159 document.createTextNode( getImage() ) ); 160 product.appendChild( temp ); return product; } // end method getXML 165 } returns newly created Element

 2002 Prentice Hall. All rights reserved. Outline Fig XMLGenerator interface for generating XML Element s for public properties. 1 // XMLGenerator.java 2 // XMLGenerator is an interface for classes that can generate 3 // XML Elements. The XML element returned by method getXML 4 // should contain Elements for each public property. 5 package com.deitel.advjhtp1.bookstore.model; 6 7 // third-party packages 8 import org.w3c.dom.*; 9 10 public interface XMLGenerator { // build an XML element for this Object 13 public Element getXML( Document document ); 14 }

 2002 Prentice Hall. All rights reserved ProductModel

 2002 Prentice Hall. All rights reserved ProductModel Fig Product Entity (Part 1 of 2)and deployment settings.

 2002 Prentice Hall. All rights reserved ProductModel

 2002 Prentice Hall. All rights reserved Order Implementation Entity EJB Order –represents an order placed at Deitel Bookstore –consists of list of Product s and associated quantities customerID of Customer

 2002 Prentice Hall. All rights reserved. Outline Fig Order remote interface for modifying details of Order EJB instances. Line 17 Lines Line 24 1 // Order.java 2 // Order is the remote interface for entity EJB Order. 3 package com.deitel.advjhtp1.bookstore.ejb; 4 5 // Java core packages 6 import java.rmi.RemoteException; 7 8 // Java extension packages 9 import javax.ejb.*; // Deitel packages 12 import com.deitel.advjhtp1.bookstore.model.*; public interface Order extends EJBObject { // get Order details as OrderModel 17 public OrderModel getOrderModel() throws RemoteException; // set shipped flag 20 public void setShipped( boolean flag ) 21 throws RemoteException; // get shipped flag 24 public boolean isShipped() throws RemoteException; 25 } returns OrderModel containing Order details marks Order as been shipped from warehouse indicates Order has been shipped

 2002 Prentice Hall. All rights reserved OrderEJB Implementation OrderEJB –Order remote reference implementation –declares public members for container-managed persistence

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines Lines // OrderEJB.java 2 // Entity EJB Order represents an Order, including the 3 // orderID, Order date, total cost and whether the Order 4 // has shipped. 5 package com.deitel.advjhtp1.bookstore.ejb; 6 7 // Java core packages 8 import java.util.*; 9 import java.text.DateFormat; 10 import java.rmi.RemoteException; // Java extension packages 13 import javax.ejb.*; 14 import javax.naming.*; 15 import javax.rmi.PortableRemoteObject; // Deitel packages 18 import com.deitel.advjhtp1.bookstore.model.*; public class OrderEJB implements EntityBean { 21 private EntityContext entityContext; 22 private InitialContext initialContext; 23 private DateFormat dateFormat; // container-managed fields 26 public Integer orderID; 27 public Integer customerID; 28 public String orderDate; 29 public boolean shipped; // get Order details as OrderModel 32 public OrderModel getOrderModel() throws EJBException 33 { 34 // construct new OrderModel 35 OrderModel orderModel = new OrderModel(); public members for container managed persistence construct OrderModel instance containing Order details

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines Lines // look up OrderProduct EJB to retrieve list 38 // of Products contained in the Order 39 try { // populate OrderModel data members with data from Order 42 orderModel.setOrderID( orderID ); 43 orderModel.setOrderDate( dateFormat.parse( orderDate ) ); 44 orderModel.setShipped( shipped ); initialContext = new InitialContext(); Object object = initialContext.lookup( 49 "java:comp/env/ejb/OrderProduct" ); OrderProductHome orderProductHome = 52 ( OrderProductHome ) PortableRemoteObject.narrow( 53 object, OrderProductHome.class ); // get OrderProduct records for Order 56 Collection orderProducts = 57 orderProductHome.findByOrderID( orderID ); Iterator iterator = orderProducts.iterator(); // OrderProductModels to place in OrderModel 62 Collection orderProductModels = new ArrayList(); // get OrderProductModel for each Product in Order 65 while ( iterator.hasNext() ) { 66 OrderProduct orderProduct = ( OrderProduct ) 67 PortableRemoteObject.narrow( iterator.next(), 68 OrderProduct.class ); 69 populate OrderModel obtain OrderProduct records

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines Line 76 Lines // get OrderProductModel for OrderProduct record 71 OrderProductModel orderProductModel = 72 orderProduct.getOrderProductModel(); // add OrderProductModel to list of 75 // OrderProductModels in the Order 76 orderProductModels.add( orderProductModel ); 77 } // add Collection of OrderProductModels to OrderModel 80 orderModel.setOrderProductModels( orderProductModels ); } // end try // handle exception working with OrderProduct EJB 85 catch ( Exception exception ) { 86 throw new EJBException( exception ); 87 } return orderModel; } // end method getOrderModel // set shipped flag 94 public void setShipped( boolean flag ) 95 { 96 shipped = flag; 97 } // get shipped flag 100 public boolean isShipped() 101 { 102 return shipped; 103 } 104 retrieve each OrderProductModel add OrderProductModel to Collection add Collection to OrderModel

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines Line // create new Order EJB using given OrderModel and userID 106 public Integer ejbCreate( OrderModel order, String userID ) 107 throws CreateException 108 { 109 // retrieve unique value for primary key of this 110 // Order using SequenceFactory EJB 111 try { 112 initialContext = new InitialContext(); Object object = initialContext.lookup( 115 "java:comp/env/ejb/SequenceFactory" ); SequenceFactoryHome sequenceFactoryHome = 118 ( SequenceFactoryHome ) 119 PortableRemoteObject.narrow( 120 object, SequenceFactoryHome.class ); // find sequence for CustomerOrder table 123 SequenceFactory sequenceFactory = 124 sequenceFactoryHome.findByPrimaryKey( 125 "CustomerOrders" ); // get next unique orderID 128 orderID = sequenceFactory.getNextID(); // get date, cost, shipped flag and list of 131 // OrderProduct from provided OrderModel 132 orderDate = dateFormat.format( order.getOrderDate() ); 133 shipped = order.getShipped(); // get OrderProductModels that comprise OrderModel 136 Collection orderProductModels = 137 order.getOrderProductModels(); 138 generates unique ID for order creates Order EJB using data in OrderModel and userID

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines // create OrderProduct EJBs for each Product in 140 // Order to keep track of quantity 141 object = initialContext.lookup( 142 "java:comp/env/ejb/OrderProduct" ); OrderProductHome orderProductHome = 145 ( OrderProductHome ) PortableRemoteObject.narrow( 146 object, OrderProductHome.class ); Iterator iterator = orderProductModels.iterator(); // create an OrderProduct EJB with Product's 151 // ISBN, quantity and orderID for this Order 152 while ( iterator.hasNext() ) { OrderProductModel orderProductModel = 155 ( OrderProductModel ) iterator.next(); // set orderID for OrderProduct record 158 orderProductModel.setOrderID( orderID ); // create OrderProduct EJB instance 161 orderProductHome.create( orderProductModel ); 162 } // get customerID for customer placing Order 165 object = initialContext.lookup( 166 "java:comp/env/ejb/Customer" ); CustomerHome customerHome = 169 ( CustomerHome ) PortableRemoteObject.narrow( 170 object, CustomerHome.class ); 171 create OrderProduct records

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. Lines Line // use provided userID to find Customer 173 Customer customer = 174 customerHome.findByUserID( userID ); customerID = ( Integer ) customer.getPrimaryKey(); } // end try // handle exception when looking up EJBs 181 catch ( Exception exception ) { 182 throw new CreateException( exception.getMessage() ); 183 } return null; } // end method ejbCreate // perform any necessary post-creation tasks 190 public void ejbPostCreate( OrderModel order, String id ) {} // set EntityContext 193 public void setEntityContext( EntityContext context ) 194 { 195 entityContext = context; 196 dateFormat = DateFormat.getDateTimeInstance( 197 DateFormat.FULL, DateFormat.SHORT, Locale.US ); 198 } // unset EntityContext 201 public void unsetEntityContext() 202 { 203 entityContext = null; 204 } 205 retrieve Customer EJB for given userID set customerID for Order

 2002 Prentice Hall. All rights reserved. Outline Fig OrderEJB implementation of Order remote interface. 206 // activate Order EJB instance 207 public void ejbActivate() 208 { 209 orderID = ( Integer ) entityContext.getPrimaryKey(); 210 } // passivate Order EJB instance 213 public void ejbPassivate() 214 { 215 orderID = null; 216 } // remove Order EJB instanceCus 219 public void ejbRemove() {} // store Order EJB data in database 222 public void ejbStore() {} // load Order EJB data from database 225 public void ejbLoad() {} 226 }

 2002 Prentice Hall. All rights reserved OrderHome Interface Creates Order instances. Finds existing Order instances.

 2002 Prentice Hall. All rights reserved. Outline Fig OrderHome interface for finding and creating Order EJB instances. Lines Lines Lines // OrderHome.java 2 // OrderHome is the home interface for entity EJB Order. 3 package com.deitel.advjhtp1.bookstore.ejb; 4 5 // Java core packages 6 import java.util.*; 7 import java.rmi.RemoteException; 8 9 // Java extension packages 10 import javax.ejb.*; // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; public interface OrderHome extends EJBHome { // create Order using given OrderModel and userID 18 public Order create( OrderModel orderModel, String userID ) 19 throws RemoteException, CreateException; // find Order using given orderID 22 public Order findByPrimaryKey( Integer orderID ) 23 throws RemoteException, FinderException; // find Orders for given customerID 26 public Collection findByCustomerID( Integer customerID ) 27 throws RemoteException, FinderException; 28 } creates new Order locates existing Order retrieves Collection of Order s for given Customer

 2002 Prentice Hall. All rights reserved OrderModel Encapsulates details of an Order EJB in Serializable object.

 2002 Prentice Hall. All rights reserved. Outline Fig OrderModel class for serializing Order data. Lines // OrderModel.java 2 // OrderModel represents an Order and contains the order ID, 3 // date, total cost and a boolean indicating whether or not the 4 // order has shipped. 5 package com.deitel.advjhtp1.bookstore.model; 6 7 // Java core packages 8 import java.io.*; 9 import java.util.*; 10 import java.text.*; // third-party packages 13 import org.w3c.dom.*; public class OrderModel implements Serializable, 16 XMLGenerator { // OrderModel properties 19 private Integer orderID; 20 private Date orderDate; 21 private boolean shipped; 22 private Collection orderProductModels; // construct empty OrderModel 25 public OrderModel() 26 { 27 orderProductModels = new ArrayList(); 28 } // set order ID 31 public void setOrderID( Integer id ) 32 { 33 orderID = id; 34 } 35 have associated set and get methods

 2002 Prentice Hall. All rights reserved. Outline Fig OrderModel class for serializing Order data. 36 // get order ID 37 public Integer getOrderID() 38 { 39 return orderID; 40 } // set order date 43 public void setOrderDate( Date date ) 44 { 45 orderDate = date; 46 } // get order date 49 public Date getOrderDate() 50 { 51 return orderDate; 52 } // get total cost 55 public double getTotalCost() 56 { 57 double total = 0.0; Iterator iterator = orderProductModels.iterator(); // calculate Order's total cost 62 while ( iterator.hasNext() ) { // get next OrderProduct in ShoppingCart 65 OrderProductModel orderProductModel = 66 ( OrderProductModel ) iterator.next(); ProductModel productModel = 69 orderProductModel.getProductModel(); 70

 2002 Prentice Hall. All rights reserved. Outline Fig OrderModel class for serializing Order data. 71 // add OrderProduct extended price to total 72 total += ( productModel.getPrice() * 73 orderProductModel.getQuantity() ); 74 } return total; 77 } // set shipped flag 80 public void setShipped( boolean orderShipped ) 81 { 82 shipped = orderShipped; 83 } // get shipped flag 86 public boolean getShipped() 87 { 88 return shipped; 89 } // set list of OrderProductModels 92 public void setOrderProductModels( Collection models ) 93 { 94 orderProductModels = models; 95 } // get OrderProductModels 98 public Collection getOrderProductModels() 99 { 100 return Collections.unmodifiableCollection( 101 orderProductModels ); 102 } 103

 2002 Prentice Hall. All rights reserved. Outline Fig OrderModel class for serializing Order data. Lines // get XML representation of Order 105 public Element getXML( Document document ) 106 { 107 // create order Element 108 Element order = document.createElement( "order" ); // create orderID Element 111 Element temp = document.createElement( "orderID" ); 112 temp.appendChild( document.createTextNode( 113 String.valueOf( getOrderID() ) ) ); 114 order.appendChild( temp ); // get DateFormat for writing Date to XML document 117 DateFormat formatter = DateFormat.getDateTimeInstance( 118 DateFormat.DEFAULT, DateFormat.MEDIUM, Locale.US ); // create orderDate Element 121 temp = document.createElement( "orderDate" ); 122 temp.appendChild( document.createTextNode( 123 formatter.format( getOrderDate() ) ) ); 124 order.appendChild( temp ); NumberFormat costFormatter = 127 NumberFormat.getCurrencyInstance( Locale.US ); // create totalCost Element 130 temp = document.createElement( "totalCost" ); 131 temp.appendChild( document.createTextNode( 132 costFormatter.format( getTotalCost() ) ) ); 133 order.appendChild( temp ); // create shipped Element 136 temp = document.createElement( "shipped" ); 137 from interface XMLGenerator

 2002 Prentice Hall. All rights reserved. Outline Fig OrderModel class for serializing Order data. 138 if ( getShipped() ) 139 temp.appendChild( 140 document.createTextNode( "yes" ) ); 141 else 142 temp.appendChild( 143 document.createTextNode( "no" ) ); order.appendChild( temp ); // create orderProducts Element 148 Element orderProducts = 149 document.createElement( "orderProducts" ); Iterator iterator = getOrderProductModels().iterator(); // add orderProduct element for each OrderProduct 154 while ( iterator.hasNext() ) { 155 OrderProductModel orderProductModel = 156 ( OrderProductModel ) iterator.next(); orderProducts.appendChild( 159 orderProductModel.getXML( document ) ); 160 } order.appendChild( orderProducts ); return order; } // end method getXML 167 }

 2002 Prentice Hall. All rights reserved OrderModel (cont.)

 2002 Prentice Hall. All rights reserved OrderModel (cont.)

 2002 Prentice Hall. All rights reserved OrderModel (cont.)

 2002 Prentice Hall. All rights reserved OrderProduct Implementation EJB OrderProduct –Many-to-many relationship between Orders and Products

 2002 Prentice Hall. All rights reserved OrderProduct Remote Interface OrderProduct remote interface –Business methods for the OrderProduct EJB

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProduct remote interface for modifying details of OrderProduct EJB instances. Lines // OrderProduct.java 2 // OrderProduct is the remote interface for entity EJB 3 // OrderProduct. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.rmi.RemoteException; 8 9 // Java extension packages 10 import javax.ejb.*; // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; public interface OrderProduct extends EJBObject { // get OrderProduct details as OrderProductModel 18 public OrderProductModel getOrderProductModel() 19 throws RemoteException; 20 } Method getOrderProductModel returns an OrderProductModel that contains the details of an OrderProduct record.

 2002 Prentice Hall. All rights reserved OrderProductEJB Implementation OrderProductEJB –Implements OrderProduct remote interface

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductEJB implementation of OrderProduct remote interface. Lines Lines // OrderProductEJB.java 2 // Entity EJB OrderProductEJB represents the mapping between 3 // a Product and an Order, including the quantity of the 4 // Product in the Order. 5 package com.deitel.advjhtp1.bookstore.ejb; 6 7 // Java core packages 8 import java.rmi.RemoteException; 9 10 // Java extension packages 11 import javax.ejb.*; 12 import javax.naming.*; 13 import javax.rmi.PortableRemoteObject; // Deitel packages 16 import com.deitel.advjhtp1.bookstore.model.*; public class OrderProductEJB implements EntityBean { 19 private EntityContext entityContext; // container-managed fields 22 public String ISBN; 23 public Integer orderID; 24 public int quantity; // get OrderProduct details as OrderProductModel 27 public OrderProductModel getOrderProductModel() 28 throws EJBException 29 { 30 OrderProductModel model = new OrderProductModel(); // get ProductModel for Product in this OrderProduct 33 try { 34 Context initialContext = new InitialContext(); 35 Declare container-managed fields ISBN, orderID and quantity. Method getOrderProductModel returns the details of the OrderProduct record as an OrderProductModel.

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductEJB implementation of OrderProduct remote interface. 36 // look up Product EJB 37 Object object = initialContext.lookup( 38 "java:comp/env/ejb/Product" ); // get ProductHome interface 41 ProductHome productHome = ( ProductHome ) 42 PortableRemoteObject.narrow( object, 43 ProductHome.class ); // find Product using its ISBN 46 Product product = 47 productHome.findByPrimaryKey( ISBN ); // get ProductModel 50 ProductModel productModel = 51 product.getProductModel(); // set ProductModel in OrderProductModel 54 model.setProductModel( productModel ); } // end try // handle exception when looking up Product EJB 59 catch ( Exception exception ) { 60 throw new EJBException( exception ); 61 } // set orderID and quantity in OrderProductModel 64 model.setOrderID( orderID ); 65 model.setQuantity( quantity ); return model; } // end method getOrderProductModel 70

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductEJB implementation of OrderProduct remote interface. Lines Lines Line // set OrderProduct details using OrderProductModel 72 private void setOrderProductModel( OrderProductModel model ) 73 { 74 ISBN = model.getProductModel().getISBN(); 75 orderID = model.getOrderID(); 76 quantity = model.getQuantity(); 77 } // create OrderProduct for given OrderProductModel 80 public OrderProductPK ejbCreate( OrderProductModel model ) 81 { 82 setOrderProductModel( model ); 83 return null; 84 } // perform any necessary post-creation tasks 87 public void ejbPostCreate( OrderProductModel model ) {} // set EntityContext 90 public void setEntityContext( EntityContext context ) 91 { 92 entityContext = context; 93 } // unset EntityContext 96 public void unsetEntityContext() 97 { 98 entityContext = null; 99 } // activate OrderProduct EJB instance 102 public void ejbActivate() 103 { 104 OrderProductPK primaryKey = 105 ( OrderProductPK ) entityContext.getPrimaryKey(); Set the details of the OrderProduct record, using data from the OrderProductModel argument. The EJB container calls method ejbCreate to create new instances of the OrderProduct EJB. Invokes method setOrderProductModel to complete the creation of the OrderProduct record.

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductEJB implementation of OrderProduct remote interface ISBN = primaryKey.getISBN(); 108 orderID = primaryKey.getOrderID(); 109 } // passivate OrderProduct EJB instance 112 public void ejbPassivate() 113 { 114 ISBN = null; 115 orderID = null; 116 } // remove OrderProduct EJB instance 119 public void ejbRemove() {} // store OrderProduct EJB data in database 122 public void ejbStore() {} // load OrderProduct EJB data from database 125 public void ejbLoad() {} 126 }

 2002 Prentice Hall. All rights reserved OrderProductHome Interface OrderProductHome interface –Creates new OrderProduct EJB instance –Locates existing OrderProduct records

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductHome interface for finding and creating OrderProduct EJB instances. Lines Lines Lines // OrderProductHome.java 2 // OrderProductHome is the home interface for entity EJB 3 // OrderProduct. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.util.Collection; 8 import java.rmi.RemoteException; 9 10 // Java extension packages 11 import javax.ejb.*; // Deitel packages 14 import com.deitel.advjhtp1.bookstore.model.*; public interface OrderProductHome extends EJBHome { // create OrderProduct using given OrderProductModel 19 public OrderProduct create( OrderProductModel model ) 20 throws RemoteException, CreateException; // find OrderProduct for given orderID 23 public Collection findByOrderID( Integer orderID ) 24 throws RemoteException, FinderException; // find OrderProduct for given primary key 27 public OrderProduct findByPrimaryKey( OrderProductPK pk ) 28 throws RemoteException, FinderException; 29 } Method create corresponds to method ejbCreate of the OrderProductEJB implementation. locates all OrderProduct records with the provided orderID and returns a Collection of OrderProduct remote references. Locates an OrderProduct record, using an instance of the OrderProductPK primary-key class.

 2002 Prentice Hall. All rights reserved OrderProductPK Primary-Key Class OrderProductPK –Primary-key class for the OrderProduct EJB

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductPK primary-key class for OrderProduct EJB. Lines // OrderProductPK.java 2 // OrderProductPK is a primary-key class for entity EJB 3 // OrderProduct. 4 package com.deitel.advjhtp1.bookstore.ejb; 5 6 // Java core packages 7 import java.io.*; 8 9 public class OrderProductPK implements Serializable { // primary-key fields 12 public String ISBN; 13 public Integer orderID; // no-argument constructor 16 public OrderProductPK() {} // construct OrderProductPK with ISBN and orderID 19 public OrderProductPK( String isbn, Integer id ) 20 { 21 ISBN = isbn; 22 orderID = id; 23 } // get ISBN 26 public String getISBN() 27 { 28 return ISBN; 29 } // get orderID 32 public Integer getOrderID() 33 { 34 return orderID; 35 } Two public data members ISBN and orderID correspond to OrderProductEJB’s two primary-key fields.

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductPK primary-key class for OrderProduct EJB. Lines Lines // calculate hashCode for this Object 38 public int hashCode() 39 { 40 return getISBN().hashCode() ^ getOrderID().intValue(); 41 } // custom implementation of Object equals method 44 public boolean equals( Object object ) 45 { 46 // ensure object is instance of OrderProductPK 47 if ( object instanceof OrderProductPK ) { 48 OrderProductPK otherKey = 49 ( OrderProductPK ) object; // compare ISBNs and orderIDs 52 return ( getISBN().equals( otherKey.getISBN() ) 53 && getOrderID().equals( otherKey.getOrderID() ) ); 54 } return false; 57 } 58 } The overridden implementations of methods hasCode and equals enable the EJB container and OrderProduct EJB clients to determine if two OrderProduct EJB instances are equal by comparing their primary-key class instances.

 2002 Prentice Hall. All rights reserved OrderProductModel OrderProductModel –Represents an OrderProduct record

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductMode l class for serializing OrderProduct data. 1 // OrderProductModel.java 2 // OrderProductModel represents a Product and its quantity in 3 // an Order or ShoppingCart. 4 package com.deitel.advjhtp1.bookstore.model; 5 6 // Java core packages 7 import java.io.*; 8 import java.util.*; 9 import java.text.*; // third-party packages 12 import org.w3c.dom.*; public class OrderProductModel implements Serializable, 15 XMLGenerator { // OrderProductModel properties 18 private ProductModel productModel; 19 private int quantity; 20 private Integer orderID; // set ProductModel 23 public void setProductModel( ProductModel model ) 24 { 25 productModel = model; 26 } // get ProductModel 29 public ProductModel getProductModel() 30 { 31 return productModel; 32 } 33

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductMode l class for serializing OrderProduct data. 34 // set quantity 35 public void setQuantity( int productQuantity ) 36 { 37 quantity = productQuantity; 38 } // get quantity 41 public int getQuantity() 42 { 43 return quantity; 44 } // set orderID 47 public void setOrderID( Integer id ) 48 { 49 orderID = id; 50 } // get orderID 53 public Integer getOrderID() 54 { 55 return orderID; 56 } // get XML representation of OrderProduct 59 public Element getXML( Document document ) 60 { 61 // create orderProduct Element 62 Element orderProduct = 63 document.createElement( "orderProduct" ); // append ProductModel product Element 66 orderProduct.appendChild ( 67 getProductModel().getXML( document ) ); 68

 2002 Prentice Hall. All rights reserved. Outline Fig OrderProductMode l class for serializing OrderProduct data. 69 // create quantity Element 70 Element temp = document.createElement( "quantity" ); 71 temp.appendChild( document.createTextNode( 72 String.valueOf ( getQuantity() ) ) ); 73 orderProduct.appendChild( temp ); return orderProduct; 76 } 77 }

 2002 Prentice Hall. All rights reserved OrderProductModel (Cont.)

 2002 Prentice Hall. All rights reserved OrderProductModel (Cont.) Fig (Part 1 of 2) OrderProduct entity and deployment settings.

 2002 Prentice Hall. All rights reserved OrderProductModel (Cont.)

 2002 Prentice Hall. All rights reserved OrderProductModel (Cont.)