Example #1
0
        /// <summary>
        /// Write with custom separators
        /// All separators can be set - segment terminator, data element terminator, component data element terminator and repetition delimiter
        /// When not set, the default separators for EDIFACT standard are used
        /// UNA segment is automatically applied if the separators are different than the default
        /// </summary>
        static void WriteWithCustomSeparators()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            var invoice = CreateInvoice("1");

            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream, Encoding.Default, Environment.NewLine);

                //  Set a custom segment separator.
                var separators = new Separators('|', Separators.Edifact.ComponentDataElement,
                                                Separators.Edifact.DataElement, Separators.Edifact.RepetitionDataElement, Separators.Edifact.Escape);

                //  Write the UNB with the custom separator set
                writer.Write(Helpers.CreateUnb("1"), separators);
                writer.Write(invoice);

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Example #2
0
        /// <summary>
        /// Batch multiple transactions in the same functional group\EDI stream
        /// </summary>
        static void WriteMultipleInvoices()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream, Encoding.Default, Environment.NewLine);

                writer.Write(Helpers.CreateUnb("1"));

                //  1.  Write the first invoice
                writer.Write(CreateInvoice("1"));

                //  2.  Write the second invoice
                writer.Write(CreateInvoice("2"));

                //  3.  Write any subsequent invoices...

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Example #3
0
        /// <summary>
        /// Generate and write EDI document to a stream
        /// </summary>
        static void WriteSingleInvoiceToStream()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            //  1.  Construct the invoice message with data from database, service or domain objects\logic.
            var invoice = CreateInvoice("1");

            //  2.  Validate it to ensure the object adheres to the rule
            //  Always skip trailer validation because all trailers are automatically generated by the writer
            MessageErrorContext errorContext;

            if (invoice.IsValid(out errorContext, true))
            {
                Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber);

                //  3.  Write to a stream
                using (var stream = new MemoryStream())
                {
                    //  4.  Use CRLF(new line) as segment postfix for clarity
                    //  Always agree postfixes and separators with the trading partner
                    var writer = new EdifactWriter(stream, Encoding.Default, Environment.NewLine);

                    //  5.  Begin with UNB segment
                    writer.Write(Helpers.CreateUnb("1"));
                    //  6.  Write all transactions
                    writer.Write(invoice);
                    //  No need to close any of the above

                    //  7.  Always flush at the end to release the cache
                    writer.Flush();

                    Debug.Write(Helpers.LoadString(stream));
                }
            }
            else
            {
                //  The purchase order is invalid
                //  Report it back to the sender, log, etc.

                //  Inspect MessageErrorContext for the validation errors
                var errors = errorContext.Flatten();

                Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name, errorContext.ControlNumber);
                foreach (var error in errors)
                {
                    Debug.WriteLine(error);
                }
            }
        }
Example #4
0
        private static string BuildAck(UNB originalUnb, UNG originalUng, EdiMessage ack, int unbControlNumber = 1, int ungControlNumber = 1)
        {
            var memoryStream = new MemoryStream();
            var writer       = new EdifactWriter(memoryStream, Encoding.Default, Environment.NewLine);

            writer.Write(originalUnb.ToAckUnb(unbControlNumber.ToString()));
            if (originalUng != null)
            {
                writer.Write(originalUng.ToAckUng(ungControlNumber.ToString()));
            }
            writer.Write(ack);
            writer.Flush();
            memoryStream.Position = 0;
            using (var reader = new StreamReader(memoryStream))
                return(reader.ReadToEnd());
        }
Example #5
0
        public static string Generate(List <EdiItem> items, Separators separators, string postFix,
                                      Encoding encoding = null, UNA una = null)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream, encoding, postFix);
                if (una != null)
                {
                    writer.Write(una);
                }
                foreach (var item in items)
                {
                    var message = item as EdiMessage;
                    if (message != null)
                    {
                        writer.Write(message);
                        continue;
                    }

                    var gs = item as UNG;
                    if (gs != null)
                    {
                        writer.Write(gs);
                        continue;
                    }

                    var ge = item as UNE;
                    if (ge != null)
                    {
                        continue;
                    }

                    var unb = item as UNB;
                    if (unb != null)
                    {
                        writer.Write(unb, separators);
                    }
                }
                writer.Flush();

                return(CommonHelper.LoadString(stream));
            }
        }
Example #6
0
        public void TestNoPreserveWhiteSpace()
        {
            // ARRANGE
            const string sample   = "EdiWeave.UnitTests.Edifact.Edi.Edifact_INVOIC_D00A_WriteNoPreserveWhitespace.txt";
            var          expected = CommonHelper.LoadString(sample, Encoding.UTF8);
            string       actual;

            // ACT
            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream, Encoding.UTF8, Environment.NewLine);

                writer.Write(EdifactHelper.CreateUnb());
                writer.Write(EdifactHelper.CreateInvoice());
                writer.Flush();

                actual = CommonHelper.LoadString(stream);
            }

            // ASSERT
            Assert.AreEqual(expected, actual);
        }
Example #7
0
        /// <summary>
        /// Write with explicit UNA
        /// </summary>
        static void WriteWithUna()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            var invoice = CreateInvoice("1");

            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream);

                //  Write the custom UNA
                writer.Write(Separators.Edifact.ToUna());
                writer.Write(Helpers.CreateUnb("1"));
                writer.Write(invoice);

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Example #8
0
        /// <summary>
        /// Generate and write EDI document to a file
        /// </summary>
        static void WriteSingleInvoiceToFile()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            //  1.  Construct the invoice message with data from database, service or domain objects\logic.
            var invoice = CreateInvoice("1");

            // Ensure the object adheres to the rule
            // Skip trailer validation
            MessageErrorContext errorContext;

            if (invoice.IsValid(out errorContext, true))
            {
                Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name,
                                errorContext.ControlNumber);

                //  3.  Use default encoding and no segment postfix
                //  Write directly to a file
                var writer = new EdifactWriter(@"C:\Test\Output.txt", false);

                writer.Write(Helpers.CreateUnb("1"));
                writer.Write(invoice);

                //  4.  Always flush at the end to release the cache
                writer.Flush();

                writer.Dispose();

                Debug.WriteLine("Written to file.");
            }
            else
            {
                //  The purchase order is invalid
            }
        }
Example #9
0
        /// <summary>
        /// Write transactions with whitespace
        /// By default properties set to whitespace are omitted
        /// </summary>
        static void WriteSegmentWithWhitespace()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            var invoice = CreateInvoice("1");

            //  Initialize some properties with blanks
            invoice.BGM.Responsetypecoded_04 = "";

            using (var stream = new MemoryStream())
            {
                //  Set the PreserveWhitespace flag to true
                var writer = new EdifactWriter(stream, null, "", true);

                writer.Write(Helpers.CreateUnb("1"));
                writer.Write(invoice);

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Example #10
0
        /// <summary>
        /// Batch multiple interchanges in the same EDI stream
        /// </summary>
        static void WriteMultipleInterchanges()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            using (var stream = new MemoryStream())
            {
                var writer = new EdifactWriter(stream, Encoding.Default, Environment.NewLine);

                //  1.  Write the first interchange
                writer.Write(Helpers.CreateUnb("1"));
                writer.Write(CreateInvoice("1"));

                //  2.  Write the second interchange
                //  No need to close the previous interchange with a IEA
                writer.Write(Helpers.CreateUnb("2"));
                writer.Write(CreateInvoice("1"));

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }