Implementing a Validation Class

With the Business Components for Java framework developers can create classes and use them to create validator types. A validation class must implement the JbiValidator interface, which provides two key methods: vetoableChange and validateValue. The Business Component framework calls vetoableChange and passes in a PropertyChangeEvent when an attribute value is set. Typically, you implement vetoableChange to call validateValue. If validateValue returns false, vetoableChange builds an exception with information about what failed. Your implementation of validateValue can also throw an exception of its own (which must extend ValidationException). See About Error Handling for information about programming with exceptions.

The Business Component framework provides a skeleton class to which you can add code to implement a validation class (or you can create one yourself from scratch). To generate the skeleton class, do the following.

  1. In the Navigation panel, right-click a Business Component project file, then choose Edit to display the Business Component Project Wizard.

  2. In the Registered Rules tab, click New to display the Validation Rule Wizard. Fill in fields and choose options as desired for your class.

  3. Click OK to close the Validation Rule Wizard, then click Finish to close the Business Component Project Wizard.

The Business Component framework generates a Java skeleton class with the name you specified. Add code to this class to implement your validation class.

After you have implemented your class, you need to make a change to the JDeveloper IDE classpath to allow the Entity Object Wizard to access it:

  1. In the Workspace view of the Navigator, right-click your class and choose Make.
  2. Close JDeveloper. 
  3. Navigate to <JDeveloper>\bin\jdeveloper.ini and add the following entry to the IDEClassPath, so it can find the compiled validation class:
    ..\myclasses
  4.   Save jdeveloper.ini.
  5.   Restart JDeveloper.

The following code implements a validation class named DemoCardValidator. In validateValue it implements business logic that is too complex to define using the Business Components' built-in rules. After compiling it, you can use it to create a validator type and then apply it to an Entity Object (for example, to the CreditCardNumber attribute of a Customer entity).

package d2e;
import oracle.jbo.server.rules.JbiValidator;
import oracle.jbo.server.util.VetoableChangeListener;
import oracle.jbo.server.util.PropertyChangeEvent;
import oracle.jbo.server.ValidationException;
public class DemoCardValidator implements JbiValidator {
private String description = " Verifies a credit card number. ";
private final int mMinValue = 100; // Arbitrary value for demo.
/**
* Return true if value is valid
*/
  public boolean validateValue(Object value) {
// Validates a credit card number by comparing the
// sum of the digits to mMinValue, defined above.
int checkSum = 0;
    // Assume the following card number format:
// "1234567890123456"
    String cardNo = value.toString();
for (int i = 0; i < cardNo.length(); i++) {
checkSum += Integer.parseInt(cardNo.substring(i, i + 1));
}
return (checkSum > mMinValue) ? true : false;
}
/**
* Invoked by framework for validation
  */
  public void vetoableChange(PropertyChangeEvent pce)
throws ValidationException {
Object newValue = (pce.getNewValue());
if (!validateValue(newValue)) {
Object objs[] = new Object[2];
objs[0] = pce.getPropertyName() + this.getDescription();
objs[1] = newValue;
String errCode = "001";
ValidationException ve = new ValidationException(errCode, objs);
throw ve;
}
}
/**
* Description of what this class validates
*/
    public String getDescription() {
return description;
}
/**
* Description of what this class validates
*/
public void setDescription(String str) {
description = str;
  }
}