Implementing the Enterprise Bean Classes

Listing 12.7 shows the OrderEJB bean class.

Listing 12.7. The Full Text of day12/OrderEJB.java
package day12;
import java.util.*;
import java.io.*;
import java.rmi.*;
import javax.naming.*;
import javax.ejb.*;
public abstract class OrderEJB implements EntityBean {
protected EntityContext ctx;
/* get and set methods for cmp fields */
public abstract String getOrderId();
public abstract void setOrderId(String orderId);
public abstract String getStudentId();
public abstract void setStudentId(String studentid);
public abstract java.sql.Timestamp getOrderDate();
public abstract void setOrderDate(java.sql.Timestamp timestamp);
public abstract String getStatus();
public abstract void setStatus(String status);
public abstract double getAmount();
public abstract void setAmount(double amount);
/* get and set methods for relationship fields */
public abstract Collection getLineItems();
public abstract void setLineItems(Collection lineItems);
/* business methods */
public void addLineItem(String courseId, double fee) {
   try {
      Context ctx = new InitialContext();
      OrderLineItemLocalHome home = (OrderLineItemLocalHome)
         ctx.lookup("day12/OrderLineItemLocal");
      String lineItemId = getUniqueId();
      OrderLineItemLocal item =
         home.create(lineItemId, courseId, fee) ;
      getLineItems().add(item);
   } catch(Exception e) {
      throw new EJBException("Error adding line item:", e);
   }
}
public Collection getOrderLineItems() {
    Vector clientLineItems = new Vector();
    Collection lineitems = getLineItems();
    java.util.Iterator iterator = lineitems.iterator();
    ClientLineItem item;
    while (iterator.hasNext()) {
       OrderLineItemLocal litem = (OrderLineItemLocal)iterator.next();
       item = new ClientLineItem(litem.getOrderLineItemId(),
                                 litem.getCourseId(), litem.getFee());
       clientLineItems.add(item);
    }
    return clientLineItems;
 }
/* Callback methods */
public void setEntityContext(EntityContext ctx) {
   this.ctx = ctx;
}
public void unsetEntityContext() {
    this.ctx = null;
}
 public void ejbActivate() {}
 public void ejbPassivate() {}
 public void ejbStore() {}
 public void ejbLoad() {}
 public void ejbRemove() throws RemoveException {
    print("Removing Order id:" + (String)ctx.getPrimaryKey() );
 }
 public String ejbCreate(String studentId,
    String status, double amount) throws CreateException {
    String orderId = getUniqueId();
    setOrderId(orderId);
    setStudentId(studentId);
    setStatus(status);
    setAmount(amount);
    setOrderDate(new java.sql.Timestamp(System.currentTimeMillis()));
    print("Creating Order id:" + orderId );
    return null;
 }
 public void ejbPostCreate(String studentId,
   String courseId, double amount) throws CreateException {}
 String getUniqueId(){
    return new Long(System.currentTimeMillis()).toString();
 }
 void print(String s) {
    System.out.println(s);
 }
}

The OrderEJB entity bean implements the javax.ejb.EntityBean interface and is defined as an abstract class. It consists of abstract accessor methods for the persistent fields orderId, studentId, orderDate, status, and amount, and the container-managed persistent field, lineItems. The addLineItem() business method creates a new line item and adds it to the persistent managed relationship. The getOrderLineItems() business method retrieves the line items in this order using getLineItems() method, and creates a collection of ClientLineItem objects. This method makes a view of the line items that are in this order available to the client. It implements the methods setEntityContext(), unsetEntityContext(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), and ejbRemove() as defined in the javax.ejb.EntityBean interface. The ejbCreate() method initializes the entity bean instance by assigning the input arguments to the persistent fields. The ejbCreate() method returns null. The bean implements the ejbPostCreate() method that corresponds to the ejbCreate() method.

Listing 12.8 shows the OrderLineItemEJB bean class.

Listing 12.8. The Full Text of day12/OrderLineItemEJB.java
package day12;
import javax.naming.*;
import javax.ejb.*;
import java.util.*;
public abstract class OrderLineItemEJB implements EntityBean {
 protected EntityContext ctx;
 /* get and set methods for cmp fields */
 public OrderLineItemEJB() {}
 public abstract String getOrderLineItemId();
 public abstract void setOrderLineItemId(String id);
 public abstract String getCourseId();
 public abstract void setCourseId(String courseId);
 public abstract double getFee();
 public abstract void setFee(double fee);
 /* get and set methods for relationship fields */
 public abstract OrderLocal getOrder();
 public abstract void setOrder(OrderLocal order);
 /* Callback methods */
 public void setEntityContext(EntityContext ctx) {
    this.ctx = ctx;
 }
 public void unsetEntityContext() {
    this.ctx = null;
 }
 public void ejbActivate() {}
 public void ejbPassivate() {}
 public void ejbStore() {}
 public void ejbLoad() {}
 public String ejbCreate(String orderLineItemId, String courseId,
               double fee) throws CreateException {
    setOrderLineItemId(orderLineItemId);
    setCourseId(courseId);
    setFee(fee);
    print("Creating OrderLineItem id:" + orderLineItemId );
    return null;
 }
 public void ejbPostCreate(String orderLineItemId, String  courseId,
                        double fee) throws CreateException {}
 public void ejbRemove() {
    print("Removing OrderLineItem id:" + (String)ctx.getPrimaryKey() );
 }
 void print(String s) {
    System.out.println(s);
 }
}

The OrderLineItemEJB entity bean implements the javax.ejb.EntityBean interface and is defined as an abstract class. It consists of abstract accessor methods for the persistent fields orderLineItemId, courseId, and fee, and the container-managed persistent field, order. It implements the methods setEntityContext(), unsetEntityContext(), ejbActivate(), ejbPassivate(), ejbLoad(), ejbStore(), and ejbRemove() as defined in the javax.ejb.EntityBean interface. The ejbCreate() method initializes the entity bean instance by assigning the input arguments to the persistent fields. The ejbCreate() method returns null. The bean implements the ejbPostCreate() method that corresponds to the ejbCreate() method.

ClientLineItem is a value class that is used in the client view. Listing 12.9 shows the ClientLineItem class.

Listing 12.9. The Full Text of day12/ClientLineItem.java
package day12;
public class ClientLineItem implements java.io.Serializable {
   private String orderLineItemId;
   private String courseId;
   private double fee;
   public ClientLineItem(String orderLineItemId,
                    String courseId, double fee) {
      this.orderLineItemId = orderLineItemId;
      this.courseId = courseId;
      this.fee = fee;
   }
   public String getOrderLineItemId() {
      return orderLineItemId ;
   }
   public double getFee() {
      return fee;
   }
   public String getCourseId() {
      return courseId;
   }
}

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.142.2