Exemplo n.º 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 X12 standard are used
        /// </summary>
        static void WriteWithCustomSeparators()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

            var invoice = CreateInvoice("00000001");

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

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

                //  Write the ISA with the custom separator set
                writer.Write(Helpers.CreateIsa("000011111"), separators);
                writer.Write(Helpers.CreateGs("111111111"));
                writer.Write(invoice);

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Exemplo n.º 2
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 X12Writer(stream, Encoding.Default, Environment.NewLine);

                //  1.  Write the first interchange
                writer.Write(Helpers.CreateIsa("000011111"));
                writer.Write(Helpers.CreateGs("111111111"));
                writer.Write(CreateInvoice("00000001"));

                //  2.  Write the second interchange
                //  No need to close the previous interchange with a IEA
                writer.Write(Helpers.CreateIsa("000022222"));
                writer.Write(Helpers.CreateGs("111111111"));
                writer.Write(CreateInvoice("00000002"));

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Exemplo n.º 3
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("00000001");

            //  Initialize some properties with blanks
            invoice.BIG.ReleaseNumber_05             = "";
            invoice.BIG.ChangeOrderSequenceNumber_06 = "";

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

                writer.Write(Helpers.CreateIsa("000011111"));
                writer.Write(Helpers.CreateGs("111111111"));
                writer.Write(invoice);

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Batch multiple transactions under multiple functional groups in the same EDI stream
        /// </summary>
        static void WriteMultipleGroups()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

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

                writer.Write(Helpers.CreateIsa("000011111"));

                //  1.  Write the first group
                writer.Write(Helpers.CreateGs("111111111"));
                //  Write the transactions...
                writer.Write(CreateInvoice("00000001"));

                //  2.  Write the second group
                //  No need to close the previous group with a GE
                writer.Write(Helpers.CreateGs("222222222"));
                //  Write the transactions...
                writer.Write(CreateInvoice("00000002"));

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Exemplo n.º 5
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 X12Writer(stream, Encoding.Default, Environment.NewLine);

                writer.Write(Helpers.CreateIsa("000011111"));
                writer.Write(Helpers.CreateGs("111111111"));

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

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

                //  3.  Write any subsequent invoice...

                writer.Flush();

                Debug.Write(Helpers.LoadString(stream));
            }
        }
Exemplo n.º 6
0
        /// <summary>
        /// Generate and write claim to a stream
        /// </summary>
        static void WriteSingleClaimToStream()
        {
            Debug.WriteLine("******************************");
            Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
            Debug.WriteLine("******************************");

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

            //  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 (claim.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 X12Writer(stream, Encoding.Default, Environment.NewLine);

                    //  5.  Begin with ISA segment
                    writer.Write(CreateIsa("000011111"));
                    //  6.  Follow up with GS segment
                    writer.Write(CreateGs("111111111"));
                    //  7.  Write all transactions
                    //  Batch up as many as needed
                    writer.Write(claim);
                    //  No need to close any of the above

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

                    Debug.Write(LoadString(stream));
                }
            }
            else
            {
                //  The claim 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);
                }
            }
        }
Exemplo n.º 7
0
        public static string BuildAck(ISA originalIsa, GS originalGs, EdiMessage ack, AckVersion ackVersion, int isaControlNumber = 1, int gsControlNumber = 1)
        {
            var memoryStream = new MemoryStream();
            var writer       = new X12Writer(memoryStream, Encoding.Default, Environment.NewLine);

            writer.Write(originalIsa.ToAckIsa(isaControlNumber.ToString().PadLeft(9, '0'), ackVersion));
            writer.Write(originalGs.ToAckGs(gsControlNumber.ToString(), ackVersion));
            writer.Write(ack);
            writer.Flush();
            memoryStream.Position = 0;
            using (var reader = new StreamReader(memoryStream))
                return(reader.ReadToEnd());
        }
Exemplo n.º 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("00000001");

            const string folder = @"C:\test";

            if (!Directory.Exists(folder))
            {
                Directory.CreateDirectory(folder);
            }

            //  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.  Use default encoding and no segment postfix
                //  Write directly to a file
                var writer = new X12Writer(string.Format("{0}\\output.txt", folder), false);

                writer.Write(Helpers.CreateIsa("000011111"));
                writer.Write(Helpers.CreateGs("111111111"));
                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
            }
        }
Exemplo n.º 9
0
        public static string Generate(List <EdiItem> items, Separators separators, string postFix,
                                      Encoding encoding = null)
        {
            using (var stream = new MemoryStream())
            {
                var writer = new X12Writer(stream, encoding, postFix);
                foreach (var item in items)
                {
                    var message = item as EdiMessage;
                    if (message != null)
                    {
                        writer.Write(message);
                        continue;
                    }

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

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

                    var ta1 = item as TA1;
                    if (ta1 != null)
                    {
                        writer.Write(ta1);
                    }

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

                return(CommonHelper.LoadString(stream));
            }
        }
Exemplo n.º 10
0
        public void TestNoPreserveWhiteSpace()
        {
            // ARRANGE
            const string sample   = "EdiWeave.UnitTests.X12.Edi.X12_810_00204_WriteNoPreserveWhitespace.txt";
            var          expected = CommonHelper.LoadString(sample);
            string       actual;

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

                writer.Write(X12Helper.CreateIsa());
                writer.Write(X12Helper.CreateGs());
                writer.Write(X12Helper.CreateInvoice());
                writer.Flush();

                actual = CommonHelper.LoadString(stream);
            }

            // ASSERT
            Assert.AreEqual(expected, actual);
        }