/// <summary>
        /// Create the response message
        /// </summary>
        public MARC.Everest.Interfaces.IInteraction Create(MARC.Everest.Interfaces.IInteraction request, RegistryQueryResult results, List <IResultDetail> dtls)
        {
            // GEt the config services
            ISystemConfigurationService configService = Context.GetService(typeof(ISystemConfigurationService)) as ISystemConfigurationService;
            ILocalizationService        localeService = Context.GetService(typeof(ILocalizationService)) as ILocalizationService;

            var retHl7v3 = new List <MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201304UV02.Patient, object> >(results.Results.Count);

            UvDeComponentUtil dCompUtil = new UvDeComponentUtil();

            dCompUtil.Context = this.Context;

            PRPA_IN201309UV02 rqst = request as PRPA_IN201309UV02;

            // Convert results to HL7v3
            foreach (RegistrationEvent res in results.Results)
            {
                var retRec = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201304UV02.Patient, object>(
                    false,
                    dCompUtil.CreateRegistrationEventDetail(res, dtls)
                    );
                if (retRec.RegistrationEvent == null)
                {
                    retRec = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201304UV02.Patient, object>(
                        false,
                        new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.RegistrationEvent <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201304UV02.Patient, object>()
                    {
                        NullFlavor = NullFlavor.NoInformation
                    }
                        );
                }
                retHl7v3.Add(retRec);
            }

            if (retHl7v3.Count > 1)
            {
                dtls.Add(new InsufficientRepetitionsResultDetail(ResultDetailType.Warning, localeService.GetString("MSGE06E"), null));
            }

            // Create the response
            PRPA_IN201310UV02 response = new PRPA_IN201310UV02
                                         (
                Guid.NewGuid(),
                DateTime.Now,
                PRPA_IN201310UV02.GetInteractionId(),
                ProcessingID.Production,
                "T",
                AcknowledgementCondition.Never,
                MessageUtil.CreateReceiver(rqst.Sender),
                MessageUtil.CreateSenderUv(new Uri(rqst.Receiver[0].Telecom.Value), configService),
                null
                                         )
            {
                Acknowledgement = new List <MARC.Everest.RMIM.UV.NE2008.MCCI_MT100300UV01.Acknowledgement>()
                {
                    new MARC.Everest.RMIM.UV.NE2008.MCCI_MT100300UV01.Acknowledgement(
                        dtls.Count(a => a.Type == ResultDetailType.Error) == 0 ? AcknowledgementType.ApplicationAcknowledgementAccept : AcknowledgementType.ApplicationAcknowledgementError,
                        new MARC.Everest.RMIM.UV.NE2008.MCCI_MT100200UV01.TargetMessage(request.Id)
                        )
                }
            };

            response.controlActProcess = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.ControlActProcess <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201307UV02.QueryByParameter, MARC.Everest.RMIM.UV.NE2008.PRPA_MT201304UV02.Patient, object>("EVN")
            {
                Id       = SET <II> .CreateSET(new II(configService.Custodianship.Id.Domain, Guid.NewGuid().ToString())),
                Code     = new CD <string>(PRPA_IN201310UV02.GetTriggerEvent().Code, PRPA_IN201310UV02.GetTriggerEvent().CodeSystem),
                QueryAck = new MARC.Everest.RMIM.UV.NE2008.QUQI_MT120001UV01.QueryAck(
                    rqst.controlActProcess.queryByParameter.QueryId,
                    "complete",
                    (AcknowledgementType)response.Acknowledgement[0].TypeCode == AcknowledgementType.ApplicationAcknowledgementError ? QueryResponse.ApplicationError : results.TotalResults == 0 ? QueryResponse.NoDataFound : QueryResponse.DataFound,
                    results.TotalResults,
                    results.Results.Count,
                    results.TotalResults - results.Results.Count - results.StartRecordNumber
                    ),
                queryByParameter = rqst.controlActProcess.queryByParameter
            };

            response.controlActProcess.LanguageCode = MessageUtil.GetDefaultLanguageCode(this.Context);
            if (retHl7v3.Count > 0)
            {
                response.controlActProcess.Subject.Add(retHl7v3[0]);
            }
            return(response);
        }
        /// <summary>
        /// Create the interaction type
        /// </summary>
        public MARC.Everest.Interfaces.IInteraction Create(MARC.Everest.Interfaces.IInteraction request, RegistryQueryResult results, List <IResultDetail> dtls)
        {
            // GEt the config services
            ISystemConfigurationService configService = Context.GetService(typeof(ISystemConfigurationService)) as ISystemConfigurationService;

            var retHl7v3 = new List <MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201310UV02.Patient, object> >();
            //List<MARC.Everest.RMIM.CA.R020403.MFMI_MT700746CA.Subject2<MARC.Everest.RMIM.CA.R020403.PRPA_MT101104CA.IdentifiedEntity>> retHl7v3 = new List<MARC.Everest.RMIM.CA.R020403.MFMI_MT700746CA.Subject2<MARC.Everest.RMIM.CA.R020403.PRPA_MT101104CA.IdentifiedEntity>>(results.Results.Count());
            UvDeComponentUtil dCompUtil = new UvDeComponentUtil();

            dCompUtil.Context = this.Context;

            PRPA_IN201305UV02 rqst = request as PRPA_IN201305UV02;

            // Convert results to HL7v3
            foreach (RegistrationEvent res in results.Results)
            {
                var retRec = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201310UV02.Patient, object>(
                    true,
                    dCompUtil.CreateRegistrationEvent(res, dtls)
                    );
                if (retRec.RegistrationEvent == null)
                {
                    retRec = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.Subject1 <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201310UV02.Patient, object>(
                        false,
                        new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.RegistrationEvent <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201310UV02.Patient, object>()
                    {
                        NullFlavor = NullFlavor.NoInformation
                    }
                        );
                }
                retHl7v3.Add(retRec);
            }

            // HACK: Sort by confidence score (if present)
            //retHl7v3.Sort((a, b) =>
            //    a != null && b != null &&
            //    a.RegistrationEvent != null && b.RegistrationEvent != null &&
            //    a.RegistrationEvent.Subject1 != null && b.RegistrationEvent.Subject1 != null &&
            //    a.RegistrationEvent.Subject1.registeredRole != null && b.RegistrationEvent.Subject1.registeredRole != null &&
            //    (a.RegistrationEvent.Subject1.registeredRole.SubjectOf1.Count > 0) && (b.RegistrationEvent.Subject1.registeredRole.SubjectOf1.Count > 0) ?
            //    (b.RegistrationEvent.Subject1.registeredRole.SubjectOf1[0].QueryMatchObservation.Value as INT).CompareTo((a.RegistrationEvent.Subject1.registeredRole.SubjectOf1[0].QueryMatchObservation.Value as INT)) : 0);

            // Create the response
            PRPA_IN201306UV02 response = new PRPA_IN201306UV02
                                         (
                Guid.NewGuid(),
                DateTime.Now,
                PRPA_IN201306UV02.GetInteractionId(),
                ProcessingID.Production,
                "T",
                AcknowledgementCondition.Never,
                MessageUtil.CreateReceiver(rqst.Sender),
                MessageUtil.CreateSenderUv(new Uri(rqst.Receiver[0].Telecom.Value), configService),
                null
                                         )
            {
                Acknowledgement = new List <MARC.Everest.RMIM.UV.NE2008.MCCI_MT100300UV01.Acknowledgement>()
                {
                    new MARC.Everest.RMIM.UV.NE2008.MCCI_MT100300UV01.Acknowledgement(
                        dtls.Count(a => a.Type == ResultDetailType.Error) == 0 ? AcknowledgementType.ApplicationAcknowledgementAccept : AcknowledgementType.ApplicationAcknowledgementError,
                        new MARC.Everest.RMIM.UV.NE2008.MCCI_MT100200UV01.TargetMessage(request.Id)
                        )
                }
            };

            response.controlActProcess = new MARC.Everest.RMIM.UV.NE2008.MFMI_MT700711UV01.ControlActProcess <MARC.Everest.RMIM.UV.NE2008.PRPA_MT201306UV02.QueryByParameter, MARC.Everest.RMIM.UV.NE2008.PRPA_MT201310UV02.Patient, object>("EVN")
            {
                Id       = SET <II> .CreateSET(new II(configService.Custodianship.Id.Domain, Guid.NewGuid().ToString())),
                Code     = new CD <string>(PRPA_IN201306UV02.GetTriggerEvent().Code, PRPA_IN201306UV02.GetTriggerEvent().CodeSystem),
                QueryAck = new MARC.Everest.RMIM.UV.NE2008.QUQI_MT120001UV01.QueryAck(
                    rqst.controlActProcess.queryByParameter.QueryId,
                    "complete",
                    (AcknowledgementType)response.Acknowledgement[0].TypeCode == AcknowledgementType.ApplicationAcknowledgementError ? QueryResponse.ApplicationError : results.TotalResults == 0 ? QueryResponse.NoDataFound : QueryResponse.DataFound,
                    results.TotalResults,
                    results.Results.Count,
                    results.TotalResults - results.Results.Count - results.StartRecordNumber
                    ),
                queryByParameter = rqst.controlActProcess.queryByParameter
            };

            response.controlActProcess.LanguageCode = MessageUtil.GetDefaultLanguageCode(this.Context);

            if (retHl7v3.Count > 0)
            {
                response.controlActProcess.Subject.AddRange(retHl7v3);
            }
            return(response);
        }