Writing a Client

Listing 12.15 shows a client that is used to test the order-line item relationship.

Listing 12.15. The Full Text of day12/Client.java
package day12;
import java.util.*;
import javax.naming.*;
import javax.ejb.*;
public class Client {
public static void main(String[] args) {
   print("Starting Client . . .
");
   Context initialContext = null;
   OrderHome orderHome = null;
   Order order = null;
   try {
      print("Looking up the order home via JNDI.
");
      initialContext = new InitialContext();
      Object object = initialContext.lookup("day12/Order");
      orderHome = (OrderHome)
       javax.rmi.PortableRemoteObject.narrow(object,OrderHome.class);
      object = (Order)orderHome.create("1", "Submitted", 100);
      order = (Order)
       javax.rmi.PortableRemoteObject.narrow(object,Order.class);
      String orderId = order.getOrderId();
      print("Created a new order:" + orderId + " .
");
      print("Adding some line items to the order.
");
      order.addLineItem("1", 200);
      order.addLineItem("2", 300);
      order.addLineItem("3", 300);
      print("Retrieving line items of the order.
");
      Collection collection = order.getOrderLineItems();
      Iterator it = collection.iterator();
      while (it.hasNext()) {
     ClientLineItem item =(ClientLineItem)javax.rmi.PortableRemoteObject.narrow
                   (it.next(), ClientLineItem.class);
        print("Item id:" + item.getOrderLineItemId() +
               " Course id:" + item.getCourseId() +
               " Fee:" + item.getFee()
            );
      }
      print("Removing order:" + orderId + " .
");
      order.remove();
   } catch ( Exception e) {
      e.printStackTrace();
   }
 }
 static void print(String s) {
   System.out.println(s);
 }
}

The client locates the OrderHome home interface of the deployed enterprise bean via JNDI. The client uses the remote home interface to create an Order entity object. The client adds three line items to the order using the addLineItem() method and then retrieves the orders using the getOrderLineItems() method of the Order interface. The client finally removes the order. Because the cascade-delete element is specified on the line items' side of the Order-LineItems relationship, this should trigger the removal of line items that are attached to the order.

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

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