public void TestGetFieldMSH() { List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "MSH")); if (lStr == null || lStr.Count <= 0 || lStr.Count > 1) { Assert.IsNull(lStr); } _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[0], 2); Assert.AreNotSame("CareLogic^2.16.840.1.113883.3.1452.100.4", data); data = _processHL7.GetField(_encoding, lStr[0], 2.1); Assert.AreNotSame("CareLogic", data); data = _processHL7.GetField(_encoding, lStr[0], 3); Assert.AreNotSame("TVFFC1", data); data = _processHL7.GetField(_encoding, lStr[0], 8); Assert.AreNotSame("ORM^O01", data); data = _processHL7.GetField(_encoding, lStr[0], 8.2); Assert.AreNotSame("O01", data); data = _processHL7.GetField(_encoding, lStr[0], 11); Assert.AreNotSame("2.3", data); }
public void TestGetFieldPID() { List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "PID")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, 1)); _processHL7 = ProcessHL7Message.Instance; var fName = _processHL7.GetField(_encoding, lStr[0], 5.1); //char enCodeChar = (parse.GetField(_encoding, lMsg[0], 1).Substring(0, 1))[0]; // not using the enCodeChar to parse subfields // get the first name of the patient // PID 5.2 fName = parse.GetField(_encoding, lStr[0], 5.1); Assert.AreNotSame("TIMOTHY", fName); var name1 = parse.GetField(_encoding, lStr[0], 5.0); Assert.AreNotSame("TEST^TIMOTHY^", fName); var name2 = parse.GetField(_encoding, lStr[0], 5); Assert.AreNotSame("TEST^TIMOTHY^", fName); }
public void TestGetFieldDG1() { // "DG1|1||779.34^^I9|||W" int nIdx = 1; List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "DG1")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, nIdx)); _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 1); Assert.AreNotSame(nIdx, data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 3.1); Assert.AreNotSame(notExpected: "779.34", actual: data); }
public void TestGetFieldNTE3() { // NTE|1||POC Barbiturates Result:Negative| int nIdx = 3; List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "NTE")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, nIdx)); _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 1); Assert.AreNotSame(nIdx, data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 3); Assert.AreNotSame(notExpected: "POC Buprenorphine Result:Positive", actual: data); }
public void TestGetFieldOBR_POC() { int nIdx = 2; List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "OBR")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, nIdx)); _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 1); Assert.AreNotSame(nIdx, data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 2); Assert.AreNotSame("EA194979A", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4); Assert.AreNotSame("POC^POC Testing", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4.1); Assert.AreNotSame("POC", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4.2); Assert.AreNotSame("POC Testing", data); }
public void AddSubComponent() { var enc = new HL7Encoding(); Segment PID = new Segment("PID", enc); Field f = new Field(enc); Component c = new Component(enc); c.IsSubComponentized = true; SubComponent sc1 = new SubComponent("A", enc); SubComponent sc2 = new SubComponent("B", enc); c.AddSubComponent(sc1); c.AddSubComponent(sc2); f.AddNewComponent(c); // Creates a new Field PID.AddNewField(f, 1); Message message = new Message(); message.AddNewSegment(PID); var str = message.SerializeMessage(false); Assert.AreEqual("PID|A&B\r", str); }
public MSH(HL7Encoding _encoding) { mMsgEncoding = _encoding; RequiredFields = ReqFields(); this.Errors = new List <string>(); this.SegmentMsg = string.Empty; }
public string Encode(IHL7Message hl7Message, HL7Encoding encoding, bool validate) { if (HL7Encoding.XML == encoding) { return(xmlParser.Encode(hl7Message.Message)); } return(pipeParser.Encode(hl7Message.Message)); }
/// <summary> /// GetPV1 /// Get the HL7 IN3 segment and parse into the IN2 object. /// Also perform field validation of required fields and types /// Verify no errors have been detected during processing /// </summary> /// <param name="line">HL7 segment to parse</param> /// <returns>IN3 object</returns> public IN3 GetIN3(HL7Encoding _encode, string line) { const string fnName = "GetIN3"; List <SegmentError> segError = null; IN3 ins = new IN3(); int nIdx = 0; try { ins.SegmentMsg = line; ins.Segment = "IN3"; segError = Validate(ins, _encode); // var enumCnt = Enum.GetNames(typeof(mshElements)).Length; foreach (int i in Enum.GetValues(typeof(in3Elements))) { nIdx = i; object obj = GetElement(_encode, line, i); if (obj == null) { // check if this a required field string sTmp1 = ((Gt1Elements)i).ToString(); RequiredField rqFld = ins.RequiredFields.Find(x => x.FieldName.Equals(sTmp1)); if (rqFld != null && rqFld.IsRequired) { ins.Errors.Add(string.Format("{0}:{1} - WARNING Element ({2}) not found in segment, at field {3}", modName, fnName, ((in3Elements)i).ToString(), i)); } continue; } switch ((in3Elements)i) { case in3Elements.Segment: ins.Segment = (string)obj; break; case in3Elements.SeqId: if (!string.IsNullOrEmpty((string)obj)) { ins.SeqId = int.Parse((string)obj); } break; default: ins.Errors.Add(string.Format("{0}:{1} - Error element ({2}) not found", modName, fnName, ((in3Elements)i).ToString())); break; } } } catch (Exception ex) { string sErr = string.Format("Exception:{0}:{1} - ({2}) {3}", modName, fnName, ((in3Elements)nIdx).ToString(), ex); ins.Errors.Add(sErr); Console.WriteLine(sErr); } return(ins); }
/// <summary> /// IsSegmentFound - in the list check if the list contains the entry based on the nIdx value /// This can be simpler by just checking if the nIdx value is less than or equal to the list count /// </summary> /// <param name="parse"></param> /// <param name="_encoding"></param> /// <param name="lStr"></param> /// <param name="nIdx"></param> /// <returns></returns> private static bool IsSegmentFound(HL7Parser parse, HL7Encoding _encoding, List <string> lStr, int nIdx) { Boolean bFound = false; int nSequenceFld = 1; foreach (var line in lStr) { if (parse.GetField(_encoding, line, nSequenceFld).Equals(nIdx.ToString())) // get the sequence field value { bFound = true; } } return(bFound); }
/// <summary> /// return HL7Encoding object that contains /// the Field separator and HL7 Encoding characters /// Use the /// </summary> /// <param name="hl7Msg"></param> /// <returns></returns> private static HL7Encoding GetHL7Encoding(List <string> hl7Msg) { HL7Encoding _encoding = null; foreach (var line in hl7Msg) { if (line.Substring(0, 3).Equals("MSH")) { // ok we have a Message Header line // now lets get the decoding informaiton _encoding = new HL7Encoding((line.Substring(3, 5)).ToCharArray()); break; } } return(_encoding); }
/// <summary> /// GetMessageType - Return the message type ORM or ORU or null if not found /// <param name="lsMsg"></param> /// <returns>string</returns> /// </summary> private static string GetMessageType(List <string> lsMsg) { HL7Encoding _encoding = null; foreach (var line in lsMsg) { if (line.Substring(0, 3).Equals("MSH")) { // ok we have a Message Header line // now lets get the decoding informaiton _encoding = new HL7Encoding((line.Substring(3, 5)).ToCharArray()); // split the string on the field separator string[] sTmp = line.Split(_encoding.FieldSeparator); return(sTmp[8].Substring(0, 3)); } } return(null); }
public void AddFieldTest() { var enc = new HL7Encoding(); Segment PID = new Segment("PID", enc); // Creates a new Field PID.AddNewField("1", 1); // Overwrites the old Field PID.AddNewField("2", 1); Message message = new Message(); message.AddNewSegment(PID); var str = message.SerializeMessage(false); Assert.AreEqual("PID|2\r", str); }
public void CustomDelimiterTest() { var encoding = new HL7Encoding { FieldDelimiter = '1', ComponentDelimiter = '2', SubComponentDelimiter = '3', RepeatDelimiter = '4', EscapeCharacter = '5' }; var message = new Message(); message.Encoding = encoding; message.AddSegmentMSH("FIRST", "SECOND", "THIRD", "FOURTH", "FIFTH", "ORU2R05F5", "SIXTH", "SEVENTH", "2.8"); var result = message.SerializeMessage(false); Assert.AreEqual("MSH124531", result.Substring(0, 9)); }
public void TestGetFieldOBR_NU3025() { int nIdx = 1; List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "OBR")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, nIdx)); _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 1); Assert.AreNotSame(nIdx, data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 2); Assert.AreNotSame("EA194979A", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4); Assert.AreNotSame("NU3025^PDX Custom Panel-Urine", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4.1); Assert.AreNotSame("NU3025", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 4.2); Assert.AreNotSame("PDX Custom Panel-Urine", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 16.1); Assert.AreNotSame("1010101010", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 16.2); Assert.AreNotSame("Support", data); data = _processHL7.GetField(_encoding, lStr[nIdx - 1], 16.3); Assert.AreNotSame("Qualifacts", data); }
public void AddRepeatingField() { var enc = new HL7Encoding(); Segment PID = new Segment("PID", enc); Field f = new Field(enc); Field f1 = new Field("A", enc); Field f2 = new Field("B", enc); f.HasRepetitions = true; f.AddRepeatingField(f1); f.AddRepeatingField(f2); // Creates a new Field PID.AddNewField(f, 1); Message message = new Message(); message.AddNewSegment(PID); var str = message.SerializeMessage(false); Assert.AreEqual("PID|A~B\r", str); }
public void TestGetFieldIN1() { int nIdx = 1; List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "IN1")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } Assert.IsTrue(IsSegmentFound(parse, _encoding, lStr, nIdx)); // not using the enCodeChar to parse subfields // get the first name of the patient // IN1 Address=5.1, City=5.3, City=5.4, Zip=5.5, Country=5.6, xxx=5.9 var data = parse.GetField(_encoding, lStr[nIdx - 1], 5.1); Assert.AreNotSame("PO BOX 2941&&", data); data = parse.GetField(_encoding, lStr[nIdx - 1], 5.3); Assert.AreNotSame("Hartford", data); data = parse.GetField(_encoding, lStr[nIdx - 1], 5.4); Assert.AreNotSame("CT", data); data = parse.GetField(_encoding, lStr[nIdx - 1], 5.5); Assert.AreNotSame("06104", data); data = parse.GetField(_encoding, lStr[nIdx - 1], 5.6); Assert.AreNotSame("USA", data); data = parse.GetField(_encoding, lStr[nIdx - 1], 5.9); Assert.AreNotSame("Hartford", data); }
public void TestGetFieldORC() { List <string> lMsg = Initialize; HL7Parser parse = new HL7Parser(); // we need to get the HL7 field separator and encoding characters HL7Encoding _encoding = GetHL7Encoding(lMsg); List <string> lStr = new List <string>(parse.GetSegment(lMsg, "ORC")); if (lStr == null || lStr.Count <= 0) { Assert.IsNull(lStr); } _processHL7 = ProcessHL7Message.Instance; var data = _processHL7.GetField(_encoding, lStr[0], 1); Assert.AreNotSame("1", data); data = _processHL7.GetField(_encoding, lStr[0], 2); Assert.AreNotSame("EA194979A", data); }
public void AddComponentsTest() { var encoding = new HL7Encoding(); //Create a Segment with name ZIB Segment newSeg = new Segment("ZIB", encoding); // Create Field ZIB_1 Field ZIB_1 = new Field("ZIB1", encoding); // Create Field ZIB_5 Field ZIB_5 = new Field("ZIB5", encoding); // Create Component ZIB.5.3 Component com1 = new Component("ZIB.5.3_", encoding); // Add Component ZIB.5.3 to Field ZIB_5 ZIB_5.AddNewComponent(com1, 3); // Overwrite the same field again ZIB_5.AddNewComponent(new Component("ZIB.5.3", encoding), 3); // Add Field ZIB_1 to segment ZIB, this will add a new filed to next field location, in this case first field newSeg.AddNewField(ZIB_1); // Add Field ZIB_5 to segment ZIB, this will add a new filed as 5th field of segment newSeg.AddNewField(ZIB_5, 5); // Add segment ZIB to message var message = new Message(this.HL7_ADT); message.AddNewSegment(newSeg); string serializedMessage = message.SerializeMessage(false); Assert.AreEqual("ZIB|ZIB1||||ZIB5^^ZIB.5.3\r", serializedMessage); }
/// <summary> /// Validate - Validate the required fields for the given HL7 segment object /// </summary> /// <param name="seg">HL7 IN1 segment object</param> /// <returns>list<SegmentError></returns> private List <SegmentError> Validate(IN1 seg, HL7Encoding _encode) { const string fnName = "Validate"; List <SegmentError> segErrors = new List <SegmentError>(); try { foreach (var rqFld in seg.RequiredFields) { if (rqFld.IsRequired) { Object obj = GetField(_encode, seg.SegmentMsg, rqFld.FieldIdx); if (string.IsNullOrEmpty((string)obj)) { segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - field {2} Value is required cannot be null", modName, fnName, rqFld.FieldName))); break; // leave } switch (rqFld.FieldType.ToLower()) { case "int": bool bAns = int.TryParse(((string)obj), out int nValue); if (!bAns) { segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - field {2} type is 'int' value is required cannot be null", modName, fnName, rqFld.FieldName))); } break; case "string": string sTmp = (string)obj; // check if string is greate than fieldLength if (sTmp.Length > rqFld.FieldLength) { segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - field {2} type is 'string' value is greater than max size {3}", modName, fnName, rqFld.FieldName, rqFld.FieldLength))); } if (rqFld.FieldName.Equals(MshElements.MessageType.ToString()) && "MSH".Equals(seg.SegmentMsg)) { // split the string ORM^O01. Validate ORM is first field if (!"ORM^O01".Equals(sTmp)) { segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - Message type must be ORM^O01 : (" + (string)obj + ")", modName, fnName))); } } break; case "date": // the field is a date field but is a string in the HL7 message switch (((string)obj).Length) { case 8: case 12: case 14: // good break; default: segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - field {2} type is 'date' value out of range YYYYMMDD, YYYYMMDDHHMM, YYYYMMDDHHMMSS", modName, fnName, rqFld.FieldName))); break; } break; default: segErrors.Add(new SegmentError(rqFld.HL7Segment, rqFld.FieldName, string.Format("{0}:{1} - FieldType ({2}) is undefined", modName, fnName, rqFld.FieldType.ToLower()))); break; } } } } catch (Exception exp) { string sTmp = string.Format("{0}:{1} - EXCEPTION ({2})", modName, fnName, exp); segErrors.Add(new SegmentError(seg.Segment, "N/A", sTmp)); } return(segErrors); }
/// <summary> /// GetPV1 /// Get the HL7 IN1 segment and parse into the IN1 object. /// Also perform field validation of required fields and types /// Verify no errors have been detected during processing /// </summary> /// <param name="line">HL7 segment to parse</param> /// <returns>IN1 object</returns> public IN1 GetIN1(HL7Encoding _encode, string line) { const string fnName = "GetIN1"; List <SegmentError> segError = null; IN1 in1 = new IN1(); int nIdx = 0; try { in1.SegmentMsg = line; in1.Segment = "IN1"; segError = Validate(in1, _encode); // var enumCnt = Enum.GetNames(typeof(mshElements)).Length; foreach (int i in Enum.GetValues(typeof(In1Elements))) { nIdx = i; object obj = GetElement(_encode, line, i); if (obj == null) { // check if this a required field string sTmp1 = ((Gt1Elements)i).ToString(); RequiredField rqFld = in1.RequiredFields.Find(x => x.FieldName.Equals(sTmp1)); if (rqFld != null && rqFld.IsRequired) { in1.Errors.Add(string.Format("{0}:{1} - WARNING Element ({2}) not found in segment, at field {3}", modName, fnName, ((In1Elements)i).ToString(), i)); } continue; } switch ((In1Elements)i) { case In1Elements.Segment: in1.Segment = (string)obj; break; case In1Elements.SeqId: if (!string.IsNullOrEmpty((string)obj)) { in1.SeqId = int.Parse((string)obj); } break; case In1Elements.AdmissionCode: in1.AddmissinCode = (string)obj; break; case In1Elements.AdmissionDate: in1.AdmissionDate = (string)obj; break; case In1Elements.AgreementCode: in1.AgreementCode = (string)obj; break; case In1Elements.AssignmentBenefits: in1.AssignmentBenefits = (string)obj; break; case In1Elements.Authorization: in1.Authorization = (string)obj; break; case In1Elements.BenefitsPriority: in1.BenefitsPriority = (string)obj; break; case In1Elements.BillingStatus: in1.BillingStatus = (string)obj; break; case In1Elements.CompanyAddress: in1.CompanyAddress = (string)obj; break; case In1Elements.CompanyCode: in1.CompanyCode = (string)obj; break; case In1Elements.CompanyContact: in1.CompanyContact = (string)obj; break; case In1Elements.CompanyName: in1.CompanyName = (string)obj; break; case In1Elements.CompanyPhone: in1.CompanyPhone = (string)obj; break; case In1Elements.CompanyPlanCode: in1.CompanyPlanCode = (string)obj; break; case In1Elements.CoordinationBenefits: in1.CoordinationBenefits = (string)obj; break; case In1Elements.CoverageType: in1.CoverageType = (string)obj; break; case In1Elements.DelayBefore: in1.DelayBefore = (string)obj; break; case In1Elements.EffectiveDate: in1.EffectiveDate = (string)obj; break; case In1Elements.EligibilityCode: in1.EligibilityCode = (string)obj; break; case In1Elements.EligibilityDate: in1.EligibilityDate = (string)obj; break; case In1Elements.EmployerAddress: in1.EmployerAddress = (string)obj; break; case In1Elements.EmployerId: in1.EmployerId = (string)obj; break; case In1Elements.EmployerName: in1.EmployerName = (string)obj; break; case In1Elements.EmploymentStatus: in1.EmploymentStatus = (string)obj; break; case In1Elements.ExpirationDate: in1.ExpirationDate = (string)obj; break; case In1Elements.GroupName: in1.GroupName = (string)obj; break; case In1Elements.GroupNumber: in1.GroupNumber = (string)obj; break; case In1Elements.Handicap: in1.Handicap = (string)obj; break; case In1Elements.InsuredAddress: in1.InsuredAddress = (string)obj; break; case In1Elements.InsuredDOB: in1.InsuredDOB = (string)obj; break; case In1Elements.InsuredIdNumber: in1.InsuredIdNumber = (string)obj; break; case In1Elements.InsuredName: in1.InsuredName = (string)obj; break; case In1Elements.InsuredSex: in1.InsuredSex = (string)obj; break; case In1Elements.PlayType: in1.PlanType = (string)obj; break; case In1Elements.PolicyAmount: in1.PolicyAmount = (string)obj; break; case In1Elements.PolicyDays: in1.PolicyDays = (string)obj; break; case In1Elements.PolicyDeductible: in1.PolicyDeductible = (string)obj; break; case In1Elements.PolicyNumber: in1.PolicyNumber = (string)obj; break; case In1Elements.PreAdmin: in1.PreAdmin = (string)obj; break; case In1Elements.PriorInsurancePlanId: in1.PriorInsurancePlanId = (string)obj; break; case In1Elements.RelationshipTo: in1.RelationshipTo = (string)obj; break; case In1Elements.ReleaseCode: in1.ReleaseCode = (string)obj; break; case In1Elements.ReserveDays: in1.ReserveDays = (string)obj; break; case In1Elements.RoomPrivate: in1.RoomPrivate = (string)obj; break; case In1Elements.RoomSemiPrivate: in1.RoomSemiPrivate = (string)obj; break; case In1Elements.ShortName: in1.ShortName = (string)obj; break; case In1Elements.VerificationBy: in1.VerificationBy = (string)obj; break; case In1Elements.VerificationDate: in1.VerificationDate = (string)obj; break; case In1Elements.VerificationStatus: in1.VerificationStatus = (string)obj; break; default: in1.Errors.Add(string.Format("{0}:{1} - Error element ({0}) not found", modName, fnName, ((In1Elements)i).ToString())); break; } } } catch (Exception ex) { string sErr = string.Format("Exception:{0}:{1} - ({2}) {3}", modName, fnName, ((In1Elements)nIdx).ToString(), ex); in1.Errors.Add(sErr); Console.WriteLine(sErr); } return(in1); }
/// <inheritdoc/> public HNMessage CreateRequestMessage(string pharmacyId, string userId, string ipAddress) { CultureInfo culture = CultureInfo.CreateSpecificCulture("en-CA"); culture.DateTimeFormat.DateSeparator = "/"; HL7Encoding encoding = new HL7Encoding(); Message m = new Message(); DateTime utc = DateTime.UtcNow; TimeZoneInfo localtz = TimeZoneInfo.FindSystemTimeZoneById(this.timeZoneId); DateTime localDateTime = TimeZoneInfo.ConvertTimeFromUtc(utc, localtz); // MSH - Message Header m.AddSegmentMSH( this.hnClientConfig.SendingApplication, this.hnClientConfig.SendingFacility, this.hnClientConfig.ReceivingApplication, this.hnClientConfig.ReceivingFacility, $"{userId}:{ipAddress}", $"{HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE}^00", TRACE, this.hnClientConfig.ProcessingID, this.hnClientConfig.MessageVersion); m.SetValue("MSH.7", string.Format(culture, "{0:yyyy/MM/dd HH:mm:ss}", localDateTime)); // HNClient specific date format m.SetValue("MSH.9", HNClientConfiguration.PATIENT_PROFILE_MESSAGE_TYPE); // HNClient doesn't recognize event types (removes ^00 from message type) // ZCA - Claims Standard Message Header Segment zca = new Segment(HNClientConfiguration.SEGMENT_ZCA, encoding); zca.AddNewField(string.Empty); // BIN zca.AddNewField(this.hnClientConfig.ZCA.CPHAVersionNumber); // CPHA Version Number zca.AddNewField(this.hnClientConfig.ZCA.TransactionCode); // Transaction Code zca.AddNewField(this.hnClientConfig.ZCA.SoftwareId); // Provider Software ID zca.AddNewField(this.hnClientConfig.ZCA.SoftwareVersion); // Provider Software Version m.AddNewSegment(zca); // ZCB - Provider Information Segment zcb = new Segment(HNClientConfiguration.SEGMENT_ZCB, encoding); zcb.AddNewField(this.hnClientConfig.ZCB.PharmacyId); // Pharmacy ID Code zcb.AddNewField(localDateTime.ToString("yyMMdd", culture)); // Provider Transaction Date zcb.AddNewField(TRACE); // Trace Number m.AddNewSegment(zcb); // ZPL - Location Information Segment zpl = new Segment(HNClientConfiguration.SEGMENT_ZPL, encoding); zpl.AddNewField(pharmacyId); // Requested PharmaNet Location Identifier zpl.AddNewField(string.Empty); // PharmaNet Location Nam zpl.AddNewField(string.Empty); // Location Type Code zpl.AddNewField(string.Empty); // Address Line 1 zpl.AddNewField(string.Empty); // Address Line 2 zpl.AddNewField(string.Empty); // City or Municipality zpl.AddNewField(string.Empty); // Province Code zpl.AddNewField(string.Empty); // Postal Code zpl.AddNewField(string.Empty); // Country Code zpl.AddNewField(string.Empty); // Telecom Type Code zpl.AddNewField(string.Empty); // Effective Date zpl.AddNewField(string.Empty); // Area Code zpl.AddNewField(string.Empty); // Telephone Number zpl.AddNewField(string.Empty); // Termination Date zpl.AddNewField(this.hnClientConfig.ZPL.TransactionReasonCode); // Transaction Reason Code m.AddNewSegment(zpl); // ZZZ - Transaction Control // ZZZ|TIL||1111|P1|nnnnnnnnnn|||||ZZZ1 Segment zzz = new Segment(HNClientConfiguration.SEGMENT_ZZZ, encoding); zzz.AddNewField(HNClientConfiguration.PHARMACY_PROFILE_TRANSACTION_ID); // Transaction ID zzz.AddNewField(string.Empty); // Response Status (Empty) zzz.AddNewField(TRACE); // Trace Number zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerIdRef); // Practitioner ID Reference zzz.AddNewField(this.hnClientConfig.ZZZ.PractitionerId); // Practitioner ID m.AddNewSegment(zzz); HNMessage retMessage = new HNMessage(); retMessage.Message = m.SerializeMessage(false); return(retMessage); }
/// <summary> /// GetOrders - get all the segments that make up an order /// { /// ORC - Required (Order) /// [ /// OBR - Required /// [{NTE}] - Optional, if present can repeat /// [{DG1}] - Optional, if present can repeat /// [{ /// OBX - Required /// [{NTE}] - Optional, if present can repeat /// }] /// ] /// [{CTI}] - Optional, if present can repeat /// [BLG] - Optional /// } /// /// </summary> /// <param name="hl7Msg">HL7 ORM message</param> /// <returns>List<HL7Orders> object</returns> public List <HL7Order> GetOrders(HL7Encoding _encode, List <string> hl7Msg) { HL7Order order = null; HL7Details od = null; HL7Observation obs = null; List <HL7Order> hl7Orders = new List <HL7Order>(); BuildOrders bldOrders = new BuildOrders(); BuildNTE bldNTE = new BuildNTE(); ErrorMsg errorMsg = new ErrorMsg(); List <ErrorMsg> lErrorMsg = new List <ErrorMsg>(); string line = string.Empty; bool bOrder = false; // order bool bOrderDetails = false; // order details bool bObservation = false; // observation try { // search for the ORC segment for (int nIdx = 0; nIdx < hl7Msg.Count; nIdx++) { line = hl7Msg[nIdx].ToString(); string sTmp = GetField(_encode, line, 0); if (string.IsNullOrEmpty(sTmp)) { lErrorMsg.Add(new ErrorMsg(1, line)); } else { // Enum.TryParse<Segments>(sTmp, out sResult); switch (((Segments)Enum.Parse(typeof(Segments), sTmp))) { case Segments.ORC: // start HL7 order if (order != null) { if (bOrderDetails) { if (bObservation) { if (obs != null) { od.Observations.Add(obs); obs = null; } bObservation = false; } if (od != null) { order.HL7Details.Add(od); od = null; } bOrderDetails = false; } // we are working on a current order and a new order was found // we need to add the existing order to the array // create new order and start again hl7Orders.Add(order); order = null; } // lets start again order = new HL7Order { ORCSegment = new BuildORC().GetORC(_encode, line) // build ORC object }; bOrder = true; // working on new order bOrderDetails = false; bObservation = false; break; case Segments.OBR: // order details if (od != null) { // already building Order Detail object // new message found add existing order detial and start again order.HL7Details.Add(od); od = null; } od = new HL7Details("ORM") { OBRSegment = new BuildOBR().GetOBR(_encode, line) }; bOrderDetails = true; bObservation = false; break; case Segments.DG1: // order details od.DG1Segments.Add(new BuildDG1().GetDG1(_encode, line)); bObservation = false; break; // Observations consist of OBX, NTE case Segments.OBX: // Observations if (obs != null) { od.Observations.Add(obs); obs = null; } obs = new HL7Observation("ORM") { OBXSegment = new BuildOBX().GetOBX(_encode, line, "ORM") }; bObservation = true; break; case Segments.NTE: // note the order of the if statements matter. // Observations than orderDetails than order if (bObservation) // NTE for Observations { obs.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for OBR group } else if (bOrderDetails) // NTE for Order details { od.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for OBR group } else if (bOrder) // NTE for Order { order.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for ORC group } break; case Segments.BLG: order.BLGSegment = new BuildBLG().GetBLG(_encode, line); // build BLG break; case Segments.CTI: order.CTISegments.Add(new BuildCTI().GetCTI(_encode, line)); // build CT1 break; default: // nIdx = hl7Msg.Count; // we are done, leave break; } } } // we need to finish what we stated if (bOrder) { if (order != null) { if (bOrderDetails) { if (bObservation) { if (obs != null) { od.Observations.Add(obs); obs = null; } bObservation = false; } if (od != null) { order.HL7Details.Add(od); od = null; } bOrderDetails = false; } // we are working on a current order and a new order was found // we need to add the existing order to the array // create new order and start again hl7Orders.Add(order); order = null; } } } catch (Exception ex) { string sErr = string.Format("BuildOrders:GetOrders: Exception {0}", ex); errorMsg.Message = sErr; Console.WriteLine(sErr); } return(hl7Orders); }
/// <summary> /// GetHeader /// Build the hl7 message header (MSH) /// Each HL7 object contains a list of errors. /// Verify no errors have been detected during processing /// </summary> /// <param name="hl7Msg">hl7 message</param> /// <returns>HL7Header object</returns> public HL7Header GetHeader(List <string> hl7Msg) { HL7Encoding hl7Encoding = null; MSH msh = null; NTE nte = null; bool bMSHFound = false; List <NTE> notes = new List <NTE>(); List <ErrorMsg> lErrorMsg = new List <ErrorMsg>(); string line = string.Empty; try { // search for the MSH segment should be the first one // any following NTE at the top level we need to keep with the MSH Header object for (int nIdx = 0; nIdx < hl7Msg.Count; nIdx++) { line = hl7Msg[nIdx].ToString(); // index 0 must be the MSH segment if (line.Substring(0, 3).Equals("MSH")) { // ok we have a Message Header line // now lets get the decoding informaiton hl7Encoding = new HL7Encoding((line.Substring(3, 5)).ToCharArray()); } else { continue; // not found get next line } string sTmp = GetField(hl7Encoding, line, 0); // should be the message segment if (string.IsNullOrEmpty(sTmp)) { lErrorMsg.Add(new ErrorMsg(1, line)); } else { // Enum.TryParse<Segments>(sTmp, out sResult); switch (((Segments)Enum.Parse(typeof(Segments), sTmp))) { case Segments.MSH: msh = new BuildMSH().GetMSH(hl7Encoding, line); bMSHFound = true; break; case Segments.NTE: // can be more than one if (bMSHFound) // only keep NTE segments on this level { nte = new BuildNTE().GetNTE(hl7Encoding, line); notes.Add(nte); } break; default: nIdx = hl7Msg.Count; // we are done, leave break; } } } } catch (Exception ex) { string sErr = string.Format("GetHeader:GetHeader: Exception {0}", ex); msh.Errors.Add(sErr); Console.WriteLine(sErr); } return(new HL7Header(msh, notes, hl7Encoding)); }
public SubComponent(string val, HL7Encoding encoding) { this.Encoding = encoding; this.Value = val; }
public void NotEncodingTest() { var enc = new HL7Encoding().Encode("<1"); Assert.AreEqual(enc, "<1"); }
/// <summary> /// GetPatient - get all the segments that make up a patient /// /// PID ------------| --- Patient /// [PD1] ----------| --- Optinal /// [{NTE}] --------| --- Notes /// [PV1] ----------| --- Patient Visit /// [PV2] ----------| --- Patient Visit /// IN1 ------------| --- Insurance /// [IN2] ----------| --- Insurance /// [IN3] ----------| --- Insurance /// [GT1] ----------| --- Guanator /// [{AL1}] --------| --- ??? /// /// </summary> /// <param name="hl7Msg">HL7 ORM message</param> /// <returns>HL7Patient</returns> public HL7Patient GetPatient(HL7Encoding _encode, List <string> hl7Msg, string msgType) { HL7Patient pat = new HL7Patient(msgType); List <NTE> notes = new List <NTE>(); string line = string.Empty; bool bPIDFound = false; try { // search for the PID segment // any following NTE at the top level we need to keep with the MSH Header object for (int nIdx = 0; nIdx < hl7Msg.Count; nIdx++) { line = hl7Msg[nIdx].ToString(); string sTmp = GetField(_encode, line, 0); if (string.IsNullOrEmpty(sTmp)) { pat.Errors.Add(string.Format("BuildPatient:GetPatient: Error Segment not found ({0})", line)); } else { // Enum.TryParse<Segments>(sTmp, out sResult); switch (((Segments)Enum.Parse(typeof(Segments), sTmp))) { case Segments.PID: // we found the start pat.PIDSegment = new BuildPID().GetPID(_encode, line, msgType); bPIDFound = true; break; case Segments.NTE: // can be more than one if (bPIDFound) // only keep NTE segments on this level { pat.NTESegments.Add(new BuildNTE().GetNTE(_encode, line)); } break; case Segments.PV1: pat.Visit1 = new BuildPV1().GetPV1(_encode, line); break; case Segments.PV2: pat.Visit2 = new BuildPV2().GetPV2(_encode, line); break; case Segments.IN1: pat.Insurance1 = new BuildIN1().GetIN1(_encode, line); break; case Segments.IN2: pat.Insurance2 = new BuildIN1().GetIN1(_encode, line); break; case Segments.IN3: pat.Insurance3 = new BuildIN1().GetIN1(_encode, line); break; case Segments.GT1: pat.GT1Segment = new BuildGT1().GetGT1(_encode, line); break; case Segments.AL1: pat.AL1Segments.Add(new BuildAL1().GetAL1(_encode, line)); break; default: if (bPIDFound) { nIdx = hl7Msg.Count; // we are done, leave } break; } } } } catch (Exception ex) { string sErr = string.Format("BuildPatient:GetPatient: Exception {0}", ex); pat.Errors.Add(sErr); Console.WriteLine(sErr); } return(pat); }
/// <summary> /// GetOBR /// Get the HL7 OBR segment and parse into the OBR object. /// Also perform field validation of required fields and types /// Verify no errors have been detected during processing /// </summary> /// <param name="line">HL7 segment to parse</param> /// <returns>OBX object</returns> public OBX GetOBX(HL7Encoding _encode, string line, string messageType) { const string fnName = "GetOBX"; List <SegmentError> segError = null; OBX obx = new OBX(messageType); int nIdx = 0; try { obx.SegmentMsg = line; obx.Segment = "OBR"; segError = Validate(obx, _encode, messageType); // var enumCnt = Enum.GetNames(typeof(mshElements)).Length; foreach (int i in Enum.GetValues(typeof(obxElements))) { nIdx = i; object obj = GetElement(_encode, line, i); if (obj == null) { // check if this a required field string sTmp1 = ((Gt1Elements)i).ToString(); RequiredField rqFld = obx.RequiredFields.Find(x => x.FieldName.Equals(sTmp1)); if (rqFld != null && rqFld.IsRequired) { obx.Errors.Add(string.Format("{0}:{1} - WARNING Element ({2}) not found in segment, at field {3}", modName, fnName, ((obxElements)i).ToString(), i)); } continue; } switch ((obxElements)i) { case obxElements.Segment: obx.Segment = (string)obj; break; case obxElements.SeqId: if (!string.IsNullOrEmpty((string)obj)) { obx.SeqId = int.Parse((string)obj); } break; case obxElements.ObservationIdentifier: if (!string.IsNullOrEmpty((string)obj)) { obx.TestCode = GetComponent(_encode, (string)obj, (int)observationIdentifier.testCode); obx.TestCodeDescription = GetComponent(_encode, (string)obj, (int)observationIdentifier.testCodeDescription); obx.LimTestCode = GetComponent(_encode, (string)obj, (int)observationIdentifier.limTestCode); obx.LimTestCodeDescription = GetComponent(_encode, (string)obj, (int)observationIdentifier.limTestCodeDescription); obx.CodingSystem = GetComponent(_encode, (string)obj, (int)observationIdentifier.codingSystem); } break; case obxElements.ObservationSubId: if (!string.IsNullOrEmpty((string)obj)) { obx.ObservationSubId = (string)obj; } break; case obxElements.ObservationValue: if (!string.IsNullOrEmpty((string)obj)) { obx.ObservationValue = (string)obj; } break; case obxElements.ValueType: if (!string.IsNullOrEmpty((string)obj)) { obx.ValueType = (string)obj; } break; case obxElements.Units: if (!string.IsNullOrEmpty((string)obj)) { obx.Units = (string)obj; } break; case obxElements.ReferenceRange: if (!string.IsNullOrEmpty((string)obj)) { obx.ReferenceRange = (string)obj; } break; case obxElements.AbnormalFlags: if (!string.IsNullOrEmpty((string)obj)) { obx.AbnormalFlags = (string)obj; } break; case obxElements.Probability: if (!string.IsNullOrEmpty((string)obj)) { obx.Probability = (string)obj; } break; case obxElements.AbnormalTest: if (!string.IsNullOrEmpty((string)obj)) { obx.AbnormalTest = (string)obj; } break; case obxElements.ResultStatus: if (!string.IsNullOrEmpty((string)obj)) { obx.ResultStatus = (string)obj; } break; case obxElements.ObservedLastDate: if (!string.IsNullOrEmpty((string)obj)) { obx.ObservedLastDate = (string)obj; } break; case obxElements.AccessChecks: if (!string.IsNullOrEmpty((string)obj)) { obx.AccessChecks = (string)obj; } break; case obxElements.ObservationDateTime: if (!string.IsNullOrEmpty((string)obj)) { obx.ObservationDateTime = (string)obj; } break; case obxElements.Producers_ID: if (!string.IsNullOrEmpty((string)obj)) { obx.Producers_ID = (string)obj; } break; default: obx.Errors.Add(string.Format("{0}:{1} - Error element ({2}) not found", modName, fnName, ((obxElements)i).ToString())); break; } } } catch (Exception ex) { string sErr = string.Format("Exception:{0}:{1} - ({2}) {3}", modName, fnName, ((obxElements)nIdx).ToString(), ex); obx.Errors.Add(sErr); Console.WriteLine(sErr); } return(obx); }
public string Encode(HL7Encoding hL7Encoding) { return(m_HL7Parser.Encode(this, hL7Encoding, true)); }
/// <summary> /// GetPV1 /// Get the HL7 ORC segment and parse into the orc object. /// Also perform field validation of required fields and types /// Verify no errors have been detected during processing /// </summary> /// <param name="line">HL7 segment to parse</param> /// <returns>ORC object</returns> public ORC GetORC(HL7Encoding _encode, string line) { const string fnName = "GetORC"; List <SegmentError> segError = null; ORC orc = new ORC(); int nIdx = 0; try { orc.SegmentMsg = line; orc.Segment = "ORC"; segError = Validate(orc, _encode); // var enumCnt = Enum.GetNames(typeof(mshElements)).Length; foreach (int i in Enum.GetValues(typeof(orcElements))) { nIdx = i; object obj = GetElement(_encode, line, i); if (obj == null) { // check if this a required field string sTmp1 = ((Gt1Elements)i).ToString(); RequiredField rqFld = orc.RequiredFields.Find(x => x.FieldName.Equals(sTmp1)); if (rqFld != null && rqFld.IsRequired) { orc.Errors.Add(string.Format("{0}:{1} - WARNING Element ({2}) not found in segment, at field {3}", modName, fnName, ((orcElements)i).ToString(), i)); } continue; } switch ((orcElements)i) { case orcElements.Segment: orc.Segment = (string)obj; break; case orcElements.EnteredBy: if (!string.IsNullOrEmpty((string)obj)) { orc.EnteredBy = (string)obj; } break; case orcElements.FillerOrderNumber: if (!string.IsNullOrEmpty((string)obj)) { orc.FillerOrderNumber = (string)obj; } break; case orcElements.OrderControl: if (!string.IsNullOrEmpty((string)obj)) { orc.OrderControl = (string)obj; } break; case orcElements.OrderingPhysician: if (!string.IsNullOrEmpty((string)obj)) { orc.NPI = GetComponent(_encode, (string)obj, (int)orcProvider.NPI); orc.LastName = GetComponent(_encode, (string)obj, (int)orcProvider.LastName); orc.FirstName = GetComponent(_encode, (string)obj, (int)orcProvider.FirstName); } break; case orcElements.OrderStatus: if (!string.IsNullOrEmpty((string)obj)) { orc.OrderStatus = (string)obj; } break; case orcElements.Parent: if (!string.IsNullOrEmpty((string)obj)) { orc.Parent = (string)obj; } break; case orcElements.PlacerGroupNumber: if (!string.IsNullOrEmpty((string)obj)) { orc.PlacerGroupNumber = (string)obj; } break; case orcElements.PlacerOrderNumber: if (!string.IsNullOrEmpty((string)obj)) { orc.PlacerOrderNumber = (string)obj; } break; case orcElements.QuantityTiming: if (!string.IsNullOrEmpty((string)obj)) { orc.QuantityTiming = (string)obj; } break; case orcElements.ResponseFlag: if (!string.IsNullOrEmpty((string)obj)) { orc.ResponseFlag = (string)obj; } break; case orcElements.TransactionDateTime: if (!string.IsNullOrEmpty((string)obj)) { orc.TransactionDateTime = (string)obj; } break; case orcElements.VerifiedBy: if (!string.IsNullOrEmpty((string)obj)) { orc.VerifiedBy = (string)obj; } break; default: orc.Errors.Add(string.Format("{0}:{1} - Error element ({2}) not found", modName, fnName, (orcElements)i).ToString()); break; } } } catch (Exception ex) { string sErr = string.Format("Exception:{0}:{1} - ({2}) {3}", modName, fnName, ((orcElements)nIdx).ToString(), ex); orc.Errors.Add(sErr); Console.WriteLine(sErr); } return(orc); }