2011年1月5日 星期三

JDeveloper - Validations in ADF (Custom Validation Rule)

Attribute-Level Validation Rules: You can create a new attribute in an entity and then create a new validation rules for this attribute or existing attribute.

Employees Entity:

We don't mention how to create a attribute here. Just show the steps how to new a validation rule for an attribute "PhoneNumber".

Before you click "Add Validation Rule" button to apply a new validation rule to this attribute "PhoneNumber", we would like to new a Custom Validation Rule. Choose a package "test.app.model.framework" and then right click your mouse and click new to get the pop-up dialog "New Gallery".

Choose the Validation Rule from the ADF Business Components in the pop-up dialog "New Gallery" and then press OK button to get the pop-up dialog "Create Validation Rule Class".

In the pop-up dialog "Create Validation Rule Class", type the Name, Package, Extends, Rule Display Name and Rule Description of custom validation rule as below.


After that, a Custom Validation Rule is created. Of course, you should apply some property and validation logic in the class and its class method "validateValue". If you just follow the source code in the book, it may be something wrong in the run time. Please get the source code at the last part of this tutorial.

You can manage your registered rules in ADF Model in Project Properties.


Then, we go back to the Entity "Employees" and select the existing attribute "PhoneNumber". Expend the block "Validation Rules: PhoneNumber" and click "Add Validation Rule" button.

Now, you can see a line separate the system provide rule type and custom validation rule type:

Just choose the custom validation rule type "RegionalPhone" and fill in the property in the "Rule Definition Block":

The following figure shows that a Custom Validation Rule "RegionalPhone" is applied in the existing attribute "PhoneNumber".

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

package test.app.model.framework;
import java.util.regex.Pattern;
import oracle.jbo.ValidationException;
import oracle.jbo.rules.JboValidatorContext;
import oracle.jbo.rules.JboValidatorInterface;
public class RegionalPhoneValidator implements JboValidatorInterface {
    private String description = "Check whether a phone number is correct";
  private String areaCodeDelimiter;
  private String prefixDelimiter;
  private String suffixDelimiter;
 
  private static final String THREE_DIGIT_PATTERN = "[0-9]{3}";
  private static final String FOUR_DIGIT_PATTERN = "[0-9]{4}";
  private Pattern phonePattern = null; // This field is initialized when first needed
    public RegionalPhoneValidator() {
    }
  /**
   * Return true if value is valid.
   */
  public boolean validateValue(Object value) {
    if (value == null) return true;
    if (phonePattern == null) {
        initPhonePattern();
    }
    return phonePattern.matcher(value.toString()).matches();
  }
 
  private void initPhonePattern() {
    String areaCodeRegexp = createRegexp(areaCodeDelimiter, THREE_DIGIT_PATTERN);
    String prefixRegexp = createRegexp(prefixDelimiter, THREE_DIGIT_PATTERN);
    String suffixRegexp = createRegexp(suffixDelimiter, FOUR_DIGIT_PATTERN);
    phonePattern = Pattern.compile(areaCodeRegexp+prefixRegexp+suffixRegexp);
  }
 
  private String createRegexp(String delimiter,
    String basePattern) {
    String regexp;
    if (delimiter != null) {
    switch (delimiter.length()) {
       case 0: regexp = basePattern;
    break;
       case 1: regexp = basePattern + delimiter;
    break;
       default:
        int interIndex = delimiter.lastIndexOf(" ");
        String firstPart = delimiter.substring(0, interIndex);
        String lastPart = delimiter.substring(interIndex+1, delimiter.length());
        regexp = firstPart + basePattern + lastPart;
        //regexp = delimiter.charAt(0) + basePattern + delimiter.charAt(1);
    }
    }
    else
        regexp = basePattern;
    return regexp;
  }
 
  /**
   * Invoked by framework for validation.
   */
  public void validate(JboValidatorContext ctx) {
      if (!validateValue(ctx.getNewValue())) {
          throw new ValidationException("demo.model.PhoneValidator validation failed");
      }
  }
  /**
   * Description of what this class validates.
   */
  public String getDescription() {
      return description;
  }
  /**
   * Description of what this class validates.
   */
  public void setDescription(String str) {
      description = str;
  }
  public void setAreaCodeDelimiter(String areaCodeDelimiter) {
      this.areaCodeDelimiter = areaCodeDelimiter;
  }
  public String getAreaCodeDelimiter() {
      return areaCodeDelimiter;
  }
  public void setPrefixDelimiter(String prefixDelimiter) {
      this.prefixDelimiter = prefixDelimiter;
  }
  public String getPrefixDelimiter() {
      return prefixDelimiter;
  }
  public void setSuffixDelimiter(String suffixDelimiter) {
      this.suffixDelimiter = suffixDelimiter;
  }
  public String getSuffixDelimiter() {
      return suffixDelimiter;
  }
}

沒有留言:

張貼留言