Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Xml;
- using Microsoft.BizTalk.Message.Interop;
- using Microsoft.BizTalk.Component.Interop;
- namespace Delta.Import.PipelineComponents
- {
- [ComponentCategory(CategoryTypes.CATID_PipelineComponent)]
- [ComponentCategory(CategoryTypes.CATID_DisassemblingParser)]
- //[System.Runtime.InteropServices.Guid("57D51828-C973-4a62-A534-6DB3CB3CB251")]
- [System.Runtime.InteropServices.Guid("6118B8F0-8684-4ba2-87B4-8336D70BD4F7")]
- public class FPES2Custom3DayDisassembler :
- IBaseComponent,
- IComponentUI,
- IDisassemblerComponent,
- IPersistPropertyBag
- {
- #region Private Members
- System.Collections.Queue qOutputMsgs = new System.Collections.Queue();
- IBaseMessagePart msgPart;
- string textProperty = string.Empty;
- private string systemPropertiesNamespace = @"http://schemas.microsoft.com/BizTalk/2003/system-properties";
- #endregion
- #region PropertyBagVariables
- public string _rootNodeName;
- public string _debugLevel;
- #endregion
- #region PropertyBagProperties
- public string DefaultTargetNamespace
- {
- get { return _rootNodeName; }
- set { _rootNodeName = value; }
- }
- /// <summary>
- /// Debug Parm to allow BizTalk Admin to turn it on/off from BizTalk Admin Console
- /// </summary>
- public string DebugLevel
- {
- get { return _debugLevel; }
- set
- {
- // Setup to allow for None, Informational, Warning, Error, Verbose in the future.
- if (value == "OFF" || value == "NONE")
- {
- DebugLevel = "";
- }
- else
- {
- DebugLevel = value.ToUpper();
- }
- }
- }
- #endregion
- #region IBaseComponent Members
- public string Description
- {
- get { return "FPES2 - Custom 3Day Disassembler"; }
- }
- public string Name
- {
- get { return "FPES2Custom3DayDisassembler"; }
- }
- public string Version
- {
- get { return "1.0.0.0"; }
- }
- #endregion
- #region IPersistPropertyBag Members
- public void GetClassID(out Guid classID)
- {
- classID = new Guid("25984614-BCFD-4c47-82FC-4A2300B76438");
- }
- public void InitNew()
- {
- }
- public void Load(IPropertyBag propertyBag, int errorLog)
- {
- object val = ReadPropertyBag(propertyBag, "RootNodeName");
- if (val != null)
- _rootNodeName = (string)val;
- object val2 = ReadPropertyBag(propertyBag, "DebugLevel");
- if (val2 != null)
- _debugLevel = (string)val2;
- }
- public void Save(IPropertyBag propertyBag, bool clearDirty,
- bool saveAllProperties)
- {
- propertyBag.Write("RootNodeName", _rootNodeName);
- propertyBag.Write("DebugLevel", _debugLevel);
- }
- #endregion
- #region PropertyBagHelpers
- /// <summary>
- /// Reads property value from property bag.
- /// </summary>
- /// <param name="pb">Property bag.</param>
- /// <param name="propName">Name of property.</param>
- /// <returns>Value of the property.</returns>
- private static object ReadPropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName)
- {
- object val = null;
- try
- {
- pb.Read(propName, out val, 0);
- }
- catch (ArgumentException)
- {
- //Do not bother about this
- }
- catch (Exception ex)
- {
- string errorMsg = "Error in ReadPropertyBag: " + ex.Message;
- System.Diagnostics.EventLog.WriteEntry("PipelineComponent", errorMsg);
- throw new ApplicationException(errorMsg);
- }
- return val;
- }
- /// <summary>
- /// Writes property values into a property bag.
- /// </summary>
- /// <param name="pb">Property bag.</param>
- /// <param name="propName">Name of property.</param>
- /// <param name="val">Value of property.</param>
- private static void WritePropertyBag(Microsoft.BizTalk.Component.Interop.IPropertyBag pb, string propName, object val)
- {
- try
- {
- pb.Write(propName, ref val);
- }
- catch (Exception ex)
- {
- string errorMsg = "Error in WritePropertyBag2: " + ex.Message;
- System.Diagnostics.EventLog.WriteEntry("PipelineComponent", errorMsg);
- throw new ApplicationException(errorMsg);
- }
- }
- #endregion
- #region IComponentUI Members
- public IntPtr Icon
- {
- get { return System.IntPtr.Zero; }
- }
- public System.Collections.IEnumerator Validate(object projectSystem)
- {
- return null;
- }
- #endregion
- #region IDisassemblerComponent Members
- public void Disassemble(IPipelineContext pContext, IBaseMessage pInMsg)
- {
- string trcSource = "DeltaPipeline:SetNSForFPES2Msg";
- string trcCorrId = pInMsg.Context.Read("InterchangeID", "http://schemas.microsoft.com/BizTalk/2003/system-properties").ToString();
- // database size in trace is varchar(36), so we don't want the curly braces in the GUID
- trcCorrId = trcCorrId.Replace("{", "");
- trcCorrId = trcCorrId.Replace("}", "");
- string trcGUID = trcCorrId;
- string strTraceMessage = null;
- string trcCode = "0100";
- strTraceMessage = "Start Debatcher _debugLevel=" + _debugLevel;
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcGUID, trcCode, strTraceMessage,
- null, trcCorrId);
- System.Diagnostics.Trace.WriteLine("Pipeline Disassemble Stage Enter");
- msgPart = pInMsg.BodyPart;
- Stream originalStream = pInMsg.BodyPart.GetOriginalDataStream();
- try
- {
- if (msgPart != null)
- {
- if (originalStream != null)
- {
- string stream = string.Empty;
- // do the disassembling
- //byte[] arrByte = ConvertToByteArray(stream); // sample code from author
- //originalStream = (new MemoryStream(arrByte)); // sample code from author
- XmlDocument xmlDoc = new XmlDocument();
- xmlDoc.Load(pInMsg.BodyPart.Data);
- strTraceMessage = "xmlDoc.Load";
- trcCode = "0200";
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcGUID, trcCode, strTraceMessage,
- xmlDoc.OuterXml, trcCorrId);
- string strXPath = "//FlightLeg";
- if (xmlDoc.DocumentElement.Name == "CoreFlightData")
- {
- strXPath = "//FlightLeg";
- }
- XmlNodeList xmlNodeList = xmlDoc.SelectNodes(strXPath);
- if (xmlNodeList.Count == 0)
- {
- trcCode = "0210";
- strTraceMessage = "XPath not found: " + strXPath;
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcGUID, trcCode, strTraceMessage,
- xmlDoc.OuterXml, trcCorrId);
- }
- int loopCounter = 0;
- foreach (XmlNode xmlNode in xmlNodeList)
- {
- loopCounter++;
- string newMessageText =
- "<" + xmlDoc.DocumentElement.Name + ">" +
- "<Event code='1111' gmtTs='2012-06-13T09:39:14Z' evntSeqNbr=''>" +
- xmlNode.OuterXml +
- "</Event>" +
- "</" + xmlDoc.DocumentElement.Name + ">";
- XmlDocument xmlDocOut = new XmlDocument();
- xmlDocOut.LoadXml(newMessageText);
- if (_debugLevel == "VERBOSE")
- {
- trcCode = "0300";
- strTraceMessage = "New Message #" + loopCounter + " pInMsg.BodyPartName=" + pInMsg.BodyPartName;
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcGUID, trcCode, strTraceMessage,
- newMessageText, trcCorrId);
- }
- // The GetNext method will return messages to BizTalk from
- // this queue of IBaseMessages
- // convert string to stream
- //MemoryStream outStream = new MemoryStream
- // (System.Text.Encoding.UTF8.GetBytes(newMessageText));
- MemoryStream outStream = new MemoryStream();
- xmlDocOut.Save(outStream);
- // ?? not sure if this is needed
- // msgPart.Data = outStream;
- // load stream into IBaseMessage
- IBaseMessage outMsg;
- outMsg = pContext.GetMessageFactory().CreateMessage();
- outStream.Position = 0;
- outStream.Seek(0, SeekOrigin.Begin);
- // Using variable here results in lowercase "body"
- //outMsg.AddPart(pInMsg.BodyPartName,
- // other sources show uppercase "Body", so far doesn't seem to make any difference
- outMsg.AddPart("Body",
- pContext.GetMessageFactory().CreateMessagePart(),
- true);
- outMsg.BodyPart.Data = outStream;
- // copy over all context from incoming message, then change only what is needed
- outMsg.Context = pInMsg.Context;
- /*
- outMsg.Context.Promote("MessageType",
- "http://schemas.microsoft.com/BizTalk/2003/system-properties",
- "http://qt.bts.external.delta.schemas/FPES2#CoreFlightData");
- outMsg.Context.Promote("SchemaStrongName",
- "http://schemas.microsoft.com/BizTalk/2003/system-properties",
- "SchemaAssembly full name");
- */
- //outMsg.BodyPart.GetOriginalDataStream().Position = 0;
- //outMsg.BodyPart.GetOriginalDataStream().Seek(0, SeekOrigin.Begin);
- qOutputMsgs.Enqueue(outMsg);
- // pContext.ResourceTracker.AddResource(outStream); // ????
- }
- }
- }
- }
- catch (Exception ex)
- {
- trcCode = "0210";
- strTraceMessage = "Pipeline trcCode=" + trcCode + " Exception:" + ex.ToString();
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcGUID, trcCode, strTraceMessage,
- null, trcCorrId);
- throw;
- }
- //originalStream.Seek(0, SeekOrigin.Begin);
- //msgPart.Data = originalStream;
- /*
- //IBaseMessage outMsg = pInMsg;
- outMsg.BodyPart.Data = originalStream;
- // promote MessageType and Schema SchemaStrongName
- // in order to the Biztalk to have a unique key
- // for evaluating subscription
- // As an example for Namespace#Root= http://TestCustomPipeline.Schema1#roottagName
- // As an example for SchemaAssembly full name=
- // TestCustomPipeline.Schema1, TestCustomPipeline,
- // Version=1.0.0.0, Culture=neutral, PublicKeyToken=f38e6d751bb874eb
- qOutputMsgs.Enqueue(outMsg);
- */
- System.Diagnostics.Trace.WriteLine("Pipeline Disassemble Stage Exit");
- }
- public IBaseMessage GetNext(IPipelineContext pContext)
- {
- string trcSource = "DeltaPipeline:SetNSForFPES2Msg";
- string trcCorrId = System.Guid.NewGuid().ToString();
- string strTraceMessage = null;
- string trcCode = "0100";
- try
- {
- if (qOutputMsgs.Count > 0)
- {
- IBaseMessage msg = (IBaseMessage)qOutputMsgs.Dequeue();
- //XmlDocument xmlDoc = new XmlDocument();
- // xmlDoc.Load(msg.BodyPart.Data);
- //string textFromBodyPart = ConvertToString(msg.BodyPart.Data);
- trcCode = "1101";
- strTraceMessage = "GetNext qOutputMsgs.Count = " + qOutputMsgs.Count +
- " CanSeek=" + msg.BodyPart.GetOriginalDataStream().CanSeek;
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcCorrId, trcCode, strTraceMessage,
- null, trcCorrId);
- /*
- trcCode = "1102";
- string xmlDebug = "<Wrapper><![CDATA[" + textFromBodyPart + "]]></Wrapper>";
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcCorrId, trcCode, strTraceMessage,
- xmlDebug, trcCorrId);
- */
- // http://social.msdn.microsoft.com/Forums/en-US/biztalkgeneral/thread/1186c141-f8ce-4642-9abb-31e73949e5b5
- //msg.BodyPart.GetOriginalDataStream().Position = 0;
- //msg.BodyPart.GetOriginalDataStream().Seek(0, SeekOrigin.Begin);
- //ulong nSize = 0;
- //bool bFlag;
- //msg.BodyPart.GetSize(out nSize, out bFlag);
- return msg;
- }
- else
- {
- // we have already dequeued everything, so return a null message to signal that we are all done.
- trcCode = "1140";
- strTraceMessage = "GetNext qOutputMsgs.Count <= 0";
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcCorrId, trcCode, strTraceMessage,
- null, trcCorrId);
- return null;
- }
- }
- catch (Exception ex)
- {
- string tempTrcCode = "1199";
- strTraceMessage = "TrcCode=" + trcCode + ": GetNext exception: " + ex.ToString();
- Shared.Utility.Log.WriteEntryConditional(
- System.Diagnostics.TraceLevel.Info,
- trcSource, trcCorrId, tempTrcCode, strTraceMessage,
- null, trcCorrId);
- throw ex;
- }
- }
- #endregion
- #region Private Helper Functions
- private byte[] ConvertToByteArray(string strInput)
- {
- return Encoding.ASCII.GetBytes(strInput);
- }
- private string ConvertToString(Stream inStream)
- {
- byte[] buffer = new byte[inStream.Length];
- inStream.Read(buffer, 0, (int)inStream.Length);
- return Encoding.ASCII.GetString(buffer);
- }
- #endregion
- } // end class
- } // end namespace
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement