独自のXML Validator Beanを作成してJDeveloperに追加すると、他の開発者が同じ検証を宣言的に再利用できます。カスタム検証規則は次の3つの手順で作成します。
スケルトン・クラスを生成するには、次のようにします。
次に、生成したスケルトン・クラスにコードを追加して検証クラスを実装します。
検証クラスを実装するには、次のようにします。
検証クラスでは、vetoableChange
およびvalidateValue
という2つの主なメソッドを提供するJbiValidator
インタフェースを実装します。ビジネス・コンポーネント・フレームワークでは、属性値が設定される場合、vetoableChange
がコールされ、PropertyChangeEvent
に渡されます。通常は、validateValue
をコールするためにvetoableChange
を実装します。validateValue
がfalseを返した場合、vetoableChange
により、失敗した内容に関する情報とともに例外がスローされます。validateValue
の実装で、(ValidationException
を拡張する必要がある)独自の例外をスローすることもできます。
たとえば次のコードでは、DemoCardValidator
という名前の検証クラスが実装されます。validateValue
で、ビルトインの規則では定義できない、複雑なビジネス・ロジックが実装されます。コンパイル後、このクラスを使用してValidatorタイプを作成し、次にエンティティ・オブジェクト(たとえば、Customer
エンティティのCreditCardNumber
属性)に適用できます。
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; } }
次に、新規クラスをJDeveloperでアクセス可能にします。これを実行するには、JDeveloperのIDEクラスパスを変更し、エンティティ・オブジェクト・エディタでこのパスにアクセスできるようにします。
新規Validator BeanをIDEで表示するには、次のようにします。
<JDev_install>
/jdev/bin/jdev.conf
に移動し、次のエントリを追加します。
AddJavaLibPath ../myclasses
jdev.conf
を保存します。
JDeveloperを再起動します。
これで、Validator BeanがJDeveloperでアクセス可能になりました。カスタムValidatorの使用方法の詳細は、「Oracle ADF Business Componentsでのカスタムの検証規則の使用方法」を参照してください。
Oracle ADF Business Componentsでの宣言型の検証規則の使用
Copyright © 1997, 2007, Oracle. All rights reserved.