/// <summary> Fills in the details of an Application Reject message, including response and /// error codes, and a text error message. This is the method to override if you want /// to respond differently. /// </summary> public virtual void fillDetails(Message ack) { try { //populate MSA and ERR with generic error ... Segment msa = (Segment) ack.get_Renamed("MSA"); Terser.set_Renamed(msa, 1, 0, 1, 1, "AR"); Terser.set_Renamed(msa, 3, 0, 1, 1, "No appropriate destination could be found to which this message could be routed."); //this is max length //populate ERR segment if it exists (may not depending on version) Structure s = ack.get_Renamed("ERR"); if (s != null) { Segment err = (Segment) s; Terser.set_Renamed(err, 1, 0, 4, 1, "207"); Terser.set_Renamed(err, 1, 0, 4, 2, "Application Internal Error"); Terser.set_Renamed(err, 1, 0, 4, 3, "HL70357"); } } catch (System.Exception e) { throw new NuGenApplicationException("Error trying to create Application Reject message: " + e.Message); } }
/// <summary> Fills in the details of an Application Reject message, including response and /// error codes, and a text error message. This is the method to override if you want /// to respond differently. /// </summary> public virtual void fillDetails(Message ack) { try { //populate MSA and ERR with generic error ... Segment msa = (Segment)ack.get_Renamed("MSA"); Terser.set_Renamed(msa, 1, 0, 1, 1, "AR"); Terser.set_Renamed(msa, 3, 0, 1, 1, "No appropriate destination could be found to which this message could be routed."); //this is max length //populate ERR segment if it exists (may not depending on version) Structure s = ack.get_Renamed("ERR"); if (s != null) { Segment err = (Segment)s; Terser.set_Renamed(err, 1, 0, 4, 1, "207"); Terser.set_Renamed(err, 1, 0, 4, 2, "Application Internal Error"); Terser.set_Renamed(err, 1, 0, 4, 3, "HL70357"); } } catch (System.Exception e) { throw new NuGenApplicationException("Error trying to create Application Reject message: " + e.Message); } }
/// <summary> Formats a Message object into an HL7 message string using this parser's /// default encoding ("VB"). /// </summary> /// <throws> HL7Exception if the data fields in the message do not permit encoding </throws> /// <summary> (e.g. required fields are null) /// </summary> protected internal override System.String doEncode(Message source) { //get encoding characters ... Segment msh = (Segment)source.get_Renamed("MSH"); System.String fieldSepString = Terser.get_Renamed(msh, 1, 0, 1, 1); if (fieldSepString == null) { throw new NuGenHL7Exception("Can't encode message: MSH-1 (field separator) is missing"); } char fieldSep = '|'; if (fieldSepString != null && fieldSepString.Length > 0) { fieldSep = fieldSepString[0]; } System.String encCharString = Terser.get_Renamed(msh, 2, 0, 1, 1); if (encCharString == null) { throw new NuGenHL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); } if (encCharString.Length != 4) { throw new NuGenHL7Exception("Encoding characters '" + encCharString + "' invalid -- must be 4 characters", NuGenHL7Exception.DATA_TYPE_ERROR); } NuGenEncodingCharacters en = new NuGenEncodingCharacters(fieldSep, encCharString); //pass down to group encoding method which will operate recursively on children ... return(encode((Group)source, en)); }
/// <summary> Logs the given exception and creates an error message to send to the /// remote system. /// /// </summary> /// <param name="encoding">The encoding for the error message. If <code>null</code>, uses default encoding /// </param> public static System.String logAndMakeErrorMessage(System.Exception e, Segment inHeader, Parser p, System.String encoding) { // create error message ... System.String errorMessage = null; try { Message out_Renamed = NuGenDefaultApplication.makeACK(inHeader); Terser t = new Terser(out_Renamed); //copy required data from incoming message ... try { t.set_Renamed("/MSH-10", MessageIDGenerator.Instance.NewID); } catch (System.IO.IOException ioe) { throw new NuGenHL7Exception("Problem creating error message ID: " + ioe.Message); } //populate MSA ... t.set_Renamed("/MSA-1", "AE"); //should this come from HL7Exception constructor? t.set_Renamed("/MSA-2", Terser.get_Renamed(inHeader, 10, 0, 1, 1)); System.String excepMessage = e.Message; if (excepMessage != null) { t.set_Renamed("/MSA-3", excepMessage.Substring(0, (System.Math.Min(80, excepMessage.Length)) - (0))); } /* Some earlier ACKs don't have ERRs, but I think we'll change this within HAPI * so that there is a single ACK for each version (with an ERR). */ //see if it's an HL7Exception (so we can get specific information) ... if (e.GetType().Equals(typeof(NuGenHL7Exception))) { Segment err = (Segment)out_Renamed.get_Renamed("ERR"); ((NuGenHL7Exception)e).populate(err); } else { t.set_Renamed("/ERR-1-4-1", "207"); t.set_Renamed("/ERR-1-4-2", "Application Internal Error"); t.set_Renamed("/ERR-1-4-3", "HL70357"); } if (encoding != null) { errorMessage = p.encode(out_Renamed, encoding); } else { errorMessage = p.encode(out_Renamed); } } catch (System.IO.IOException ioe) { throw new NuGenHL7Exception("IOException creating error response message: " + ioe.Message, NuGenHL7Exception.APPLICATION_INTERNAL_ERROR); } return(errorMessage); }
/// <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> Creates and returns an acknowledgement -- the details are determined by fillDetails().</summary> public virtual Message processMessage(Message in_Renamed) { Message out_Renamed = null; try { //get default ACK out_Renamed = makeACK((Segment) in_Renamed.get_Renamed("MSH")); fillDetails(out_Renamed); } catch (System.Exception e) { throw new NuGenApplicationException("Couldn't create response message: " + e.Message); } return out_Renamed; }
/// <summary> Creates and returns an acknowledgement -- the details are determined by fillDetails().</summary> public virtual Message processMessage(Message in_Renamed) { Message out_Renamed = null; try { //get default ACK out_Renamed = makeACK((Segment)in_Renamed.get_Renamed("MSH")); fillDetails(out_Renamed); } catch (System.Exception e) { throw new NuGenApplicationException("Couldn't create response message: " + e.Message); } return(out_Renamed); }
/// <summary> Processes an incoming message string and returns the response message string. /// Message processing consists of parsing the message, finding an appropriate /// Application and processing the message with it, and encoding the response. /// Applications are chosen from among those registered using /// <code>registerApplication</code>. The Parser is obtained from the Connection /// associated with this Responder. /// </summary> protected internal virtual System.String processMessage(System.String incomingMessageString) { Message incomingMessageObject = null; System.String outgoingMessageString = null; try { incomingMessageObject = parser.parse(incomingMessageString); } catch (NuGenHL7Exception e) { outgoingMessageString = logAndMakeErrorMessage(e, parser.getCriticalResponseData(incomingMessageString), parser, parser.getEncoding(incomingMessageString)); } if (outgoingMessageString == null) { try { //optionally check integrity of parse try { if (checkWriter != null) { checkParse(incomingMessageString, incomingMessageObject, parser); } } catch (System.IO.IOException) { } //message validation (in terms of optionality, cardinality) would go here *** NuGenApplication app = findApplication(incomingMessageObject); Message response = app.processMessage(incomingMessageObject); //Here we explicitly use the same encoding as that of the inbound message - this is important with GenericParser, which might use a different encoding by default outgoingMessageString = parser.encode(response, parser.getEncoding(incomingMessageString)); } catch (System.Exception e) { outgoingMessageString = logAndMakeErrorMessage(e, (Segment)incomingMessageObject.get_Renamed("MSH"), parser, parser.getEncoding(incomingMessageString)); } } return(outgoingMessageString); }
/// <seealso cref="Genetibase.NuGenHL7.parser.Parser.doParse(java.lang.String, java.lang.String)"> /// </seealso> protected internal override Message doParse(System.String message, System.String version) { Message result = null; char fieldSep = message[3]; NuGenEncodingCharacters ec = new NuGenEncodingCharacters(fieldSep, message.Substring(4, (8) - (4))); SupportClass.Tokenizer tok = new SupportClass.Tokenizer(message.Substring(4), System.Convert.ToString(new char[] { fieldSep, ourSegmentSeparator }), true); System.String[] mshFields = getMSHFields(tok, fieldSep); System.Object[] structure = getStructure(mshFields[8], ec.ComponentSeparator); StructRef root = (StructRef)myEventGuideMap[structure[0]]; if (root == null) { result = myPipeParser.parse(message); } else { int csIndex = mshFields[11].IndexOf((System.Char)ec.ComponentSeparator); result = instantiateMessage((System.String)structure[1], version, ((System.Boolean)structure[2])); StructRef mshRef = null; lock (root) { mshRef = root.getSuccessor("MSH"); root.reset(); } Segment msh = (Segment)result.get_Renamed("MSH"); for (int i = 0; i < mshRef.Fields.Length; i++) { int fieldNum = mshRef.Fields[i]; parse(mshFields[fieldNum - 1], msh, fieldNum, ec); } parse(tok, result, root, ec); } return(result); }
/// <summary> Formats a Message object into an HL7 message string using this parser's /// default encoding ("VB"). /// </summary> /// <throws> HL7Exception if the data fields in the message do not permit encoding </throws> /// <summary> (e.g. required fields are null) /// </summary> protected internal override System.String doEncode(Message source) { //get encoding characters ... Segment msh = (Segment) source.get_Renamed("MSH"); System.String fieldSepString = Terser.get_Renamed(msh, 1, 0, 1, 1); if (fieldSepString == null) throw new NuGenHL7Exception("Can't encode message: MSH-1 (field separator) is missing"); char fieldSep = '|'; if (fieldSepString != null && fieldSepString.Length > 0) fieldSep = fieldSepString[0]; System.String encCharString = Terser.get_Renamed(msh, 2, 0, 1, 1); if (encCharString == null) throw new NuGenHL7Exception("Can't encode message: MSH-2 (encoding characters) is missing"); if (encCharString.Length != 4) throw new NuGenHL7Exception("Encoding characters '" + encCharString + "' invalid -- must be 4 characters", NuGenHL7Exception.DATA_TYPE_ERROR); NuGenEncodingCharacters en = new NuGenEncodingCharacters(fieldSep, encCharString); //pass down to group encoding method which will operate recursively on children ... return encode((Group) source, en); }
/// <summary> Processes an incoming message string and returns the response message string. /// Message processing consists of parsing the message, finding an appropriate /// Application and processing the message with it, and encoding the response. /// Applications are chosen from among those registered using /// <code>bindApplication</code>. /// /// </summary> /// <returns> {text, charset} /// </returns> private System.String[] processMessage(System.String incomingMessageString, System.Collections.IDictionary theMetadata) { ; Message incomingMessageObject = null; System.String outgoingMessageString = null; System.String outgoingMessageCharset = null; try { incomingMessageObject = myParser.parse(incomingMessageString); } catch (NuGenHL7Exception e) { outgoingMessageString = Responder.logAndMakeErrorMessage(e, myParser.getCriticalResponseData(incomingMessageString), myParser, myParser.getEncoding(incomingMessageString)); } if (outgoingMessageString == null) { try { //message validation (in terms of optionality, cardinality) would go here *** NuGenReceivingApplication app = findApplication(incomingMessageObject); theMetadata[RAW_MESSAGE_KEY] = incomingMessageString; Message response = app.processMessage(incomingMessageObject, theMetadata); //Here we explicitly use the same encoding as that of the inbound message - this is important with GenericParser, which might use a different encoding by default outgoingMessageString = myParser.encode(response, myParser.getEncoding(incomingMessageString)); Terser t = new Terser(response); outgoingMessageCharset = t.get_Renamed("MSH-18"); } catch (System.Exception e) { outgoingMessageString = Responder.logAndMakeErrorMessage(e, (Segment)incomingMessageObject.get_Renamed("MSH"), myParser, myParser.getEncoding(incomingMessageString)); } } return(new System.String[] { outgoingMessageString, outgoingMessageCharset }); }