当座預金口座の借方記入メッセージ・サブフローの作成

当座預金口座の借方記入メッセージ・サブフローを作成するには、以下の手順に従ってください。

  1. 次のように新規メッセージ・フローを作成します。
    1. 既存の SCANodesSample メッセージ・ブローカー・プロジェクトを右クリックし、「新規」>「メッセージ・フロー」をクリックします。
    2. メッセージ・フロー名」を CurrentAccountDebit に設定し、「終了」をクリックします。
  2. メッセージ・フロー・エディターで、次の表にリストされているノードを追加して名前変更します。
    ノード・タイプ ノード名
    Input Input
    JavaCompute RetrieveCurrentAccountBalance
    JavaCompute DebitCurrentAccountBalance
    JavaCompute UpdateCurrentAccountBalance
    Output Output

  3. 以下の表に示すようにノードを接続します。

    ノード名 ターミナル 接続先のノード
    Input Out RetrieveCurrentAccountBalance
    RetrieveCurrentAccountBalance Out DebitCurrentAccountBalance
    DebitCurrentAccountBalance Out UpdateCurrentAccountBalance
    UpdateCurrentAccountBalance Out Output
  4. RetrieveCurrentAccountBalance ノードを以下のようにカスタマイズします。
    1. RetrieveCurrentAccountBalance ノードをダブルクリックし、「プロジェクト名」が SCANodesSampleJava であることを確認し、「次へ」をクリックします。
    2. ビルド・パス上のソース・フォルダー」と「デフォルト出力フォルダー」に SCANodesSampleJava が入っていることを確認し、「次へ」をクリックします。
    3. 「パッケージ」sca.broker.sample.currentaccount に 設定し、「次へ」をクリックします。
    4. メッセージの変更用クラス」テンプレートを選択し、「終了」をクリックします。
    5. RetrieveCurrentAccountBalance ノードを右クリックし、「Java を開く」をクリックします。
    6. 次の入力ステートメントを追加します。
      import java.io.BufferedReader;
      import java.io.File;
      import java.io.FileReader;
      import java.io.IOException;
      		
    7. 次の Java コードを追加します。
      MbMessage locEnv = inAssembly.getLocalEnvironment();
      // create new message
      MbMessage newLocEnv = new MbMessage(locEnv);
      BufferedReader bufferedReader = null;
      try {
        //Read the current account balance from the local file system
        File file = null;
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("windows") > -1) {
            file = new File("C:\\tmp\\CurrentAccount.txt");        	
        } else {
            file = new File("/tmp/CurrentAccount.txt");        	
        }
        //Construct the BufferedReader object
        bufferedReader = new BufferedReader(new FileReader(file));            
        //Put the current account balance in the LE
        MbElement variables = newLocEnv.getRootElement()
        .createElementAsLastChild
        (MbElement.TYPE_NAME, "Variables", null);
        variables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
        "oldBalance", 
        bufferedReader.readLine());           
      } catch (Exception e) {
        throw new MbUserException( 
        CurrentAccount_RetrieveCurrentAccountBalance.class.getName(),
      					"evaluate()",
      					"",
      					"",
      					e.getMessage(),
      					null);
      } finally {
        //Close the BufferedReader
        try {
          if (bufferedReader != null)
            bufferedReader.close();
        } catch (IOException e) {
          throw new MbUserException( 
          CurrentAccount_RetrieveCurrentAccountBalance.class.getName(),
          					"evaluate()",
          					"",
          					"",
          					e.getMessage(),
          					null);
        }
      }
      MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly,
      				newLocEnv, inAssembly.getExceptionList(), 
      				inAssembly.getMessage());
              
    8. 以下の Java コードは、ここで作成した MbMessageAssembly と競合するため、除去します。
      MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly, outMessage);
         		
  5. DebitCurrentAccountBalance ノードを以下のようにカスタマイズします。
    1. DebitCurrentAccountBalance ノードをダブルクリックし、「プロジェクト名」が SCANodesSampleJava であることを確認し、「次へ」をクリックします。
    2. ビルド・パス上のソース・フォルダー」と「デフォルト出力フォルダー」に SCANodesSampleJava が入っていることを確認し、「次へ」をクリックします。
    3. 「パッケージ」sca.broker.sample.currentaccount に 設定し、「次へ」をクリックします。
    4. メッセージの作成用クラス」テンプレートを選択し、「終了」をクリックします。
    5. DebitCurrentAccountBalance ノードを右クリックし、「Java を開く」をクリックします。
    6. 次の入力ステートメントを追加します。
      import java.math.BigDecimal;
      		
    7. 次の Java コードを追加します。
      //Go to tree roots
      MbElement inRoot = inMessage.getRootElement();			
      MbElement outRoot = outMessage.getRootElement();	
      MbMessage locEnv = inAssembly.getLocalEnvironment();
      MbMessage newLocEnv = new MbMessage();
      //Get the operation name
      MbElement sca = locEnv.getRootElement().getFirstElementByPath("SCA");
      MbElement input = sca.getFirstElementByPath("Input");
      MbElement operation = input.getFirstElementByPath("Operation");
      String operationName = operation.getValue().toString();			
      //Get the old balance
      MbElement variables = locEnv.getRootElement().getFirstElementByPath("Variables");
      float oldBalance = Float.parseFloat(variables.getFirstElementByPath("oldBalance").getValue().toString());			
      //Get the amount to transfer
      MbElement soap = inRoot.getFirstElementByPath("SOAP");
      MbElement body = soap.getFirstElementByPath("Body");
      MbElement action = body.getLastChild();
      MbElement request = action.getLastChild();
      MbElement amount = request.getFirstElementByPath("amount");
      float transferValue = Float.parseFloat(amount.getValue().toString());	
      //Calculate the difference
      float newBalance = oldBalance - transferValue;
      MbElement newVariables = newLocEnv.getRootElement().createElementAsLastChild(MbElement.TYPE_NAME, "Variables", null);
      //Setup the message reply
      MbElement newXMLNSC = outRoot.createElementAsLastChild
      (MbXMLNSC.PARSER_NAME);
      MbElement newOperation = newXMLNSC.createElementAsLastChild
      (MbElement.TYPE_NAME, 
      operationName + "Response", null);
      newOperation.setNamespace("http://CurrentAccount/");
      MbElement newResponse = newOperation.createElementAsLastChild
      (MbElement.TYPE_NAME, 
      "CurrentAccountResponse", null);
      //If the difference is negative then do not accept the 
      transfer and retain old balance
      if (newBalance < 0) {	                 
      newVariables.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
      "newBalance", 
      new BigDecimal(oldBalance).setScale
      (2, java.math.BigDecimal.ROUND_HALF_UP).toString());	              
      newResponse.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
      "accept", "no");
      //Otherwise accept the transfer and set new balance in LE
      } else {	              newVariables.createElementAsLastChild
      (MbElement.TYPE_NAME_VALUE, 
      "newBalance", new BigDecimal(newBalance).setScale
      (2, java.math.BigDecimal.ROUND_HALF_UP).toString());	           
       newResponse.createElementAsLastChild(MbElement.TYPE_NAME_VALUE, 
       "accept", "yes");				
      }
      // Create the new assembly with the new property overrides
      MbMessageAssembly outAssembly = new MbMessageAssembly(
      					inAssembly,
      					newLocEnv,
      					inAssembly.getExceptionList(),
      					outMessage);
              
    8. 以下の Java コードは、ここで作成した MbMessageAssembly と競合するため、除去します。
      MbMessageAssembly outAssembly = new MbMessageAssembly(inAssembly, outMessage);
         		
  6. UpdateCurrentAccountBalance ノードを以下のようにカスタマイズします。
    1. UpdateCurrentAccountBalance ノードをダブルクリックし、「プロジェクト名」が SCANodesSampleJava であることを確認し、「次へ」をクリックします。
    2. ビルド・パス上のソース・フォルダー」と「デフォルト出力フォルダー」に SCANodesSampleJava が入っていることを確認し、「次へ」をクリックします。
    3. 「パッケージ」sca.broker.sample.currentaccount に 設定し、「次へ」をクリックします。
    4. メッセージのフィルタリング用クラス」テンプレートを選択し、「終了」をクリックします。
    5. UpdateCurrentAccountBalance ノードを右クリックし、「Java を開く」をクリックします。
    6. 次の入力ステートメントを追加します。
      import java.io.BufferedWriter;
      import java.io.File;
      import java.io.FileWriter;
      import java.io.IOException;
      import java.math.BigDecimal;
      		
    7. 次の Java コードを追加します。
      MbMessage locEnv = assembly.getLocalEnvironment();		
      //Read the new balance from the LE
      MbElement variables = locEnv.getRootElement().getFirstElementByPath("Variables");
      String newBalance = new BigDecimal(variables.getFirstElementByPath
      ("newBalance").getValue().toString()).setScale(2, BigDecimal.ROUND_HALF_UP).toString();
      BufferedWriter bufferedWriter = null;     
      try {
        //Update current account balance on local file system
        File file = null;;
        String OS = System.getProperty("os.name").toLowerCase();
        if (OS.indexOf("windows") > -1) {
            file = new File("C:\\tmp\\CurrentAccount.txt");        	
        } else {
            file = new File("/tmp/CurrentAccount.txt");        	
        }
        //Construct the BufferedWriter object
        bufferedWriter = new BufferedWriter(new FileWriter(file));
        //Start writing new balance to the output stream
        bufferedWriter.write(newBalance);           
      } catch (Exception e) {
        throw new MbUserException( CurrentAccount_UpdateCurrentAccountBalance.class.getName(),
      					"evaluate()",
      					"",
      					"",
      					e.getMessage(),
      					null);
      } finally {
        //Close the BufferedWriter
        try {
          if (bufferedWriter != null) {
            bufferedWriter.flush();
            bufferedWriter.close();
          }
        } catch (IOException e) {
          throw new MbUserException( CurrentAccount_UpdateCurrentAccountBalance.class.getName(),
          					"evaluate()",
          					"",
          					"",
          					e.getMessage(),
          					null);
        }
      }
              

これで、当座預金口座の貸方記入メッセージ・サブフローを作成することができるようになりました。当座預金口座の貸方記入メッセージ・サブフローの作成を参照してください。

「サンプルを拡張する」に戻る

サンプルのホームに戻る