Implementing the Session Bean Class

Before you actually implement the business methods, you need to carry out some preparation.

First, you will add a variable named bookingHome, which will be instantiated with the entity bean QuickBookingBean. Whenever an instance of the QuickOrderProcessorBean is created, this variable will also be instantiated with the local home interface of the entity bean. For this reason, you will initialize the variable bookingHome in the method ejbCreate().

In addition, you will add some private auxiliary methods to the standard implementation of the bean class:




You use this method to generate a booking ID.


You need this method whenever the JavaBean object QuickBookingModel is to be filled with the data from the entity bean QuickBookingBean.


You need this method to get the corresponding date object from a string entered at the client end.


You have created the session bean QuickOrderProcessorBean.

The structure of your project QuickCarRentalEjb is currently displayed in the J2EE Explorer.


       1.      In the project structure, expand the QuickCarRentalEjb ® ejb-jar.xml node and double-click the name QuickOrderProcessorBean, if you have not already done so.

       2.      In the overview that appears, choose Navigate to Bean Class.

The Java Editor opens and displays the generated source code of the bean class QuickOrderProcessorBean.

       3.      Add the private variable bookingHome to the start of the body of the class:


public class QuickOrderProcessorBean implements SessionBean {


   private QuickBookingLocalHome bookingHome;


       4.      Then, at the end of the class body (after ejbCreate()), add the following implementations for the methods generateId(), getBookingModel(), and getDate():


    * Create Method.


   public void ejbCreate() throws CreateException {

       // TODO : Implement



   private String generateId() {

       Date temp = new Date();

       String stemp = String.valueOf(temp.hashCode());

       if (stemp.length() < 10)

          return stemp.substring(1, stemp.length());


          return stemp.substring(1, 9);



   private QuickBookingModel getBookingModel(QuickBookingLocal booking) {

      QuickBookingModel data = new QuickBookingModel();







       int days =

              (int) ((booking.getDateTo().getTime()

                 - booking.getDateFrom().getTime())

                 / (1000 * 60 * 60 * 24));


       data.setPrice(Float.toString(40*days) + " EUR");

       return data;



   private Date getDate(java.lang.String dateString) throws QuickCarRentalException{



        return Constants.FORMATTER.parse(dateString);


       catch(java.text.ParseException pe){


          throw new QuickCarRentalException("The date has to be in format dd.MM.yyyy, for example 21.04.2004");




Make sure that the method used here, generateId(), is not used to implement an ID in a live system. In this case, we are simply simulating generating an ID. In a live system,  always use the correct ID generation process.

       5.      If necessary, correct the formatting of these lines by choosing Source ® Format from the context menu.

       6.      To add the import statements, position the cursor anywhere in the Java Editor and choose Source ® Organize Imports from the context menu.

Since the class Date is available twice, you will now be asked which package you want to use for Date.

       7.      Choose the package java.util and confirm by choosing Finish.

       8.      If you are asked to choose between different Constants classes, select

The following import statements are added to the source code:



import java.util.Date;


import javax.ejb.CreateException;

import javax.ejb.SessionBean;

import javax.ejb.SessionContext;






The Java Editor no longer displays any errors.

       9.      Navigate to the implementation of the method ejbCreate() and replace  // TODO : Implement with the following code:


       try {

          Context ctx = new InitialContext();

          bookingHome =

              (QuickBookingLocalHome) ctx.lookup(



       } catch (NamingException e) {

          throw new CreateException(e.getMessage());



   10.      If necessary, correct the formatting of these new lines of code.

   11.      To add the appropriate import statements, choose Source ® Organize Imports from the context menu.

Since the class Context occurs twice, you will now be asked which package you want to use for Context.

   12.      Choose the package javax.naming.Contextand confirm by choosing Finish.

The appropriate import statements are added to the source code.

   13.      Save the contents of the editor by choosing the appropriate icon from the toolbar.


The Developer Studio updates and compiles your project sources. (Note: Compilation only occurs if you are using the Workbench standard settings.) Your Tasks view should no longer show any other errors.

Next step:

Throwing the Exception QuickCarRentalException