internal string buildMatchPatientsRequest(Patient patient)
        {
            if (patient.Name == null || patient.SSN == null)
            {
                throw new Exception("Must have last name and SSN");
            }

            PatientMatchesRequest msg = new PatientMatchesRequest();

            msg.MSH.SendingApplication   = MpiConstants.VQQ_SENDING_APP;
            msg.MSH.SendingFacility      = MpiConstants.VQQ_SENDING_FACILITY;
            msg.MSH.ReceivingApplication = MpiConstants.VQQ_RECEIVING_APP;
            msg.MSH.ReceivingFacility    = MpiConstants.VQQ_RECEIVING_FACILITY;
            msg.MSH.MessageCode          = MpiConstants.VQQ_MSG_CODE;
            msg.MSH.EventTrigger         = MpiConstants.VQQ_TRIGGER;
            msg.MSH.MessageControlID     = MpiConstants.VQQ_MSG_CTL;
            msg.MSH.ProcessingID         = MpiConstants.VQQ_PROCESSING_ID;
            msg.MSH.VersionID            = MpiConstants.VQQ_VERSION_ID;
            msg.MSH.AcceptAckType        = MpiConstants.VQQ_ACCEPT_ACK_TYPE;
            msg.MSH.ApplicationAckType   = MpiConstants.VQQ_APP_ACK_TYPE;
            msg.MSH.CountryCode          = MpiConstants.VQQ_COUNTRY_CODE;

            msg.VTQ.QueryTag          = MpiConstants.VQQ_QUERY_TAG;
            msg.VTQ.FormatCode        = MpiConstants.VQQ_FORMAT_CODE;
            msg.VTQ.QueryName         = MpiConstants.VQQ_QUERY_NAME_FUZZY;
            msg.VTQ.VirtualTableName  = MpiConstants.VQQ_VIRTUAL_TABLE;
            msg.VTQ.SelectionCriteria = new ArrayList();
            msg.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_SSN.FieldName, "EQ", patient.SSN.toString(), "OR"));
            msg.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_LASTNAME.FieldName, "EQ", patient.Name.Lastname, "AND"));
            msg.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_FIRSTNAME.FieldName, "EQ", patient.Name.Firstname, "AND"));
            msg.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_DOB.FieldName, "EQ", patient.DOB, "AND"));
            msg.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_SEX.FieldName, "EQ", patient.Gender, ""));

            msg.RDF.NColumns    = 12;
            msg.RDF.Columns     = new RdfColumn[12];
            msg.RDF.Columns[0]  = new RdfColumn(MpiConstants.FLD_ICN);
            msg.RDF.Columns[1]  = new RdfColumn(MpiConstants.FLD_SSN);
            msg.RDF.Columns[2]  = new RdfColumn(MpiConstants.FLD_LASTNAME);
            msg.RDF.Columns[3]  = new RdfColumn(MpiConstants.FLD_FIRSTNAME);
            msg.RDF.Columns[4]  = new RdfColumn(MpiConstants.FLD_MIDDLENAME);
            msg.RDF.Columns[5]  = new RdfColumn(MpiConstants.FLD_NAME_SUFFIX);
            msg.RDF.Columns[6]  = new RdfColumn(MpiConstants.FLD_SSN);
            msg.RDF.Columns[7]  = new RdfColumn(MpiConstants.FLD_DOB);
            msg.RDF.Columns[8]  = new RdfColumn(MpiConstants.FLD_SEX);
            msg.RDF.Columns[9]  = new RdfColumn(MpiConstants.FLD_DECEASED_DATE);
            msg.RDF.Columns[10] = new RdfColumn(MpiConstants.FLD_CMOR);
            msg.RDF.Columns[11] = new RdfColumn(MpiConstants.FLD_SITES);

            return(msg.toMessage());
        }
        internal string buildMatchSsnRequest(string ssn)
        {
            if (!SocSecNum.isValid(ssn))
            {
                throw new Exception("Invalid SSN");
            }
            PatientMatchesRequest hl7 = getBlankHl7Request();

            hl7.VTQ.QueryName         = MpiConstants.VQQ_QUERY_NAME_FUZZY;
            hl7.VTQ.SelectionCriteria = new ArrayList(2);
            hl7.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_SSN.FieldName, "EQ", ssn, "OR"));
            hl7.VTQ.SelectionCriteria.Add(
                new SelectionCriterion(MpiConstants.FLD_LASTNAME.FieldName, "EQ", "", ""));
            return(hl7.toMessage());
        }