その他の属性の変換方法

属性の変換は、以下の方法で対話式に行うことができます。

このセクションでは、次のカスタム変換のインプリメント方法を説明します。

内容ベースのロジック

Customer.CustomerStatus = 'Inactive' 
 if SAP_CustomerMaster.DeleteInd = 'X'. 
 

あるいは、CustomerStatus = 'Active' です。

カスタム変換を作成し、すべてのコードを作成して内容ベースのロジックを自分でインプリメントすることができます。しかし、より適切な方法は、DeleteInd (SAP_CustomerMaster オブジェクト内) と CustomerStatus 間に移動変換を作成することから開始する方法です (属性の移動手順については、"ソース属性の宛先属性へのコピー"を参照)。

この結果、Map Designer Express により移動変換が生成されます。サンプル・コードを以下に示します。

{
    Object _cw_CpBTBSourceValue = null;
  
    //
    // RETRIEVE SOURCE
    // ---------------
    //
    // Retrieve the source value from the source business object and
    // place it in a local variable for code safety.
    //
    _cw_CpBTBSourceValue = ObjSAP_CustomerMaster.get("DeleteInd");
  
    //
    // SET DESTINATION
    // ---------------
    //
    // Put the source value into the destination business object
    // attribute.
    //
    {
       Object _cw_SetSrcVal = _cw_CpBTBSourceValue;
       BusObj _cw_SetDestBusObj = ObjCustomer;
       String _cw_SetDestAttr = "CustomerStatus";
  
       //
       // Set the destination value only if neither
       // source nor destination is null.
       //
       if ((_cw_SetSrcVal != null) && (_cw_SetDestBusObj != null))
       {
          if (dataValidationLevel >= 1)
          {
             if (!_cw_SetDestBusObj.validData(_cw_SetDestAttr, _cw_SetSrcVal))
             {
                String warningMessage =
 "Invalid data encountered when attempting to set the value of the
 ¥'_cw_SetDestAttr¥' attribute of BusObj ¥'_cw_SetDestBusObj¥' while running
 map ¥'" + getName() + "¥'.  The invalid value was ¥'" + _cw_SetSrcVal
 + "\'.";
                //
                // Log a warning about this failure.
                //
                logWarning(warningMessage);
                if (failOnInvalidData)
                {
                   //
                   // Fail the map execution with a warning message.
                   //
                   throw new MapFailureException(warningMessage);
                }
             }
          }
 // SECTION THAT NEEDS TO BE UPDATED WITH THE LOGIC
  
       if (_cw_SetSrcVal != null)
          {
          if (_cw_SetSrcVal instanceof BusObj)
             {
             //
             // Since BusObjs are not immutable, we need to make
             // a copy of the source object before actually
             // putting it into the destination attribute.
             //
             _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                ((BusObj)_cw_SetSrcVal).duplicate());
             }
          else if (_cw_SetSrcVal instanceof BusObjArray)
             {
             //
             // Since BusObjs are not immutable, we need to make
             // a copy of the source object before actually
             // putting it into the destination attribute.
             //
             _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                ((BusObjArray)_cw_SetSrcVal).duplicate());
             }
          else
             {
             //
             // Since our version of simple data types are immutable in
             // Java (Strings included), we do not have to make a copy
             // of the source value here.
             //
             _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, _cw_SetSrcVal);
             }
          }
       }
    }
 }
  
 // HERE IS THE MODIFIED CODE
  
    if (_cw_SetSrcVal != null)
       {
       if (((String)_cw_SetSrcVal).equals("X"))
          _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, "Inactive");
       else
          _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, "Active");
       }
  
       }
    }
 }
 

Activity Editor の「ファイル」メニューから、「保管」を選択して変更を保管します。

動詞ベース・ロジック

Customer.CustomerStatus = 'Inactive' if Verb = 'Create' あるいは、CustomerStatus = 'Active' です。

内容ベースのロジックの手順に従います。ただし、以下の条件ステートメントを使用して、生成されたコードのセクションを置き換えます。

