/// <summary> /// Write Invoices /// </summary> static void Write() { using (var stream = new MemoryStream()) { var transaction = EancomTransactionBuilders.BuildInvoice("1"); MessageErrorContext mec; if (transaction.IsValid(out mec, true)) { // valid using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(transaction); } var ediString = stream.LoadToString(); } else { // invalid var errors = mec.Flatten(); } } }
/// <summary> /// Generate and write EDI document to a file. /// </summary> public static void WriteSingleInvoiceToFile() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice var invoice = EdifactTransactionBuilders.BuildInvoice("1"); // 2. Validate it by skipping trailer validation MessageErrorContext errorContext; if (invoice.IsValid(out errorContext, new ValidationSettings { SkipTrailerValidation = true })) { Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber); // 3. Write directly to a file using (var writer = new EdifactWriter(@"C:\Test\Output.txt", false)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(invoice); } Debug.WriteLine("Written to file."); } else { // The invoice is invalid } }
/// <summary> /// Write without auto trailers /// </summary> public static void WriteWithoutAutoTrailers() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var stream = new MemoryStream()) { // Set AutoTrailers to false using (var writer = new EdifactWriter(stream, new EdifactWriterSettings { AutoTrailers = false })) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(EdifactTransactionBuilders.BuildInvoice("1")); // trailers need to be manually written } using (var writer = new StreamWriter(stream)) { var unz = new UNZ(); unz.InterchangeControlCount_1 = "1"; unz.InterchangeControlReference_2 = "000000001"; writer.Write(unz.ToEdi(Separators.Edifact)); writer.Flush(); Debug.Write(stream.LoadToString()); } } }
/// <summary> /// Write transactions with whitespace. /// </summary> public static void WriteSegmentWithWhitespace() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var invoice = EdifactTransactionBuilders.BuildInvoice("1"); // Initialize a blank property invoice.BGM.Responsetypecoded_04 = ""; using (var stream = new MemoryStream()) { // Set PreserveWhitespace flag to true using (var writer = new EdifactWriter(stream, new EdifactWriterSettings() { PreserveWhitespace = true })) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(invoice); } Debug.Write(stream.LoadToString()); } }
/// <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)); } }
/// <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)); } }
/// <summary> /// Batch multiple transactions under multiple functional groups in the same EDI stream. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); // 1. Write the first group writer.Write(SegmentBuilders.BuildUng("1", "INVOIC")); writer.Write(EF_EDIFACT_D96A_INVOIC_Builder.BuildInvoice("1")); // 2. Write the second group // No need to close the previous group with a UNE writer.Write(SegmentBuilders.BuildUng("2", "ORDERS")); writer.Write(EF_EDIFACT_D96A_ORDERS_Builder.BuildPurchaseOrder("1")); } Debug.Write(stream.LoadToString()); } }
/// <summary> /// Write Purchase Orders /// </summary> static void Write() { using (var stream = new MemoryStream()) { var transaction = EdifactTransactionBuilders.BuildPurchaseOrder("1"); MessageErrorContext mec; if (transaction.IsValid(out mec, new ValidationSettings { SkipTrailerValidation = true })) { // valid using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(transaction); } var ediString = stream.LoadToString(); } else { // invalid var errors = mec.Flatten(); } } }
/// <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); } } }
/// <summary> /// Write Invoices /// </summary> static void Write() { var transaction = BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(transaction); } var ediString = stream.LoadToString(); } }
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()); }
/// <summary> /// Write Passenger Data /// </summary> static void Write() { var transaction = BuildPassengerData("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("13052210400995")); writer.Write(SegmentBuilders.BuildUng("13052210400995", "PNRGOV")); writer.Write(transaction); } var ediString = stream.LoadToString(); } }
/// <summary> /// Write Passenger List /// </summary> static void Write() { var transaction = BuildPassengerList("PAXLST16"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(SegmentBuilders.BuildUng("16", "PAXLST")); writer.Write(transaction); } var ediString = stream.LoadToString(); } }
/// <summary> /// Generate and write EDI document to a stream. /// </summary> public static void WriteSinglePurchaseOrderToStream() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the purchase order var po = EdifactTransactionBuilders.BuildPurchaseOrder("1"); // 2. Validate it by skipping trailer validation MessageErrorContext errorContext; if (po.IsValid(out errorContext, new ValidationSettings { SkipTrailerValidation = true })) { Debug.WriteLine("Message {0} with control number {1} is valid.", errorContext.Name, errorContext.ControlNumber); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { // 3. Begin with UNB segment writer.Write(SegmentBuilders.BuildUnb("1")); // 4. Then write the purchase order(s) writer.Write(po); } Debug.Write(stream.LoadToString()); } } else { // The purchase order is invalid Debug.WriteLine("Message {0} with control number {1} is invalid with errors:", errorContext.Name, errorContext.ControlNumber); // List all error messages var errors = errorContext.Flatten(); foreach (var error in errors) { Debug.WriteLine(error); } } }
/// <summary> /// Write with segment postfix. /// </summary> public static void WriteWithSegmetPostfix() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var invoice = EdifactTransactionBuilders.BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream, Encoding.UTF8, Environment.NewLine)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(invoice); } Debug.Write(stream.LoadToString()); } }
/// <summary> /// Generate and write EDI document to a file. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice var invoice = EF_EDIFACT_D96A_INVOIC_Builder.BuildInvoice("1"); // 2. Write directly to a file // Change the patch to a file on your machine using (var writer = new EdifactWriter(@"C:\Test\Output.txt", false)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(invoice); } Debug.WriteLine("Written to file."); }
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)); } }
/// <summary> /// Write Price Catalogue /// </summary> static void Write() { var transaction = BuildPriceCatalogue("ME000001"); var xml = transaction.Serialize(); File.WriteAllText(@"C:\Test\serialized2.xml", xml.Root.ToString()); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(transaction); } var ediString = stream.LoadToString(); } }
private static async Task WriteEdiFactAsnResponse(IEdiItem ediItem, IEnumerable <string> serialNumbers, string responseFilePath) { var controlNumber = new DateTimeOffset(DateTime.UtcNow).ToUnixTimeMilliseconds().ToString(); var transaction = ediItem is TSORDERS item ? (await BuildDespatchAdviceForOrder(item, serialNumbers, controlNumber)) : DespatchAdviceGRBuilder.BuildDespatchAdvice(controlNumber, (TSDESADV)ediItem); using (var stream = File.Create(responseFilePath)) { using (var writer = new EdifactWriter(stream, new EdifactWriterSettings { Encoding = Encoding.Default })) { writer.Write(Separators.Edifact.ToUna()); writer.Write(EDIFactSegmentBuilders.BuildUnb(controlNumber)); writer.Write(transaction); } } }
/// <summary> /// Build functional CONTRL acknowledgment using the original UNB and UNG. /// </summary> public static string BuildAck(UNB originalUnb, UNG originalUng, TSCONTRL ack, int unbControlNumber = 1, int ungControlNumber = 1) { var memoryStream = new MemoryStream(); using (var writer = new EdifactWriter(memoryStream, new EdifactWriterSettings() { Encoding = Encoding.Default, Postfix = Environment.NewLine })) { writer.Write(originalUnb.ToAckUnb(unbControlNumber.ToString())); if (originalUng != null) { writer.Write(originalUng.ToAckUng(ungControlNumber.ToString())); } writer.Write(ack); } memoryStream.Position = 0; using (var reader = new StreamReader(memoryStream)) return(reader.ReadToEnd()); }
/// <summary> /// Write to stream without envelopes - no UNB, UNG, UNE or UNZ. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var stream = new MemoryStream()) { // Set the separators using (var writer = new EdifactWriter(stream, new EdifactWriterSettings() { Separators = Separators.Edifact })) { writer.Write(EF_EDIFACT_D96A_INVOIC_Builder.BuildInvoice("1")); writer.Write(EF_EDIFACT_D96A_INVOIC_Builder.BuildInvoice("2")); } Debug.Write(stream.LoadToString()); } }
/// <summary> /// Write to stream without envelopes - no UNB, UNG, UNE or UNZ. /// </summary> public static void WriteWithoutEnvelopes() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var stream = new MemoryStream()) { // Set the separators using (var writer = new EdifactWriter(stream, new EdifactWriterSettings() { Separators = Separators.Edifact })) { writer.Write(EdifactTransactionBuilders.BuildInvoice("1")); writer.Write(EdifactTransactionBuilders.BuildInvoice("2")); } Debug.Write(stream.LoadToString()); } }
/// <summary> /// Generate and write EDI document to a stream. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); // 1. Construct the invoice var invoice = SegmentBuilders.BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { // 2. Begin with UNB segment writer.Write(SegmentBuilders.BuildUnb("1")); // 3. Then write the invoice(s) writer.Write(invoice); } Debug.Write(stream.LoadToString()); } }
/// <summary> /// Pull the last purchase order from DB /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var db = new EDIFACTContext()) { var po = db.TSORDERS.OrderByDescending(o => o.Id).FirstOrDefault(); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(po); } Debug.Write(stream.LoadToString()); } } }
/// <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)); } }
/// <summary> /// Write with segment postfix. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var invoice = SegmentBuilders.BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream, new EdifactWriterSettings() { Postfix = Environment.NewLine })) { writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(invoice); } Debug.Write(stream.LoadToString()); } }
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); }
/// <summary> /// Batch multiple interchanges in the same EDI stream. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { // 1. Write the first interchange writer.Write(SegmentBuilders.BuildUnb("1")); writer.Write(SegmentBuilders.BuildInvoice("1")); // 2. Write the second interchange // No need to close the previous interchange with a IEA writer.Write(SegmentBuilders.BuildUnb("2")); writer.Write(SegmentBuilders.BuildInvoice("1")); } Debug.Write(stream.LoadToString()); } }
/// <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 } }
/// <summary> /// Write with custom separators, by default it uses the standard separators. /// UNA segment is automatically applied if any of the custom separators is different than the default. /// </summary> public static void Run() { Debug.WriteLine("******************************"); Debug.WriteLine(MethodBase.GetCurrentMethod().Name); Debug.WriteLine("******************************"); var invoice = EF_EDIFACT_D96A_INVOIC_Builder.BuildInvoice("1"); using (var stream = new MemoryStream()) { using (var writer = new EdifactWriter(stream)) { // 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(SegmentBuilders.BuildUnb("1"), separators); writer.Write(invoice); } Debug.Write(stream.LoadToString()); } }