Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


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

1  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 19.3.1 ShoppingCart Remote Interface 19.3.2 ShoppingCartEJB Implementation 19.3.3 ShoppingCartHome Interface 19.4 Product Implementation 19.4.1 Product Remote Interface 19.4.2 ProductEJB Implementation 19.4.3 ProductHome Interface 19.4.4 ProductModel 19.5 Order Implementation 19.5.1 Order Remote Interface 19.5.2 OrderEJB Implementation 19.5.3 OrderHome Interface 19.5.4 OrderModel

2  2002 Prentice Hall. All rights reserved. Chapter 19 Enterprise Java Case Study: Business Logic Part 1 19.6 OrderProduct Implementation 19.6.1 OrderProduct Remote Interface 19.6.2 OrderProductEJB Implementation 19.6.3 OrderProductHome Interface 19.6.4 OrderProductPK Primary-Key Class 19.6.5 OrderProductModel

3  2002 Prentice Hall. All rights reserved. 19.1 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

4  2002 Prentice Hall. All rights reserved. 19.2 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

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

6  2002 Prentice Hall. All rights reserved. 19.3 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

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

8  2002 Prentice Hall. All rights reserved. Outline Fig. 19.2 ShoppingCart remote interface for adding, removing and updating Product s, checking out and calculating Order ’s total cost. Line 20 Lines 23-24 Lines 27-28 Lines 32-34 1 // 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; 12 13 // Deitel packages 14 import com.deitel.advjhtp1.bookstore.model.*; 15 import com.deitel.advjhtp1.bookstore.exceptions.*; 16 17 public interface ShoppingCart extends EJBObject { 18 19 // get contents of ShoppingCart 20 public Collection getContents() throws RemoteException; 21 22 // add Product with given ISBN to ShoppingCart 23 public void addProduct( String isbn ) 24 throws RemoteException, ProductNotFoundException; 25 26 // remove Product with given ISBN from ShoppingCart 27 public void removeProduct( String isbn ) 28 throws RemoteException, ProductNotFoundException; 29 30 // 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

9  2002 Prentice Hall. All rights reserved. Outline Fig. 19.2 ShoppingCart remote interface for adding, removing and updating Product s, checking out and calculating Order ’s total cost. Lines 37-38 Line 41 36 // checkout ShoppingCart (i.e., create new Order) 37 public Order checkout( String userID ) 38 throws RemoteException, ProductNotFoundException; 39 40 // 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

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

11  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 24 Lines 27-30 Lines 33-36 1 // 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; 10 11 // Java extension packages 12 import javax.ejb.*; 13 import javax.naming.*; 14 import javax.rmi.PortableRemoteObject; 15 16 // Deitel packages 17 import com.deitel.advjhtp1.bookstore.model.*; 18 import com.deitel.advjhtp1.bookstore.exceptions.*; 19 20 public class ShoppingCartEJB implements SessionBean { 21 private SessionContext sessionContext; 22 23 // OrderProductModels (Products & quantities) in ShoppingCart 24 private Collection orderProductModels; 25 26 // create new ShoppingCart 27 public void ejbCreate() 28 { 29 orderProductModels = new ArrayList(); 30 } 31 32 // get contents of ShoppingCart 33 public Collection getContents() 34 { 35 return orderProductModels; contain OrderProductModel sinitializes Collection returns contents of ShoppingCart

12  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines 44-62 Lines 54-59 36 } 37 38 // 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(); 45 46 while ( iterator.hasNext() ) { 47 OrderProductModel orderProductModel = 48 ( OrderProductModel ) iterator.next(); 49 50 ProductModel productModel = 51 orderProductModel.getProductModel(); 52 53 // if Product is in ShoppingCart, increment quantity 54 if ( productModel.getISBN().equals( isbn ) ) { 55 56 orderProductModel.setQuantity( 57 orderProductModel.getQuantity() + 1 ); 58 59 return; 60 } 61 62 } // end while 63 64 // if Product is not in ShoppingCart, find Product with 65 // given ISBN and add OrderProductModel to ShoppingCart 66 try { 67 InitialContext context = new InitialContext(); 68 69 Object object = context.lookup( 70 "java:comp/env/ejb/Product" ); if isbn found, increase corresponding quantity number determine if product already in ShoppingCart

