Writing Validation Code in setAttribute Methods

You can specify business logic by editing specific validation methods. You start by using the Entity Object Wizard to add methods to an entity object. To write an attribute-level rule, use the wizard to add accessor methods to the entity object. An accessor method has the general name of getAttribute or setAttribute, where Attribute is replaced by the name of an entity's attribute.

The Business Component for Java framework defines the signatures for these methods and generates skeleton code; you add the code to implement the validation logic. For example, given an Emp entity object, the framework could generate a setSal method to validate the Sal attribute.

To write attribute-level validation code, edit an entity object's setAttribute method (use the Entity Object Wizard to generate a skeleton). By default, the skeleton method contains a call to Entity.setAttributeInternal. Insert your custom validation code before this call.

For example, suppose a Customer entity object has an attribute named creditCardNumber that represents the customer's credit card number. To validate the number, the following code calculates the sum of the digits and compares it to a given value. If the sum is less than that value, the method throws an exception. Otherwise, the card number is valid, and the code calls setAttributeInternal to continue the validation process.

public class Customer extends oracle.jbo.rt.Entity {
...
  public void setCreditCardNumber(String cardNo) {
    int checkSum = 0;
    // Minimum value chosen arbitrarily for this example.
    int MIN_VAL = 100;
    // Assume the following card number format:
    // "1234567890123456"
    for (int i = 0; i < cardNo.length(); i++) {
      checkSum += Integer.parseInt(cardNo.substring(i, i + 1));
    }
    if (checkSum < MIN_VAL) {
      throw new JboException("Invalid card number");
    }
    // Continue the validation process.
    // Generated calls follow.
    setAttributeInternal(CREDTICARDNUMBER, cardNo)      
  }
...
}

The following example method enforces a rule that requires a unique key value before setting a distribution ID. If the value is not unique, the method throws an exception; otherwise, it continues with the validation process.

public void setDistributionId(Number quantity)  {
  Object[] reqDistKey = new Object[1];
  reqDistKey[0] = this.distId;
  // If the key exists, then it's not unique.
  if (getDefinitionObject()
.findInstance((ApplicationModuleImpl)getApplicationModule(),
      new Key(reqDistKey)) != null )
  {
      throw new JboException("Must be unique");
  }
  // Generated calls follow.
  setAttributeInternal( DISTRIBUTIONID, quantity );
}