package ejava.examples.ejb.session.client;

import ejava.examples.ejb.session.bean.TellerRemoteHome;
import ejava.examples.ejb.session.bean.TellerRemote;
import ejava.examples.ejb.session.Account;
import ejava.examples.ejb.session.BankException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;
import java.rmi.RemoteException;
import java.util.Hashtable;

public class TellerAccessor {
   TellerRemote teller_;

   public TellerAccessor() throws Exception {
      this(new InitialContext(), "java:comp/env/ejb/TellerRemoteHome");
   }

   public TellerAccessor(Context jndi, String name) throws Exception {
      Object object = jndi.lookup(name);
      TellerRemoteHome tHome = (TellerRemoteHome)PortableRemoteObject.narrow(
         object, TellerRemoteHome.class);
      teller_ = tHome.create();
   }
   public void close() {
       try {
          if (teller_ != null) teller_.remove();
       }
       catch (Exception ignored) {}
   }

   public Account getAccount(int accountId)
        throws RemoteException, BankException {
      return teller_.getAccount(accountId);
   }
   public Account deposit(int accountId, double amount)
        throws RemoteException, BankException {
      teller_.deposit(accountId, amount);
      return teller_.getAccount(accountId);
   }
   public Account withdraw(int accountId, double amount)
        throws RemoteException, BankException {
      teller_.withdraw(accountId, amount);
      return teller_.getAccount(accountId);
   }
   public void transfer(int fromId, int toId, double amount)
        throws RemoteException, BankException {
      teller_.transfer(fromId, toId, amount);
   }


   public static final void main(String[] args) throws Exception {
      String jndiUrl = "t3://localhost:7001";
      String jndiClass = "weblogic.jndi.WLInitialContextFactory";
      String name = "ejava.examples.ejb.session.TellerRemoteHome";
      String user = null;
      String password = null;
      String command = null;
      Integer id = null;
      Integer toId = null;
      Integer fromId = null;
      Double amount = null;

      for(int i=0; i<args.length; i++) {
         if (args[i].equals("-jndiClass")) jndiClass = args[++i];
         if (args[i].equals("-jndiUrl")) jndiUrl = args[++i];
         if (args[i].equals("-name")) name = args[++i];
         if (args[i].equals("-user")) user = args[++i];
         if (args[i].equals("-password")) password = args[++i];
         if (args[i].equals("-command")) command = args[++i];
         if (args[i].equals("-amount")) amount = new Double(args[++i]);
         if (args[i].equals("-account")) id = new Integer(args[++i]);
         if (args[i].equals("-to")) toId = new Integer(args[++i]);
         if (args[i].equals("-from")) fromId = new Integer(args[++i]);
      }
      Hashtable props = new Hashtable();
      props.put(Context.INITIAL_CONTEXT_FACTORY, jndiClass);
      props.put(Context.PROVIDER_URL, jndiUrl);
      if (user != null) props.put(Context.SECURITY_PRINCIPAL, user);
      if (password != null) props.put(Context.SECURITY_CREDENTIALS, password);
      Context jndi = new InitialContext(props);

      if (command != null) {
         TellerAccessor teller = new TellerAccessor(jndi, name);
         if (command.equals("getAccount")) {
            Account account = teller.getAccount(id.intValue());
            System.out.println("account=" + account);
         }
         else if (command.equals("deposit")) {
            Account account=teller.deposit(id.intValue(),amount.doubleValue());
            System.out.println("deposit " + amount + " to account:" + account);
         }
         else if (command.equals("withdraw")) {
            Account account=teller.withdraw(id.intValue(),amount.doubleValue());
            System.out.println("withdraw " + amount + " to account:" + account);
         }
         else if (command.equals("transfer")) {
            teller.transfer(
                fromId.intValue(), toId.intValue(), amount.doubleValue());
            System.out.println("transfer complete");
         }
         teller.close();
      }
   }
}

