// DeptClient.java

package hr;

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 EJB.
 */

public class DeptClient
{
    private static Context nctx = null;

    public static final String hrBean = "java:comp/env/ejb/Hr";
    public static HrRemoteHome hrHome = null;
    public static HrRemote hrRemote = null;

    public static void main(String[] args)
    {
        //run interactive client
        new DeptClient().run();
    }

    public void run()
    {
        System.out.println("DeptClient.main(): client started...");
        try {
            nctx = new InitialContext();

            hrHome = (HrRemoteHome)PortableRemoteObject.narrow(nctx.lookup(hrBean),
                                                               HrRemoteHome.class);
            if(hrHome != null)
                System.out.println("DeptClient.main(): got hrHome");

            hrRemote = hrHome.create();
            if(hrRemote != null)
                System.out.println("DeptClient.main(): got hrRemote");

            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

            String action;
            while (true) {
                System.out.print("[L]ist| [A]dd | [E]dit | [D]elete | | [J]oin | [Q]uit: ");
                System.out.flush();
                action = in.readLine();
                if (action.length() == 0 || action.toUpperCase().charAt(0) == 'Q') break;
                Dept dept;
                Integer deptNo;
                String deptName;
                Emp emp;
                Collection depts, emps;
                Iterator iterator;
                int i;

                switch (action.toUpperCase().charAt(0)) {
                case 'A':
                    System.out.print("Enter integer value for deptNo: ");
                    System.out.flush();
                    deptNo = new Integer(in.readLine());
      
                    System.out.print("Enter string value for deptName: ");
                    System.out.flush();
                    deptName = in.readLine();
      
                    dept = new Dept(deptNo, deptName);
                    hrRemote.addDept(dept);

                    System.out.println("DeptClient.main(): created dept " + deptNo);
                    break;

                case 'L':
                    depts = hrRemote.getDepts();
                    iterator = depts.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        dept = (Dept)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + dept.getDeptNo()
                                           + " " + dept.getDeptName());
                    }
                    System.out.println("DeptClient.main(): found " + depts.size());
                    break;
	
                case 'D':
                    System.out.print("Enter integer value for deptNo: ");
                    System.out.flush();
                    deptNo = new Integer(in.readLine());

                    dept = new Dept(deptNo, null);
                    hrRemote.removeDept(dept);
                    System.out.println("DeptClient.main(): removed " + deptNo);
                    break;

                case 'J':
                    System.out.print("Enter integer value for deptNo: ");
                    System.out.flush();
                    deptNo = new Integer(in.readLine());
      
                    dept = new Dept(deptNo, null);
                    emps = hrRemote.getDeptEmployees(dept);
                    iterator = emps.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        emp = (Emp)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + emp.getEmpNo()
                                           + " " + emp.getEmpName());
                    }
                    System.out.println("DeptClient.main(): found " + emps.size());
                    break;

                case 'E':
                    System.out.print("Enter integer value for deptNo: ");
                    System.out.flush();
                    deptNo = new Integer(in.readLine());

                    System.out.print("Enter string value for deptName: ");
                    System.out.flush();
                    deptName = in.readLine();
      
                    dept = new Dept(deptNo, deptName);
                    hrRemote.setDept(dept);
                    System.out.println("DeptClient.main(): edited " + deptNo);
                    break;

                default:
                    continue;
                }
            }
        } catch(NumberFormatException e) {
            System.err.println("NumberFormatException: " + e.getMessage());
        } catch(RemoteException e) {
            System.err.println("RemoteException: " + e.getMessage());
            e.printStackTrace();
        } 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());
        } catch(NullPointerException e) {
            System.err.println("NullPointerException: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
