A.5 SelectionBean ImplementationHere is the complete implementation of the SelectionBean entity bean class used in the entity application example in Chapter 8. This class illustrates the EJB 2.0 container-managed persistence approach. Code Example A.5 Abstract Schema for SelectionBean Entity Bean Classpackage com.wombat.benefits; import javax.ejb.*; import javax.naming.Context; import javax.naming.InitialContext; import com.wombat.AbstractEntityBean; import com.wombat.plan.Plan; public abstract class SelectionBean extends AbstractEntityBean { // container-managed persistence fields // primary key field public abstract Integer getEmployeeNumber(); public abstract void setEmployeeNumber(Integer n); public abstract int getCoverage(); public abstract void setCoverage(int c); public abstract boolean getSmokerStatus(); public abstract void setSmokerStatus(boolean s); // container-managed relationship fields public abstract Plan getMedicalPlan(); public abstract void setMedicalPlan(Plan p); public abstract Plan getDentalPlan(); public abstract void setDentalPlan(Plan p); // values obtained from environment private boolean checkPlanType; private EmployeeHome employeeHome; /******************************************************************/ /* business methods from local interface /******************************************************************/ public SelectionCopy getCopy() { SelectionCopy copy = new SelectionCopy(); Employee emp=null; try { emp = employeeHome.findByPrimaryKey(getEmployeeNumber()); } catch ( FinderException ex ) { throw new EJBException(ex); } copy.setEmployee(emp); copy.setCoverage(getCoverage()); copy.setMedicalPlan(getMedicalPlan()); copy.setDentalPlan(getDentalPlan()); copy.setSmoker(getSmokerStatus()); return copy; } public void updateFromCopy(SelectionCopy copy) throws SelectionException { if (!getEmployeeNumber().equals( copy.getEmployee().getEmployeeNumber())) throw new SelectionException( "can't change employee in selection"); updateMedicalPlan(copy.getMedicalPlan()); updateDentalPlan(copy.getDentalPlan()); setSmokerStatus(copy.isSmoker()); updateCoverage(copy.getCoverage()); } /******************************************************************/ /* Helper methods to validate new CMP field values before setting /* them /******************************************************************/ private void updateCoverage(int v) throws SelectionException { switch (v) { case Plan.EMPLOYEE_ONLY: case Plan.EMPLOYEE_SPOUSE: case Plan.EMPLOYEE_SPOUSE_CHILDREN: setCoverage(v); break; default: throw new SelectionException( SelectionException.INVAL_COVERAGE); } } private void updateMedicalPlan(Plan p) throws SelectionException { if (checkPlanType) { int type = p.getPlanType(); if (type != Plan.MEDICAL_PLAN) throw new SelectionException( SelectionException.INVAL_PLAN_TYPE); } setMedicalPlan(p); } private void updateDentalPlan(Plan p) throws SelectionException { if (checkPlanType) { int type = p.getPlanType(); if (type != Plan.DENTAL_PLAN) throw new SelectionException( SelectionException.INVAL_PLAN_TYPE); } setDentalPlan(p); } /***************************************************************** / /* EntityBean life-cycle methods */ /***************************************************************** / public Integer ejbCreate(SelectionCopy copy) throws SelectionException, CreateException { setEmployeeNumber(copy.getEmployee().getEmployeeNumber()); setSmokerStatus(copy.isSmoker()); updateCoverage(copy.getCoverage()); return null; // ejbCreate returns null in CMP beans } public void ejbPostCreate(SelectionCopy copy) throws SelectionException, CreateException { // set relationships (cannot be set in ejbCreate) updateMedicalPlan(copy.getMedicalPlan()); updateDentalPlan(copy.getDentalPlan()); } public void setEntityContext(EntityContext ctx) { super.setEntityContext(ctx); readEnvironment(); } private void readEnvironment() { try { Context ictx = new InitialContext(); Boolean val = (Boolean)ictx.lookup( "java:comp/env/checkPlanType"); checkPlanType = val.booleanValue(); employeeHome = (EmployeeHome)ictx.lookup( "java:comp/env/ejb/EmployeeEJB"); } catch (Exception ex) { throw new EJBException(ex); } } } |