Chapter 12 - Code Snippets ========================== p.384, under MethodValidator: public boolean validateEmail(String value) { /* Start out assuming the email is valid. */ boolean isValid=true; int length=value.length(); /* Emails longer than 8 are invalid. */ if (length>8) { isValid=false; } else { /* Non-alphabetic and non-uppercase characters are invalid */ for (int i=0; i'Z') { isValid=false; } } } return isValid; } ---------------------------------------------------------------------------------- p.387: public void setEmail(String value) throws oracle.jbo.JboException { boolean isValid=true; int length=value.length(); if (length > 8) { isValid=false; } else { for (int i=0; i'Z') { isValid=false; } } } /* If the email is valid, set the attribute. */ if (isValid) { setAttributeInternal(EMAIL, value); } /* Otherwise, throw an exception. */ else { throw new oracle.jbo.JboException( "An email address must be uppercase " + "and have at most 8 characters."); } } ---------------------------------------------------------------------------------- p.390: public void setAddr(AddressTyp value) { if (value.getCountryId().equals("US")) setAttributeInternal(ADDR, value); else throw new oracle.jbo.JboException( "Address must be in the United States."); } ---------------------------------------------------------------------------------- p.394: protected void validateEntity() throws oracle.jbo.JboException { super.validateEntity(); if (getJobId().endswith("MAN") { if (getSalary().intValue() < 10000) { throw new oracle.jbo.JboException( "Managers must have a salary of at least 10000."); } } else { if (getSalary.intValue() > 9999) { throw new oracle.jbo.Exception( "Non-managers cannot have a salary more than 9999."); } } } ---------------------------------------------------------------------------------- p.396, bottom of the page: int length=mData.length(); if (length > 8) { throw new oracle.jbo.JboException( "An email address must have at most 8 characters."); } else { for (int i=0; i'Z') { throw new oracle.jbo.JboException( "An email address must be uppercase."); } } } ---------------------------------------------------------------------------------- p.400: protected void create(AttributeList attributeList) { super.create(attributeList); Date currDate=new Date(Date.getCurrentDate()); setHireDate(currDate); } ---------------------------------------------------------------------------------- p.407, step 4: protected void create(AttributeList attributeList) { super.create(attributeList); SequenceImpl empSeq=new SequenceImpl("EMPLOYEES_SEQ", getDBTransaction()); setEmployeeId(empSeq.getSequenceNumber()); } step 2 at the bottom of the page: public Number getYearlyPay() { /* Calculate YearlyPay if it is null and salary is * set. */ if ( getAttributeInternal(YEARLYPAY) == null && getSalary() != null ) { /* Base yearly salary is 12 times monthly salary. */ int value = 12 * getSalary().intValue(); if (getCommissionPct() != null) /* Estimate commission based on sales expectations of * five times salary. */ { value += (int) ( value * 5 * getCommissionPct().doubleValue() / 100 ); } populateAttribute(YEARLYPAY, new Number(Value)); } return (Number) getAttributeInternal(YEARLYPAY); } ---------------------------------------------------------------------------------- p.408, step 2: public void setSalary(Number value) { setAttributeInternal(SALARY, value); populateAttribute(YEARLYPAY, null); } step 4: public void setCommissionPct(Number value) { setAttributeInternal(COMMISSIONPCT, value); populateAttribute(YEARLYPAY, null); } ---------------------------------------------------------------------------------- p.409, step 2: /* Start out assuming the employee's salary is low enough. */ boolean ok = true; int mySal = value.intValue(); /* getManagerIdEmployees() is the accessor that returns * the manager. */ EmployeesImpl manager = getManagerIdEmployees(); /* If the employee has a manager, make sure the employee's * salary is the same or lower as the manager's. */ if (manager != null) { if (manager.getSalary().intValue() < mySal) { ok = false; } } step 3: if (ok) { setAttributeInternal(SALARY, value); } else { throw new oracle.jbo.JboException( "Managers' salaries cannot be lower than their employees'." ); } ---------------------------------------------------------------------------------- p.410, step 1: /* getEmployees() is the accessor that returns all reporting * employees */ RowIterator reports = getEmployees(); /* current will hold each reporting employee as you cycle * through them */ EmployeesImpl current; /* Cycle through the reporting employees. Make sure each * has a salary low enough. */ while (reports.hasNext()) { current = (EmployeesImpl) reports.next(); if (current.getSalary().intValue() > mySal) ok = false; } ----------------------------------------------------------------------------------