Writing Custom Pipeline component to modify the source file name.

In BizTalk EDI application I needed to have custom file name of the destination file and I end up writing a custom component. I could have used orchestration to modify the "ReceivedFileName" but somehow it was not working. May be becasue of EDI Encode component but that should not be the case. Any way.. the other thing was I also needed to add the seuence of the file to the FileName. I found that BizTalk databases does not store the sequence of the received file so either I needed to add some tracking info or I should use some application configuration values. I chose to use application configuration value. I needed to get the file name as "SO_Date_Sequence". Below is the code for the same.

#region IComponent interface public methods

        //public bool isRecordSequence = true;
        public IBaseMessage Execute(IPipelineContext context, IBaseMessage message)
        {
           
           
            try
            {

                string customSourceFileName = RecordSequence();
                    IBaseMessageContext messageContext = message.Context;
                    messageContext.Write("ReceivedFileName", "http://schemas.microsoft.com/BizTalk/2003/file-properties", customSourceFileName);
                               


            }
            catch (Exception ex)
            {
                if (message != null)
                {
                    message.SetErrorInfo(ex);
                }
                throw ex;
            }
            return message;
        }

        #endregion
        #region IComponent interface private methods

        private string RecordSequence()
        {
            string filename;
            try
            {
                //Get the current date
                string currentDate = System.DateTime.UtcNow.Date.ToString("MMddyyyy");

                //Read the xml file
                XmlTextReader reader = new XmlTextReader("C:\\BT Projects\\BungeeTemp\\Sequence_output.xml");
                XmlDocument xdoc = new XmlDocument();
                xdoc.Load(reader);

                //Close the reader
                reader.Close();

                //Get the current count
                int count = Convert.ToInt32(xdoc.SelectSingleNode("//SequenceCount").InnerText);

               
                    if (xdoc.SelectSingleNode("//EDIReceiveDate").InnerText == currentDate)
                    {
                        count++;
                        xdoc.SelectSingleNode("//SequenceCount").InnerText = count.ToString();
                    }
                    else
                    {
                        xdoc.SelectSingleNode("//EDIReceiveDate").InnerText = currentDate;
                        xdoc.SelectSingleNode("//SequenceCount").InnerText = "1";
                    }

                    xdoc.Save("C:\\BT Projects\\BungeeTemp\\Sequence_output.xml");
                    //isRecordSequence = false;

                    filename = "SO_" + currentDate + "_" + xdoc.SelectSingleNode("//SequenceCount").InnerText;
                    return filename;
                }               
                           
            catch (Exception ex)
            {
                throw (ex);
            }

        }

        #endregion

Cheers,

posted @ Monday, November 2, 2009 8:41 AM
Print