A.12 ProvidencePlanBean Class

A.12 ProvidencePlanBean Class

Code Example A.15 shows the source code for the ProvidencePlanBean class described in Chapter 9.

Code Example A.15 ProvidencePlanBean Class Source Code
 package com.star.plan; import javax.ejb.*; import javax.naming.*; import java.util.*; import java.rmi.RemoteException; import com.wombat.plan.*; import com.wombat.AbstractEntityBean; import com.star.plan.providence.*; public class ProvidencePlanBean extends AbstractEntityBean {     private PlanInfo planInfo;     private ProvidenceWebPort providence;     private DoctorHome doctorHome;     private static final String MEDICAL_PLAN = "MEDICAL_PLAN";     private static final String DENTAL_PLAN = "DENTAL_PLAN"; /******************************************************************/ /* business methods from local interface     */ /******************************************************************/     public String getPlanId() {     return (String)entityContext.getPrimaryKey();     }     public int getPlanType() {     String planType = getPlanInfo().getPlanType();     if ( planType.equals(MEDICAL_PLAN) )         return Plan.MEDICAL_PLAN;     else         return Plan.DENTAL_PLAN;     }     public String getPlanName() {     return getPlanInfo().getPlanName();     }     public double getAgeFactor() {     return getPlanInfo().getAgeFactor();     }     public void setAgeFactor(double a) {         throw new EJBException(                "Unsupported operation for Providence Health plan");     }     public double getCoverageFactor() {     return getPlanInfo().getCoverageFactor();     }     public void setCoverageFactor(double c) {         throw new EJBException(            "Unsupported operation for Providence Health plan");     }     public double getSmokerCost() {         return getPlanInfo().getSmokerCost();     }     public void setSmokerCost(double cost) {         throw new EJBException(            "Unsupported operation for Providence Health plan");     }     public double getCost(int coverage, int age, boolean isSmoker)         throws PlanException, RemoteException     {         return providence.getCost(getPlanId(), coverage, age,                isSmoker);     }     public void addDoctor(Doctor doctor) throws PlanException {         throw new PlanException(                "Unsupported operation for Providence Health plan");     }     public boolean removeDoctor(Doctor doctor) throws PlanException {         throw new PlanException(                "Unsupported operation for Providence Health plan");     }     public Collection getAllDoctors()            throws FinderException, RemoteException {     DoctorInfo[] doctors =            providence.getDoctorInfo(getPlanId(), null);     return convertToCollection(doctors);     }     public Collection getDoctorsByName(String fname, String lname)         throws FinderException, RemoteException {     DoctorInfo doctorQuery = new DoctorInfo();     doctorQuery.setFirstName(fname);     doctorQuery.setLastName(lname);     DoctorInfo[] doctors =            providence.getDoctorInfo(getPlanId(), doctorQuery);     return convertToCollection(doctors);     }     public Collection getDoctorsBySpecialty(String specialty)             throws FinderException, RemoteException {     DoctorInfo doctorQuery = new DoctorInfo();     doctorQuery.setSpecialty(specialty);     DoctorInfo[] doctors =            providence.getDoctorInfo(getPlanId(), doctorQuery);     return convertToCollection(doctors);     } /******************************************************************/ /* home business methods from local home interface     */ /******************************************************************/     // Update smoker costs for all plans.     public void ejbHomeUpdateSmokerCosts(double cost)            throws FinderException {         throw new EJBException(                "Unsupported operation for Providence Health plan");     }     // Get all medical plan names.     public String[] ejbHomeGetMedicalPlanNames()            throws FinderException, RemoteException {     PlanInfo[] plans =            providence.getPlanInfo(null, MEDICAL_PLAN, null);     String[] planNames = new String[plans.length];     for ( int i=0; i<plans.length; i++ ) {         planNames[i] = plans[i].getPlanName();     }     return planNames;     }     // Get all dental plan names.     public String[] ejbHomeGetDentalPlanNames()            throws FinderException, RemoteException {     PlanInfo[] plans =            providence.getPlanInfo(null, DENTAL_PLAN, null);     String[] planNames = new String[plans.length];     for ( int i=0; i<plans.length; i++ ) {         planNames[i] = plans[i].getPlanName();     }     return planNames;     } /******************************************************************/ /* find methods                */ /******************************************************************/     public String ejbFindByPrimaryKey(String planId)                throws FinderException, RemoteException {        try {            PlanInfo[] plans =                providence.getPlanInfo(planId, null, null);        } catch ( Exception ex ) {            throw new ObjectNotFoundException();        }     return planId;     }     public Collection ejbFindMedicalPlans()            throws FinderException, RemoteException {        PlanInfo[] plans =            providence.getPlanInfo(null, MEDICAL_PLAN, null);        ArrayList planIds = new ArrayList();        for ( int i=0; i<plans.length; i++ ) {            planIds.add(plans[i].getPlanId());        }        return planIds;     }     public Collection ejbFindDentalPlans()            throws FinderException, RemoteException {        PlanInfo[] plans =            providence.getPlanInfo(null, DENTAL_PLAN, null);        ArrayList planIds = new ArrayList();        for ( int i=0; i<plans.length; i++ ) {            planIds.add(plans[i].getPlanId());        }        return planIds;     }     public Collection ejbFindByDoctor(String firstName,            String lastName)            throws FinderException, RemoteException {        DoctorInfo doctorQuery = new DoctorInfo();        doctorQuery.setFirstName(firstName);        doctorQuery.setLastName(lastName);        PlanInfo[] plans =               providence.getPlanInfo(null, null, doctorQuery);        ArrayList planIds = new ArrayList();        for ( int i=0; i<plans.length; i++ ) {            planIds.add(plans[i].getPlanId());        }        return planIds;     } /******************************************************************/ /* create methods             */ /******************************************************************/     public String ejbCreate(String planId, String planName,            int planType, double coverageFactor, double ageFactor,            double smokerCost) throws CreateException     {        throw new CreateException(            "Create not supported for Providence Health plan");     }     public void ejbPostCreate(String planId, String planName,            int planType, double coverageFactor, double ageFactor,            double smokerCost) throws CreateException     {} /******************************************************************/ /* miscellaneous methods     */ /******************************************************************/     public void setEntityContext(EntityContext ctx) {        super.setEntityContext(ctx);        try {            InitialContext ic = new InitialContext();            ProvidenceWebService service =                (ProvidenceWebService)ic.lookup(                "java:comp/env/service/ProvidenceWebService");            providence = service.getProvidenceWebSvcPort();            doctorHome = (DoctorHome)ic.lookup(                   "java:comp/env/ejb/ProvidenceDoctorEJB");        }        catch ( Exception ex ) {             throw new EJBException(ex);        }     }     public void ejbRemove() throws RemoveException {        throw new RemoveException(            "Remove not supported for Providence Health plan");     }     public void ejbPassivate() {        planInfo = null;     }     public void ejbActivate() {     }     private Collection convertToCollection(DoctorInfo[] doctors) {        Collection doctorsColl = new ArrayList();        for ( int i=0; i<doctors.length; i++ ) {            doctorsColl.add(getDoctor(doctors[i]));        }            return doctorsColl;     }     private Doctor getDoctor(DoctorInfo doctorInfo) {     try {         return doctorHome.findByPrimaryKey(                new DoctorPkey(doctorInfo.getFirstName(),                           doctorInfo.getLastName()));     } catch ( Exception ex ) {         throw new EJBException(ex);     }     }     private PlanInfo getPlanInfo() {     if ( planInfo == null ) {         try {         planInfo =             providence.getPlanInfo(getPlanId(), null, null)[0];         } catch ( Exception ex ) {         throw new EJBException(ex);         }     }     return planInfo;     } } 


Applying Enterprise Javabeans
Applying Enterprise JavaBeans(TM): Component-Based Development for the J2EE(TM) Platform
ISBN: 0201702673
EAN: 2147483647
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net