Tuesday, 12 May 2009

2-How to Develop Container-Managed-Relationships

FILE OrderARSLocalHome.java
package cmrex;

import java.util.*;
import java.rmi.*;
import javax.ejb.*;
public interface OrderARSLocalHome extends EJBLocalHome {
/* Create methods */
public OrderARSLocal create(String studentId,
String status, double amount) throws CreateException;
/* Finder methods */
public OrderARSLocal findByPrimaryKey(String key)
throws FinderException;
}

FILE OrderARSLocal.java
package cmrex;

import java.util.*;
import java.rmi.*;
import javax.ejb.*;
public interface OrderARSLocal extends EJBLocalObject {
public String getOrderId();
public String getStudentId();
public void setStudentId(String studentId);
public double getAmount();
public void setAmount(double amount);
public java.sql.Timestamp getOrderDate();
public void setOrderDate(java.sql.Timestamp date);
public String getStatus();
public void setStatus(String status);
public void addLineItem(String courseId, double fee);
public Collection getOrderLineItems();
}
**********************************************************
FILE OrderARSEJB.java
package cmrex;

import java.util.*;
import java.io.*;
import java.rmi.*;
import javax.naming.*;
import javax.ejb.*;
public abstract class OrderARSEJB 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();
OrderARSLineItemLocalHome home = (OrderARSLineItemLocalHome)
ctx.lookup("cmrex/OrderARSLineItemLocal");
String lineItemId = getUniqueId();
OrderARSLineItemLocal 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()) {
OrderARSLineItemLocal litem = (OrderARSLineItemLocal)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);
}
}