Add business rule in the setter method of an entity attribute

Environment (JDeveloper 11.1.1.5.0, ADF BC, hr schema)

Most of us know how we can add a validation rule using methodValidator on an attribute. However, this is not the only way that we can add a business rule for an attribute, we can add our business rule in the setter method for that attribute.

In this post I will explain how to add business rule in the setter method of an attribute and describe the difference if we add a business rule using methodValidator. This example based on hr schema, mainly on Employees table, and supposed that you already have built your BC. Our business rule is that the PHONE_NUMBER column should be at least 9 digits.

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;
    }

What happen when we run our application?

when we run the application and set the phone number field, the following steps take place:

  1. the view object layer calls setAttribute(“PhoneNumber”,”123456789″) method.
  2. setAttribute(“PhoneNumber”,”123456789″) method calls setPhoneNumber(String value) method in our EmployeesEntityImpl class.
  3. setPhoneNumber(String value) calls setAttributeInternal(PHONENUMBER, value) where PHONENUMBER is an integer to identify PhoneNumber attribute.
  4. setAttributeInternal(PHONENUMBER, value) method calls validatePhoneNumber(String phonenumber) method.
  5. 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) and setAttribute(“PhoneNumber”,”123456789″) methods is that setAttribute(“PhoneNumber”,”123456789″) invokes the set method for this attribute in a subclass of this Entity Object (if a subclass is generated). The set method name is derived from the attribute name: for example, the method setPhoneNumber pertains to an attribute named “PhoneNumber”.But setAttributeInternal(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
     */  

public void setPhoneNumber(String value) throws oracle.jbo.JboException {
        if (value.length() >= 9)
            setAttributeInternal(PHONENUMBER, value);
        else
            throw new oracle.jbo.JboException(“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:

  1. the view object layer calls setAttribute(“PhoneNumber”,”123456789″) method.
  2. setAttribute(“PhoneNumber”,”123456789″) method calls setPhoneNumber(String value) method in our EmployeesEntityImpl class.
  3. now the business rule is checked in the setter method, if passed then it calls setAttributeInternal(PHONENUMBER, value) method, otherwise it throws the exception and notifies the user with business rule violation.

So, what are the differences between methodValidator business rule and business rule validation inside the setter method.

  1. 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.
  2. 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.
  3. methodValidator can be used for several attributes, as an example the previously mentioned validatePhoneNumber method can be used for WorkPhone and HomePhone attributes.

see also: How to Create a Custom JSF Validator


Advertisements

3 Comments

Filed under ADF

3 responses to “Add business rule in the setter method of an entity attribute

  1. Ammar Sajdi

    mohammad

    Some validation would not require server side processing, how about client side validation. For example, the example you used (checking the length of an attribute) can be validated at the client !

    regards

    ammar sajdi

    • Hello Ammar,
      The answer to your question is yes. You can build your own custom ADF Faces validator, and surely you can create a client-side version of the validator .ADF Faces client-side validation works in the same way that standard validation works on the server, except that JavaScript is used on the client..

  2. Pingback: JavaPins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s