public void TestSpecialCharacterEntryEndingSlash() { PipeParser parser = new PipeParser(); ORU_R01 oru = new ORU_R01(); oru.MSH.MessageType.MessageType.Value = "ORU"; oru.MSH.MessageType.TriggerEvent.Value = "R01"; oru.MSH.EncodingCharacters.Value = @"^~\&"; oru.MSH.VersionID.VersionID.Value = "2.3.1"; oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).GetOBSERVATION(0).OBX.ValueType.Value = "FT"; oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).OBR.SetIDOBR.Value = "1"; Varies v = oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).GetOBSERVATION(0).OBX.GetObservationValue(0); ST text = new ST(oru); text.Value = @"This\.br\is\.br\A Test~"; v.Data = text; string encodedData = parser.Encode(oru); IMessage msg = parser.Parse(encodedData); oru = (ORU_R01)msg; FT data = (FT)oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).GetOBSERVATION(0).OBX.GetObservationValue(0).Data; Assert.AreEqual(@"This\.br\is\.br\A Test~", data.Value); }
public void TestValidHl7Data() { PipeParser parser = new PipeParser(); ORU_R01 oru = new ORU_R01(); oru.MSH.MessageType.MessageType.Value = "ORU"; oru.MSH.MessageType.TriggerEvent.Value = "R01"; oru.MSH.EncodingCharacters.Value = @"^~\&"; oru.MSH.VersionID.VersionID.Value = "2.3.1"; oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).GetOBSERVATION(0).OBX.ValueType.Value = "FT"; oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).OBR.SetIDOBR.Value = "1"; Varies v = oru.GetPATIENT_RESULT(0).GetORDER_OBSERVATION(0).GetOBSERVATION(0).OBX.GetObservationValue(0); ST text = new ST(oru); text.Value = @"Th&is\.br\is\.br\A T|est\"; v.Data = text; string encodedData = parser.Encode(oru); //Console.WriteLine(encodedData); string[] segs = encodedData.Split('\r'); string[] fields = segs[2].Split('|'); string data = fields[5]; Assert.AreEqual(@"Th\T\is\.br\is\.br\A T\F\est\E\", data); }
/// <summary> Encodes a Varies type by extracting it's data field and encoding that. Returns true /// if the data field (or one of its components) contains a value. /// </summary> private bool EncodeVaries(Varies datatypeObject, XmlElement datatypeElement) { bool hasData = false; if (datatypeObject.Data != null) { hasData = Encode(datatypeObject.Data, datatypeElement); } return(hasData); }
/// <summary> /// Returns the component (or sub-component, as the case may be) at the given index. If it does /// not exist, it is added as an "extra component". /// If comp > 1 is requested from a Varies with GenericPrimitive data, the data is set to /// GenericComposite (this avoids the creation of a chain of ExtraComponents on /// GenericPrimitives). /// Components are numbered from 1. /// </summary> /// /// <exception cref="ApplicationException"> Thrown when an Application error condition occurs. </exception> /// /// <param name="type"> The type. </param> /// <param name="comp"> The component. </param> /// /// <returns> The component. </returns> private static IType getComponent(IType type, int comp) { IType ret = null; if (typeof(Varies).IsAssignableFrom(type.GetType())) { Varies v = (Varies)type; try { if (comp > 1 && typeof(GenericPrimitive).IsAssignableFrom(v.Data.GetType())) { v.Data = new GenericComposite(v.Message); } } catch (DataTypeException de) { System.String message = "Unexpected exception copying data to generic composite: " + de.Message; log.Error(message, de); throw new System.ApplicationException(message); } ret = getComponent(v.Data, comp); } else { if (typeof(IPrimitive).IsAssignableFrom(type.GetType()) && comp == 1) { ret = type; } else if (typeof(GenericComposite).IsAssignableFrom(type.GetType()) || (typeof(IComposite).IsAssignableFrom(type.GetType()) && comp <= numStandardComponents(type))) { //note that GenericComposite can return components > number of standard components try { ret = ((IComposite)type)[comp - 1]; } catch (System.Exception e) { //TODO: This may not be the write exception type: Error() was originally thrown, but was not in project. throw new ApplicationException( "Internal error: HL7Exception thrown on getComponent(x) where x < # standard components.", e); } } else { ret = type.ExtraComponents.getComponent(comp - numStandardComponents(type) - 1); } } return(ret); }
/// <summary> Returns the component (or sub-component, as the case may be) at the given /// index. If it does not exist, it is added as an "extra component". /// If comp > 1 is requested from a Varies with GenericPrimitive data, the /// data is set to GenericComposite (this avoids the creation of a chain of /// ExtraComponents on GenericPrimitives). /// Components are numbered from 1. /// </summary> private static ca.uhn.hl7v2.model.Type getComponent(model.Type type, int comp) { model.Type ret = null; if (typeof(Varies).IsAssignableFrom(type.GetType())) { Varies v = (Varies)type; try { if (comp > 1 && typeof(GenericPrimitive).IsAssignableFrom(v.Data.GetType())) { v.Data = new GenericComposite(v.Message); } } catch (DataTypeException de) { //UPGRADE_TODO: The equivalent in .NET for method 'java.lang.Throwable.getMessage' may return a different value. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1043'" System.String message = "Unexpected exception copying data to generic composite: " + de.Message; log.error(message, de); throw new System.ApplicationException(message); } ret = getComponent(v.Data, comp); } else { if (typeof(Primitive).IsAssignableFrom(type.GetType()) && comp == 1) { ret = type; } else if (typeof(GenericComposite).IsAssignableFrom(type.GetType()) || (typeof(Composite).IsAssignableFrom(type.GetType()) && comp <= numStandardComponents(type))) { //note that GenericComposite can return components > number of standard components try { ret = ((Composite)type).getComponent(comp - 1); } catch (System.Exception e) { //TODO: This may not be the write exception type: Error() was originally thrown, but was not in project. throw new ApplicationException("Internal error: HL7Exception thrown on getComponent(x) where x < # standard components.", e); } } else { ret = type.ExtraComponents.getComponent(comp - numStandardComponents(type) - 1); } } return(ret); }
/** * Returns a single repetition of Primary Key Value - MFE(MFE-4). * @param rep the repetition number (this is a repeating field) * @throws HL7Exception if the repetition number is invalid. */ public Varies getPrimaryKeyValueMFE(int rep) { Varies ret = null; try { Type t = this.getField(4, rep); ret = (Varies)t; } catch (System.Exception ex) { HapiLogFactory.getHapiLog(GetType()).error("Unexpected problem obtaining field value. This is a bug.", ex); throw new System.Exception("An unexpected error ocurred", ex); } return(ret); }
/// <summary> /// Returns a single repetition of Observation Value(OBX-5). throws HL7Exception if the /// repetition number is invalid. /// </summary> /// /// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception> /// /// <param name="rep"> The repetition number (this is a repeating field) </param> /// /// <returns> The observation value. </returns> public Varies GetObservationValue(int rep) { Varies ret = null; try { IType t = this.GetField(5, rep); ret = (Varies)t; } catch (System.Exception ex) { HapiLogFactory.GetHapiLog(GetType()).Error("Unexpected problem obtaining field value. This is a bug.", ex); throw new System.Exception("An unexpected error ocurred", ex); } return(ret); }
/// <summary> Returns a single repetition of Primary Key Value - MFE (MFE-4).</summary> /// <param name="rep">the repetition number (this is a repeating field) /// </param> /// <throws> HL7Exception if the repetition number is invalid. </throws> public virtual Varies getPrimaryKeyValueMFE(int rep) { Varies ret = null; try { Type t = this.getField(4, rep); ret = (Varies)t; } catch (System.InvalidCastException) { throw new Exception(); } return(ret); }
/// <summary> Returns a single repetition of Observation Value (OBX-5).</summary> /// <param name="rep">the repetition number (this is a repeating field) /// </param> /// <throws> HL7Exception if the repetition number is invalid. </throws> public virtual Varies getObservationValue(int rep) { Varies ret = null; try { Type t = this.getField(5, rep); ret = (Varies)t; } catch (System.InvalidCastException) { throw new Exception(); } return(ret); }
/// <summary> Returns the component (or sub-component, as the case may be) at the given /// index. If it does not exist, it is added as an "extra component". /// If comp > 1 is requested from a Varies with GenericPrimitive data, the /// data is set to GenericComposite (this avoids the creation of a chain of /// ExtraComponents on GenericPrimitives). /// Components are numbered from 1. /// </summary> private static Genetibase.NuGenHL7.model.Type getComponent(Genetibase.NuGenHL7.model.Type type, int comp) { Genetibase.NuGenHL7.model.Type ret = null; if (typeof(Varies).IsAssignableFrom(type.GetType())) { Varies v = (Varies)type; try { if (comp > 1 && typeof(GenericPrimitive).IsAssignableFrom(v.Data.GetType())) { v.Data = new GenericComposite(v.Message); } } catch (DataTypeException de) { System.String message = "Unexpected exception copying data to generic composite: " + de.Message; throw new System.ApplicationException(message); } ret = getComponent(v.Data, comp); } else { if (typeof(Primitive).IsAssignableFrom(type.GetType()) && comp == 1) { ret = type; } else if (typeof(GenericComposite).IsAssignableFrom(type.GetType()) || (typeof(Composite).IsAssignableFrom(type.GetType()) && comp <= numStandardComponents(type))) { //note that GenericComposite can return components > number of standard components try { ret = ((Composite)type).getComponent(comp - 1); } catch (System.Exception e) { throw new Exception("Internal error: HL7Exception thrown on getComponent(x) where x < # standard components.", e); } } else { ret = type.ExtraComponents.getComponent(comp - numStandardComponents(type) - 1); } } return(ret); }
/// <summary> /// Parses an XML element into a Varies by determining whether the element is primitive or /// composite, calling setData() on the Varies with a new generic primitive or composite as /// appropriate, and then calling parse again with the new Type object. /// </summary> /// /// <param name="datatypeObject"> The datatype object. </param> /// <param name="datatypeElement"> Element describing the datatype. </param> private void ParseVaries(Varies datatypeObject, System.Xml.XmlElement datatypeElement) { //figure out what data type it holds //short nodeType = datatypeElement.getFirstChild().getNodeType(); if (!this.HasChildElement(datatypeElement)) { //it's a primitive datatypeObject.Data = new GenericPrimitive(datatypeObject.Message); } else { //it's a composite ... almost know what type, except that we don't have the version here datatypeObject.Data = new GenericComposite(datatypeObject.Message); } Parse(datatypeObject.Data, datatypeElement); }
/// <summary> /// Parses an XML element into a Varies by determining whether the element is primitive or /// composite, calling setData() on the Varies with a new generic primitive or composite as appropriate, /// and then calling parse again with the new Type object. /// </summary> private void ParseVaries(Varies datatypeObject, XmlElement datatypeElement) { // figure out what data type it holds if (!HasChildElement(datatypeElement)) { // it's a primitive datatypeObject.Data = new GenericPrimitive(datatypeObject.Message); } else { // it's a composite ... almost know what type, except that we don't have the version here datatypeObject.Data = new GenericComposite(datatypeObject.Message); } Parse(datatypeObject.Data, datatypeElement); }
/// <summary> Returns all repetitions of Observation Value (OBX-5). </summary> /// /// <exception cref="Exception"> Thrown when an exception error condition occurs. </exception> /// /// <returns> An array of varies. </returns> public Varies[] GetObservationValue() { Varies[] ret = null; try { IType[] t = this.GetField(5); ret = new Varies[t.Length]; for (int i = 0; i < ret.Length; i++) { ret[i] = (Varies)t[i]; } } catch (HL7Exception he) { HapiLogFactory.GetHapiLog(this.GetType()).Error("Unexpected problem obtaining field value. This is a bug.", he); throw new System.Exception("An unexpected error ocurred", he); } catch (System.Exception cce) { HapiLogFactory.GetHapiLog(GetType()).Error("Unexpected problem obtaining field value. This is a bug.", cce); throw new System.Exception("An unexpected error ocurred", cce); } return(ret); }
/// <summary> Populates the given Segment object with data from the given XML Element. </summary> /// <summary> for the given Segment, or if there is an error while setting individual field /// values. </summary> /// /// <param name="segmentObject"> The segment object. </param> /// <param name="segmentElement"> Element describing the segment. </param> public virtual void Parse(ISegment segmentObject, System.Xml.XmlElement segmentElement) { SupportClass.HashSetSupport done = new SupportClass.HashSetSupport(); // for (int i = 1; i <= segmentObject.NumFields(); i++) { // String elementName = makeElementName(segmentObject, i); // done.add(elementName); // parseReps(segmentObject, segmentElement, elementName, i); // } System.Xml.XmlNodeList all = segmentElement.ChildNodes; for (int i = 0; i < all.Count; i++) { System.String elementName = all.Item(i).Name; if (System.Convert.ToInt16(all.Item(i).NodeType) == (short)System.Xml.XmlNodeType.Element && !done.Contains(elementName)) { done.Add(elementName); int index = elementName.IndexOf('.'); if (index >= 0 && elementName.Length > index) { //properly formatted element System.String fieldNumString = elementName.Substring(index + 1); int fieldNum = System.Int32.Parse(fieldNumString); this.ParseReps(segmentObject, segmentElement, elementName, fieldNum); } else { log.Debug( "Child of segment " + segmentObject.GetStructureName() + " doesn't look like a field: " + elementName); } } } //set data type of OBX-5 if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(segmentObject, this.Factory); } }
/// <summary> Populates the given Segment object with data from the given XML Element.</summary> /// <throws> HL7Exception if the XML Element does not have the correct name and structure. </throws> /// <summary> for the given Segment, or if there is an error while setting individual field values. /// </summary> public virtual void Parse(ISegment segmentObject, XmlElement segmentElement, ParserOptions parserOptions) { parserOptions = parserOptions ?? DefaultParserOptions; var done = new SupportClass.HashSetSupport(); // for (int i = 1; i <= segmentObject.NumFields(); i++) { // String elementName = makeElementName(segmentObject, i); // done.add(elementName); // parseReps(segmentObject, segmentElement, elementName, i); // } var all = segmentElement.ChildNodes; for (var i = 0; i < all.Count; i++) { var elementName = all.Item(i).Name; if (Convert.ToInt16(all.Item(i).NodeType) == (short)XmlNodeType.Element && !done.Contains(elementName)) { done.Add(elementName); var index = elementName.IndexOf('.'); if (index >= 0 && elementName.Length > index) { // properly formatted element var fieldNumString = elementName.Substring(index + 1); var fieldNum = int.Parse(fieldNumString); ParseReps(segmentObject, segmentElement, elementName, fieldNum); } else { Log.Debug("Child of segment " + segmentObject.GetStructureName() + " doesn't look like a field: " + elementName); } } } // set data type of OBX-5 if (segmentObject.GetType().FullName.IndexOf("OBX") >= 0) { Varies.FixOBX5(segmentObject, Factory, parserOptions); } }
/// <summary> Returns all repetitions of Primary Key Value - MFE (MFE-4).</summary> public virtual Varies[] getPrimaryKeyValueMFE() { Varies[] ret = null; try { Type[] t = this.getField(4); ret = new Varies[t.Length]; for (int i = 0; i < ret.Length; i++) { ret[i] = (Varies)t[i]; } } catch (System.InvalidCastException) { throw new Exception(); } catch (NuGenHL7Exception) { throw new Exception(); } return(ret); }
private static void ProduceOBX(OBX obx, ObservationResult result, int index) { obx.SetIDOBX.Value = index.ToString(); obx.ValueType.Value = result.ValueType.ToString(); // LOINC obx.ObservationIdentifier.Identifier.Value = result.LoincId; obx.ObservationIdentifier.Text.Value = result.LoincName; obx.ObservationIdentifier.NameOfCodingSystem.Value = "LN"; //结果 Varies value = obx.GetObservationValue(0); (value.Data as GenericPrimitive).Value = result.Value; //单位 obx.Units.Identifier.Value = result.Unit; //obx.Units.Text.Value = "Micromole / Liter"; //obx.Units.NameOfCodingSystem.Value = "ISO+"; //参考值 obx.ReferencesRange.Value = result.ReferencesRange; //标记 ID flag = obx.GetAbnormalFlags(0); flag.Value = result.AbnormalFlag.ToString(); obx.NatureOfAbnormalTest.Value = "N"; //结果状态 obx.ObservationResultStatus.Value = result.ObservationResultStatus.ToString(); //结果时间 obx.DateTimeOfTheObservation.TimeOfAnEvent.Value = result.ObservationDateTime.ToString("yyyyMMddHHmmss"); }
/// <summary> /// Parses a segment string and populates the given Segment object. /// <para> /// Unexpected fields are added as Varies' at the end of the segment. /// </para> /// </summary> /// <param name="destination">Segment to parse the segment string into.</param> /// <param name="segment">Encoded segment.</param> /// <param name="encodingChars">Encoding characters to be used.</param> /// <param name="repetition">The repetition number of this segment within its group.</param> /// <param name="parserOptions">Contains configuration that will be applied when parsing.</param> /// <exception cref="HL7Exception"> /// If the given string does not contain the given segment or if the string is not encoded properly. /// </exception> public virtual void Parse(ISegment destination, string segment, EncodingCharacters encodingChars, int repetition, ParserOptions parserOptions) { parserOptions = parserOptions ?? DefaultParserOptions; var fieldOffset = 0; if (IsDelimDefSegment(destination.GetStructureName())) { fieldOffset = 1; // set field 1 to fourth character of string Terser.Set(destination, 1, 0, 1, 1, Convert.ToString(encodingChars.FieldSeparator)); } var fields = Split(segment, Convert.ToString(encodingChars.FieldSeparator)); for (var i = 1; i < fields.Length; i++) { var reps = Split(fields[i], Convert.ToString(encodingChars.RepetitionSeparator)); if (Log.DebugEnabled) { Log.Debug(reps.Length + "reps delimited by: " + encodingChars.RepetitionSeparator); } // MSH-2 will get split incorrectly so we have to fudge it ... var isMSH2 = IsDelimDefSegment(destination.GetStructureName()) && i + fieldOffset == 2; if (isMSH2) { reps = new string[1]; reps[0] = fields[i]; } for (var j = 0; j < reps.Length; j++) { try { var statusMessage = $"Parsing field {i + fieldOffset} repetition {j}"; Log.Debug(statusMessage); var field = destination.GetField(i + fieldOffset, j); if (isMSH2) { Terser.GetPrimitive(field, 1, 1).Value = reps[j]; } else { Parse(field, reps[j], encodingChars); } } catch (HL7Exception e) { // set the field location and throw again ... e.FieldPosition = i; if (repetition > 1) { e.SegmentRepetition = repetition; } e.SegmentName = destination.GetStructureName(); throw; } } } // set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.FixOBX5(destination, Factory, parserOptions); } }
/// <summary> /// Processes the varies. /// "Varies" are the data in the OBX segment, the sending application can set the type hence generically the OBX /// value field is a variant type. /// The "Varies" data parameter contains the data in type IType (hence being passed back to process field). /// </summary> /// <param name="varies">The varies.</param> /// <param name="fieldDescription">The field description.</param> /// <param name="fieldCount">The field count.</param> /// <param name="parentNode">The parent node.</param> private static void ProcessVaries(Varies varies, string fieldDescription, string fieldCount, FieldGroup parentNode) { ProcessField(varies.Data, fieldDescription, fieldCount, parentNode); }
/// <summary> Parses a segment string and populates the given Segment object. Unexpected fields are /// added as Varies' at the end of the segment. /// /// </summary> /// <throws> HL7Exception if the given string does not contain the </throws> /// <summary> given segment or if the string is not encoded properly /// </summary> public virtual void Parse(ISegment destination, System.String segment, EncodingCharacters encodingChars) { int fieldOffset = 0; if (IsDelimDefSegment(destination.GetStructureName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.Set(destination, 1, 0, 1, 1, System.Convert.ToString(encodingChars.FieldSeparator)); } System.String[] fields = Split(segment, System.Convert.ToString(encodingChars.FieldSeparator)); //destination.setName(fields[0]); for (int i = 1; i < fields.Length; i++) { System.String[] reps = Split(fields[i], System.Convert.ToString(encodingChars.RepetitionSeparator)); if (log.DebugEnabled) { log.Debug(reps.Length + "reps delimited by: " + encodingChars.RepetitionSeparator); } //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = IsDelimDefSegment(destination.GetStructureName()) && i + fieldOffset == 2; if (isMSH2) { reps = new System.String[1]; reps[0] = fields[i]; } for (int j = 0; j < reps.Length; j++) { try { System.Text.StringBuilder statusMessage = new System.Text.StringBuilder("Parsing field "); statusMessage.Append(i + fieldOffset); statusMessage.Append(" repetition "); statusMessage.Append(j); log.Debug(statusMessage.ToString()); //parse(destination.GetField(i + fieldOffset, j), reps[j], encodingChars, false); IType field = destination.GetField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { Parse(field, reps[j], encodingChars); } } catch (HL7Exception e) { //set the field location and throw again ... e.FieldPosition = i; e.SegmentRepetition = MessageIterator.getIndex(destination.ParentStructure, destination).rep; e.SegmentName = destination.GetStructureName(); throw e; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }