DatabaseCustom Pipeline Component for the DB2 Adapter

Custom Pipeline Component for the DB2 Adapter

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

One of the things that I find most enjoyable about BizTalk is the ability to interface with so many different types of systems. With this ability, there always hurdles in getting these systems to work well with each other.

Recently, I developed an integration from DB2 to MS CRM. One of the challenges was pulling the XML from the adapter into BizTalk. The adapter’s setup is very similar to the SQL adapter, but the resulting XML could not be translated by BizTalk. To bring in the XML, white spacing, encoding, as well as namespaces had to be updated.

Create the Pipeline Custom Component

The first step was to create a new custom pipeline component that would be put into the receive pipeline of the inbound DB2 record.I used a Decode component for this task. For the Decode component, I had the class inherit IBaseComponent, IComponentUI, and IComponent.

IBaseComponent

There are three properties needed for inheritance: Name, Description, and Version.

[Browsable(false)]
public string Description
{
   get
   {
      return "Component to format DB2 XML for contacts";
   }
}

[Browsable(false)]
public string Name
{
   get
   {
      return "DB2 Contact Formatter";
   }
}

[Browsable(false)]
public string Version
{
   get
   {
      return "1.0.0.0";
   }
}

IComponentUI

The IComponentUI properties are used within the Editor. There are two required properties.

#region IComponentUI

[Browsable(false)]
public IntPtr Icon
{
   get { return IntPtr.Zero; }
}

public System.Collections.IEnumerator Validate(object obj)
{
   return null;
}
#endregion

IComponent

The main processing will be done in the IComponent. You will override the Execute method to modify the inbound XML into a value that BizTalk can accept.

As you step through the execute method, the first task is to capture the inbound document, which is part of the inbound message. Here, you put it into a Stream so that you can translate it easily. From the Stream, you then read the stream value into a string utilizing the Unicode encoding.

The following section manipulates the data. In my usage with the DB2 adapter, the first character was always a space, so I strip that off. I replace the nodes with the namespace info property for self subscribing. Afterwards, I write out the value as a UTF8-encoded string for BizTalk to evaluate.

#region IComponent

public IBaseMessage Execute(IPipelineContext pc, IBaseMessage inmsg)
{
   string originalDataString = string.Empty;

   try
   {
      Stream originalMessageStream =
         inmsg.BodyPart.GetOriginalDataStream();
      byte[] bufferOriginalMessage =
         new byte[originalMessageStream.Length];

      originalMessageStream.Read(bufferOriginalMessage, 0,
         Convert.ToInt32(originalMessageStream.Length));

      originalDataString =
         System.Text.UnicodeEncoding.Unicode.
         GetString(bufferOriginalMessage);

   }
   catch (Exception ex)
   {
      throw new ApplicationException("Error in reading original
         message: " + ex.Message);
   }

   string temp = string.Empty;
   try
   {
      originalDataString = originalDataString.Substring(1);

      temp = originalDataString.Replace("xmlns=
         "http://DB2ReceiveContact"",
         "xmlns:ns0="http://DB2ReceiveContact"");
      temp = temp.Replace("<?xml version="1.0"?>",
         "<?xml version="1.0" encoding="utf-8" ?>");
      temp = temp.Replace("DB2Contact", "ns0:DB2Contact");
      temp = temp.Replace("><FT112", "> <ns0:FT112");
   }
   catch (System.Exception ex)
   {
      throw new ApplicationException("Error executing pipeline: "
         + ex.Message);
   }


      byte[] bufferOoutgoingMessage =
         System.Text.ASCIIEncoding.ASCII.GetBytes(temp);
      inmsg.BodyPart.Data =
         new MemoryStream(bufferOoutgoingMessage);


      return inmsg;
   }
#endregion

Adding a Component to the Pipeline

Once you compile your assembly, you add it to the the ..Program FilesMicrosoft BizTalk Server 2006Pipeline Components folder. The component will now be available in the BizTalk Pipelline Components Tab. Right-click the BizTalk Pipeline Components Tab and select Choose Items. Click the BizTalk Pipeline Components Tab and scroll through the list to find your component.

Summary

With a little bit of custom code, you can streamline your integrations with DB2. It is much simpler by far than writing a custom application using ODBC connections to pull the data for you.

About the Author

Drew Block, MCSD, is a .NET Architect of enterprise integration and web applications. Drew is a Senior Manager at Crowe Chizek in Indianapolis, Indiana where he architects, designs, and manages the development of Microsoft-based solutions. You can reach Drew at ablock@crowechizek.com.

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories