/// <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)); }
/// <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> /// GetResults - get the HL7Result object based on the hl7 message /// </summary> /// <param name="_encode"></param> /// <param name="hl7Msg"></param> /// <returns></returns> public List <HL7Results> GetResults(HL7Encoding _encode, List <string> hl7Msg) { HL7Results result = null; HL7Details od = null; HL7Observation obs = null; List <HL7Results> hl7Results = new List <HL7Results>(); BuildResults bldResult = new BuildResults(); BuildNTE bldNTE = new BuildNTE(); var errorMsg = new ErrorMsg(); List <ErrorMsg> lErrorMsg = new List <ErrorMsg>(); string line = string.Empty; bool bResult = false; // result bool bResultDetails = false; // result 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 (result != null) { if (bResultDetails) { if (bObservation) { if (obs != null) { od.Observations.Add(obs); obs = null; } bObservation = false; } if (od != null) { result.HL7Details.Add(od); od = null; } bResultDetails = 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 hl7Results.Add(result); result = null; } // lets start again result = new HL7Results { ORCSegment = new BuildORC().GetORC(_encode, line) // build ORC object }; bResult = true; // working on new order bResultDetails = 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 result.HL7Details.Add(od); od = null; } od = new HL7Details("ORU") { OBRSegment = new BuildOBR().GetOBR(_encode, line) }; bResultDetails = true; bObservation = false; break; // Observations consist of OBX, NTE case Segments.OBX: // Observations if (obs != null) { od.Observations.Add(obs); obs = null; } obs = new HL7Observation("ORU") { OBXSegment = new BuildOBX().GetOBX(_encode, line, "ORU") }; bObservation = true; // if ("ED".Equals(obs.OBXSegment.ValueType)) // { // // this is the Embedded PDF segment so lets end this // bObservation = true; // } break; case Segments.NTE: // Note the order matters keep this order // if (bObservation) // NTE for Observations { obs.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for OBR group } else if (bResultDetails) // NTE for Order details { od.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for OBR group } else if (bResult) // NTE for Order { result.NTESegments.Add(bldNTE.GetNTE(_encode, line)); // build NTE for ORC group } break; default: // nIdx = hl7Msg.Count; // we are done, leave break; } } } // we need to finish what we stated if (bResult) { if (result != null) { if (bResultDetails) { if (bObservation) { if (obs != null) { od.Observations.Add(obs); obs = null; } bObservation = false; } if (od != null) { result.HL7Details.Add(od); od = null; } bResultDetails = 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 hl7Results.Add(result); result = null; } } } catch (Exception ex) { string sErr = string.Format("BuildResults:GetResults: Exception {0}", ex); errorMsg.Message = sErr; Console.WriteLine(sErr); } return(hl7Results); }