A.10 InsurancePlanAdminBean ClassCode Example A.13 shows the source code for the InsurancePlanAdminBean class described in Chapter 9. Code Example A.13 InsurancePlanAdminBean Class Source Codepackage com.star.admin; import javax.ejb.*; import javax.naming.InitialContext; import java.util.Collection; import com.wombat.plan.*; public class InsurancePlanAdminBean implements SessionBean { private SessionContext sessionContext; private PlanHome planHome; private DoctorHome doctorHome; public InsurancePlanAdminBean(){} public void createInsurancePlan(String planId, String planName, int planType, double coverageFactor, double ageFactor, double smokerCost) { try { Plan newPlan = planHome.create(planId, planName, planType, coverageFactor, ageFactor, smokerCost); } catch ( CreateException ex ) { throw new EJBException(ex); } } public PlanInfo getPlanInfo(String planId) { try { Plan plan = planHome.findByPrimaryKey(planId); PlanInfo planInfo = new PlanInfo(); planInfo.planId = plan.getPlanId(); planInfo.planName = plan.getPlanName(); planInfo.planType = plan.getPlanType(); planInfo.coverageFactor = plan.getCoverageFactor(); planInfo.ageFactor = plan.getAgeFactor(); planInfo.smokerCost = plan.getSmokerCost(); return planInfo; } catch ( Exception ex ) { throw new EJBException(ex); } } public void addDoctors(String planId, DoctorInfo[] doctors) { try { Plan plan = planHome.findByPrimaryKey(planId); for ( int i=0; i<doctors.length; i++ ) { // Find or create a Doctor bean. DoctorPkey pkey = new DoctorPkey(doctors[i].firstName, doctors[i].lastName); Doctor doctor; try { doctor = doctorHome.findByPrimaryKey(pkey); } catch ( FinderException fe ) { doctor = doctorHome.create(doctors[i].firstName, doctors[i].lastName, doctors[i].specialty, doctors[i].hospitals, doctors[i].practiceSince); } plan.addDoctor(doctor); } } catch ( Exception ex ) { throw new EJBException(ex); } } public void removeDoctors(String planId, DoctorInfo[] doctors) { try { Plan plan = planHome.findByPrimaryKey(planId); for ( int i=0; i<doctors.length; i++ ) { // Find a Doctor bean. DoctorPkey pkey = new DoctorPkey(doctors[i].firstName, doctors[i].lastName); Doctor doctor; try { doctor = doctorHome.findByPrimaryKey(pkey); } catch ( FinderException ex ) { throw new EJBException(ex); } plan.removeDoctor(doctor); } } catch ( Exception ex ) { throw new EJBException(ex); } } public DoctorInfo[] getAllDoctors(String planId) { try { Plan plan = planHome.findByPrimaryKey(planId); Collection doctors = plan.getAllDoctors(); return (DoctorInfo[])doctors.toArray( new Doctor[doctors.size()]); } catch ( Exception ex ) { throw new EJBException(ex); } } public void setSmokerCost(String planId, double cost) { try { Plan plan = planHome.findByPrimaryKey(planId); plan.setSmokerCost(cost); } catch ( Exception ex ) { throw new EJBException(ex); } } public void setCoverageFactor(String planId, double s) { try { Plan plan = planHome.findByPrimaryKey(planId); plan.setCoverageFactor(s); } catch ( Exception ex ) { throw new EJBException(ex); } } public void setAgeFactor(String planId, double s) { try { Plan plan = planHome.findByPrimaryKey(planId); plan.setAgeFactor(s); } catch ( Exception ex ) { throw new EJBException(ex); } } /* Life-cycle methods */ public void setSessionContext(SessionContext sessionContext) { this.sessionContext = sessionContext; try { // Look up local home object for PlanEJB using JNDI. InitialContext initialContext = new InitialContext(); planHome = (PlanHome)initialContext.lookup( "java:comp/env/ejb/PlanEJB"); doctorHome = (DoctorHome)initialContext.lookup( "java:comp/env/ejb/DoctorEJB"); } catch ( Exception ex ) { throw new EJBException(ex); } } public void ejbCreate() {} public void ejbRemove() {} public void ejbActivate() {} public void ejbPassivate() {} } |