private variablesType BuildTerms(CriteriaGroupModel criteriaGroup)
        {
            variablesType vt = new variablesType();
            operation     o  = new operation();

            vt.name      = criteriaGroup.CriteriaGroupName;
            vt.operation = o;

            o.@operator = "Or";

            List <variable>  variableList  = new List <variable>();
            List <operation> operationList = new List <operation>();

            foreach (var term in criteriaGroup.Terms)
            {
                switch (term.TermName)
                {
                case "ICD9CodeSelector":
                    operation codeOperation = new operation();
                    codeOperation.@operator = "Or";
                    operationList.Add(codeOperation);
                    string[] codes = term.Args["Codes"].Split(',');

                    if (codes.Length > 0)
                    {
                        IList <variable> codeVariables = new List <variable>();
                        foreach (string code in codes)
                        {
                            codeVariables.Add(new variable {
                                name = "Code", value = code.Trim()
                            });
                        }

                        codeOperation.variable = codeVariables.ToArray <variable>();
                    }

                    //v.Add(new variable { name = "Codes", value = term.Args["Codes"] });
                    break;

                case "ZipCodeSelector":
                    operation zipOperation = new operation();
                    zipOperation.@operator = "Or";
                    operationList.Add(zipOperation);
                    string[] zipCodes = term.Args["Codes"].Split(',');

                    if (zipCodes.Length > 0)
                    {
                        IList <variable> codeVariables = new List <variable>();
                        foreach (string code in zipCodes)
                        {
                            codeVariables.Add(new variable {
                                name = "ZipCode", value = code.Trim()
                            });
                        }

                        zipOperation.variable = codeVariables.ToArray <variable>();
                    }

                    break;

                case "RaceSelector":
                    operation raceOperation = new operation();
                    raceOperation.@operator = "Or";
                    operationList.Add(raceOperation);

                    string[] races = term.Args["Race"].Split(',');

                    if (races.Length > 0 && !string.IsNullOrEmpty(races[0]))
                    {
                        IList <variable> raceVariables = new List <variable>();
                        foreach (string race in races)
                        {
                            raceVariables.Add(new variable {
                                name = "Race", value = Convert.ToInt32(race).ToString()
                            });
                        }

                        raceOperation.variable = raceVariables.ToArray <variable>();
                    }
                    break;

                case "EthnicitySelector":
                    operation ethnicityOperation = new operation();
                    ethnicityOperation.@operator = "Or";
                    operationList.Add(ethnicityOperation);

                    string[] ethnicities = term.Args["Ethnicity"].Split(',');

                    if (ethnicities.Length > 0 && !string.IsNullOrEmpty(ethnicities[0]))
                    {
                        IList <variable> ethnicityVariables = new List <variable>();
                        foreach (string race in ethnicities)
                        {
                            ethnicityVariables.Add(new variable {
                                name = "Ethnicity", value = Convert.ToInt32(race).ToString()
                            });
                        }

                        ethnicityOperation.variable = ethnicityVariables.ToArray <variable>();
                    }
                    break;

                case "DiseaseSelector":
                    variableList.Add(new variable {
                        name = "Disease", value = term.Args["Disease"]
                    });
                    break;

                case "AgeRange":
                    // Age Range
                    operation ageOperation = new operation();
                    ageOperation.@operator = "And";
                    ageOperation.variable  = new variable[2];
                    operationList.Add(ageOperation);
                    ageOperation.variable[0] = new variable {
                        name = "Age", value = term.Args["MaxAge"], @operator = "<="
                    };
                    ageOperation.variable[1] = new variable {
                        name = "Age", value = term.Args["MinAge"], @operator = ">="
                    };
                    break;

                case "Gender":
                    // Sex Selector
                    var sex = Convert.ToInt32(term.Args["Sex"]);
                    if (sex == SexSelectionList.Male.Code || sex == SexSelectionList.Female.Code)
                    {
                        variableList.Add(new variable {
                            name = "Sex", value = SexSelectionList.GetName(sex).Substring(0, 1)
                        });
                    }
                    else
                    {
                        operation sexOperation = new operation();
                        operationList.Add(sexOperation);
                        sexOperation.@operator = "Or";
                        IList <variable> sexVariables = new List <variable>();
                        sexVariables.Add(new variable {
                            name = "Sex", value = SexSelectionList.Male.Name.Substring(0, 1)
                        });
                        sexVariables.Add(new variable {
                            name = "Sex", value = SexSelectionList.Female.Name.Substring(0, 1)
                        });
                        sexOperation.variable = sexVariables.ToArray <variable>();
                    }
                    break;

                case "Visits":
                    // Visits
                    var visits = Convert.ToInt32(term.Args["MinVisits"]);
                    if (visits > 0)
                    {
                        variableList.Add(new variable {
                            name = "Visits", value = visits.ToString()
                        });
                    }
                    break;

                case "Asthma":
                    //To-do Jamie
                    break;
                }
            }

            o.variable   = variableList.ToArray();
            o.operation1 = operationList.ToArray();

            // Observation Period
            var observationPeriod = (from t in criteriaGroup.Terms
                                     where t.TermName == "ObservationPeriod"
                                     select t).FirstOrDefault();

            if (observationPeriod != null)
            {
                // Observation Period Range
                operation periodOperation = new operation();
                periodOperation.@operator = "And";
                periodOperation.variable  = new variable[2];
                operationList.Add(periodOperation);
                DateTime sasZeroDate = DateTime.Parse("1960-01-01");

                List <variable> periodOperationList = new List <variable>();
                if (Boolean.Parse(observationPeriod.Args["UseStartPeriod"]))
                {
                    int sasStartPeriod = (DateTime.Parse(observationPeriod.Args["StartPeriod"]).Subtract(sasZeroDate)).Days;
                    periodOperationList.Add(new variable {
                        name = "Observation_Period", value = sasStartPeriod.ToString(), @operator = ">="
                    });
                }
                if (Boolean.Parse(observationPeriod.Args["UseEndPeriod"]))
                {
                    int sasEndPeriod = (DateTime.Parse(observationPeriod.Args["EndPeriod"]).Subtract(sasZeroDate)).Days;
                    periodOperationList.Add(new variable {
                        name = "Observation_Period", value = sasEndPeriod.ToString(), @operator = "<="
                    });
                }
                periodOperation.variable = periodOperationList.ToArray();

                operation oo = new operation();
                oo.@operator     = "And";
                oo.operation1    = new operation[2];
                oo.operation1[0] = o;
                oo.operation1[1] = periodOperation;

                vt.operation = oo;
            }

            return(vt);
        }
        private byte[] BuildRequest(IDnsRequestContext request, ConditionsModel m)
        {
            request_builder requestBuilder = LoadReportHeader(request);

            requestBuilder.request.variables                     = new variablesType();
            requestBuilder.request.variables.operation           = new operation();
            requestBuilder.request.variables.operation.@operator = "And";

            IList <variable> variables = new List <variable>();

            // Observation Period Range
            DateTime sasZeroDate    = DateTime.Parse("1960-01-01");
            var      sasStartPeriod = m.StartPeriodDate == null ? 0 : (m.StartPeriodDate.Value.Subtract(sasZeroDate)).Days;
            var      sasEndPeriod   = m.EndPeriodDate == null ? 0 : (m.EndPeriodDate.Value.Subtract(sasZeroDate)).Days;

            variables.Add(new variable {
                name = "Observation_Period", value = sasStartPeriod.ToString(), @operator = ">="
            });
            variables.Add(new variable {
                name = "Observation_Period", value = sasEndPeriod.ToString(), @operator = "<="
            });

            IList <operation> operations = new List <operation>();

            variables.Add(new variable {
                name = "Disease", value = m.Disease.ToString()
            });

            // Age Range
            variables.Add(new variable {
                name = "Age", value = m.MaxAge.ToString(), @operator = "<="
            });
            variables.Add(new variable {
                name = "Age", value = m.MinAge.ToString(), @operator = ">="
            });

            // Sex Selector
            if (m.Sex == SexSelectionList.Male.Code || m.Sex == SexSelectionList.Female.Code)
            {
                variables.Add(new variable {
                    name = "Sex", value = SexSelectionList.GetName(m.Sex).Substring(0, 1)
                });
            }
            else
            {
                operation sexOperation = new operation();
                operations.Add(sexOperation);
                sexOperation.@operator = "Or";
                IList <variable> sexVariables = new List <variable>();
                sexVariables.Add(new variable {
                    name = "Sex", value = SexSelectionList.Male.Name.Substring(0, 1)
                });
                sexVariables.Add(new variable {
                    name = "Sex", value = SexSelectionList.Female.Name.Substring(0, 1)
                });
                sexOperation.variable = sexVariables.ToArray <variable>();
            }

            // Race Selector
            string[] races = m.Race.Split(',');
            if (races.Length > 0 && !string.IsNullOrEmpty(races[0]))
            {
                operation raceOperation = new operation();
                operations.Add(raceOperation);
                raceOperation.@operator = "Or";
                IList <variable> raceVariables = new List <variable>();
                foreach (string race in races)
                {
                    if (race.NullOrEmpty())
                    {
                        continue;
                    }
                    raceVariables.Add(new variable {
                        name = "Race", value = RaceConvert(race)
                    });
                }
                raceOperation.variable = raceVariables.ToArray <variable>();
            }

            if (variables.Count > 0)
            {
                requestBuilder.request.variables.operation.variable = variables.ToArray <variable>();
            }

            if (operations.Count > 0)
            {
                requestBuilder.request.variables.operation.operation1 = operations.ToArray <operation>();
            }

            LoadReportSelector(m, requestBuilder);

            return(SerializeRequest(requestBuilder));
        }