// EmpClient.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 EmpClient
{
    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 EmpClient().run();
    }

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

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

            hrRemote = hrHome.create();
            if(hrRemote != null)
                System.out.println("EmpClient.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;
                //Employee
                Emp emp, mgr;
                Integer empNo, mgrNo;
                String empName;
                Float salary;
                Collection emps, directs;

                //Department
                Dept dept;
                Integer deptNo;

                //Project
                Project project;
                Integer projectNo;
                Collection projects;

                //Address
                Address address;
                String street, city, state, zip;

                //Phone
                Phone phone;
                String phoneNo, phoneType;
                Collection phones;

                //generic utility
                Iterator iterator;
                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());
      
                    emp = new Emp(empNo, empName, salary);
                    hrRemote.addEmp(emp);

                    System.out.println("EmpClient.main(): created emp " + empNo);
                    break;

                case 'L':
                    emps = hrRemote.getEmps();
                    iterator = emps.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        emp = (Emp)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + emp.getEmpNo()
                                           + " " + emp.getEmpName()
                                           + " " + emp.getSalary());
                    }
                    System.out.println("EmpClient.main(): found " + emps.size());
                    break;
	
                case 'D':
                    System.out.print("Enter integer value for empNo: ");
                    System.out.flush();
                    empNo = new Integer(in.readLine());

                    emp = new Emp(empNo, null, null);
                    hrRemote.removeEmp(emp);
                    System.out.println("EmpClient.main(): removed " + empNo);
                    break;

                case 'J':
                    System.out.print("Enter integer value for empNo: ");
                    System.out.flush();
                    empNo = new Integer(in.readLine());
                    emp = new Emp(empNo, null, null);

                    address = hrRemote.getEmpAddress(emp);
                    if(address == null)
                        System.out.println("EmpClient.main(): address = null");
                    else
                        System.out.println("EmpClient.main(): address ="
                                           + " " + address.getAddress());

                    phones = hrRemote.getEmpPhones(emp);
                    System.out.println("EmpClient.main(): phones = " + phones.size());
                    iterator = phones.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        phone = (Phone)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + phone.getPhone());
                    }

                    dept = hrRemote.getEmpDepartment(emp);
                    if(dept == null)
                        System.out.println("EmpClient.main(): dept = null");
                    else
                        System.out.println("EmpClient.main(): dept ="
                                           + " " + dept.getDeptNo()
                                           + " " + dept.getDeptName());

                    projects = hrRemote.getEmpProjects(emp);
                    System.out.println("EmpClient.main(): projects = " + projects.size());
                    iterator = projects.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        project = (Project)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + project.getProjectNo()
                                           + " " + project.getProjectName());
                    }

                    mgr = hrRemote.getEmpManager(emp);
                    if(mgr == null)
                        System.out.println("EmpClient.main(): mgr = null");
                    else
                        System.out.println("EmpClient.main(): mgr ="
                                           + " " + mgr.getEmpNo()
                                           + " " + mgr.getEmpName());

                    directs = hrRemote.getMgrDirects(emp);
                    System.out.println("EmpClient.main(): directs = " + directs.size());
                    iterator = directs.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        emp = (Emp)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + emp.getEmpNo()
                                           + " " + emp.getEmpName());
                    }

                    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());
      
                    emp = new Emp(empNo, empName, salary);
                    hrRemote.setEmp(emp);
                    System.out.println("EmpClient.main(): edited " + empNo);

                    System.out.print("Edit emp address [Y]es | [N]o: ");
                    action = in.readLine();
                    if(action.length() == 0) action = "Q";
                    switch (action.toUpperCase().charAt(0)) {
                    case 'Y':
                        System.out.print("Enter string value for street: ");
                        System.out.flush();
                        street = in.readLine();
      
                        System.out.print("Enter string value for city: ");
                        System.out.flush();
                        city = in.readLine();
                        
                        System.out.print("Enter string value for state: ");
                        System.out.flush();
                        state = in.readLine();
                        
                        System.out.print("Enter string value for zip: ");
                        System.out.flush();
                        zip = in.readLine();
      
                        address = new Address(street, city, state, zip);
                        hrRemote.setEmpAddress(emp, address);
                        System.out.println("EmpClient.main(): edited " + empNo);
                        break;

                    case 'N':
                        break;
                    default:
                        break;
                    }

                    System.out.print("Edit emp phones [Y]es | [N]o: ");
                    action = in.readLine();
                    if(action.length() == 0) action = "N";
                    switch (action.toUpperCase().charAt(0)) {
                    case 'Y':
                        phones = new ArrayList();
                        while(true) {
                            System.out.print("Enter string value for phoneNo: ");
                            System.out.flush();
                            phoneNo = action = in.readLine();
                            if(action.length() == 0) break;

                            System.out.print("Enter string value for phoneType: ");
                            System.out.flush();
                            phoneType = in.readLine();

                            phone = new Phone(phoneNo, phoneType);
                            phones.add(phone);
                            System.out.println("Enter another or return to conclude list");
                        }
                        hrRemote.setEmpPhones(emp, phones);
                        System.out.println("ProjectClient.main(): edited " + empNo);
                        break;

                    case 'N':
                        break;
                    default:
                        break;
                    }

                    System.out.print("Edit emp department [Y]es | [N]o: ");
                    action = in.readLine();
                    if(action.length() == 0) action = "N";
                    switch (action.toUpperCase().charAt(0)) {
                    case 'Y':
                        System.out.print("Enter integer value for deptNo: ");
                        System.out.flush();
                        deptNo = new Integer(in.readLine());
                        
                        dept = new Dept(deptNo, null);
                        
                        hrRemote.setEmpDepartment(emp, dept);
                        System.out.println("EmpClient.main(): edited " + empNo);
                        break;

                    case 'N':
                        break;
                    default:
                        break;
                    }

                    System.out.print("Edit emp manager [Y]es | [N]o: ");
                    action = in.readLine();
                    if(action.length() == 0) action = "N";
                    switch (action.toUpperCase().charAt(0)) {
                    case 'Y':
                        System.out.print("Enter integer value for mgrNo: ");
                        System.out.flush();
                        mgrNo = new Integer(in.readLine());
                        
                        mgr = new Emp(mgrNo, null, null);
                        
                        hrRemote.setEmpManager(emp, mgr);
                        System.out.println("EmpClient.main(): edited " + empNo);
                        break;

                    case 'N':
                        break;
                    default:
                        break;
                    }

                    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();
        }
    }
}
