package ejava.ejb.personnel.client;

import ejava.ejb.personnel.RegistrarHome;
import ejava.ejb.personnel.Registrar;
import ejava.ejb.personnel.Person;
import javax.ejb.FinderException;
import javax.ejb.CreateException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Properties;
import java.util.Collection;
import java.util.Iterator;

public class RegistrarClient extends PersonClient {
   private static final String USAGE =
      "java ejava.ejb.personnel.RegistrarClient " +
         "[-user <username>] " +
	 "[-password <password>] " +
	 "<-add | -list>";

   public RegistrarClient(String user, String password) {
      super(user, password);
   }

   public void listPersonnel() 
      throws NamingException, RemoteException, CreateException {

      RegistrarHome rHome = getRegistrarHome();
      Registrar registrar = rHome.create();

      String people = registrar.listPersonnelByName("","");
      System.out.println(people);
   }

   public void addPerson(
      String firstName, String lastName,
      String address, String phoneNumber) 
      throws NamingException, RemoteException, CreateException {

      RegistrarHome rHome = getRegistrarHome();
      Registrar registrar = rHome.create();

      Person newPerson = registrar.add(firstName, lastName,
                                      address, phoneNumber);
   }

   protected RegistrarHome getRegistrarHome() 
      throws NamingException {

      Context ctx = getInitialContext();
      Object object = ctx.lookup("RegistrarHome");
      RegistrarHome rHome = (RegistrarHome)
         PortableRemoteObject.narrow(object, RegistrarHome.class);

      return rHome;
   }

   public static void main(String args[]) {
      String user = null;
      String password = null;
      String option = "list";
      String id = null;
      String firstName = null;
      String lastName = null;
      String address = null;
      String phoneNumber = null;

      for(int i=0; i<args.length; i++) {
         if (args[i].equals("-user")) {
	    user = args[i+1];
	 }
	 else if (args[i].equals("-password")) {
	    password = args[i+1];
	 }
	 else if (args[i].equals("-list")) {
	    option = "list";
	 }
	 else if (args[i].equals("-add")) {
	    option = "add";
	    firstName = args[i+1];
	    lastName = args[i+2];
	    address = args[i+3];
	    phoneNumber = args[i+4];
	 }
      }

      try {
         RegistrarClient client = new RegistrarClient(user, password);
         if (option.equals("list")) {
	    System.out.println("listing Persons...");
	    client.listPersonnel();
         }
	 else if (option.equals("add")) {
	    System.out.println("adding Person...");
	    client.addPerson(firstName, lastName,
	                     address, phoneNumber);
	 }
	 else {
	    System.out.println("unknown option");
	 }
      }
      catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}
