// EmployeeClient.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 EmployeeClient
{
  public static void main(String[] args)
  {
    if (args.length != 0 && args[0].equals("test")) {
      // invoke automated client instead
      new EmployeeClientTest().runtest();  
      System.exit(0); 
    }

    System.out.println("EmployeeClient.main(): client started..."); 
    try {
      /**
       * Create access to the naming context.
       */
      Context context = new InitialContext();
      
      /**
       * Lookup the EmployeeHome object. The reference is retrieved from the
       * application-local context (java:comp/env). The variable is
       * specified in the assembly descriptor (META-INF/application-client.xml).
       */
      Object homeObject =
                 context.lookup("java:comp/env/EmployeeBean");
      System.out.println("EmployeeClient.main(): bean found...");
      // Narrow the reference to a EmployeeHome.
      EmployeeHome home =
	(EmployeeHome) PortableRemoteObject.narrow(homeObject,
                                                   EmployeeHome.class);
      System.out.println("EmployeeClient.main(): home narrowed...");
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
      String action;
      while (true) {
	System.out.print("[L]ist| [A]dd | [E]dit | [D]elete | [F]ind | [Q]uit: ");
	System.out.flush();
	action = in.readLine();
	if (action.length() == 0 || action.toUpperCase().charAt(0) == 'Q') break;
        Employee rec;
        Collection recs;
        Iterator iterator;
        Integer empNo;
        String empName;
        Float salary;
        int i;

	switch (action.toUpperCase().charAt(0)) {
	case 'A':
	  System.out.print("Enter integer value for empNo: ");
	  System.out.flush();
	  empNo = new Integer(in.readLine());
      
	  System.out.print("Enter string value for empName: ");
	  System.out.flush();
	  empName = in.readLine();
      
	  System.out.print("Enter float value for salary: ");
	  System.out.flush();
	  salary = new Float(in.readLine());
      
	  // Create a new record and narrow the reference.
	  rec =
	    (Employee) PortableRemoteObject.narrow(home.create(empNo, empName, salary),
						   Employee.class);
	  
	  System.out.println("EmployeeClient.main(): created " + empNo);
	  break;

	case 'L':
	  recs = home.findAll();
	  iterator = recs.iterator();
	  i = 0;
	  while(iterator.hasNext()) {
	    rec =
	      (Employee) PortableRemoteObject.narrow(iterator.next(),
						     Employee.class);
	    System.out.println("row-" + i++ + " " + rec.getEmpNo()
			       + " " + rec.getEmpName()
			       + " " + rec.getSalary());
	  }
	  System.out.println("EmployeeClient.main(): found " + recs.size());
	break;
	
	case 'F':
	  System.out.print("Enter string value for empName: ");
	  System.out.flush();
	  empName = in.readLine();

	  recs = home.findByName(empName);
	  iterator = recs.iterator();
	  i = 0;
	  while(iterator.hasNext()) {
	    rec =
	      (Employee) PortableRemoteObject.narrow(iterator.next(),
						     Employee.class);
	    System.out.println("row-" + i++ + " " + rec.getEmpNo()
			       + " " + rec.getEmpName()
			       + " " + rec.getSalary());
	  }
	  System.out.println("EmployeeClient.main(): found " + recs.size());
	break;

	case 'D':
	  System.out.print("Enter integer value for empNo: ");
	  System.out.flush();
	  empNo = new Integer(in.readLine());
	  home.remove(new EmployeePK(empNo));
	  System.out.println("EmployeeClient.main(): removed " + empNo);
	  break;
	case 'E':
	  System.out.print("Enter integer value for empNo: ");
	  System.out.flush();
	  empNo = new Integer(in.readLine());
	  System.out.print("Enter string value for empName: ");
	  System.out.flush();
	  empName = in.readLine();
      
	  System.out.print("Enter float value for salary: ");
	  System.out.flush();
	  salary = new Float(in.readLine());
      
	  rec =
	    (Employee) PortableRemoteObject.narrow(home.findByPrimaryKey
						   (new EmployeePK(empNo)),
						    Employee.class);
	  rec.setEmpName(empName);
	  rec.setSalary(salary);
	  System.out.println("EmployeeClient.main(): edited " + empNo);
	  break;
	default:
	  continue;
	}
      }
    } catch(NumberFormatException e) {
      System.err.println("NumberFormatException: " + e.getMessage());
    } catch(RemoteException e) {
      System.err.println("RemoteException: " + e.getMessage());
    } catch(IOException e) {
      System.err.println("IOException: " + 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());
    }
  }
}
