Example #1
0
        private void GetCohortAvailability(ExternalCohortTable source)
        {
            DiscoveredServer server = null;

            Exception ex = null;

            //it obviously hasn't been initialised properly yet
            if (string.IsNullOrWhiteSpace(source.Server) || string.IsNullOrWhiteSpace(source.Database))
            {
                return;
            }

            try
            {
                server = DataAccessPortal.GetInstance().ExpectDatabase(source, DataAccessContext.DataExport).Server;
            }
            catch (Exception exception)
            {
                ex = exception;
            }

            if (server == null || !server.RespondsWithinTime(3, out ex) || !source.IsFullyPopulated())
            {
                Blacklist(source, ex);
                return;
            }

            try
            {
                using (var con = server.GetConnection())
                {
                    con.Open();

                    //Get all of the project numbers and remote origin ids etc from the source in one query
                    using (var cmd = server.GetCommand(source.GetExternalDataSql(), con))
                    {
                        cmd.CommandTimeout = 120;

                        using (var r = cmd.ExecuteReader())
                        {
                            while (r.Read())
                            {
                                //really should be only one here but still they might for some reason have 2 references to the same external cohort

                                if (_cohortsByOriginId.TryGetValue(Convert.ToInt32(r["OriginID"]), out HashSet <ExtractableCohort> result))
                                {
                                    //Tell the cohorts what their external data values are so they don't have to fetch them themselves individually
                                    foreach (ExtractableCohort c in result.Where(c => c.ExternalCohortTable_ID == source.ID))
                                    {
                                        //load external data from the result set
                                        var externalData = new ExternalCohortDefinitionData(r, source.Name);

                                        //tell the cohort about the data
                                        c.InjectKnown(externalData);

                                        lock (_oProjectNumberToCohortsDictionary)
                                        {
                                            //for performance also keep a dictionary of project number => compatible cohorts
                                            if (!ProjectNumberToCohortsDictionary.ContainsKey(externalData.ExternalProjectNumber))
                                            {
                                                ProjectNumberToCohortsDictionary.Add(externalData.ExternalProjectNumber, new List <ExtractableCohort>());
                                            }

                                            ProjectNumberToCohortsDictionary[externalData.ExternalProjectNumber].Add(c);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Blacklist(source, e);
            }
        }