// HERE IS THE MODIFIED CODE
 if (_cw_SetSrcVal != null)
    {
    if (ObjSAP_CustomerMaster.getVerb() .equalsIgnoreCase("Create"))
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, "Inactive");
    else
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,"Active");
 

ソース・データが不足している場合のデフォルト値の指定

SAP_CustomerMaster.StateCustomer.CustomerAddress.State にマップします。SAP の状態が不足している場合は、デフォルトで CA に設定します。

宛先属性にソース値を設定される前に、ソース属性が null でないかどうかをチェックします。

例: この例では、ソース・データが不足していると、宛先属性にデフォルト値が設定されます。

SAP_CustomerMaster.StateCustomer.CustomerAddress.State に移動することから開始します。条件ステートメントを以下のように変更します。

// HERE IS THE MODIFIED CODE
 if (_cw_SetSrcVal != null)
    _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, _cw_SetSrcVal);
 else
    _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, "CA");
 _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, "CA");
 

ヒント:
ソース属性を宛先属性にコピーすることからコーディングを開始した場合は、デフォルト値を設定するコード行は 2 回入力する必要があります。これは、ソース属性が宛先属性に移動されるときに生成されるコードで、ソース属性が null でないことを 2 回チェックするからです。したがって、デフォルト値を入力する必要があります。

呼び出しコンテキストに基づくロジック

呼び出しコンテキストの値をチェックする必要がある場合は、String 型である組み込み変数 strInitiator を使用します。

例: 呼び出しコンテキストが EVENT_DELIVERY かどうかをチェックするには、以下のステートメントを使用します。

if (strInitiator.equals(MapExeContext.EVENT_DELIVERY))
 //rest of the code
 

ソース・データが不足する場合は、マップを強制的に失敗させる

SAP_CustomerMaster.StateCustomer.CustomerAddress.State にマップします。SAP の状態が不足している場合は、マップの実行を停止します。

SAP_CustomerMaster.StateCustomer.CustomerAddress.State に移動することから開始します。その後、1 つの if() 文を追加して、SAP の State 属性が null かどうかをチェックします。生成されるコードは、以下のようになります。

{
    Object _cw_CpBTBSourceValue = null;
  
    //
    // RETRIEVE SOURCE
    // ---------------
    // 
    // Retrieve the source value from the source business object and
    // place it in a local variable for code safety.
    //
    _cw_CpBTBSourceValue = ObjSAP_CustomerMaster.get("State");
  
    //
    // SET DESTINATION
    // ---------------
    // 
    // Put the source value into the destination business object
    // attribute.
    //
    {
       Object _cw_SetSrcVal = _cw_CpBTBSourceValue;
       BusObj _cw_SetDestBusObj = ObjCustomer;
       String _cw_SetDestAttr = "CustomerAddress.State";
    // New code
    if (_cw_SetSrcVal == null)
       {
       String errorMessage = "Data in the state attribute is missing";
       logError(errorMessage);
       //
       // Fail the map execution with a warning message.
       //
       throw new MapFailureException(errorMessage);
       }
    // End of new code
  
    //
    // Set the destination value only if neither
    // source nor destination is null.
    //
    else if ((_cw_SetSrcVal != null) && (_cw_SetDestBusObj != null))
       {
       if (dataValidationLevel >= 1)
          {
          if (!ObjCustomer.validData("CustomerAddress.State", _cw_SetSrcVal))
             {
             String warningMessage =
 "Invalid data encountered when attempting to set the value of
 the ¥"CustomerAddress.State¥" attribute of BusObj ¥'ObjCustomer¥'
 while running map ¥'" + getName() + "¥'.  The invalid value
 was ¥'" + _cw_SetSrcVal + "¥'.";
  
             //
             // Log a warning about this failure.
             //
             logWarning(warningMessage);
             if (failOnInvalidData)
                {
                //
                // Fail the map execution with a warning message.
                //
                throw new MapFailureException(warningMessage);
                }
             }
          }
  
       if (_cw_SetSrcVal != null)
          _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr, _cw_SetSrcVal);
  
       }
    }
 }
 

