// EmployeeClientTest.java

package cmpapp;

import java.io.*;
import java.util.*;
import java.rmi.RemoteException;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

/*
 * A simple client for accessing an CMP-EJB.
 */

public class EmployeeClientTest
{
  public void runtest()
  {
    Employee rec;
    Collection recs, orgs;
    Iterator iterator;

    try { 
      Context context = new InitialContext();
      Object homeObject = context.lookup("java:comp/env/EmployeeBean");  
      EmployeeHome home = (EmployeeHome)  
          PortableRemoteObject.narrow(homeObject, EmployeeHome.class); 
   
      // before anything record original state of table
      orgs = home.findAll();

      Integer i1 = new Integer(1); Integer i2 = new Integer(2);
      EmployeePK pk1 = new EmployeePK(i1); EmployeePK pk2 = new EmployeePK(i2);
      String s1 = "SCOTT"; String s2 = "SMITH";
      Float f1 = new Float(1.0); Float f2 = new Float(2.0);

      // add SCOTT
      System.out.println("Adding SCOTT ...");
      rec = (Employee) PortableRemoteObject.narrow(home.create(i1, s1, f1), Employee.class); 

      // add SCOTT again to confirm you get duplicate exception
      System.out.println("Adding SCOTT again ...");
      try {
	rec = (Employee) PortableRemoteObject.narrow(home.create(i1, s1, f1), Employee.class); 
      } catch(CreateException e) {
	//if (e.getMessage().equals("Entity already exists"))
	  System.out.println("->Expected exception: Entity already exists");
	//else throw e;
      }

      // edit to change SCOTT to BILLY
      System.out.println("Editing to replace SCOTT with BILLY ...");
      rec = (Employee)PortableRemoteObject.narrow(home.findByPrimaryKey(pk1),Employee.class);
      rec.setEmpName("BILLY");

      // find BILLY by name (aka BILL, MAC or BUDDY)
      System.out.println("Find BILLY ...");  
      recs = home.findByName("BILLY");
      iterator = recs.iterator();
      rec = (Employee) PortableRemoteObject.narrow(iterator.next(), Employee.class);
      System.out.println(">\t" + rec.getEmpNo() + "\t" + rec.getEmpName() + "\t" + rec.getSalary()); 

      // delete SCOTT
      System.out.println("Deleting SCOTT ...");
      home.remove(pk1);       

      // verify we're back were we started
      System.out.println("Verifying original state ...");
      recs = home.findAll();
      if (orgs.size() == recs.size())
      {
	System.out.println("->Verify okay.");
      } else {
	System.out.println("->Verify failed.");
      }

      iterator = orgs.iterator();
      while (iterator.hasNext()) {
	rec = (Employee) PortableRemoteObject.narrow(iterator.next(), Employee.class);
	System.out.println("<\t" + rec.getEmpNo() + "\t" + rec.getEmpName() + "\t" + rec.getSalary());
      }

      //separator
      System.out.println("--");

      iterator = recs.iterator();
      while (iterator.hasNext()) {
	rec = (Employee) PortableRemoteObject.narrow(iterator.next(), Employee.class);
	System.out.println(">\t" + rec.getEmpNo() + "\t" + rec.getEmpName() + "\t" + rec.getSalary());
      }

    } catch(NumberFormatException e) {
      System.err.println("NumberFormatException: " + e.getMessage());
    } catch(RemoteException e) {
      System.err.println("RemoteException: " + e.getMessage());
    } catch(NamingException e) {
      System.err.println("NamingException: " + e.getMessage());
    } catch(CreateException e) {
      System.err.println("CreateException: " + e.getMessage());
    } catch(RemoveException e) {
      System.err.println("RemoveException: " + e.getMessage());
    } catch(FinderException e) {
      System.err.println("FinderException: " + e.getMessage());
    }
  } 
}
