Friday, March 9, 2012

Defining Unique Key Constraints for Non-Primary Key Attributes in ADF-BC

Share this Post
Greetings,

ADF BC (Business Components) allows you to write code declaratively instead of writing spaghetti code in Java.

One of the feature is Alternate key constraint which is different from Primary and Unique key constraints available in database. Usually Primary key values are coming from database sequence so you don't have to worry about their uniqueness but if you like to validate uniqueness on other columns e.g. email address,Region or Country Name that's where Alternate key comes into action.

Following are some properties of alternate keys:

  • You can check Uniqueness at record level e.g. Department Name must be unique 
  • You can have many alternate keys as compare to Primary keys
  • You can look for a row in entity object using findByKey() method
  • If the key is found in entity objects ADF will throw TooManyObjectsException


Lets look into example (Download the complete example)

Use case:
Database schema: HR @ Oracle XE

Column Country name must be unique in Countries table. We have primary key CountryId in the table as shown in the slide




Solution:

  1. Create an Alternate key constraint on Entity object.
  2. Add Unique Key validation on Entity Object using alternate Key

Creating Alternate Key:

Right click the Entity Object and Choose "New Entity Constraint"



Enter the name of the constraint, Enter the Attribute where you want to add constraint in our case select Country Name





You can verify the Alternate key constraint Under general tab of entity object properties




Creating Unique Validation:

Go to Business Rules tab in Entity Object properties and Click Create New Validation by selecting "Entity Validators" as shown in the slide



Select "Unique Key" from Rule Type list. Select the Alternate key constraint from Keys


Go to Failure tab and Enter custom error message



Run the Application Module, Create new Record and Enter Unique country name like Brazil, You would see the similar error message.


Also on JSF page you would see error message on Commit



Happy JDeveloping,
Zeeshan Baig







Friday, March 2, 2012

Using Programmatic Validation in ADFBC

Share this Post
Greetings,

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 EntityImpl.java 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.");
        }
        super.validateEntity();
    }

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