マップが実行されて、State 属性が設定されていないと、ダイアログ・ウィンドウとサーバー画面の両方にエラー・メッセージが表示されます。サーバー画面のメッセージは、次のようになります。

[1999/09/22 17:58:51.008] [Server] 
 Sub_SaCwCustomerMaster: Error Data 
 in the state attribute is missing
 

マップは実行を停止します。

ヒント:
以下のコードを使用して、マップが失敗したシステムで生成された実際のエラー・メッセージを表示します。
try
    {
    // your code
    }
 catch (Exception e)
    {
   throw new MapFailureException(e.toString());
    }
 

メッセージ・ファイルからのメッセージのロギング

ソース・データが不足する場合は、マップを強制的に失敗させるのサンプルで、エラー・メッセージを画面に表示するための logError() メソッドの使用方法に注意してください。 汎用メッセージ・ファイル CWMapMessages txt (¥DLMs¥messages に保管されています) に用意されているメッセージを使用できます。 メッセージ・ファイルの詳細については、付録 A, メッセージ・ファイルを参照してください。

CWMapMessages.txt のフォーマットは、以下のとおりです。

# critical data is missing error
 10
 Data in the {1} attribute is missing. Map execution stopped.
 # Another error
 11
 Another error message
 

{1} の代わりに単語 State が、表示される必要のあるエラーに直接対応して使用できることに注意してください。ただし、特定の単語を直接インプリメントすると、メッセージの汎用性は失われます。別の属性にも重大なデータが存在する場合は、その特定の属性に固有のメッセージ・ファイルに、別のメッセージが必要です。

注:
CWMapMessages.txt ファイルは変更しないでください。独自のメッセージを作成する必要がある場合は、Map Designer Express の「メッセージ」タブに入力します。Map Designer により mapName_locale.txt という 名前 (例: mapName_en_US.txt) のマップ固有のエラー・メッセージ・ファイルが 作成されます (ここで、mapName はマップと同じ名前です)。サーバーにより、展開後にこのメッセージ・ファイルが ¥DLMs¥messages ディレクトリー内に保管されます。

メッセージ番号 10 を表示するには、以下のコードを使用します。

logWarning(10, "State");
 

単語 State により、メッセージ・テキスト内の {1} が置換されます。

次のメッセージが InterChange Server Express ログ・ファイルに表示されます。

[1999/09/23 10:17:43.648] [Server] 
 Sub_SaCwCustomerMaster: 
 Warning 10: Data in the State attribute is missing. 
 Map execution stopped.
 

日付の形式設定

マッピング API では、DtpDate クラスに日付の形式設定のメソッドを用意しています。表 61 に、日付の形式設定メソッドを要約します。

表 61. DtpDate クラスの日付の形式設定メソッド
日付の形式設定 DtpDate メソッド
日付から月名を取得 getMonth(), getShortMonth()
日付から月の数値を取得 getIntMonth(), getNumericMonth()
日を取得 getDayOfMonth(), getIntDay()
曜日を取得 getDayOfWeek(), getIntDayOfWeek()
日付から年を取得 getYear(), getIntYear()
時間値の取得 getHours()
日付から分値を取得 getMinutes(), getIntMinutes()
日付から秒値を取得 getSeconds(), getIntSeconds()
日付からミリ秒値を取得 getMSSince1970()
リストから最も古い日付を取得 getMinDate(), getMinDateBO()
リストから最新の日付を取得 getMaxDate(), getMaxDateBO()
指定形式に従った日付を構文解析 DtpDate()
指定またはデフォルトの形式で日付を取得 toString()
IBM 汎用日付形式へ日付を再形式設定

getCWDate()
日付への日数の追加 addDays()
日付への平日の数の追加 addWeekdays()
日付への年数の追加 addYears()
日付間の日数の計算 calcDays()
日付間の平日の数の計算 calcWeekdays()
日付の比較 after(), before()
完全な月名を使用 get12MonthNames(), set12MonthNames(), set12MonthNamesToDefault()
短い月名を使用 get12ShortMonthNames(), set12ShortMonthNames(), set12ShortMonthNamesToDefault()
曜日名の使用 get7DayNames(), set7DayNames(), set7DayNamesToDefault()

