public drn_query_builder_helper()
 {
     _qb = new drn_query_builder()
     {
         datamart           = new [] { new drn_query_builderDatamart() },
         exclusion_criteria = new [] { new drn_query_builderExclusion_criteria() },
         inclusion_criteria = new [] { new drn_query_builderInclusion_criteria() },
         index_variable     = new [] { new drn_query_builderIndex_variable() },
         report             = new [] { new drn_query_builderReport() },
         version            = "0.0.1"
     };
 }
        /// <summary>
        /// Creates a new model, and optionally restores the values from the request document
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private SPANQueryBuilderModel getModel(IDnsRequestContext context)
        {
            var m = new SPANQueryBuilderModel();

            // set up ui defaults, overridden if we deserialize
            m.ExclusionAgeSelector.Age           = 0;
            m.IndexVariable                      = "dx";
            m.IndexVariableBMISelector.BMIOption = "1";
            m.ObservationPeriod.EndPeriod        = DateTime.Today;
            m.ObservationPeriod.StartPeriod      = DateTime.Today;
            m.ReportSelector1.Column             = NA;
            m.ReportSelector1.Group              = NA;
            m.ReportSelector1.Option             = NA;
            m.ReportSelector1.Row                = NA;
            m.ReportSelector2.Column             = NA;
            m.ReportSelector2.Group              = NA;
            m.ReportSelector2.Option             = NA;
            m.ReportSelector2.Row                = NA;
            m.ReportSelector3.Column             = NA;
            m.ReportSelector3.Group              = NA;
            m.ReportSelector3.Option             = NA;
            m.ReportSelector3.Row                = NA;
            m.ReportSelector4.Column             = NA;
            m.ReportSelector4.Group              = NA;
            m.ReportSelector4.Option             = NA;
            m.ReportSelector4.Row                = NA;
            m.ReportSelector5.Column             = NA;
            m.ReportSelector5.Group              = NA;
            m.ReportSelector5.Option             = NA;
            m.ReportSelector5.Row                = NA;

            if ((context.Documents != null) && (context.Documents.Where(d => d.Name == REQUEST_FILENAME).Count() > 0))
            {
                var doc = context.Documents.Where(d => d.Name == REQUEST_FILENAME).First();
                using (var db = new DataContext())
                {
                    using (var docStream = new DocumentStream(db, doc.ID))
                    {
                        XmlSerializer serializer = new XmlSerializer(typeof(drn_query_builder));
                        using (XmlTextReader reader = new XmlTextReader(docStream))
                        {
                            drn_query_builder deserializedModel = (drn_query_builder)serializer.Deserialize(reader);

                            // set up enrollment selector
                            m.EnrollmentSelector.After      = Convert.ToInt32(deserializedModel.enroll_post);
                            m.EnrollmentSelector.Continuous = deserializedModel.enroll_cont == "y";
                            m.EnrollmentSelector.Prior      = Convert.ToInt32(deserializedModel.enroll_prior);

                            // set up exclusion age selector
                            var agevar = deserializedModel.exclusion_criteria.FirstOrDefault().age_var;
                            if (agevar != null)
                            {
                                m.ExclusionAgeSelector.Age         = Convert.ToInt32(agevar[0].age);
                                m.ExclusionAgeSelector.AgeOperator = agevar[0].age_operator;
                            }

                            // set up exclusion Dx selector
                            var dxvar = deserializedModel.exclusion_criteria.FirstOrDefault().dx_var;
                            if (dxvar != null)
                            {
                                //m.ExclusionDxSelector.SelectedCodes = dxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.ExclusionDxSelector.BoolOperator = dxvar[0].bool_operator;
                            }

                            // set up exclusion Px selector
                            var pxvar = deserializedModel.exclusion_criteria.FirstOrDefault().px_var;
                            if (pxvar != null)
                            {
                                //m.ExclusionPxSelector.SelectedCodes = pxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.ExclusionPxSelector.BoolOperator = pxvar[0].bool_operator;
                            }

                            // set up exclusion Rx selector
                            var rxvar = deserializedModel.exclusion_criteria.FirstOrDefault().rx_var;
                            if (rxvar != null)
                            {
                                //m.ExclusionRxSelector.SelectedCodes = rxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.ExclusionRxSelector.BoolOperator = rxvar[0].bool_operator;
                            }

                            // set up inclusion Dx selector
                            dxvar = deserializedModel.inclusion_criteria.FirstOrDefault().dx_var;
                            if (dxvar != null)
                            {
                                //m.InclusionDxSelector.SelectedCodes = dxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.InclusionDxSelector.BoolOperator = dxvar[0].bool_operator;
                            }

                            // set up inclusion Px selector
                            pxvar = deserializedModel.inclusion_criteria.FirstOrDefault().px_var;
                            if (pxvar != null)
                            {
                                //m.InclusionPxSelector.SelectedCodes = pxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.InclusionPxSelector.BoolOperator = pxvar[0].bool_operator;
                            }

                            // set up inclusion Rx selector
                            rxvar = deserializedModel.inclusion_criteria.FirstOrDefault().rx_var;
                            if (rxvar != null)
                            {
                                //m.InclusionRxSelector.SelectedCodes = rxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.InclusionRxSelector.BoolOperator = rxvar[0].bool_operator;
                            }

                            m.IndexVariable = deserializedModel.index_variable.FirstOrDefault().index_code;

                            // set up index variable age selector
                            agevar = deserializedModel.index_variable.FirstOrDefault().age_var;
                            if (agevar != null)
                            {
                                m.IndexVariableAgeSelector.Age         = Convert.ToInt32(agevar[0].age);
                                m.IndexVariableAgeSelector.AgeAsOfDate = Convert.ToDateTime(agevar[0].as_of);
                                m.IndexVariableAgeSelector.AgeOperator = agevar[0].age_operator;
                            }
                            ;

                            // set up index variable bmi selector
                            var bmivar = deserializedModel.index_variable.FirstOrDefault().bmi_var;
                            if (bmivar != null)
                            {
                                m.IndexVariableBMISelector.BMI       = bmivar[0].Value;
                                m.IndexVariableBMISelector.BMIOption = bmivar[0].group;
                            }
                            ;

                            // set up index variable Dx selector
                            dxvar = deserializedModel.index_variable.FirstOrDefault().dx_var;
                            if (dxvar != null)
                            {
                                //m.IndexVariableDxSelector.SelectedCodes = dxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.IndexVariableDxSelector.BoolOperator = dxvar[0].bool_operator;
                            }

                            // set up index variable Px selector
                            pxvar = deserializedModel.index_variable.FirstOrDefault().px_var;
                            if (pxvar != null)
                            {
                                //m.IndexVariablePxSelector.SelectedCodes = pxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.IndexVariablePxSelector.BoolOperator = pxvar[0].bool_operator;
                            }

                            // set up index variable Rx selector
                            rxvar = deserializedModel.index_variable.FirstOrDefault().rx_var;
                            if (rxvar != null)
                            {
                                //m.IndexVariableRxSelector.SelectedCodes = rxvar[0].code.Select(c => new LookupListValue() { ItemCode = c.Value });
                                //m.IndexVariableRxSelector.BoolOperator = rxvar[0].bool_operator;
                            }

                            // set up observation period range
                            m.ObservationPeriod.EndPeriod   = Convert.ToDateTime(deserializedModel.period_end);
                            m.ObservationPeriod.StartPeriod = Convert.ToDateTime(deserializedModel.period_start);

                            // set up reports
                            if (deserializedModel.report != null)
                            {
                                foreach (var repvar in deserializedModel.report)
                                {
                                    switch (repvar.Value)
                                    {
                                    case "Report0":
                                        m.ReportSelector1.Column = repvar.column ?? NA;
                                        m.ReportSelector1.Group  = repvar.group ?? NA;
                                        m.ReportSelector1.Option = repvar.option ?? NA;
                                        m.ReportSelector1.Row    = repvar.row ?? NA;
                                        break;

                                    case "Report1":
                                        m.ReportSelector2.Column = repvar.column ?? NA;
                                        m.ReportSelector2.Group  = repvar.group ?? NA;
                                        m.ReportSelector2.Option = repvar.option ?? NA;
                                        m.ReportSelector2.Row    = repvar.row ?? NA;
                                        break;

                                    case "Report2":
                                        m.ReportSelector3.Column = repvar.column ?? NA;
                                        m.ReportSelector3.Group  = repvar.group ?? NA;
                                        m.ReportSelector3.Option = repvar.option ?? NA;
                                        m.ReportSelector3.Row    = repvar.row ?? NA;
                                        break;

                                    case "Report3":
                                        m.ReportSelector4.Column = repvar.column ?? NA;
                                        m.ReportSelector4.Group  = repvar.group ?? NA;
                                        m.ReportSelector4.Option = repvar.option ?? NA;
                                        m.ReportSelector4.Row    = repvar.row ?? NA;
                                        break;

                                    case "Report4":
                                        m.ReportSelector5.Column = repvar.column ?? NA;
                                        m.ReportSelector5.Group  = repvar.group ?? NA;
                                        m.ReportSelector5.Option = repvar.option ?? NA;
                                        m.ReportSelector5.Row    = repvar.row ?? NA;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            return(m);
        }