-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathValidateCustomEDICodes.cs
108 lines (93 loc) · 4.52 KB
/
ValidateCustomEDICodes.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using EdiFabric.Core.Annotations.Edi;
using EdiFabric.Core.ErrorCodes;
using EdiFabric.Core.Model.Edi;
using EdiFabric.Core.Model.Edi.ErrorContexts;
using EdiFabric.Examples.EDIFACT.Common;
using EdiFabric.Framework.Readers;
using EdiFabric.Templates.EdifactD96A;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
namespace EdiFabric.Examples.EDIFACT.ValidateEDI
{
class ValidateCustomEDICodes
{
/// <summary>
/// Validate with custom EDI codes, different than the standard EDI codes
/// </summary>
public static void Run()
{
Debug.WriteLine("******************************");
Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
Debug.WriteLine("******************************");
// Set EDI codes map where the original code type will be substituted with the partner-specific code type
Dictionary<Type, Type> codeSetMap = new Dictionary<Type, Type>();
codeSetMap.Add(typeof(EDIFACT_ID_1225), typeof(EDIFACT_ID_1225_PartnerA));
Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + Config.TestFilesPath + @"\EDIFACT\MixedTransactions.txt");
List<IEdiItem> ediItems;
using (var reader = new EdifactReader(ediStream, "EdiFabric.Templates.Edifact"))
ediItems = reader.ReadToEnd().ToList();
var purchaseOrders = ediItems.OfType<TSORDERS>();
foreach (var purchaseOrder in purchaseOrders)
{
// Validate using EDI codes map
MessageErrorContext errorContext;
if (!purchaseOrder.IsValid(out errorContext, new ValidationSettings { DataElementTypeMap = codeSetMap }))
{
// Invalid code value
var customCodeError = errorContext.Errors.SingleOrDefault(e => e.Errors.Any(de => de.Code == DataElementErrorCode.InvalidCodeValue));
// Report it back to the sender, log, etc.
var errors = errorContext.Flatten();
}
else
{
// purchaseOrder is valid, handle it downstream
}
}
}
/// <summary>
/// Validate with custom EDI codes, different than the standard EDI codes. Load the code dynamically at runtime.
/// </summary>
public static void Run2()
{
Debug.WriteLine("******************************");
Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
Debug.WriteLine("******************************");
// Set EDI codes map where the original code type will be substituted with the partner-specific code type
var codeSetMap = new Dictionary<string, List<string>>();
codeSetMap.Add("EDIFACT_ID_1001", new List<string> { "A", "B", "C", "D", "E" });
Stream ediStream = File.OpenRead(Directory.GetCurrentDirectory() + Config.TestFilesPath + @"\EDIFACT\MixedTransactions.txt");
// Ensure that the codeset map is set on the reader
List<IEdiItem> ediItems;
using (var reader = new EdifactReader(ediStream, "EdiFabric.Templates.Edifact", new EdifactReaderSettings { DataElementCodesMap = codeSetMap }))
ediItems = reader.ReadToEnd().ToList();
var purchaseOrders = ediItems.OfType<TSORDERS>();
foreach (var purchaseOrder in purchaseOrders)
{
// Validate using EDI codes map
MessageErrorContext errorContext;
if (!purchaseOrder.IsValid(out errorContext, new ValidationSettings { DataElementCodesMap = codeSetMap }))
{
// Invalid code value
var customCodeError = errorContext.Errors.SingleOrDefault(e => e.Errors.Any(de => de.Code == DataElementErrorCode.InvalidCodeValue));
// Report it back to the sender, log, etc.
var errors = errorContext.Flatten();
}
else
{
// purchaseOrder is valid, handle it downstream
}
}
}
}
[Serializable()]
[DataContract()]
[EdiCodes(",LL,PP,")]
public class EDIFACT_ID_1225_PartnerA
{
}
}