変換の入力の検証

すべての ZIETrans リッチ・クライアント変換は、com.ibm.hats.rcp.transform.IPrePostSubmitHandler インターフェースを実装します。これにより、特定の変換が表示されるとき、ZIETrans ランタイムに対して要求が発行される直前および直後に何が行われるかを制御できます。このインターフェースの 1 次機能によって、ZIETrans ランタイムに対して要求が発行される前に、ユーザーから変換に提供された値を検証できます。1 つ以上のユーザーの応答に問題が見つかった場合、ユーザーは、要求のキャンセル、パラメーターの変更、コマンドの変更、およびユーザーへのメッセージの表示を行うことができます。
注: 検証は、変換クラスの handlePreSubmit メソッドで実行する必要があります。これは、ZIETrans ランタイムに対して要求が発行される直前に、このメソッドが呼び出されるためです。要求が一度発行されると、その変更もキャンセルも行えません。

次のサンプルでは、ユーザーが行った入力に対する検証の実行方法を示しています。無効値が指定された場合、実行依頼はキャンセルされ、ユーザーに対してメッセージが表示されます。

変換クラスで handlePreSubmit メソッドがオーバーライドされていない場合、以下の手順を実行してください。
  1. (Java™ エディターで) 変換のソースを右クリックし、「ソース」>「メソッドのオーバーライド/実装」を選択します。
  2. 「RcpTransformation」ノードを展開し、「handlePreSubmit(CommandEvent)」の横にチェック・マークを付け、「OK」をクリックします。
  3. 変換ソースにスタブ・メソッドが挿入されます (Java コード・スタイルの設定によっては、コード例が下記のようにならない場合があります)。
        public void handlePreSubmit(CommandEvent event) {
            // TODO Auto-generated method stub
            super.handlePreSubmit(event);
        }
次のコード・サンプルは、仮想部分数量 (hypothetical part quantity) フィールドの範囲検査を実行する方法を示しています。ユーザーが指定した数量が 500 以下の場合、メッセージが表示され、実行依頼はキャンセルされます。このフィールドは、この変換によって変換されるホスト画面の 4 行目、20 カラム目にあると想定します。既存の handlePreSubmit(CommandEvent) メソッドを以下に置き換えます:
    public void handlePreSubmit(CommandEvent event) {
        // Avoid validation if the user is attempting to disconnect the session
        if (event.getCommand().equals(RuntimeConstants.CMD_DISCONNECT)) {
            return;
        }

        // Retrieve the value of the "quantity" field using the 
        // IHostScreenDataAccessService interface
        // (which is accessed by calling the getHostScreenDataAccessService() 
        // method on the session service)
        // The getFieldValue() method takes three parameters 
        //
        //   Returns the value of host screen field at the specified position.
        //
        //   @param startPos The start position of the field.
        //   @param offset The offset position of the field.
        //   @param length The length of the field.
        //
        //   @return The value of the host screen field.
        // 
        // The Component.convertRowColToPos() method converts a row, column value 
        // into a linear screen position. 
        // "5" indicates the length of the field.
        String quantityString = getSessionService().
                            getHostScreenDataAccessService().
                            getFieldValue(Component.convertRowColToPos(4, 20, 
                            getHostScreen().getSizeCols()),0, 5);

        // Convert the numeric string into an int
        int quantity = 0;
        try {
            quantity = Integer.parseInt(quantityString);
        } catch (Exception ex) {
        }

        // Check to ensure the quantity entered is greater than 500
        if (quantity < 500) {
            // Display a message to the user indicating what the problem is
            MessageDialog.openError(getShell(), 
                            getSessionService().getApplication().getName(),
                    "You must order at least 500 units.");

            // Cancel the event (this prevents the request from being made)
            event.setCanceled(true);
        }
    }