13  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 77 Lines 84-85 Line 87 Line 88 Line 91 Lines 96-101 71 72 ProductHome productHome = ( ProductHome ) 73 PortableRemoteObject.narrow( object, 74 ProductHome.class ); 75 76 // find Product with given ISBN 77 Product product = productHome.findByPrimaryKey( isbn ); 78 79 // get ProductModel 80 ProductModel productModel = product.getProductModel(); 81 82 // create OrderProductModel for ProductModel and set 83 // its quantity 84 OrderProductModel orderProductModel = 85 new OrderProductModel(); 86 87 orderProductModel.setProductModel( productModel ); 88 orderProductModel.setQuantity( 1 ); 89 90 // add OrderProductModel to ShoppingCart 91 orderProductModels.add( orderProductModel ); 92 93 } // end try 94 95 // handle exception when finding Product record 96 catch ( FinderException finderException ) { 97 finderException.printStackTrace(); 98 99 throw new ProductNotFoundException( "The Product " + 100 "with ISBN " + isbn + " was not found." ); 101 } 102 103 // 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

14  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines 114-132 Lines 126-130 Lines 135-137 106 } 107 108 } // end method addProduct 109 110 // remove Product with given ISBN from ShoppingCart 111 public void removeProduct( String isbn ) 112 throws ProductNotFoundException 113 { 114 Iterator iterator = orderProductModels.iterator(); 115 116 while ( iterator.hasNext() ) { 117 118 // get next OrderProduct in ShoppingCart 119 OrderProductModel orderProductModel = 120 ( OrderProductModel ) iterator.next(); 121 122 ProductModel productModel = 123 orderProductModel.getProductModel(); 124 125 // remove Product with given ISBN from ShoppingCart 126 if ( productModel.getISBN().equals( isbn ) ) { 127 orderProductModels.remove( orderProductModel ); 128 129 return; 130 } 131 132 } // end while 133 134 // throw exception if Product not found in ShoppingCart 135 throw new ProductNotFoundException( "The Product " + 136 "with ISBN " + isbn + " was not found in your " + 137 "ShoppingCart." ); 138 139 } // end method removeProduct 140 iterate through OrderProductModel s if ProductModel found, remove thrown when product not found

15  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines 151-154 Lines 168-171 141 // 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." ); 149 150 // remove Product if productQuantity less than 1 151 if ( productQuantity == 0 ) { 152 removeProduct( isbn ); 153 return; 154 } 155 156 Iterator iterator = orderProductModels.iterator(); 157 158 while ( iterator.hasNext() ) { 159 160 // get next OrderProduct in ShoppingCart 161 OrderProductModel orderProductModel = 162 ( OrderProductModel ) iterator.next(); 163 164 ProductModel productModel = 165 orderProductModel.getProductModel(); 166 167 // set quantity for Product with given ISBN 168 if ( productModel.getISBN().equals( isbn ) ) { 169 orderProductModel.setQuantity( productQuantity ); 170 return; 171 } 172 173 } // end while 174 removes product if product quantity is 0 updates product quantity if product found

16  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines 176-178 Lines 192-201 175 // throw exception if Product not found in ShoppingCart 176 throw new ProductNotFoundException( "The Product " + 177 "with ISBN " + isbn + " was not found in your " + 178 "ShoppingCart." ); 179 180 } // end method setProductQuantity 181 182 // 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 " + 189 "no Products found in your ShoppingCart." ); 190 191 // create OrderModel for Order details 192 OrderModel orderModel = new OrderModel(); 193 194 // set OrderModel's date to today's Date 195 orderModel.setOrderDate( new Date() ); 196 197 // set list of OrderProduct in OrderModel 198 orderModel.setOrderProductModels( orderProductModels ); 199 200 // set OrderModel's shipped flag to false 201 orderModel.setShipped( false ); 202 203 // use OrderHome interface to create new Order 204 try { 205 InitialContext context = new InitialContext(); 206 207 // 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

