/// <summary>
        /// Adds the sites, sources, and variables from an ODM database to the DataQueryModel.
        /// </summary>
        /// <param name="dataQueryModel">The data query model.</param>
        /// <param name="db2">The DB2.</param>
        /// <param name="odmDb">The odm db.</param>
        /// <returns></returns>
        public static DataQueryModel AddSitesSourcesVars(DataQueryModel dataQueryModel, ODMDbDataContext db2, string odmDb)
        {
            foreach (var item in db2.Sites)
            {
                dataQueryModel.Sites.Add(new SiteModel()
                {
                    Site = item, OdmDb = odmDb
                });
            }
            foreach (var item in db2.Sources)
            {
                dataQueryModel.Sources.Add(new SourceModel()
                {
                    Source = item, OdmDb = odmDb
                });
            }
            var variableList = from vars in db2.Variables
                               orderby vars.VariableName
                               select vars;

            foreach (var item in variableList)
            {
                dataQueryModel.Variables.Add(new VariableModel()
                {
                    Variable = item, OdmDb = odmDb
                });
            }
            return(dataQueryModel);
        }
        /// <summary>
        /// Adds the regions to the DataQueryModel.
        /// </summary>
        /// <param name="dataQueryModel">The data query model.</param>
        /// <param name="db">The DB.</param>
        /// <param name="odmDb">The odm db.</param>
        /// <returns></returns>
        public static DataQueryModel AddRegions(DataQueryModel dataQueryModel, HISDataContext db)
        {
            var regionsList = from rg in db.Regions
                              orderby rg.RegionName
                              select rg;

            foreach (Region item in regionsList)
            {
                dataQueryModel.Regions.Add(new RegionModel()
                {
                    Region = item
                });
            }
            return(dataQueryModel);
        }
        /// <summary>
        /// Get a new DataQueryModel from submitted information.
        /// </summary>
        /// <param name="db">The db.</param>
        /// <param name="dataQueryForm">The data query form.</param>
        /// <returns></returns>
        public static DataQueryModel NewDataQueryModel(HISDataContext db, DataQueryFormModel dataQueryForm)
        {
            var dataQueryModel = new DataQueryModel()
            {
                Regions       = new List <RegionModel>(),
                Sites         = new List <SiteModel>(),
                Sources       = new List <SourceModel>(),
                Variables     = new List <VariableModel>(),
                SeriesCatalog = new List <TimeSeriesModel>(),
                DataQueryForm = dataQueryForm
            };

            var items = db.ODMDatabases;

            dataQueryModel = DataQueryRepository.AddRegions(dataQueryModel, db);

            foreach (var odmDb in items)
            {
                ODMDbDataContext db2 = DataQueryRepository.GetODMDbDataContext(odmDb);

                dataQueryModel = DataQueryRepository.AddSitesSourcesVars(dataQueryModel, db2, odmDb.Title);

                if (dataQueryForm != null)
                {
                    List <RegionDatabase> matches = (from rd in db.RegionDatabases
                                                     where (rd.DatabaseID == odmDb.DatabaseID)
                                                     select rd).ToList <RegionDatabase>();

                    var isInRegion = false;

                    if (matches != null)
                    {
                        foreach (var match in matches)
                        {
                            if (dataQueryForm.RegionIDs.Any(s => s.ID == match.RegionID))
                            {
                                isInRegion = true;
                            }
                        }
                    }

                    if (isInRegion)
                    {
                        foreach (var item in db2.TimeSeries)
                        {
                            if (dataQueryForm.VariableIDs.Count > 0)
                            {
                                if (
                                    dataQueryForm.VariableIDs.Any(s => (s.ID == item.VariableID) & (s.OdmDb == odmDb.Title)) ||
                                    (
                                        (dataQueryForm.CustomVariable != null) &&
                                        db2.Variables.SingleOrDefault(v => v.VariableID == item.VariableID).VariableName.ToUpper().Contains(dataQueryForm.CustomVariable.ToUpper())
                                    )
                                    )
                                {
                                    dataQueryModel.SeriesCatalog.Add(new TimeSeriesModel()
                                    {
                                        TimeSeries = item, OdmDb = odmDb.Title
                                    });
                                }
                            }
                            else
                            {
                                if ((dataQueryForm.CustomVariable != null) &&
                                    db2.Variables.SingleOrDefault(v => v.VariableID == item.VariableID).VariableName.ToUpper().Contains(dataQueryForm.CustomVariable.ToUpper()))
                                {
                                    dataQueryModel.SeriesCatalog.Add(new TimeSeriesModel()
                                    {
                                        TimeSeries = item, OdmDb = odmDb.Title
                                    });
                                }
                            }
                        }
                    }
                }
            }


            var allRegionIDs = new List <DbID>();
            var allVarIDs    = new List <DbID>();

            foreach (var item in dataQueryModel.Regions)
            {
                allRegionIDs.Add(new DbID()
                {
                    ID = item.Region.RegionID, OdmDb = item.OdmDb
                });
            }
            foreach (var item in dataQueryModel.Variables)
            {
                allVarIDs.Add(new DbID()
                {
                    ID = item.Variable.VariableID, OdmDb = item.OdmDb
                });
            }

            if (dataQueryModel.DataQueryForm == null)
            {
                dataQueryModel.DataQueryForm = new DataQueryFormModel()
                {
                    RegionIDs   = allRegionIDs,
                    VariableIDs = allVarIDs
                };
            }

            return(dataQueryModel);
        }