.NETCompute Node 範例的 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"]);
    }
}

回到範例首頁