.NETCompute ノードのサンプルの C# コード

Filter ノード

Filter ノードのクラス・ファイルの全体のコンテンツには、以下の C# コードを使用します。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using IBM.Broker.Plugin;

namespace SampleDotNetProject
{
    /// 
    /// FilterNode Class
    /// 
    public class FilterNode : NBComputeNode
    {
        /// 
        /// Evaluate Method
        /// 
        /// 
        public override void Evaluate(NBMessageAssembly inputAssembly)
        {
            NBOutputTerminal outTerminal = OutputTerminal("Out");
            NBOutputTerminal altTerminal = OutputTerminal("Alternate");
            NBOutputTerminal failureTerminal = OutputTerminal("Failure");

            NBMessage inputMessage = inputAssembly.Message;
            NBElement root = inputMessage.RootElement;

            #region UserCode
            // Add user code in this region to filter the message

            // The following expression deliberately uses "LastChild" in preference to "FirstChild"
            //  in case an XML Declaration is present!
            switch (root[NBParsers.XMLNSC.ParserName].LastChild.Name)
            {
                case "LoyaltyProgram":
                    outTerminal.Propagate(inputAssembly);
                    break;
                case "SaleEnvelope":
                    altTerminal.Propagate(inputAssembly);
                    break;
                default:
                    failureTerminal.Propagate(inputAssembly);
                    break;
            }
            #endregion UserCode
        }
    }
}

Modify ノード

Modify ノードの UserCode 領域は、以下の C# コードを使用します。

#region UserCode                   
NBElement xmlRoot = outputRoot[NBParsers.XMLNSC.ParserName];
NBElement xmlDecl = xmlRoot[NBParsers.XMLNSC.XmlDeclaration, "XmlDeclaration"];
if (xmlDecl == null)
{
    // Create an XML Declaration if required                                
    NBParsers.XMLNSC.CreateXmlDeclaration(xmlRoot, "1.0", "UTF-8", "yes");
}
string notargetnamespace = "";
string namespaceStore = "http://www.example.org/store";
NBElement storeDetails = xmlRoot[notargetnamespace, "LoyaltyProgram"][namespaceStore, "StoreDetails"];
string storeName = "";
string storeStreet = "";
string storeTown = "Happyville";
switch((string)storeDetails[namespaceStore,"StoreID"]) {         
case "001":   
    storeName = "Broker Brothers Central";
    storeStreet = "Exuberant Avenue";
    break;                  
case "002":
    storeName = "Broker Brothers Mall";
    storeStreet = "Enthusiastic Crescent";
    break;                  
case "003":
    storeName = "Broker Brothers District";
    storeStreet = "Peaceful Road";
    break;                  
}
storeDetails.CreateLastChild(namespaceStore, "StoreName", storeName);
storeDetails.CreateLastChild(namespaceStore, "StoreStreet", storeStreet);
storeDetails.CreateLastChild(namespaceStore, "StoreTown", storeTown);
#endregion UserCode

Create ノード

Create ノードの UserCode 領域は、以下の C# コードを使用します。

#region UserCode
outputRoot["Properties"]["MessageSet"].SetValue("DotNetLibrary");
outputRoot["Properties"]["MessageType"].SetValue("File");                
outputRoot.CreateLastChildUsingNewParser(NBParsers.DFDL.ParserName);
NBElement File = outputRoot[NBParsers.DFDL.ParserName].CreateFirstChild(null, "File");
NBElement inxmlRoot = inputRoot[NBParsers.XMLNSC.ParserName];
IEnumerable<NBElement> invoices = inxmlRoot["SaleEnvelope"]["SaleList"].Children("Invoice");
foreach (NBElement invoice in invoices)
{
    TransformInvoice(File, invoice);
}
// Define Local Environment override to dynamically control the MQOutput node                                
NBElement outputLE = outAssembly.LocalEnvironment.RootElement;
NBElement mqLE = outputLE.CreateFirstChild(null, "Destination").CreateFirstChild(null, "MQ");
mqLE.CreateFirstChild(null, "DestinationData").CreateFirstChild(null, "queueName","DOTNET.OUT");             
#endregion UserCode

前述のコードでは、以下に示す TransformInvoice メソッドも参照しています。 .NETCompute ノードで使用される C# クラスにこのメソッドを貼り付ける場合、必ず Evaluate メソッドの外側 (CopyMessageHeaders メソッドの直前など) に置くようにしてください。

private static void TransformInvoice(NBElement outputFileElement, NBElement inputInvoiceElement)
{
    // This method creates a structure based on the Invoice Element in the input message
    IEnumerable<NBElement> items = inputInvoiceElement.Children("Item");
    foreach (NBElement item in items)
    {
        NBElement record = outputFileElement.CreateLastChild(null, "Record");
        string notargetnamespace = "";
        record.CreateLastChild(notargetnamespace, "Code1", (string)item["Code", 0]);
        record.CreateLastChild(notargetnamespace, "Code2", (string)item["Code", 1]);
        record.CreateLastChild(notargetnamespace, "Code3", (string)item["Code", 2]);
        record.CreateLastChild(notargetnamespace, "Description", (string)item["Description"]);
        record.CreateLastChild(notargetnamespace, "Category", (string)item["Category"]);
        record.CreateLastChild(notargetnamespace, "Price", (decimal)item["Price"]);
        record.CreateLastChild(notargetnamespace, "Quantity", (Int32)item["Quantity"]);
    }
}

サンプルのホームに戻る