17  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Line 217 Line 220 Line 223 210 211 OrderHome orderHome = ( OrderHome ) 212 PortableRemoteObject.narrow( object, 213 OrderHome.class ); 214 215 // create new Order using OrderModel and 216 // Customer's userID 217 Order order = orderHome.create( orderModel, userID ); 218 219 // empty ShoppingCart for further shopping 220 orderProductModels = new ArrayList(); 221 222 // return Order EJB that was created 223 return order; 224 225 } // end try 226 227 // handle exception when looking up Order EJB 228 catch ( Exception exception ) { 229 throw new EJBException( exception ); 230 } 231 232 } // end method checkout 233 234 // get total cost for Products in ShoppingCart 235 public double getTotal() 236 { 237 double total = 0.0; 238 Iterator iterator = orderProductModels.iterator(); 239 240 // 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

18  2002 Prentice Hall. All rights reserved. Outline Fig. 19.3 ShoppingCartEJB eimplementation of ShoppingCart remote interface. Lines 251-252 243 // get next OrderProduct in ShoppingCart 244 OrderProductModel orderProductModel = 245 ( OrderProductModel ) iterator.next(); 246 247 ProductModel productModel = 248 orderProductModel.getProductModel(); 249 250 // add OrderProduct extended price to total 251 total += ( productModel.getPrice() * 252 orderProductModel.getQuantity() ); 253 } 254 255 return total; 256 257 } // end method getTotal 258 259 // set SessionContext 260 public void setSessionContext( SessionContext context ) 261 { 262 sessionContext = context; 263 } 264 265 // activate ShoppingCart EJB instance 266 public void ejbActivate() {} 267 268 // passivate ShoppingCart EJB instance 269 public void ejbPassivate() {} 270 271 // remove ShoppingCart EJB instance 272 public void ejbRemove() {} 273 } calculate total cost of items in ShoppingCart

19  2002 Prentice Hall. All rights reserved. 19.3.3 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

20  2002 Prentice Hall. All rights reserved. Outline Fig. 19.4 ShoppingCartHome interface for creating ShopppingCart EJB instances. Lines 15-16 1 // 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.*; 11 12 public interface ShoppingCartHome extends EJBHome { 13 14 // create new ShoppingCart EJB 15 public ShoppingCart create() 16 throws RemoteException, CreateException; 17 } implementation provided by EJB container

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

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

23  2002 Prentice Hall. All rights reserved. 19.4 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.

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

25  2002 Prentice Hall. All rights reserved. Outline Fig. 19.7 Product remote interface for modifying details of Product EJB instances. Lines 17-18 1 // 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.*; 10 11 // Deitel packages 12 import com.deitel.advjhtp1.bookstore.model.*; 13 14 public interface Product extends EJBObject { 15 16 // get Product details as ProductModel 17 public ProductModel getProductModel() 18 throws RemoteException; 19 } enables ProductModel retrieval

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

27  2002 Prentice Hall. All rights reserved. Outline Fig. 19.8 ProductEJB implementation of Product remote interface. Line 18-24 Lines 27-43 Line 30 Lines 33-39 1 // 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.*; 13 14 public class ProductEJB implements EntityBean { 15 private EntityContext entityContext; 16 17 // 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; 25 26 // get Product details as ProductModel 27 public ProductModel getProductModel() 28 { 29 // construct new ProductModel 30 ProductModel productModel = new ProductModel(); 31 32 // 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

28  2002 Prentice Hall. All rights reserved. Outline Fig. 19.8 ProductEJB implementation of Product remote interface. Lines 50-56 Line 63 36 productModel.setTitle( title ); 37 productModel.setPrice( price ); 38 productModel.setPages( pages ); 39 productModel.setImage( image ); 40 41 return productModel; 42 43 } // end method getProductModel 44 45 // 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(); 57 58 } // end method setProductModel 59 60 // create instance of Product EJB using given ProductModel 61 public String ejbCreate( ProductModel productModel ) 62 { 63 setProductModel( productModel ); 64 return null; 65 } 66 67 // perform any necessary post-creation tasks 68 public void ejbPostCreate( ProductModel productmodel ) {} 69 set Product ’s details initialize EJB with provided ProductModel

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

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

