C# code for the .NETCompute Node sample

Filter node

The content of the whole of the Filter node's class file uses the following C# code:

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 node

The Modify node's UserCode region uses the following C# code:

#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 node

The Create node's UserCode region uses the following C# code:

#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

The above code also references a TransformInvoice method, which is shown below. If you are pasting this method into the C# class which is used by the .NETCompute node, ensure that it is placed outside the Evaluate method (for example just before the CopyMessageHeaders method).

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"]);
    }
}

Back to sample home