/// <summary> /// Converts an <see cref="XAD"/> address instance to an <see cref="EntityAddress"/> instance. /// </summary> /// <param name="address">The address to be converted.</param> /// <returns>Returns the converted entity address.</returns> public static EntityAddress ConvertAddress(XAD address) { return(MessageUtil.ConvertAddresses(new XAD[] { address }).FirstOrDefault()); }
/// <summary> /// Converts an <see cref="XPN"/> instance to an <see cref="EntityName"/> instance. /// </summary> /// <param name="name">The name to be converted.</param> /// <returns>Returns the converted name.</returns> public static EntityName ConvertName(XPN name) { return(MessageUtil.ConvertNames(new XPN[] { name }).FirstOrDefault()); }
/// <summary> /// Creates a negative acknowledgement message. /// </summary> /// <param name="request">The request.</param> /// <param name="errors">The errors.</param> /// <param name="errType">Type of the error.</param> /// <returns>Returns the created negative acknowledgement message instance.</returns> public static IMessage CreateNack(IMessage request, List <IResultDetail> errors, Type errType) { var ack = errType.GetConstructor(Type.EmptyTypes).Invoke(null) as IMessage; if (errors.Any()) { tracer.TraceEvent(TraceEventType.Warning, 0, "Validation Errors:"); errors.ForEach(o => tracer.TraceEvent(TraceEventType.Error, 0, $"\t{o.Type} : {o.Message}")); } var terser = new Terser(ack); MessageUtil.UpdateMSH(terser, request); var errLevel = 0; var ec = 0; foreach (var dtl in errors) { try { ISegment errSeg; if (ack.Version == "2.5") { errSeg = terser.getSegment($"/ERR({ec++})"); } else { errSeg = terser.getSegment(string.Format("/ERR", ec++)); } if (errSeg is ERR) { var tErr = UpdateERR(errSeg as ERR, dtl); if (tErr > errLevel) { errLevel = tErr; } } else if (errSeg is NHapi.Model.V25.Segment.ERR) { var tErr = UpdateERR(errSeg as ERR, dtl); if (tErr > errLevel) { errLevel = tErr; } } } catch (Exception e) { Trace.TraceError(e.ToString()); } } terser.Set("/MSA-1", errLevel == 0 ? "AA" : errLevel == 1 ? "AE" : "AR"); return(ack); }
/// <summary> /// Converts an <see cref="CX"/> instance to an <see cref="EntityIdentifier"/> instance. /// </summary> /// <param name="identifier">The identifier to be converted.</param> /// <returns>Returns the converted identifier.</returns> public static EntityIdentifier ConvertIdentifier(CX identifier) { return(MessageUtil.ConvertIdentifiers(new CX[] { identifier }).FirstOrDefault()); }
/// <summary> /// Handles unsupported messages. /// </summary> /// <param name="e">The received message event arguments.</param> /// <returns>Returns a message.</returns> public IMessage HandleMessage(Hl7MessageReceivedEventArgs e) { return(MessageUtil.CreateNack(e.Message, "AR", "200", "Unsupported message type")); }
/// <summary> /// Handle a received message on the LLP interface. /// </summary> /// <param name="e">The HL7 message received event arguments.</param> public IMessage HandleMessage(Hl7MessageReceivedEventArgs e) { IMessage response = null; try { if (e.Message.Version == "2.3.1" || e.Message.Version == "2.5") { var identityProvider = ApplicationContext.Current.GetService <IDeviceIdentityProviderService>(); var msh = e.Message.GetStructure("MSH") as MSH; // get the device identity by device name, as the device will have to be registered in OpenIZ var deviceIdentity = identityProvider.GetIdentity(msh.SendingApplication.NamespaceID.Value); AuthenticationContext.Current = new AuthenticationContext(new GenericPrincipal(deviceIdentity, new string[] {})); // Get the MSH segment var terser = new Terser(e.Message); var trigger = terser.Get("/MSH-9-2"); this.traceSource.TraceEvent(TraceEventType.Information, 0, "Message is of type {0} {1}", e.Message.GetType().FullName, trigger); switch (trigger) { case "Q23": if (e.Message is NHapi.Model.V25.Message.QBP_Q21) { response = HandlePixQuery(e.Message as NHapi.Model.V25.Message.QBP_Q21, e); } else { response = MessageUtil.CreateNack(e.Message, "AR", "200", ApplicationContext.Current.GetLocaleString("MSGE074")); } break; case "A01": case "A04": case "A05": if (e.Message is NHapi.Model.V231.Message.ADT_A01) { response = HandleAdmit((NHapi.Model.V231.Message.ADT_A01)e.Message, e); } else { response = MessageUtil.CreateNack(e.Message, "AR", "200", ApplicationContext.Current.GetLocaleString("MSGE074")); } break; case "A08": if (e.Message is NHapi.Model.V231.Message.ADT_A01) { response = HandlePixUpdate(e.Message as NHapi.Model.V231.Message.ADT_A01, e); } else if (e.Message is NHapi.Model.V231.Message.ADT_A08) { response = HandlePixUpdate(e.Message as NHapi.Model.V231.Message.ADT_A08, e); } else { response = MessageUtil.CreateNack(e.Message, "AR", "200", ApplicationContext.Current.GetLocaleString("MSGE074")); } break; case "A40": if (e.Message is NHapi.Model.V25.Message.ADT_A39) { response = HandleMerge(e.Message as NHapi.Model.V231.Message.ADT_A39, e); } else if (e.Message is NHapi.Model.V231.Message.ADT_A40) { response = HandleMerge(e.Message as NHapi.Model.V231.Message.ADT_A40, e); } else { response = MessageUtil.CreateNack(e.Message, "AR", "200", ApplicationContext.Current.GetLocaleString("MSGE074")); } break; default: response = MessageUtil.CreateNack(e.Message, "AR", "201", ApplicationContext.Current.GetLocaleString("HL7201")); this.traceSource.TraceEvent(TraceEventType.Error, 0, "{0} is not a supported trigger", trigger); break; } } // response still null? if (response == null) { response = MessageUtil.CreateNack(e.Message, "AR", "203", ApplicationContext.Current.GetLocaleString("HL7203")); } } catch (Exception ex) { this.traceSource.TraceEvent(TraceEventType.Error, ex.HResult, "Error processing message {0} : {1}", e.Message?.Version, ex); response = MessageUtil.CreateNack(e.Message, "AR", "207", ex.Message); } return(response); }
/// <summary> /// Handles a PIX merge request. /// </summary> /// <param name="request">The request.</param> /// <param name="evt">The <see cref="Hl7MessageReceivedEventArgs" /> instance containing the event data.</param> /// <returns>IMessage.</returns> /// <exception cref="System.InvalidOperationException"></exception> /// <exception cref="System.Collections.Generic.KeyNotFoundException"></exception> internal IMessage HandleMerge(NHapi.Model.V231.Message.ADT_A39 request, Hl7MessageReceivedEventArgs evt) { // Get config var dataService = ApplicationContext.Current.GetService <IPatientRepositoryService>(); // Create a details array List <IResultDetail> details = new List <IResultDetail>(); // Validate the inbound message MessageUtil.Validate((IMessage)request, details); IMessage response = null; // Control if (request == null) { return(null); } try { // Create Data for (var i = 0; i < request.PATIENTRepetitionsUsed; i++) { var patientGroup = request.GetPATIENT(i); var survivor = MessageUtil.CreatePatient(request.MSH, request.EVN, patientGroup.PID, patientGroup.PD1, details); if (survivor == null) { throw new InvalidOperationException(ApplicationContext.Current.GetLocaleString("MSGE00A")); } var victim = dataService.Find(o => o.Identifiers.Any(id => id.Authority.DomainName == patientGroup.MRG.GetPriorAlternatePatientID(0).AssigningAuthority.NamespaceID.Value&& id.Value == patientGroup.MRG.GetPriorAlternatePatientID(0).ID.Value)).FirstOrDefault(); if (victim == null) { throw new KeyNotFoundException(); } var result = dataService.Merge(survivor, victim); } response = MessageUtil.CreateNack(request, details, typeof(ACK)); MessageUtil.UpdateMSH(new NHapi.Base.Util.Terser(response), request); (response as NHapi.Model.V25.Message.ACK).MSH.MessageType.TriggerEvent.Value = request.MSH.MessageType.TriggerEvent.Value; (response as NHapi.Model.V25.Message.ACK).MSH.MessageType.MessageStructure.Value = "ACK"; } catch (Exception e) { this.traceSource.TraceEvent(TraceEventType.Error, 0, e.ToString()); if (!details.Exists(o => o.Message == e.Message || o.Exception == e)) { details.Add(new ResultDetail(ResultDetailType.Error, e.Message, e)); } response = MessageUtil.CreateNack(request, details, typeof(NHapi.Model.V25.Message.ACK)); } return(response); }
/// <summary> /// Handle a PIX query. /// </summary> /// <param name="request">The request.</param> /// <param name="eventArgs">The <see cref="Hl7MessageReceivedEventArgs" /> instance containing the event data.</param> /// <returns>Returns the message result from the query.</returns> /// <exception cref="System.InvalidOperationException"></exception> internal IMessage HandlePixQuery(QBP_Q21 request, Hl7MessageReceivedEventArgs eventArgs) { var patientRepositoryService = ApplicationContext.Current.GetService <IPatientRepositoryService>(); var details = new List <IResultDetail>(); MessageUtil.Validate(request, details); IMessage response = null; // Control if (request == null) { return(null); } try { // Create Query Data var query = MessageUtil.CreateIDQuery(request.QPD); if (query == null) { throw new InvalidOperationException(ApplicationContext.Current.GetLocaleString("MSGE00A")); } var count = int.Parse(request?.RCP?.QuantityLimitedRequest?.Quantity?.Value ?? "0"); var offset = 0; var totalCount = 0; var result = patientRepositoryService.Find(query, offset, count, out totalCount); // Now process the result response = MessageUtil.CreateRSPK23(result, details); try { (response as RSP_K23).QPD.MessageQueryName.Identifier.Value = request.QPD.MessageQueryName.Identifier.Value; Terser reqTerser = new Terser(request), rspTerser = new Terser(response); rspTerser.Set("/QPD-1", reqTerser.Get("/QPD-1")); rspTerser.Set("/QPD-2", reqTerser.Get("/QPD-2")); rspTerser.Set("/QPD-3-1", reqTerser.Get("/QPD-3-1")); rspTerser.Set("/QPD-3-4-1", reqTerser.Get("/QPD-3-4-1")); rspTerser.Set("/QPD-3-4-2", reqTerser.Get("/QPD-3-4-2")); rspTerser.Set("/QPD-3-4-3", reqTerser.Get("/QPD-3-4-3")); rspTerser.Set("/QPD-4-1", reqTerser.Get("/QPD-4-1")); rspTerser.Set("/QPD-4-4-1", reqTerser.Get("/QPD-4-4-1")); rspTerser.Set("/QPD-4-4-2", reqTerser.Get("/QPD-4-4-2")); rspTerser.Set("/QPD-4-4-3", reqTerser.Get("/QPD-4-4-3")); } catch (Exception e) { this.traceSource.TraceEvent(TraceEventType.Error, 0, e.ToString()); } MessageUtil.UpdateMSH(new Terser(response), request); } catch (Exception e) { this.traceSource.TraceEvent(TraceEventType.Error, 0, e.ToString()); response = MessageUtil.CreateNack(request, details, typeof(RSP_K23)); var errTerser = new Terser(response); // HACK: Fix the generic ACK with a real ACK for this message errTerser.Set("/MSH-9-2", "K23"); errTerser.Set("/MSH-9-3", "RSP_K23"); errTerser.Set("/QAK-2", "AE"); errTerser.Set("/MSA-1", "AE"); errTerser.Set("/QAK-1", request.QPD.QueryTag.Value); } return(response); }