31  2002 Prentice Hall. All rights reserved. Outline Fig. 19.9 ProductHome interface for finding and creating Product EJB instances. Lines 18-19 Lines 22-23 Lines 26-27 Lines 30-31 1 // 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.*; 11 12 // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; 14 15 public interface ProductHome extends EJBHome { 16 17 // create Product EJB using given ProductModel 18 public Product create( ProductModel productModel ) 19 throws RemoteException, CreateException; 20 21 // find Product with given ISBN 22 public Product findByPrimaryKey( String isbn ) 23 throws RemoteException, FinderException; 24 25 // find all Products 26 public Collection findAllProducts() 27 throws RemoteException, FinderException; 28 29 // 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.

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

33  2002 Prentice Hall. All rights reserved. Outline Fig. 19.10 ProductModel class for serializing Product data. Line 15 Lines 18-25 1 // 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.*; 10 11 // third-party packages 12 import org.w3c.dom.*; 13 14 public class ProductModel implements Serializable, 15 XMLGenerator { 16 17 // 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; 26 27 // set ISBN 28 public void setISBN( String productISBN ) 29 { 30 ISBN = productISBN; 31 } 32 private members accessible through get and set methods implements XMLGenerator

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

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

36  2002 Prentice Hall. All rights reserved. Outline Fig. 19.10 ProductModel class for serializing Product data. Lines 112-164 103 } 104 105 // get URL of cover image 106 public String getImage() 107 { 108 return image; 109 } 110 111 // get XML representation of Product 112 public Element getXML( Document document ) 113 { 114 // create product Element 115 Element product = document.createElement( "product" ); 116 117 // create ISBN Element 118 Element temp = document.createElement( "ISBN" ); 119 temp.appendChild( 120 document.createTextNode( getISBN() ) ); 121 product.appendChild( temp ); 122 123 // create publisher Element 124 temp = document.createElement( "publisher" ); 125 temp.appendChild( 126 document.createTextNode( getPublisher() ) ); 127 product.appendChild( temp ); 128 129 // 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.

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

