- methodValidator are called during the validation rule stage (i.e. at some point during the execution of setAttributeInternal method). On the other hand, business rule inside the setter may or may not call setAttributeInternal method.
- use setter validation If you want your business rule validation comes before other validations. But if you want it to come after some validations rules use a methodValidator.
- methodValidator can be used for several attributes, as an example the previously mentioned validatePhoneNumber method can be used for WorkPhone and HomePhone attributes.
First Part.
The first part of this example is to add a business rule using validatePhoneNumber method as shown below.
/**
* Validation method for PhoneNumber.
*/
public boolean validatePhoneNumber(String phonenumber) {
if (phonenumber.length() < 9)
return false;
else
return true;
}
* Validation method for PhoneNumber.
*/
public boolean validatePhoneNumber(String phonenumber) {
if (phonenumber.length() < 9)
return false;
else
return true;
}
What happen when we run our application?
when we run the application and set the phone number field, the following steps take place:
- the view object layer calls setAttribute(“PhoneNumber”,”123456789″)method.
- setAttribute(“PhoneNumber”,”123456789″) method callssetPhoneNumber(String value) method in our EmployeesEntityImpl class.
- setPhoneNumber(String value) callssetAttributeInternal(PHONENUMBER, value) where PHONENUMBER is an integer to identify PhoneNumber attribute.
- setAttributeInternal(PHONENUMBER, value) method callsvalidatePhoneNumber(String phonenumber) method.
- if validatePhoneNumber(String phonenumber) method returns true, then PhoneNumber attribute is set with the passed value.
As we can see that at some point during the execution setPhoneNumber(String value) method calls setAttributeInternal(PHONENUMBER, value) which in turns calls the validatePhoneNumber(String phonenumber) method. The difference between setAttributeInternal(PHONENUMBER, value) andsetAttribute(“PhoneNumber”,”123456789″) methods is thatsetAttribute(“PhoneNumber”,”123456789″) invokes the set method for this attribute in a subclass of this Entity Object (if a subclass is generated). The setmethod name is derived from the attribute name: for example, the method setPhoneNumber pertains to an attribute named “PhoneNumber”.ButsetAttributeInternal(PHONENUMBER, value) validates and sets the value of an attribute by index, it sets an attribute value after validating the value against declarative-validators set for that attribute.
Second Part.
In this part we will add our business rule to the attribute’s setter method as shown below:
/**
* If you write validation code in a setter method,
* you have to throw an exception yourself
*/
* If you write validation code in a setter method,
* you have to throw an exception yourself
*/
public void setPhoneNumber(String value) throws oracle.jbo.JboException {
if (value.length() >= 9)
setAttributeInternal(PHONENUMBER, value);
else
throw new oracle.jboJboException(“Phone number should be at least 9 digits”);
}
if (value.length() >= 9)
setAttributeInternal(PHONENUMBER, value);
else
throw new oracle.jboJboException(“Phone number should be at least 9 digits”);
}
What happen when we run our application?
The setter method just described implements exactly the same business rule as the validatePhoneNumber method. When we run the application and set the phone number field, the following steps take place:
- the view object layer calls setAttribute(“PhoneNumber”,”123456789″)method.
- setAttribute(“PhoneNumber”,”123456789″) method callssetPhoneNumber(String value) method in our EmployeesEntityImpl class.
- now the business rule is checked in the setter method, if passed then it callssetAttributeInternal(PHONENUMBER, value) method, otherwise it throws the exception and notifies the user with business rule violation.