/// <param name="message">a parsed message to validate (note that MSH-9-1 and MSH-9-2 must be valued) /// </param> /// <returns> true if the message is OK /// </returns> /// <throws> HL7Exception if there is at least one error and this validator is set to fail on errors </throws> public virtual bool validate(IMessage message) { Terser t = new Terser(message); IMessageRule[] rules = myContext.getMessageRules(message.Version, t.Get("MSH-9-1"), t.Get("MSH-9-2")); ValidationException toThrow = null; bool result = true; for (int i = 0; i < rules.Length; i++) { ValidationException[] ex = rules[i].test(message); for (int j = 0; j < ex.Length; j++) { result = false; ourLog.Error("Invalid message", ex[j]); if (failOnError && toThrow == null) { toThrow = ex[j]; } } } if (toThrow != null) { throw new HL7Exception("Invalid message", toThrow); } return result; }
/// <summary> /// Test /// </summary> /// <param name="msg"></param> /// <returns></returns> public override ValidationException[] test(NHapi.Base.Model.IMessage msg) { ValidationException[] result = new ValidationException[0]; foreach (MessageRegExRuleElement rule in regexRules) { bool flag1 = (rule.Version == "*") || (msg.Version == rule.Version); string structureName = msg.GetStructureName(); string[] sNames = structureName.Split('_'); bool flag2 = (rule.MessageType == "*") || (sNames[0] == rule.MessageType); bool flag3 = (rule.TriggerEvent == "*") || (sNames[1] == rule.TriggerEvent); if (flag1 && flag2 && flag3) { Terser terser = new Terser(msg); string value = terser.Get(rule.FieldIndicator); if ((value == null) && !rule.AllowNull) { result = new ValidationException[1] { new ValidationException(string.Format("Value in '{0}' may not be null.", value)) }; } else if ((value != null) && !Regex.IsMatch(value, rule.RegEx)) { result = new ValidationException[1] { new ValidationException(string.Format("Value '{0}' in '{1}' doesn't match regex '{2}'.", value, rule.FieldIndicator, rule.RegEx)) }; } } if (result.Count() > 0) break; } return result; }
/// <summary> /// Test /// </summary> /// <param name="msg"></param> /// <returns></returns> public override ValidationException[] test(NHapi.Base.Model.IMessage msg) { ValidationException[] result = new ValidationException[0]; foreach (MessageFieldMandatoryRuleElement rule in mandatoryRules) { bool flag1 = (rule.Version == "*") || (msg.Version == rule.Version); string structureName = msg.GetStructureName(); string[] sNames = structureName.Split('_'); bool flag2 = (rule.MessageType == "*") || (sNames[0] == rule.MessageType); bool flag3 = (rule.TriggerEvent == "*") || (sNames[1] == rule.TriggerEvent); if (flag1 && flag2 && flag3) { Terser terser = new Terser(msg); string value = terser.Get(rule.FieldIndicator); if (string.IsNullOrEmpty(value)) { result = new ValidationException[1] { new ValidationException(string.Format("Field '{1}' is mandatory.", value, rule.FieldIndicator)) }; } } if (result.Count() > 0) break; } return result; }
public override bool Send(IMessage msg) { EditMessageHeader(msg); IMessage response = mllpClient.SendHL7Message(msg); Terser terser = new Terser(response); string ackCode = terser.Get("/MSA-1"); return (ackCode == "AA"); }
private bool CheckFieldFilter(IMessage message) { bool result = true; if (string.IsNullOrWhiteSpace(FieldFilter) && string.IsNullOrWhiteSpace(FieldFilterValue)) return result; Terser terser = new Terser(message); string msgFieldValue = terser.Get(FieldFilter); result = Compare(FieldFilterValue, msgFieldValue); return result; }
public bool Match(IMessage message) { bool result = false; result = Compare(Hl7Version, message.Version); if (result) { // Dib0 20150125: Changed the compare. This method doesn't work with messages // that are parsed as generic messages //result = Compare(Structurename, message.GetStructureName()); Terser terser = new Terser(message); string messageType = string.Format("{0}_{1}", terser.Get("MSH-9-1"), terser.Get("MSH-9-2")); result = Compare(Structurename, messageType); } // Check the field filter if (result) result = CheckFieldFilter(message); return result; }
public override bool Send(IMessage msg) { bool result = true; EditMessageHeader(msg); try { PipeParser parser = new PipeParser(); string res = SendRequest(parser.Encode(msg)); IMessage response = parser.Parse(res); Terser terser = new Terser(response); string ackCode = terser.Get("/MSA-1"); result = (ackCode == "AA"); } catch (Exception ex) { Logger.ErrorFormat("Error in delivering message '{0}' to Http endpoint with uri '{1}'. Error: {2}", msg.GetStructureName(), serverUri, ex.Message); result = false; } return result; }
/// <summary> /// Create an Ack message based on a received message /// </summary> /// <param name="inboundMessage">received message</param> /// <param name="ackResult">Send AA, AE or AR message.</param> /// <param name="errorMessage">The reason the message was rejected or an error. If "AA" was supplied as ackCode the errorMessage should be null.</param> /// <returns>Created ACK message</returns> public IMessage MakeACK(IMessage inboundMessage, AckTypes ackResult, string errorMessage) { IMessage ackMessage = null; // Get an object from the right ACK class string ackClassType = string.Format("NHapi.Model.V{0}.Message.ACK, NHapi.Model.V{0}", inboundMessage.Version.Replace(".", "")); Type x = Type.GetType(ackClassType); if (x != null) ackMessage = (IMessage)Activator.CreateInstance(x); else { // Fix for V2.2 and V2.1 Since tha ACK message class is missing there in NHapi if (inboundMessage.Version == "2.1") ackMessage = (IMessage)new NHapiTools.Base.CustomImplementation.V21.Messages.ACK(); if (inboundMessage.Version == "2.2") ackMessage = (IMessage)new NHapiTools.Base.CustomImplementation.V22.Messages.ACK(); } Terser inboundTerser = new Terser(inboundMessage); ISegment inboundHeader = null; inboundHeader = inboundTerser.getSegment("MSH"); // Find the HL7 version of the inbound message: string version = null; try { version = Terser.Get(inboundHeader, 12, 0, 1, 1); } catch (NHapi.Base.HL7Exception) { // I'm not happy to proceed if we can't identify the inbound // message version. throw new NHapi.Base.HL7Exception("Failed to get valid HL7 version from inbound MSH-12-1"); } // Create a Terser instance for the outbound message (the ACK). Terser terser = new Terser(ackMessage); // Populate outbound MSH fields using data from inbound message ISegment outHeader = (ISegment)terser.getSegment("MSH"); DeepCopy.copy(inboundHeader, outHeader); // Now set the message type, HL7 version number, acknowledgement code // and message control ID fields: string sendingApp = terser.Get("/MSH-3"); string sendingEnv = terser.Get("/MSH-4"); terser.Set("/MSH-3", appCommunicationName); terser.Set("/MSH-4", environmentIdentifier); terser.Set("/MSH-5", sendingApp); terser.Set("/MSH-6", sendingEnv); terser.Set("/MSH-7", DateTime.Now.ToString("yyyyMMddmmhh")); terser.Set("/MSH-9", "ACK"); terser.Set("/MSH-12", version); terser.Set("/MSA-1", Enum.GetName(typeof(AckTypes), ackResult)); terser.Set("/MSA-2", Terser.Get(inboundHeader, 10, 0, 1, 1)); // Set error message if (errorMessage != null) terser.Set("/ERR-1-1", errorMessage); return ackMessage; }
public static IMessage MakeACK(ISegment inboundHeader, string ackCode) { if (!inboundHeader.GetStructureName().Equals("MSH")) throw new NHapi.Base.HL7Exception( "Need an MSH segment to create a response ACK (got " + inboundHeader.GetStructureName() + ")"); // Find the HL7 version of the inbound message: // string version = null; try { version = Terser.Get(inboundHeader, 12, 0, 1, 1); } catch (NHapi.Base.HL7Exception) { // I'm not happy to proceed if we can't identify the inbound // message version. throw new NHapi.Base.HL7Exception("Failed to get valid HL7 version from inbound MSH-12-1"); } IMessage ackMessage = new ACK(); // Create a Terser instance for the outbound message (the ACK). Terser terser = new Terser(ackMessage); // Populate outbound MSH fields using data from inbound message ISegment outHeader = (ISegment)terser.getSegment("MSH"); DeepCopy.copy(inboundHeader, outHeader); // Now set the message type, HL7 version number, acknowledgement code // and message control ID fields: string sendingApp = terser.Get("/MSH-3"); string sendingEnv = terser.Get("/MSH-4"); terser.Set("/MSH-3", CommunicationName); terser.Set("/MSH-4", EnvironmentIdentifier); terser.Set("/MSH-5", sendingApp); terser.Set("/MSH-6", sendingEnv); terser.Set("/MSH-7", DateTime.Now.ToString("yyyyMMddmmhh")); terser.Set("/MSH-9", "ACK"); terser.Set("/MSH-12", version); terser.Set("/MSA-1", ackCode == null ? "AA" : ackCode); terser.Set("/MSA-2", Terser.Get(inboundHeader, 10, 0, 1, 1)); return ackMessage; }