ヒント:
常に DtpDateException をキャッチします。 これにより、日付形式が無効な場合、メッセージが InterChange Server Express ログに送信されることが保証されます。

汎用日付形式の使用

IBM では、汎用ビジネス・オブジェクト内の次の日付形式を使用します。

YYYYMMDD HHMMSS
 

この形式は、汎用日付形式 と呼ばれます。

汎用日付形式への変換手順

アプリケーション固有の日付をこの汎用形式に変換するには、DtpDate クラスの getCWDate() メソッドを使用します。

例: 例えば、SAP 日付属性を汎用日付にマップするには、ソース属性 (SAP 日付ストリング) を宛先属性 (汎用日付ストリング) にコピーします。

  1. DtpDate オブジェクトを作成して汎用日付を保持します。

    DtpDate() コンストラクターで SAP 日付ストリング (YYYYMMDD) を構文解析して、ソース属性 (SAP 日付ストリング) を新しい DtpDate オブジェクト (汎用日付ストリング) にコピーします。

  2. getCWDate() メソッドで、SAP 日付ストリングを汎用日付形式 (YYYYMMDD HHMMSS) に変換します。
  3. setWithCreate() メソッドで、宛先属性を作成して、その値を汎用日付形式に初期化します。

このコードの最後のセクションは、次のようになります。

// HERE IS THE MODIFIED CODE
 if (_cw_SetSrcVal != null)
    {
    try
       {
       DtpDate myDate = new DtpDate((String)_cw_SetSrcVal, "YMD");
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
       myDate.getCWDate());
       }
    catch (DtpDateException de)
       { 
       logError(5501);
       logInfo(de.getMessage());
       }
    }
 

例: Clarify 日付属性を汎用日付にマップするには、ソース属性 (Clarify 日付ストリング) を宛先属性 (汎用日付ストリング) にコピーします。

  1. DtpDate オブジェクトを作成して汎用日付を保持します。

    DtpDate() コンストラクターで Clarify 日付ストリング (MM/DD/YYYY HH:MM:SS) を構文解析して、ソース属性 (Clarify 日付ストリング) を新しい DtpDate オブジェクト (汎用日付ストリング) にコピーします。

  2. getCWDate() メソッドで、Clarify 日付ストリングを汎用日付形式 (YYYYMMDD HHMMSS) に変換します。
  3. setWithCreate() メソッドで、宛先属性を作成して、その値を汎用日付形式に初期化します。

次のコードは、Clarify 日付を汎用日付に変換します。

if (_cw_SetSrcVal != null)
    {
    try
       {
       DtpDate myDate = new DtpDate((String)_cw_SetSrcVal,
          "M/D/Y h:m:s");
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
          myDate.getCWDate());
       }
    catch (DtpDateException de)
       { 
       logError(5501);
       logInfo(de.getMessage());
       }
    }
 

汎用日付を Clarify 形式に変換するには、次のコードを使用します。

if (_cw_SetSrcVal != null)
    {
    try
       {
       DtpDate myDate = new DtpDate((String)_cw_SetSrcVal, "YMD hms");
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
       myDate.toString("M/D/Y h:m:s", true));
       }
    catch (DtpDateException de)
       { 
       logError(5501);
       logInfo(de.getMessage());
       }
 }
 

汎用日付形式からの変換手順

汎用日付形式から SAP 日付形式への変換は、次の手順で行います。

  1. DtpDate オブジェクトを作成して SAP 日付を保持します。

    DtpDate() コンストラクターで汎用日付ストリング (YYYYMMDD HHMMSS) を構文解析して、ソース属性 (汎用日付ストリング) を新しい DtpDate オブジェクト (SAP 日付ストリング) にコピーします。

  2. toString() メソッドで IBM 汎用日付形式を SAP 日付ストリング (YYYYMMDD) メソッドに変換します。
  3. setWithCreate() メソッドで宛先属性を作成し、その値を SAP 日付形式に初期化します。

