package ejava.examples.personnel.ejbclient;

import ejava.examples.personnel.ejb20.PersonRemoteHome;
import ejava.examples.personnel.ejb20.PersonRemote;
import ejava.examples.personnel.ejb20.PersonPK;
import ejava.examples.personnel.Person;


import javax.ejb.FinderException;
import javax.ejb.CreateException;
import javax.ejb.RemoveException;
import javax.naming.InitialContext;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;

import java.util.Hashtable;
import java.util.Set;
import java.util.Collection;
import java.util.Iterator;

import java.io.Serializable;

public class PersonAccessor implements Serializable {

   public PersonAccessor() {}

   public Collection list() 
      throws NamingException, RemoteException, FinderException {
      PersonRemoteHome rHome = getRemoteHome();
      return rHome.findAll();
   }

   public Set listPhoneNumbers() 
      throws NamingException, RemoteException, FinderException {
      PersonRemoteHome rHome = getRemoteHome();
      return rHome.getPhoneNumbers();
   }

   public Collection findByAddress(String address) 
      throws NamingException, RemoteException, FinderException {
      PersonRemoteHome rHome = getRemoteHome();
      return rHome.findByAddress(address);
   }

   public void insert(Person p)
      throws NamingException, RemoteException, CreateException {

      PersonRemoteHome rHome = getRemoteHome();
      rHome.create(p);
   }
   
   public void remove(String id)
      throws NamingException, RemoteException, RemoveException {

      PersonRemoteHome rHome = getRemoteHome();
      rHome.remove(new PersonPK(id));
   }

   protected PersonRemoteHome getRemoteHome() 
      throws NamingException {

      Context ctx = getInitialContext();
      Object object = ctx.lookup(getName());
      PersonRemoteHome home = (PersonRemoteHome)
         PortableRemoteObject.narrow(object, PersonRemoteHome.class);

      return home;
   }

   protected String getName() { return "java:comp/env/PersonRemoteHome"; }

   protected Context getInitialContext() throws NamingException {
      return new InitialContext();
   }

   public static void main(String args[]) throws Exception {
      
      String url = "t3://localhost:7001";
      String driver = "weblogic.jndi.WLInitialContextFactory";
      String user = null;
      String password = null;
      String name = "personnel/PersonRemoteHome";
      String command = "list";
      Person insertPerson=null;
      String removeId=null;
      String findAddress=null;
      for(int i=0; i<args.length; i++) {
         if (args[i].equals("-url"))      { url=args[++i]; }
         if (args[i].equals("-driver"))   { driver=args[++i]; }
         if (args[i].equals("-user"))     { user=args[++i]; }
         if (args[i].equals("-password")) { password=args[++i]; }
         if (args[i].equals("-name"))     { name=args[++i]; }
         if (args[i].equals("-insert"))   { 
	    String id=args[++i];
	    String fName=args[++i];
	    String lName=args[++i];
	    String address=args[++i];
	    String pNumber=args[++i];
	    insertPerson = new Person(id, fName, lName, address, pNumber);
	    command="insert";
	 }
         else if (args[i].equals("-remove"))   { 
	    removeId=args[++i];
	    command="remove";
	 }
         else if (args[i].equals("-address"))   { 
	    findAddress=args[++i];
	    command="address";
	 }
         else if (args[i].equals("-phonelist"))   { 
	    command="phonelist";
	 }
      }

      final Hashtable props = new Hashtable();
      props.put(Context.PROVIDER_URL, url);
      props.put(Context.INITIAL_CONTEXT_FACTORY, driver);
      if (user != null) { 
         props.put(Context.SECURITY_PRINCIPAL, user); 
      }
      if (password != null) { 
         props.put(Context.SECURITY_CREDENTIALS, password); 
      }

      final String contextName = name;
      PersonAccessor accessor = new PersonAccessor() {
         protected Context getInitialContext() throws NamingException {
            return new InitialContext(props);
	 }
         protected String getName() { return contextName; }
      };

      try {
         if (command.equals("list")) {
            Collection people = accessor.list();
            for(Iterator i=people.iterator(); i.hasNext(); ) {
               PersonRemote p = (PersonRemote)i.next();
	       System.out.println(p.getValues());
            }
         }
         else if (command.equals("remove")) {
            accessor.remove(removeId);
         }
         else if (command.equals("insert")) {
            accessor.insert(insertPerson);
         }
         else if (command.equals("address")) {
            Collection people = accessor.findByAddress(findAddress);
            for(Iterator i=people.iterator(); i.hasNext(); ) {
               PersonRemote p = (PersonRemote)i.next();
	       System.out.println(p.getValues());
            }
         }
         else if (command.equals("phonelist")) {
            Collection numbers = accessor.listPhoneNumbers();
            for(Iterator i=numbers.iterator(); i.hasNext(); ) {
               String number = (String)i.next();
	       System.out.println(number);
            }
         }
      }
      catch (Exception ex) {
         System.out.println(command + "failed:" + ex.getMessage());
      }


   }

}