38  2002 Prentice Hall. All rights reserved. Outline Fig. 19.11 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 { 11 12 // build an XML element for this Object 13 public Element getXML( Document document ); 14 }

39  2002 Prentice Hall. All rights reserved. 19.4.4 ProductModel

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

41  2002 Prentice Hall. All rights reserved. 19.4.4 ProductModel

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

43  2002 Prentice Hall. All rights reserved. Outline Fig. 19.14 Order remote interface for modifying details of Order EJB instances. Line 17 Lines 20-21 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.*; 10 11 // Deitel packages 12 import com.deitel.advjhtp1.bookstore.model.*; 13 14 public interface Order extends EJBObject { 15 16 // get Order details as OrderModel 17 public OrderModel getOrderModel() throws RemoteException; 18 19 // set shipped flag 20 public void setShipped( boolean flag ) 21 throws RemoteException; 22 23 // 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

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

45  2002 Prentice Hall. All rights reserved. Outline Fig. 19.15 OrderEJB implementation of Order remote interface. Lines 26-29 Lines 32-91 1 // 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; 11 12 // Java extension packages 13 import javax.ejb.*; 14 import javax.naming.*; 15 import javax.rmi.PortableRemoteObject; 16 17 // Deitel packages 18 import com.deitel.advjhtp1.bookstore.model.*; 19 20 public class OrderEJB implements EntityBean { 21 private EntityContext entityContext; 22 private InitialContext initialContext; 23 private DateFormat dateFormat; 24 25 // container-managed fields 26 public Integer orderID; 27 public Integer customerID; 28 public String orderDate; 29 public boolean shipped; 30 31 // 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

46  2002 Prentice Hall. All rights reserved. Outline Fig. 19.15 OrderEJB implementation of Order remote interface. Lines 42-44 Lines 56-57 36 37 // look up OrderProduct EJB to retrieve list 38 // of Products contained in the Order 39 try { 40 41 // populate OrderModel data members with data from Order 42 orderModel.setOrderID( orderID ); 43 orderModel.setOrderDate( dateFormat.parse( orderDate ) ); 44 orderModel.setShipped( shipped ); 45 46 initialContext = new InitialContext(); 47 48 Object object = initialContext.lookup( 49 "java:comp/env/ejb/OrderProduct" ); 50 51 OrderProductHome orderProductHome = 52 ( OrderProductHome ) PortableRemoteObject.narrow( 53 object, OrderProductHome.class ); 54 55 // get OrderProduct records for Order 56 Collection orderProducts = 57 orderProductHome.findByOrderID( orderID ); 58 59 Iterator iterator = orderProducts.iterator(); 60 61 // OrderProductModels to place in OrderModel 62 Collection orderProductModels = new ArrayList(); 63 64 // 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

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

48  2002 Prentice Hall. All rights reserved. Outline Fig. 19.15 OrderEJB implementation of Order remote interface. Lines 106-187 Line 128 105 // 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(); 113 114 Object object = initialContext.lookup( 115 "java:comp/env/ejb/SequenceFactory" ); 116 117 SequenceFactoryHome sequenceFactoryHome = 118 ( SequenceFactoryHome ) 119 PortableRemoteObject.narrow( 120 object, SequenceFactoryHome.class ); 121 122 // find sequence for CustomerOrder table 123 SequenceFactory sequenceFactory = 124 sequenceFactoryHome.findByPrimaryKey( 125 "CustomerOrders" ); 126 127 // get next unique orderID 128 orderID = sequenceFactory.getNextID(); 129 130 // get date, cost, shipped flag and list of 131 // OrderProduct from provided OrderModel 132 orderDate = dateFormat.format( order.getOrderDate() ); 133 shipped = order.getShipped(); 134 135 // 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

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

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

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

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

53  2002 Prentice Hall. All rights reserved. Outline Fig. 19.16 OrderHome interface for finding and creating Order EJB instances. Lines 18-19 Lines 22-23 Lines 26-27 1 // 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.*; 11 12 // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; 14 15 public interface OrderHome extends EJBHome { 16 17 // create Order using given OrderModel and userID 18 public Order create( OrderModel orderModel, String userID ) 19 throws RemoteException, CreateException; 20 21 // find Order using given orderID 22 public Order findByPrimaryKey( Integer orderID ) 23 throws RemoteException, FinderException; 24 25 // 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

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

55  2002 Prentice Hall. All rights reserved. Outline Fig. 19.17 OrderModel class for serializing Order data. Lines 19-22 1 // 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.*; 11 12 // third-party packages 13 import org.w3c.dom.*; 14 15 public class OrderModel implements Serializable, 16 XMLGenerator { 17 18 // OrderModel properties 19 private Integer orderID; 20 private Date orderDate; 21 private boolean shipped; 22 private Collection orderProductModels; 23 24 // construct empty OrderModel 25 public OrderModel() 26 { 27 orderProductModels = new ArrayList(); 28 } 29 30 // set order ID 31 public void setOrderID( Integer id ) 32 { 33 orderID = id; 34 } 35 have associated set and get methods

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

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

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

59  2002 Prentice Hall. All rights reserved. Outline Fig. 19.17 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" ) ); 144 145 order.appendChild( temp ); 146 147 // create orderProducts Element 148 Element orderProducts = 149 document.createElement( "orderProducts" ); 150 151 Iterator iterator = getOrderProductModels().iterator(); 152 153 // add orderProduct element for each OrderProduct 154 while ( iterator.hasNext() ) { 155 OrderProductModel orderProductModel = 156 ( OrderProductModel ) iterator.next(); 157 158 orderProducts.appendChild( 159 orderProductModel.getXML( document ) ); 160 } 161 162 order.appendChild( orderProducts ); 163 164 return order; 165 166 } // end method getXML 167 }

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

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

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

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

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

65  2002 Prentice Hall. All rights reserved. Outline Fig. 19.21 OrderProduct remote interface for modifying details of OrderProduct EJB instances. Lines 18-19 1 // 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.*; 11 12 // Deitel packages 13 import com.deitel.advjhtp1.bookstore.model.*; 14 15 public interface OrderProduct extends EJBObject { 16 17 // 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.

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

67  2002 Prentice Hall. All rights reserved. Outline Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface. Lines 22-24 Lines 27-69 1 // 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; 14 15 // Deitel packages 16 import com.deitel.advjhtp1.bookstore.model.*; 17 18 public class OrderProductEJB implements EntityBean { 19 private EntityContext entityContext; 20 21 // container-managed fields 22 public String ISBN; 23 public Integer orderID; 24 public int quantity; 25 26 // get OrderProduct details as OrderProductModel 27 public OrderProductModel getOrderProductModel() 28 throws EJBException 29 { 30 OrderProductModel model = new OrderProductModel(); 31 32 // 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.

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

69  2002 Prentice Hall. All rights reserved. Outline Fig. 19.22 OrderProductEJB implementation of OrderProduct remote interface. Lines 72-75 Lines 80-84 Line 82 71 // 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 } 78 79 // create OrderProduct for given OrderProductModel 80 public OrderProductPK ejbCreate( OrderProductModel model ) 81 { 82 setOrderProductModel( model ); 83 return null; 84 } 85 86 // perform any necessary post-creation tasks 87 public void ejbPostCreate( OrderProductModel model ) {} 88 89 // set EntityContext 90 public void setEntityContext( EntityContext context ) 91 { 92 entityContext = context; 93 } 94 95 // unset EntityContext 96 public void unsetEntityContext() 97 { 98 entityContext = null; 99 } 100 101 // 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.

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

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

72  2002 Prentice Hall. All rights reserved. Outline Fig. 19.23 OrderProductHome interface for finding and creating OrderProduct EJB instances. Lines 19-20 Lines 23-24 Lines 27-28 1 // 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.*; 12 13 // Deitel packages 14 import com.deitel.advjhtp1.bookstore.model.*; 15 16 public interface OrderProductHome extends EJBHome { 17 18 // create OrderProduct using given OrderProductModel 19 public OrderProduct create( OrderProductModel model ) 20 throws RemoteException, CreateException; 21 22 // find OrderProduct for given orderID 23 public Collection findByOrderID( Integer orderID ) 24 throws RemoteException, FinderException; 25 26 // 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.

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

74  2002 Prentice Hall. All rights reserved. Outline Fig. 19.23 OrderProductPK primary-key class for OrderProduct EJB. Lines 12-13 1 // 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 { 10 11 // primary-key fields 12 public String ISBN; 13 public Integer orderID; 14 15 // no-argument constructor 16 public OrderProductPK() {} 17 18 // construct OrderProductPK with ISBN and orderID 19 public OrderProductPK( String isbn, Integer id ) 20 { 21 ISBN = isbn; 22 orderID = id; 23 } 24 25 // get ISBN 26 public String getISBN() 27 { 28 return ISBN; 29 } 30 31 // 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.

75  2002 Prentice Hall. All rights reserved. Outline Fig. 19.23 OrderProductPK primary-key class for OrderProduct EJB. Lines 38-41 Lines 44-57 36 37 // calculate hashCode for this Object 38 public int hashCode() 39 { 40 return getISBN().hashCode() ^ getOrderID().intValue(); 41 } 42 43 // 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; 50 51 // compare ISBNs and orderIDs 52 return ( getISBN().equals( otherKey.getISBN() ) 53 && getOrderID().equals( otherKey.getOrderID() ) ); 54 } 55 56 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.

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

77  2002 Prentice Hall. All rights reserved. Outline Fig. 19.25 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.*; 10 11 // third-party packages 12 import org.w3c.dom.*; 13 14 public class OrderProductModel implements Serializable, 15 XMLGenerator { 16 17 // OrderProductModel properties 18 private ProductModel productModel; 19 private int quantity; 20 private Integer orderID; 21 22 // set ProductModel 23 public void setProductModel( ProductModel model ) 24 { 25 productModel = model; 26 } 27 28 // get ProductModel 29 public ProductModel getProductModel() 30 { 31 return productModel; 32 } 33

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

79  2002 Prentice Hall. All rights reserved. Outline Fig. 19.25 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 ); 74 75 return orderProduct; 76 } 77 }

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

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

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

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


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

Similar presentations


Ads by Google