次のコード・フラグメントは、この汎用日付から SAP 日付への変換を示します。

// HERE IS THE MODIFIED CODE
 if (_cw_SetSrcVal != null)
    {
    try
       {
       DtpDate myDate = new DtpDate((String)_cw_SetSrcVal, "YMD hms");
       _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
       myDate.toString("YMD"));
       }
    catch (DtpDateException de)
       { 
       logError(5501);
       logInfo(de.getMessage());
       }
    }
 

現在日付の取得

現在のシステム日付で初期化される DtpDate オブジェクトを作成するには、パラメーターを指定しないで DtpDate() コンストラクターを使用します。

DtpDate()
 

例: 現在日付を汎用形式にマップするには、次のコードを使用します。

//get the current date
 DtpDate myDate = new DtpDate();
 // format the date to the destination object's format and map
 _cw_SetDestBusObj.set(_cw_SetDestAttr, myDate.getCWDate());
 

現在日付をアプリケーション固有の形式に変換するには、次のコードを使用します。

//get the current date
 DtpDate myDate = new DtpDate();
 // format the date to the destination object's format and map
 _cw_SetDestBusObj.set(_cw_SetDestAttr, myDate.toString(
    "application format");
 

式ビルダーを使用したストリング変換

変換コードを作成する場合、特定の属性の参照、ストリングの操作、または API メソッドの呼び出しのために、複雑な Java 式を作成する必要があることがあります。これらの式は、Activity Editor で手動で入力することも、式ビルダーを使用して式を対話式に作成することもできます。 式ビルダーは、Activity Editor 内から使用できるユーティリティーです。

ヒント: 代わりに、Activity Editor のグラフィック表示を使用することもできます。

式ビルダーを表示するには、Activity Editor 内の式を挿入する位置にカーソルを合わせ、以下のいずれかを行います。

図 85 に、式ビルダーの主要コンポーネントを示します。

図 85. 「式ビルダー」ダイアログ

注:
API リストでは、オブジェクトが存在する場合、番号 1 は左側のウィンドウから選択されたオブジェクトを参照し、番号 2 は中央のウィンドウから選択されたオブジェクトを参照します。API を挿入する場合は、括弧で変数を囲みます (<<変数>>)。括弧と変数を値で置き換える必要があります。

このセクションでは、式ビルダーを使用して次のストリング変換を実行する方法について説明します。

大文字テキストへの変換手順

SAP_CustomerMaster.CustomerNameCustomer.AccountOpenedBy に移動して、すべての文字を大文字に変換します。

toUpperCase() ストリング処理関数を使用するには、次の手順を行います。

  1. SAP_CustomerMaster.CustomerName から Customer.AccountOpenedBy への移動変換を実行します。
  2. Customer.AccountOpenedBy に関連した変換規則列内をダブルクリックして、「Activity Editor」ウィンドウを表示します。Activity Editor で、宛先がソース・データに設定される位置までスクロールダウンします。それを変更して、次のコードのみを含むようにします。
    if (_cw_SetSrcVal != null)
        {
        //
        // Since our version of simple data types are immutable in
        // Java (Strings included), we do not have to make a copy
        // of the source value here.
        //
        _cw_SetDestBusObj.setWithCreate(
        _cw_SetDestAttr, _cw_SetSrcVal);
        }
     

    _cw_SetSrcVal 変数には SAP.CustomerName が含まれ、_cw_SetDestAttr には AccountOpenedBy が含まれます。

  3. CustomerName を大文字に変換してから、AccountOpenedB にコピーします。

    使用するメソッドがわかっている場合は、それを上記のコード行に追加します。そうでない場合は、式ビルダーを使用すると、正しいメソッドを見つけることができます。次の手順で行います。

    1. _cw_SetSrcVal をコピー (Ctrl+C) します。
    2. _cw_SetSrcVal を強調表示し (このコードは式ビルダーで生成されるコードで置き換えられます)、クリックしてツール・メニューから「式ビルダー」を選択します。
    3. 「メイン・カテゴリー」リストで「ストリング」を選択します。
    4. ストリング API のリストから toUpperCase() メソッドを選択します。
    5. 「Insert API」をクリックします。
    6. 上部ウィンドウにこのメソッドが表示されるときに、プレースホルダーである単語「string,」が、先程コピーした _cw_SetSrcVal で置き換えられます。
    7. 「OK」をクリックします。

      結果: メソッド呼び出しがコードに挿入されます。

    8. 「ファイル」メニューから「保管」オプションを選択して、コードを保管します。

ストリング処理メソッドの使用

次のストリング変換では、ストリング処理メソッド (length()substring() など) を使用して、SAP_CustomerMaster.AddressLine1Customer.CustomerAddress.AddressLine1AddressLine2 に移動します。

ストリング変換の実行手順

このストリング変換は、次の手順で行います。

  1. AddressLine1CustomerAddress.AddressLine1 への移動変換を実行します。
  2. AddressLine1 の Activity Editor ウィンドウを表示して、宛先がソース・データに設定される位置までスクロールダウンします。それを変更して、次のコードを含むようにします。
    if (_cw_SetSrcVal != null)
        //
        // Since our version of simple data types are immutable in
        // Java (Strings included), we do not have to make a copy
        // of the source value here.
        //
        {
        _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
           _cw_SetSrcVal);
        }
     

    _cw_SetSrcVal 変数には SAP.AddressLine1 が含まれ、_cw_SetDestAttr には CustomerAddress.AddressLine1 が含まれます。

  3. SAP.AddressLine1 の長さをチェックして、ストリング全体を CustomerAddress.AddressLine1 にマップするか、またはコンマに先行する SAP.AddressLine1 のサブストリングをマップします。

    コードは自分で変更することも式ビルダーを使用することもできます。どちらの場合も、String クラスの length()substring(int, int)substring(int)、および indexOf(int) メソッドを使用します。

  4. コードを以下のように変更します。
    if (_cw_SetSrcVal != null)
        {
        // first check the length of _cw_SetSrcVal
        if (((String)_cw_SetSrcVal).length() < 10)
           {
           // if it is less then 10, map it to AddressLine1
           _cw_SetDestBusObj.setWithCreate(
              _cw_SetDestAttr, _cw_SetSrcVal);
           }
        else
           {
           // if the length is not less than 10, search for comma
           int index = ((String)_cw_SetSrcVal).indexOf(",");
      
           // if comma is found, take a substring of _cw_SetSrcVal up to
           // the comma and map it to AddressLine1
           if (index != -1)
              _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                 ((String)_Cw_SetSrcVal).substring(0, index));
              // if comma is not found, take first 9 characters of
              // _cw_SetSrcVal and map them to AddressLine1
           else
              _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                 ((String)_Cw_SetSrcVal).substring(0, 9));
           }
        }
     
  5. SAP.AddressLine1CustomerAddress.AddressLine2 に移動します。コードの下部を変更して、次のコードを組み込みます。
    if (_cw_SetSrcVal != null)
        {
        // first check the length of _cw_SetSrcVal
        if (((String)_cw_SetSrcVal).length() >= 10)
           {
           // if the length is not less than 10, search for comma
           int index = ((String)_cw_SetSrcVal).indexOf(",");
           // if comma is found, take a substring of _cw_SetSrcVal after
           // the comma and map it to AddressLine2
      
           if (index != -1)
              _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                 ((String)_Cw_SetSrcVal).substring(index + 1));
              // if comma is not found, take all characters of
              // _cw_SetSrcVal starting at the 10th and map them to
              // AddressLine1
      
           else
              _cw_SetDestBusObj.setWithCreate(_cw_SetDestAttr,
                    ((String)_Cw_SetSrcVal).substring(9));
           }
        }
     

ソース・データの検証

自分でコードを作成する場合、ソース・データが null またはブランクでないようにします。ソース属性が null かどうかを検査するには、次のいずれかを使用します。

ソース属性が空のストリングかどうかを検査するには、以下を実行します。

Copyright IBM Corp. 2004