Friday, March 2, 2012

Using Programmatic Validation in ADFBC


This month of March i will be focusing on ADF business components. So first entry in this series is how to write programmatic validations for entities in ADF business components.

I have covered a similar concept using ADF Domain types  which gives us global validation control instead of writing code for specific entities.

As you might be familiar that ADF BC provides facility to write validations in declarative manner but sometimes  the use case is complex for that you can write programmatic validations as follows

1. Add a method that will do validation in class
2. Call that method as part of validation cycle in validateEntity() method which is called at commit time when any attribute has been updated.

Let's get into detail

In this example use case (Download the example)

Our business rule says that all email address should be first letter of First name and full Last name of the employee

e.g. If the name is Zeeshan Baig then email address should be ZBAIG

1. Generate EntityImpl java class by double clicking on entity, go to Java tab and click on the pencil button choose generate employeeImpl class and click OK as shown in the slide

2. Write your custom method that will perform validation. In our use case is as follows

    public boolean checkValidEmail() {
        //Email must be first letter of First name and Full last name of employee
        //e.g Zeeshan Baig should be entered as zbaig
        String validEmailRule = this.getFirstName().substring(0,1) + this.getLastName();      

        if (getEmail().compareToIgnoreCase(validEmailRule) == 0 && getEmail() != null) {
            return true;
        } else {
            return false;

3. Override validateEntity() method but clicking override button enter text validate, select method and click OK

4. Override the code of validateEntity() method as follows

    protected void validateEntity() {
        if (!checkValidEmail()) {
            throw new JboException("Email Address should start with First letter of First Name and Complete Last Name.");

5. Compile the class and Run Application module to test in ADF business component browser

6. Enter wrong email address and click Validate Button to test you should receive Error alert

7. Enter valid email as per the rule and click Validate No alerts should come click Commit you should be able to see the successful commit message in the log

Happy JDeveloping,
Zeeshan Baig

No comments:

Post a Comment