-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReadHL7FileSplitting.cs
More file actions
93 lines (81 loc) · 3.77 KB
/
ReadHL7FileSplitting.cs
File metadata and controls
93 lines (81 loc) · 3.77 KB
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
using EdiFabric.Core.Annotations.Edi;
using EdiFabric.Core.Annotations.Validation;
using EdiFabric.Core.Model.Edi;
using EdiFabric.Core.Model.Hl7;
using EdiFabric.Examples.HL7.Common;
using EdiFabric.Framework;
using EdiFabric.Framework.Readers;
using EdiFabric.Templates.Hl726;
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.HL7.ReadHL7
{
class ReadHL7FileSplitting
{
/// <summary>
/// Split a message into parts (blocks of segments) and read each part individually.
/// Use to process large transactions with repeating loops.
/// </summary>
public static void Run()
{
Debug.WriteLine("******************************");
Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
Debug.WriteLine("******************************");
Stream hl7Stream = File.OpenRead(Directory.GetCurrentDirectory() + Config.TestFilesPath + @"\MedicalRecord.txt");
// The split is driven by setting which class to split by in the template.
// Set the class to inherit from EdiItem and the parser will automatically split by it.
List<IEdiItem> hl7Items;
using (var hl7Reader = new Hl7Reader(hl7Stream, (FHS fhs, BHS bhs, MSH msh) => typeof(TSMDMT02Splitter).GetTypeInfo(), new Hl7ReaderSettings { Split = true }))
hl7Items = hl7Reader.ReadToEnd().ToList();
// Find all N1 loops, they are all different ediItems
var obxLoop = hl7Items.OfType<TSMDMT02Splitter>().Where(m => m.LoopOBX != null).SelectMany(m => m.LoopOBX);
Debug.WriteLine(string.Format("OBX parts {0}", obxLoop.Count()));
}
/// <summary>
/// Copy a message and remove unwanted parts.
/// </summary>
public static void RunWithCopy()
{
Debug.WriteLine("******************************");
Debug.WriteLine(MethodBase.GetCurrentMethod().Name);
Debug.WriteLine("******************************");
Stream hl7Stream = File.OpenRead(Directory.GetCurrentDirectory() + Config.TestFilesPath + @"\MedicalRecord.txt");
// The split is driven by setting which class to split by in the template.
// Set the class to inherit from EdiItem and the parser will automatically split by it.
List<IEdiItem> hl7Items;
using (var hl7Reader = new Hl7Reader(hl7Stream, "EdiFabric.Templates.Hl7"))
hl7Items = hl7Reader.ReadToEnd().ToList();
var medRecords = hl7Items.OfType<TSMDMT02>();
var splitMedRecords = new List<TSMDMT02>();
foreach (var medRecord in medRecords)
{
foreach (var obxLoop in medRecord.LoopOBX)
{
var splitMedRecord = medRecord.Copy() as TSMDMT02;
splitMedRecord.LoopOBX.RemoveAll(l => splitMedRecord.LoopOBX.IndexOf(l) != medRecord.LoopOBX.IndexOf(obxLoop));
splitMedRecords.Add(splitMedRecord);
}
}
foreach (var medRecord in medRecords)
Debug.WriteLine(string.Format("Original: Med Record - OBX parts {0}", medRecord.LoopOBX.Count()));
foreach (var splitMedRecord in splitMedRecords)
Debug.WriteLine(string.Format("Split: Med Record - OBX parts {0}", splitMedRecord.LoopOBX.Count()));
}
}
[Serializable()]
[DataContract()]
[Message("HL7", "MDMT02")]
public class TSMDMT02Splitter : TSMDMT02
{
[Splitter]
[DataMember]
[Required]
[Pos(9)]
public new List<Loop_OBX_TSMDMT02> LoopOBX { get; set; }
}
}