protected virtual void CalculateSignificances(string areaCode, TimePeriod timePeriod, IList<CoreDataSet> categoryDataList)
        {
            var area = AreaFactory.NewArea(_areasReader, areaCode);
            var nationalArea = GetNationalArea(area);

            var indicatorComparisonHelper = new IndicatorComparisonHelper(_indicatorMetadata,
                _grouping, _groupDataReader, _pholioReader, nationalArea);

            // Set benchmark data
            var benchmarkDataProvider = new BenchmarkDataProvider(_groupDataReader);
            AverageCalculator averageCalculator = null; // Assume parent value is in database
            CoreDataSet benchmarkData = benchmarkDataProvider.GetBenchmarkData(_grouping, timePeriod,
                averageCalculator, area);

            foreach (CoreDataSet coreDataSet in categoryDataList)
            {
                coreDataSet.SignificanceAgainstOneBenchmark =
                    (int) indicatorComparisonHelper.GetSignificance(coreDataSet, benchmarkData);
            }
        }
        private void AssignComparatorData(GroupRoot root, TimePeriod timePeriod)
        {
            IndicatorMetadata indicatorMetaData = GroupData.GetIndicatorMetadataById(root.IndicatorId);
            var benchmarkDataProvider = new BenchmarkDataProvider(GroupDataReader);

            // Comparator data
            foreach (Grouping grouping in root.Grouping)
            {
                Comparator comparator = ComparatorMap.GetComparatorById(grouping.ComparatorId, grouping.AreaTypeId);
                CoreDataSet data;
                if (comparator == null)
                {
                    data = CoreDataSet.GetNullObject(string.Empty);
                }
                else
                {
                    var comparatorArea = NearestNeighbourArea.IsNearestNeighbourAreaCode(comparator.Area.Code)
                         ? AreaFactory.NewArea(AreasReader, comparator.Area.Code.Substring(5))
                         : comparator.Area;

                    // Only get subnational data, do not want to prefetch England data as usually won't be needed
                    var dataList = grouping.ComparatorId == ComparatorIds.Subnational
                        ? root.Data
                        : null;

                    AverageCalculator averageCalculator = AverageCalculatorFactory.New(dataList, indicatorMetaData);

                    data = benchmarkDataProvider.GetBenchmarkData(grouping, timePeriod,
                            averageCalculator, comparatorArea);

                    // Calculate England data if not found in DB (this done after so do not have to read all data when necessary)
                    if (data.IsValueValid == false && grouping.ComparatorId == ComparatorIds.England)
                    {
                        dataList = GroupDataReader.GetCoreDataForAllAreasOfType(grouping, timePeriod);
                        averageCalculator = AverageCalculatorFactory.New(dataList, indicatorMetaData);
                        data = benchmarkDataProvider.GetBenchmarkData(grouping, timePeriod,
                            averageCalculator, comparatorArea);
                    }
                }
                grouping.ComparatorData = data;
            }
        }
        private void PerformComparisons(IList<CoreDataSet> dataList)
        {
            IndicatorComparer comparer = indicatorComparerFactory.New(Grouping);
            TargetComparer targetComparer = TargetComparerFactory.New(IndicatorMetadata.TargetConfig);
            new TargetComparerHelper(groupDataReader, parentArea)
                    .AssignExtraDataIfRequired(parentArea, targetComparer, Grouping, IndicatorMetadata);

            CoreDataSet benchmarkData = null;
            ICategoryComparer categoryComparer = null;
            var averageCalculator = AverageCalculatorFactory.New(dataList, IndicatorMetadata);
            if (comparer is ICategoryComparer)
            {
                categoryComparer = comparer as ICategoryComparer;
                var values = new CoreDataSetFilter(dataList).SelectValidValues().ToList();
                categoryComparer.SetDataForCategories(values);
            }
            else
            {
                benchmarkData = new BenchmarkDataProvider(groupDataReader)
                    .GetBenchmarkData(Grouping, Period, averageCalculator, parentArea);
            }

            foreach (CoreDataSet coreData in dataList)
            {
                // NHibernate may have provided a cached data object for which significance
                // has already been added
                if (coreData.Significance.ContainsKey(ComparatorId) == false)
                {
                    int significance = categoryComparer == null
                        ? (int)comparer.Compare(coreData, benchmarkData, IndicatorMetadata)
                        : categoryComparer.GetCategory(coreData);

                    coreData.Significance.Add(ComparatorId, significance);
                }

                // NHibernate may have provided a cached data object for which significance
                // has already been added
                if (coreData.Significance.ContainsKey(ComparatorIds.Target) == false)
                {
                    TargetComparer.AddTargetSignificance(coreData, targetComparer);
                }
            }
        }