Sunday, July 24, 2011

what are the differences between methodValidator and 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.
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 callssetPhoneNumber(String value) method in our EmployeesEntityImpl class.
  3. setPhoneNumber(String value) callssetAttributeInternal(PHONENUMBER, value) where PHONENUMBER is an integer to identify PhoneNumber attribute.
  4. setAttributeInternal(PHONENUMBER, value) method callsvalidatePhoneNumber(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) 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
     */  
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”);
    }
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 callssetPhoneNumber(String value) method in our EmployeesEntityImpl class.
  3. 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.

3 comments:

  1. Oops....
    This article is stolen from this blog
    http://mjabr.wordpress.com/2011/05/21/add-business-rule-in-the-setter-method-of-an-entity-attribute/

    Shame on you
    John

    ReplyDelete
  2. It was so nice article and useful to Informatica learners. we also provide oracle ADF course online training our Cubtraining is leader in providing Software Training

    ReplyDelete
  3. We are really grateful for your blog post. You will find a lot of approaches after visiting your post. Thanks for such post and please keep it up. Oracle ADF online Training

    ReplyDelete