A.6 PlanBean Implementation Class

A.6 PlanBean Implementation Class

Here is the complete implementation of the PlanBean entity bean class.

Code Example A.6 PlanBean Abstract Entity Bean Class
 package com.wombat.plan; import javax.ejb.*; import javax.mail.*; import javax.mail.internet.*; import javax.naming.*; import java.util.*; import com.wombat.AbstractEntityBean; public abstract class PlanBean extends AbstractEntityBean                implements TimedObject {     // container-managed persistence (CMP) fields     // primary key field     public abstract String getPlanId();     public abstract void setPlanId(String s);     public abstract String getPlanName();     public abstract void setPlanName(String s);     public abstract int getPlanType();     public abstract void setPlanType(int s);     public abstract double getCoverageFactor();     public abstract void setCoverageFactor(double s);     public abstract double getAgeFactor();     public abstract void setAgeFactor(double s);     public abstract double getSmokerCost();     public abstract void setSmokerCost(double s);     // container-managed relationship (CMR) fields     public abstract Collection getDoctors();     public abstract void setDoctors(Collection doctors); /******************************************************************/ /* business methods from local interface                            */ /******************************************************************/     public double getCost(int coverage, int age, boolean isSmoker)         throws PlanException     {         double cost = getCoverageFactor() * coverage                   + getAgeFactor() * age;         if ( isSmoker )             cost += getSmokerCost();         return cost;     }     public void addDoctor(Doctor doctor) throws PlanException {         getDoctors().add(doctor);     }     public boolean removeDoctor(Doctor doctor) throws PlanException {         return getDoctors().remove(doctor);     }     public Collection getAllDoctors() throws FinderException {         Collection doctors = getDoctors();         Collection doctorsCopy = new ArrayList(doctors);         return doctorsCopy;     }     public Collection getDoctorsByName(String fname, String lname)             throws FinderException {         return ejbSelectDoctorsByName(getPlanId(), fname, lname);     }     public Collection getDoctorsBySpecialty(String specialty)             throws FinderException {         return ejbSelectDoctorsBySpecialty(getPlanId(), specialty);     } /******************************************************************/ /* home business methods from local home interface */ /******************************************************************/     // Update smoker costs for all plans.     public void ejbHomeUpdateSmokerCosts(double cost)                throws FinderException {         Collection allPlans = ejbSelectAllPlans();         Iterator itr = allPlans.iterator();         while ( itr.hasNext() ) {             Plan plan = (Plan)itr.next();             plan.setSmokerCost(cost);         }     }     // Get all medical plan names.     public String[] ejbHomeGetMedicalPlanNames()                throws FinderException {         Collection names = ejbSelectPlanNames(Plan.MEDICAL_PLAN);         return (String[])names.toArray(new String[names.size()]);     }     // Get all dental plan names.     public String[] ejbHomeGetDentalPlanNames()                throws FinderException {         Collection names = ejbSelectPlanNames(Plan.DENTAL_PLAN);         return (String[])names.toArray(new String[names.size()]);     } /******************************************************************/ /* ejbSelect method declarations */ /******************************************************************/     public abstract Collection ejbSelectAllPlans()         throws FinderException;     public abstract Collection ejbSelectPlanNames(int planType)         throws FinderException;     public abstract Collection ejbSelectDoctorsByName            (String planId, String fname, String lname)            throws FinderException;     public abstract Collection ejbSelectDoctorsBySpecialty            (String planId, String specialty)            throws FinderException;     public abstract long ejbSelectNumEmployeesInPlan(Plan plan)             throws FinderException; /******************************************************************/ /* create method */ /******************************************************************/     public String ejbCreate(String planId, String planName,                int planType, double coverageFactor, double ageFactor,                double smokerCost) throws CreateException     {         setPlanId(planId);         setPlanName(planName);         setPlanType(planType);         setCoverageFactor(coverageFactor);         setAgeFactor(ageFactor);         setSmokerCost(smokerCost);         return null;     }     public void ejbPostCreate(String planId, String planName,            int planType, double coverageFactor, double ageFactor,            double smokerCost) throws CreateException     {         // Start timer for daily statistics to be e-mailed at         // midnight.         // First get a Date object for midnight starting         // from the next day.          GregorianCalendar cal = new GregorianCalendar();          int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);          cal.set(Calendar.DAY_OF_YEAR, (dayOfYear+1)%365);          cal.set(Calendar.HOUR_OF_DAY, 0);          cal.set(Calendar.MINUTE, 0);          cal.set(Calendar.SECOND, 0);          Date midnight = cal.getTime();          long interval = 1000 * 3600 * 24; // milliseconds in 1 day          TimerService timerService = entityContext.getTimerService();          timerService.createTimer(midnight, interval, null);     } /******************************************************************/ /* timeout method */ /******************************************************************/     public void ejbTimeout(javax.ejb.Timer timer) {         try {            // Get the number of employees who have subscribed            // to this plan.             long numEmployeesInThisPlan = ejbSelectNumEmployeesInPlan(                              (Plan)entityContext.getEJBLocalObject());             String emailText = "Plan " + getPlanName() + " has "                              + numEmployeesInThisPlan + " employees.";             // e-mail the text.             InitialContext ic = new InitialContext();             Session session = (Session)ic.lookup(                   "java:comp/env/MailSession");             String toAddress = (String)ic.lookup(                   "java:comp/env/toAddress");             String fromAddress = (String)ic.lookup("                   java:comp/env/fromAddress");             Message msg = new MimeMessage(session);             msg.setFrom(new InternetAddress(fromAddress));             msg.addRecipient(Message.RecipientType.TO,                              new InternetAddress(toAddress));             msg.setSubject("Statistics");             msg.setText(emailText);             Transport.send(msg);         } catch ( Exception ex ) {             throw new EJBException(ex);         }     } } 


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