// HrBean.java

package hr;

import java.util.*;
import java.rmi.*;
import javax.naming.*;
import javax.ejb.*;
import javax.rmi.PortableRemoteObject;

public class HrBean implements SessionBean
{

    public SessionContext sctx = null;
    private static Context nctx = null;

    public static final String empBean = "java:comp/env/EmpBean";
    public static EmpLocalHome empHome = null;
    public transient EmpLocal empLocal = null;
    public transient EmpLocal mgrLocal = null;
    public Emp emp = null;
    public Emp mgr = null;

    public static final String deptBean = "java:comp/env/DeptBean";
    public static DeptLocalHome deptHome = null;
    public transient DeptLocal deptLocal = null;
    public Dept dept = null;
           
    public static final String projectBean = "java:comp/env/ProjectBean";
    public static ProjectLocalHome projectHome = null;
    public transient ProjectLocal projectLocal = null;
    public Project project = null;

    public static final String addressBean = "java:comp/env/AddressBean";
    public static AddressLocalHome addressHome = null;
    public transient AddressLocal addressLocal = null;
    public Address address = null;
           
    public static final String phoneBean = "java:comp/env/PhoneBean";
    public static PhoneLocalHome phoneHome = null;
    public transient PhoneLocal phoneLocal = null;
    public Phone phone = null;

    public void ejbCreate() throws CreateException
    {
        try {
        nctx = new InitialContext();

        deptHome =
            (DeptLocalHome)PortableRemoteObject.narrow(nctx.lookup(deptBean),
                                                       DeptLocalHome.class);
        
        empHome =
            (EmpLocalHome)PortableRemoteObject.narrow(nctx.lookup(empBean),
                                                      EmpLocalHome.class);

        projectHome =
            (ProjectLocalHome)PortableRemoteObject.narrow(nctx.lookup(projectBean),
                                                          ProjectLocalHome.class);

        addressHome = 
            (AddressLocalHome)PortableRemoteObject.narrow(nctx.lookup(addressBean),
                                                          AddressLocalHome.class);

        phoneHome = 
            (PhoneLocalHome)PortableRemoteObject.narrow(nctx.lookup(phoneBean),
                                                        PhoneLocalHome.class);

        } catch(NamingException e) {
            throw new CreateException("HrBean.ejbPostCreate(): " + e.getMessage());
        }
    }

    public void ejbPostCreate() throws CreateException
    {
        // when just after bean created
    }

    // Department methods
    // ===========================================================
    public void addDept(Dept dept) throws CreateException
    {
        deptLocal = (DeptLocal)deptHome.create(dept);
    }

    public void removeDept(Dept dept) throws RemoveException
    {
        try {
            deptLocal = (DeptLocal)deptHome.findByPrimaryKey(dept.getDeptNo());
        } catch(FinderException e) {
            throw new RemoveException(e.getMessage());
        }

        deptLocal.remove();
    }

    public void setDept(Dept dept) throws FinderException
    {
        deptLocal = (DeptLocal)deptHome.findByPrimaryKey(dept.getDeptNo());
        deptLocal.setDeptName(dept.getDeptName());
    }

    public Collection getDepts() throws FinderException
    {
        ArrayList depts = new ArrayList();
        Collection deptLocals = deptHome.findAll();
        Iterator iterator = deptLocals.iterator();
        while(iterator.hasNext()) {
            deptLocal = (DeptLocal)iterator.next();
            dept = new Dept(deptLocal.getDeptNo(), deptLocal.getDeptName());
            depts.add(dept);
        }
        return depts;
    }

    public Collection getDeptEmployees(Dept dept) throws FinderException
    {
        ArrayList emps = new ArrayList();
        deptLocal = (DeptLocal)deptHome.findByPrimaryKey(dept.getDeptNo());
        Collection empLocals = deptLocal.getEmployees();
        Iterator iterator = empLocals.iterator();
        while(iterator.hasNext()) {
            empLocal = (EmpLocal)iterator.next();
            emp = new Emp(empLocal.getEmpNo(),
                          empLocal.getEmpName(),
                          empLocal.getSalary());
            emps.add(emp);
        }
        return emps;
    }

