2011年1月6日 星期四

JDeveloper - Validations in ADF (Validation Domain)

In the previous article, you know that you can create custom validation rule for an attribute. The validation rule type can be system provided (Compare, Key Exists, Length, List, Method, Range, Regular Expression, Script Expression) or user custom (Custom Validation Rule). Alternatively, you can apply custom domains which can also be used to provide validation to attribute's type. These domains, called validation domains, wrap other domains or standard Java classes (such as String) that can be used to attribute types, but include validation code as well. These domains have theirs advantages because they can be used for many attributes just by setting the attribute's type to the validation domain.

I would like to show the steps for creating a validation domain. Choose a package "test.app.model.framework" and then right click your mouse and click "New Domain" to get the pop-up dialog "Create Domain".

In the pop-up dialog "Create Domain",  type the Name of validation domain as below and then press "Next" button.


Choose the attribute value's type to "String". There are many options such as "Mandatory" or "Primary Key".

Eg 1. An attribute with mandatory domain as its type must always be mandatory.
Eg 2. An attribute with a primary key domain as its type is always part of its entity object definition's primary key.
Eg 3. An attribute with a domain that maps to VARCHAR(30) as its length can have no more than 30 characters in its values.


You can press "Next" button to view more detail before click "Finish" button.


After you click the "Finish" button, you can see that a new validation domain "JobIdDomain" is created.

 Then, you should implement your validation logic to require an attribute not be null or an attribute fit into a particular size or precision and scale in the method "validate" of the domain class "JobIdDomain.java". Please get the source code at the last part of this tutorial.

I would like to require the attribute follow the standard format for a job ID as two capital letters followed by an underscore and followed by 2-4 capital letters. Thus, I added the following code to the domain class "JobIdDomain.java" and its method "validate".

After the domain validation rule is ready, you need to apply it to any attribute. I would like to apply this validation domain "JobIdDomain" to the attribute "JobId" of the entity "Employees". Click "Edit selected attribute" button to get the pop-up dialog "Edit Attribute".

In the pop-up dialog, change the type of entity attribute from the type "String" to "test.app.model.framework.common.JobIdDomain".

The following figure shows that a Validation Domain "JobIdDomain" is applied in the attribute "JobId".


The following codes show the source code of JobIdDomain.java:

package test.app.model.framework.common;
import java.io.Serializable;
import oracle.jbo.JboException;
import oracle.jbo.Transaction;
import oracle.jbo.domain.DomainInterface;
import oracle.jbo.domain.DomainOwnerInterface;
// ---------------------------------------------------------------------
// ---    File generated by Oracle ADF Business Components Design Time.
// ---    Thu Jan 06 17:09:39 CST 2011
// ---    Custom code may be added to this class.
// ---    Warning: Do not modify method signatures of generated methods.
// ---------------------------------------------------------------------
public class JobIdDomain implements DomainInterface, Serializable {
    public JobIdDomain(String val) {
        mData = new String(val);
        validate();
    }
    private String mData;
    protected JobIdDomain() {
        mData = "";
    }
    public Object getData() {
        return mData;
    }
    /**
     * <b>Internal:</b> <em>Applications should not use this method.</em>
     */
    public void setContext(DomainOwnerInterface owner, Transaction trans,
                           Object obj) {
    }
    /**
     * Implements domain validation logic and throws a JboException on error.
     */
    protected void validate() {
        //  ### Implement custom domain validation logic here. ###
    }
    public String toString() {
        if (mData != null) {
            return mData.toString();
        }
        return "<null>";
    }
    public boolean equals(Object obj) {
        if (obj instanceof DomainInterface) {
            if (mData != null) {
                return mData.equals(((DomainInterface)obj).getData());
            }
            return ((DomainInterface)obj).getData() == null;
        }
        return false;
    }
}

沒有留言:

張貼留言