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

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

            hrHome = (HrRemoteHome)PortableRemoteObject.narrow(nctx.lookup(hrBean),
                                                               HrRemoteHome.class);

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

            hrRemote = hrHome.create();
            if(hrRemote != null)
                System.out.println("ProjectClient.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;
                Project project;
                Integer projectNo;
                String projectName;
                Iterator iterator;
                Emp emp;
                Integer empNo;
                Collection projects, emps;
                int i;

                switch (action.toUpperCase().charAt(0)) {
                case 'A':
                    System.out.print("Enter integer value for projectNo: ");
                    System.out.flush();
                    projectNo = new Integer(in.readLine());
      
                    System.out.print("Enter string value for projectName: ");
                    System.out.flush();
                    projectName = in.readLine();
      
                    project = new Project(projectNo, projectName);
                    hrRemote.addProject(project);

                    System.out.println("ProjectClient.main(): created project " + projectName);
                    break;

                case 'L':
                    projects = hrRemote.getProjects();
                    iterator = projects.iterator();
                    i = 0;
                    while(iterator.hasNext()) {
                        project = (Project)iterator.next();
                        System.out.println("row-" + i++
                                           + " " + project.getProjectNo()
                                           + " " + project.getProjectName());
                    }
                    System.out.println("ProjectClient.main(): found " + projects.size());
                    break;
	
                case 'D':
                    System.out.print("Enter integer value for projectNo: ");
                    System.out.flush();
                    projectNo = new Integer(in.readLine());
      
                    project = new Project(projectNo, null);
                    hrRemote.removeProject(project);
                    System.out.println("ProjectClient.main(): removed " + projectNo);
                    break;

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

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

                    project = new Project(projectNo, projectName);
                    hrRemote.setProject(project);
                    System.out.println("ProjectClient.main(): edited " + projectNo);

                    System.out.print("Edit employees working on project [Y]es | [N]o: ");
                    action = in.readLine();
                    if(action.length() == 0) action = "N";
                    switch (action.toUpperCase().charAt(0)) {
                    case 'Y':
                        emps = new ArrayList();
                        while(true) {
                            System.out.print("Enter integer value for empNo: ");
                            System.out.flush();
                            action = in.readLine();
                            if(action.length() == 0) break;
                            empNo = new Integer(action);
                            emp = new Emp(empNo, null, null);
                            emps.add(emp);
                            System.out.println("Enter another or return to conclude list");
                        }
                        hrRemote.setProjectEmployees(project, emps);
                        System.out.println("ProjectClient.main(): edited " + projectNo);
                        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();
        }
    }
}
