public static System.String encode(Segment source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); result.Append(source.getName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (isDelimDefSegment(source.getName())) { startAt = 2; } //loop through fields; for every field delimit any repetitions and add field delimiter after ... int numFields = source.numFields(); for (int i = startAt; i <= numFields; i++) { try { Type[] reps = source.getField(i); for (int j = 0; j < reps.Length; j++) { System.String fieldText = encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (isDelimDefSegment(source.getName()) && i == 2) { fieldText = NuGenEscape.unescape(fieldText, encodingChars); } result.Append(fieldText); if (j < reps.Length - 1) { result.Append(encodingChars.RepetitionSeparator); } } } catch (NuGenHL7Exception) { } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return(stripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator)); }
/// <summary> Populates certain required fields in a response message header, using /// information from the corresponding inbound message. The current time is /// used for the message time field, and <code>MessageIDGenerator</code> is /// used to create a unique message ID. Version and message type fields are /// not populated. /// </summary> public static void fillResponseHeader(Segment inbound, Segment outbound) { if (!inbound.getName().Equals("MSH") || !outbound.getName().Equals("MSH")) { throw new NuGenHL7Exception("Need MSH segments. Got " + inbound.getName() + " and " + outbound.getName()); } //get MSH data from incoming message ... System.String encChars = Terser.get_Renamed(inbound, 2, 0, 1, 1); System.String fieldSep = Terser.get_Renamed(inbound, 1, 0, 1, 1); System.String procID = Terser.get_Renamed(inbound, 11, 0, 1, 1); //populate outbound MSH using data from inbound message ... Terser.set_Renamed(outbound, 2, 0, 1, 1, encChars); Terser.set_Renamed(outbound, 1, 0, 1, 1, fieldSep); System.Globalization.GregorianCalendar now = new System.Globalization.GregorianCalendar(); SupportClass.CalendarManager.manager.SetDateTime(now, System.DateTime.Now); Terser.set_Renamed(outbound, 7, 0, 1, 1, CommonTS.toHl7TSFormat(now)); Terser.set_Renamed(outbound, 10, 0, 1, 1, MessageIDGenerator.Instance.NewID); Terser.set_Renamed(outbound, 11, 0, 1, 1, procID); }
/// <summary> Creates an ACK message with the minimum required information from an inbound message. /// Optional fields can be filled in afterwards, before the message is returned. Pleaase /// note that MSH-10, the outbound message control ID, is also set using the class /// <code>Genetibase.NuGenHL7.util.MessageIDGenerator</code>. Also note that the ACK messages returned /// is the same version as the version stated in the inbound MSH if there is a generic ACK for that /// version, otherwise a version 2.4 ACK is returned. MSA-1 is set to AA by default. /// /// </summary> /// <param name="inboundHeader">the MSH segment if the inbound message /// </param> /// <throws> IOException if there is a problem reading or writing the message ID file </throws> /// <throws> DataTypeException if there is a problem setting ACK values </throws> public static Message makeACK(Segment inboundHeader) { if (!inboundHeader.getName().Equals("MSH")) { throw new NuGenHL7Exception("Need an MSH segment to create a response ACK (got " + inboundHeader.getName() + ")"); } //make ACK of correct version System.String version = null; try { version = Terser.get_Renamed(inboundHeader, 12, 0, 1, 1); } catch (NuGenHL7Exception) { /* proceed with null */ } if (version == null) { version = "2.4"; } System.String ackClassName = SourceGenerator.getVersionPackageName(version) + "message.ACK"; Message out_Renamed = null; try { System.Type ackClass = System.Type.GetType(ackClassName); out_Renamed = (Message)System.Activator.CreateInstance(ackClass); } catch (System.Exception e) { throw new NuGenHL7Exception("Can't instantiate ACK of class " + ackClassName + ": " + e.GetType().FullName); } Terser terser = new Terser(out_Renamed); //populate outbound MSH using data from inbound message ... Segment outHeader = (Segment)out_Renamed.get_Renamed("MSH"); fillResponseHeader(inboundHeader, outHeader); terser.set_Renamed("/MSH-9", "ACK"); terser.set_Renamed("/MSH-12", version); terser.set_Renamed("/MSA-1", "AA"); terser.set_Renamed("/MSA-2", Genetibase.NuGenHL7.util.NuGenTerser.get_Renamed(inboundHeader, 10, 0, 1, 1)); return(out_Renamed); }
/// <summary> Populates the given error segment with information from this Exception.</summary> public virtual void populate(Segment errorSegment) { //make sure it's an ERR if (!errorSegment.getName().Equals("ERR")) { throw new NuGenHL7Exception("Can only populate an ERR segment with an exception -- got: " + errorSegment.GetType().FullName); } int rep = errorSegment.getField(1).Length; //append after existing reps if (this.SegmentName != null) { Terser.set_Renamed(errorSegment, 1, rep, 1, 1, this.SegmentName); } if (this.SegmentRepetition >= 0) { Terser.set_Renamed(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition)); } if (this.FieldPosition >= 0) { Terser.set_Renamed(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition)); } Terser.set_Renamed(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 3, "hl70357"); Terser.set_Renamed(errorSegment, 1, rep, 4, 5, this.Message); //try to get error condition text try { System.String desc = NuGenTableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 2, desc); } catch (NuGenLookupException) { } }
public static System.String encode(Segment source, NuGenEncodingCharacters encodingChars) { System.Text.StringBuilder result = new System.Text.StringBuilder(); result.Append(source.getName()); result.Append(encodingChars.FieldSeparator); //start at field 2 for MSH segment because field 1 is the field delimiter int startAt = 1; if (isDelimDefSegment(source.getName())) startAt = 2; //loop through fields; for every field delimit any repetitions and add field delimiter after ... int numFields = source.numFields(); for (int i = startAt; i <= numFields; i++) { try { Type[] reps = source.getField(i); for (int j = 0; j < reps.Length; j++) { System.String fieldText = encode(reps[j], encodingChars); //if this is MSH-2, then it shouldn't be escaped, so unescape it again if (isDelimDefSegment(source.getName()) && i == 2) fieldText = NuGenEscape.unescape(fieldText, encodingChars); result.Append(fieldText); if (j < reps.Length - 1) result.Append(encodingChars.RepetitionSeparator); } } catch (NuGenHL7Exception) { } result.Append(encodingChars.FieldSeparator); } //strip trailing delimiters ... return stripExtraDelimiters(result.ToString(), encodingChars.FieldSeparator); }
/// <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(Segment destination, System.String segment, NuGenEncodingCharacters encodingChars) { int fieldOffset = 0; if (isDelimDefSegment(destination.getName())) { fieldOffset = 1; //set field 1 to fourth character of string Terser.set_Renamed(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)); //MSH-2 will get split incorrectly so we have to fudge it ... bool isMSH2 = isDelimDefSegment(destination.getName()) && 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); //parse(destination.getField(i + fieldOffset, j), reps[j], encodingChars, false); Type field = destination.getField(i + fieldOffset, j); if (isMSH2) { Terser.getPrimitive(field, 1, 1).Value = reps[j]; } else { parse(field, reps[j], encodingChars); } } catch (NuGenHL7Exception e) { //set the field location and throw again ... e.FieldPosition = i; e.SegmentRepetition = MessageIterator.getIndex(destination.Parent, destination).rep; e.SegmentName = destination.getName(); throw e; } } } //set data type of OBX-5 if (destination.GetType().FullName.IndexOf("OBX") >= 0) { Varies.fixOBX5(destination, Factory); } }
/// <summary> Populates the given error segment with information from this Exception.</summary> public virtual void populate(Segment errorSegment) { //make sure it's an ERR if (!errorSegment.getName().Equals("ERR")) throw new NuGenHL7Exception("Can only populate an ERR segment with an exception -- got: " + errorSegment.GetType().FullName); int rep = errorSegment.getField(1).Length; //append after existing reps if (this.SegmentName != null) Terser.set_Renamed(errorSegment, 1, rep, 1, 1, this.SegmentName); if (this.SegmentRepetition >= 0) Terser.set_Renamed(errorSegment, 1, rep, 2, 1, System.Convert.ToString(this.SegmentRepetition)); if (this.FieldPosition >= 0) Terser.set_Renamed(errorSegment, 1, rep, 3, 1, System.Convert.ToString(this.FieldPosition)); Terser.set_Renamed(errorSegment, 1, rep, 4, 1, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 3, "hl70357"); Terser.set_Renamed(errorSegment, 1, rep, 4, 5, this.Message); //try to get error condition text try { System.String desc = NuGenTableRepository.Instance.getDescription(357, System.Convert.ToString(this.errCode)); Terser.set_Renamed(errorSegment, 1, rep, 4, 2, desc); } catch (NuGenLookupException) { } }
/// <summary> Populates certain required fields in a response message header, using /// information from the corresponding inbound message. The current time is /// used for the message time field, and <code>MessageIDGenerator</code> is /// used to create a unique message ID. Version and message type fields are /// not populated. /// </summary> public static void fillResponseHeader(Segment inbound, Segment outbound) { if (!inbound.getName().Equals("MSH") || !outbound.getName().Equals("MSH")) throw new NuGenHL7Exception("Need MSH segments. Got " + inbound.getName() + " and " + outbound.getName()); //get MSH data from incoming message ... System.String encChars = Terser.get_Renamed(inbound, 2, 0, 1, 1); System.String fieldSep = Terser.get_Renamed(inbound, 1, 0, 1, 1); System.String procID = Terser.get_Renamed(inbound, 11, 0, 1, 1); //populate outbound MSH using data from inbound message ... Terser.set_Renamed(outbound, 2, 0, 1, 1, encChars); Terser.set_Renamed(outbound, 1, 0, 1, 1, fieldSep); System.Globalization.GregorianCalendar now = new System.Globalization.GregorianCalendar(); SupportClass.CalendarManager.manager.SetDateTime(now, System.DateTime.Now); Terser.set_Renamed(outbound, 7, 0, 1, 1, CommonTS.toHl7TSFormat(now)); Terser.set_Renamed(outbound, 10, 0, 1, 1, MessageIDGenerator.Instance.NewID); Terser.set_Renamed(outbound, 11, 0, 1, 1, procID); }
/// <summary> Creates an ACK message with the minimum required information from an inbound message. /// Optional fields can be filled in afterwards, before the message is returned. Pleaase /// note that MSH-10, the outbound message control ID, is also set using the class /// <code>Genetibase.NuGenHL7.util.MessageIDGenerator</code>. Also note that the ACK messages returned /// is the same version as the version stated in the inbound MSH if there is a generic ACK for that /// version, otherwise a version 2.4 ACK is returned. MSA-1 is set to AA by default. /// /// </summary> /// <param name="inboundHeader">the MSH segment if the inbound message /// </param> /// <throws> IOException if there is a problem reading or writing the message ID file </throws> /// <throws> DataTypeException if there is a problem setting ACK values </throws> public static Message makeACK(Segment inboundHeader) { if (!inboundHeader.getName().Equals("MSH")) throw new NuGenHL7Exception("Need an MSH segment to create a response ACK (got " + inboundHeader.getName() + ")"); //make ACK of correct version System.String version = null; try { version = Terser.get_Renamed(inboundHeader, 12, 0, 1, 1); } catch (NuGenHL7Exception) { /* proceed with null */ } if (version == null) version = "2.4"; System.String ackClassName = SourceGenerator.getVersionPackageName(version) + "message.ACK"; Message out_Renamed = null; try { System.Type ackClass = System.Type.GetType(ackClassName); out_Renamed = (Message) System.Activator.CreateInstance(ackClass); } catch (System.Exception e) { throw new NuGenHL7Exception("Can't instantiate ACK of class " + ackClassName + ": " + e.GetType().FullName); } Terser terser = new Terser(out_Renamed); //populate outbound MSH using data from inbound message ... Segment outHeader = (Segment) out_Renamed.get_Renamed("MSH"); fillResponseHeader(inboundHeader, outHeader); terser.set_Renamed("/MSH-9", "ACK"); terser.set_Renamed("/MSH-12", version); terser.set_Renamed("/MSA-1", "AA"); terser.set_Renamed("/MSA-2", Genetibase.NuGenHL7.util.NuGenTerser.get_Renamed(inboundHeader, 10, 0, 1, 1)); return out_Renamed; }
/// <summary>Returns the expected XML element name for the given child of the given Segment </summary> private System.String makeElementName(Segment s, int child) { return(s.getName() + "." + child); }
/// <summary>Returns the expected XML element name for the given child of the given Segment </summary> private System.String makeElementName(Segment s, int child) { return s.getName() + "." + child; }