    // Employee methods
    // ===========================================================
    public void addEmp(Emp emp) throws CreateException
    {
        empLocal = (EmpLocal)empHome.create(emp);
    }

    public void removeEmp(Emp emp) throws RemoveException
    {
        try {
            empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        } catch(FinderException e) {
            throw new RemoveException(e.getMessage());
        }
        empLocal.remove();
    }

    public void setEmp(Emp emp) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        empLocal.setEmpName(emp.getEmpName());
        empLocal.setSalary(emp.getSalary());
    }

    public Collection getEmps() throws FinderException
    {
        ArrayList emps = new ArrayList();
        Collection empLocals = empHome.findAll();
        Iterator iterator = empLocals.iterator();
        while(iterator.hasNext()) {
            empLocal = (EmpLocal)iterator.next();
            emp = new Emp(empLocal.getEmpNo(),
                          empLocal.getEmpName(),
                          empLocal.getSalary());
            emps.add(emp);
        }
        return emps;
    }

    public Address getEmpAddress(Emp emp) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        addressLocal = (AddressLocal)empLocal.getAddress();
        if(addressLocal == null) return null;
        address = new Address(addressLocal.getStreet(),
                              addressLocal.getCity(),
                              addressLocal.getState(),
                              addressLocal.getZip());
        return address;
    }

    public void setEmpAddress(Emp emp, Address address) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        addressLocal = (AddressLocal)empLocal.getAddress();
        if(addressLocal == null) {
            try {
                addressLocal = addressHome.create(address);
            } catch (CreateException e) {
                throw new FinderException(e.getMessage());
            }
        } else {
            addressLocal.setStreet(address.getStreet());
            addressLocal.setCity(address.getCity());
            addressLocal.setState(address.getState());
            addressLocal.setZip(address.getZip());
        }
        empLocal.setAddress(addressLocal);
    }

    public Dept getEmpDepartment(Emp emp) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        deptLocal = (DeptLocal)empLocal.getDept();
        if(deptLocal == null) return null;
        dept = new Dept(deptLocal.getDeptNo(), deptLocal.getDeptName());
        return dept;
    }

    public void setEmpDepartment(Emp emp, Dept dept) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        deptLocal = deptHome.findByPrimaryKey(dept.getDeptNo());
        empLocal.setDept(deptLocal);
    }

    public Collection getEmpProjects(Emp emp) throws FinderException
    {
        ArrayList projects = new ArrayList();
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        Collection projectLocals = empLocal.getProjects();
        Iterator iterator = projectLocals.iterator();
        while(iterator.hasNext()) {
            projectLocal = (ProjectLocal)iterator.next();
            project = new Project(projectLocal.getProjectNo(),
                                  projectLocal.getProjectName());
            projects.add(project);
        }
        return projects;
    }

    public void setEmpProjects(Emp emp, Collection projects) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        Collection projectLocals = empLocal.getProjects();
        Iterator iterator = projects.iterator();
        while(iterator.hasNext()) {
            project = (Project)iterator.next();
            projectLocal = projectHome.findByPrimaryKey(project.getProjectNo());
            projectLocals.add(projectLocal);
        }
        empLocal.setProjects(projectLocals);
    }

    public Collection getEmpPhones(Emp emp) throws FinderException
    {
        ArrayList phones = new ArrayList();
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        Collection phoneLocals = empLocal.getPhones();
        Iterator iterator = phoneLocals.iterator();
        while(iterator.hasNext()) {
            phoneLocal = (PhoneLocal)iterator.next();
            phone = new Phone(phoneLocal.getPhoneNo(),
                              phoneLocal.getPhoneType());
            phones.add(phone);
        }
        return phones;
    }

    public void setEmpPhones(Emp emp, Collection phones) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        Collection phoneLocals = empLocal.getPhones();
        Iterator iterator = phones.iterator();
        while(iterator.hasNext()) {
            phone = (Phone)iterator.next();
            try{
                phoneLocal = phoneHome.create(phone);
            } catch (CreateException e) {
                throw new FinderException(e.getMessage());
            }
            phoneLocals.add(phoneLocal);
        }
        empLocal.setPhones(phoneLocals);
    }

    public Emp getEmpManager(Emp emp) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        mgrLocal = (EmpLocal)empLocal.getManager();
        if(mgrLocal == null) return null;
        mgr = new Emp(mgrLocal.getEmpNo(), mgrLocal.getEmpName(), mgrLocal.getSalary());
        return mgr;
    }

    public void setEmpManager(Emp emp, Emp mgr) throws FinderException
    {
        empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
        mgrLocal = (EmpLocal)empHome.findByPrimaryKey(mgr.getEmpNo());
        empLocal.setManager(mgrLocal);
    }

    public Collection getMgrDirects(Emp mgr) throws FinderException
    {
        ArrayList directs = new ArrayList();
        mgrLocal = (EmpLocal)empHome.findByPrimaryKey(mgr.getEmpNo());
        Collection empLocals = mgrLocal.getDirects();
        Iterator iterator = empLocals.iterator();
        while(iterator.hasNext()) {
            empLocal = (EmpLocal)iterator.next();
            emp = new Emp(empLocal.getEmpNo(), empLocal.getEmpName(), empLocal.getSalary());
            directs.add(emp);
        }
        return directs;
    }

    public void setMgrDirects(Emp mgr, Collection directs) throws FinderException
    {
        mgrLocal = (EmpLocal)empHome.findByPrimaryKey(mgr.getEmpNo());
        Collection empLocals = mgrLocal.getDirects();
        Iterator iterator = directs.iterator();
        while(iterator.hasNext()) {
            emp = (Emp)iterator.next();
            empLocal = (EmpLocal)empHome.findByPrimaryKey(emp.getEmpNo());
            empLocals.add(empLocal);
        }
        mgrLocal.setDirects(empLocals);
    }

    // Project methods
    // ===========================================================
    public void addProject(Project project) throws CreateException
    {
        projectLocal = (ProjectLocal)projectHome.create(project);
    }

    public void removeProject(Project project) throws RemoveException
    {
        try {
            projectLocal = (ProjectLocal)projectHome.findByPrimaryKey(project.getProjectNo());
        } catch(FinderException e) {
            throw new RemoveException(e.getMessage());
        }
        projectLocal.remove();
    }

    public void setProject(Project project) throws FinderException
    {
        projectLocal =
            (ProjectLocal)projectHome.findByPrimaryKey(project.getProjectNo());
        projectLocal.setProjectName(project.getProjectName());
    }

    public Collection getProjects() throws FinderException
    {
        ArrayList projects = new ArrayList();
        Collection projectLocals = projectHome.findAll();
        Iterator iterator = projectLocals.iterator();
        while(iterator.hasNext()) {
            projectLocal = (ProjectLocal)iterator.next();
            project = new Project(projectLocal.getProjectNo(),
                                  projectLocal.getProjectName());
            projects.add(project);
        }
        return projects;
    }

    public Collection getProjectEmployees(Project project) throws FinderException
    {
        ArrayList emps = new ArrayList();
        projectLocal =
            (ProjectLocal)projectHome.findByPrimaryKey(project.getProjectNo());
        Collection empLocals = projectLocal.getEmployees();
        Iterator iterator = empLocals.iterator();
        while(iterator.hasNext()) {
            empLocal = (EmpLocal)iterator.next();
            emp = new Emp(empLocal.getEmpNo(),
                          empLocal.getEmpName(),
                          empLocal.getSalary());
            emps.add(emp);
        }
        return emps;
    }

    public void setProjectEmployees(Project project, Collection emps) throws FinderException
    {
        projectLocal = (ProjectLocal)projectHome.findByPrimaryKey(project.getProjectNo());
        Collection empLocals = projectLocal.getEmployees();
        Iterator iterator = emps.iterator();
        while(iterator.hasNext()) {
            emp = (Emp)iterator.next();
            empLocal = empHome.findByPrimaryKey(emp.getEmpNo());
            empLocals.add(empLocal);
        }
        projectLocal.setEmployees(empLocals);
    }

    // ===========================================================
    public void setSessionContext(SessionContext sctx)
    {
        this.sctx = sctx;
    }

    public void unsetSessionContext()
    {
        this.sctx = null;
    }

    public void ejbRemove()
    {
        // when bean removed
    }

    public void ejbActivate()
    {
        // when bean activated
    }

    public void ejbPassivate()
    {
        // when bean deactivated
    }

}
