public QuinaryPopulation GetCcgQuinaryPopulation(int indicatorId, TimePeriod period, string areaCode,
            int sexId)
        {
            string sql = string.Format(SqlGetCcgPopulation, indicatorId, sexId, areaCode, AreaTypeIds.GpPractice, period.Year);

            QuinaryPopulation population = new QuinaryPopulation();

            DataTable table = ExecuteSelectSqlString(sql);

            foreach (DataRow row in table.Rows)
            {
                population.Values.Add(new QuinaryPopulationValue
                {
                    AgeId = (int)row.ItemArray[0],
                    Value = (double)row.ItemArray[1]
                });

                if (population.ChildAreaCount == 0)
                {
                    population.ChildAreaCount = (int)row.ItemArray[2];
                }
            }

            return population;
        }
 private void SetOffsetTimePeriod(Grouping grouping, int dataPointOffset, TimePeriod dataPointPeriod, YearType yearType)
 {
     if (dataPointOffset < 0)
     {
         // Ensures datapoint cannot be exceeded
         TimePeriod = dataPointPeriod;
     }
     else
     {
         TimePeriod baseline = TimePeriod.GetBaseline(grouping);
         TimePeriodIterator iterator = new TimePeriodIterator(baseline, dataPointPeriod, yearType);
         int periodCount = iterator.TimePeriods.Count;
         int index = periodCount - 1 - dataPointOffset;
         if (index < periodCount && index > 0)
         {
             TimePeriod = iterator.TimePeriods[index];
         }
         else
         {
             if (index < 0)
             {
                 // Ensures baseline cannot be overshot
                 TimePeriod = new TimePeriod { Year = InvalidYear };
             }
             else
             {
                 TimePeriod = baseline;
             }
         }
     }
 }
        private void SetSingleAreaValue(IGroupDataReader groupDataReader, Area area, Grouping grouping, TimePeriod period)
        {
            IList<CoreDataSet> dataList = groupDataReader.GetCoreData(grouping, period, area.Code);
            if (dataList.Count == 1)
            {
                var data = dataList.First();
                if (data.IsValueValid)
                {
                    Value = data.Value;

                    if (area.IsGpPractice == false)
                    {
                        // Calculate average

                        var areasReader = ReaderFactory.GetAreasReader();

                        int count = area.IsCountry ?
                            areasReader.GetAreaCountForAreaType(grouping.AreaTypeId) :
                            areasReader.GetChildAreaCount(area.Code, grouping.AreaTypeId);

                        Value /= count;
                    }
                }
            }
            else if (dataList.Count > 1)
            {
                throw new FingertipsException(string.Format(
                    "More data than expected on determination of QOF list size: area:{0}", area.Code));
            }
        }
        private static string FormatMonth(TimePeriod period, int yearTypeId)
        {
            StringBuilder sb = new StringBuilder();

            switch (yearTypeId)
            {
                case YearTypeIds.CalendarRollingYearMonthly:
                    // Calendar rolling year monthly
                    AddMonthAndYear(period, sb);
                    sb.Append(" - ");
                    AddMonthAndYear(period.GetRollingMonthlyEndPoint(), sb);
                    break;

                default:
                    int month = period.Month;
                    int year = period.Year;
                    if (DoesYearNotBeginInJanuary(yearTypeId))
                    {
                        month += 3;
                        if (month > 12)
                        {
                            month -= 12;
                            year++;
                        }
                    }

                    AddMonthAndYear(new TimePoint { Year = year, Month = month }, sb);
                    break;
            }
            return sb.ToString();
        }
        public override CoreDataSet GetData(Grouping grouping, TimePeriod timePeriod, IndicatorMetadata indicatorMetadata)
        {
            double val = practiceDataAccess.GetPracticeAggregateDataValue(grouping, timePeriod, Area.Code);

            return val.Equals(ValueData.NullValue) ?
                null :
                new CoreDataSet { Value = val };
        }
        public override CoreDataSet GetData(Grouping grouping, TimePeriod timePeriod, IndicatorMetadata indicatorMetadata)
        {
            double val = practiceDataAccess.GetGpDeprivationDecileDataValue(grouping, timePeriod, categoryArea);

            return val.Equals(ValueData.NullValue) ?
                null :
                new CoreDataSet { Value = val };
        }
        public void GetDataReturnsNullIfValueUndefined()
        {
            Grouping grouping = new Grouping();
            TimePeriod timePeriod = new TimePeriod();

            var data = CoreDataSetProvider(grouping, timePeriod, ValueData.NullValue)
                .GetData(grouping, timePeriod, null);
            Assert.IsNull(data);
        }
        public void TestAnnually()
        {
            var timePeriod = new TimePeriod
            {
                Year = 2001
            };

            Assert.AreEqual(CkanFrequency.Annually, new CkanFrequency(timePeriod).Frequency);
        }
        public void GetDataReturnsNullIfValueUndefined()
        {
            Grouping grouping = new Grouping();
            TimePeriod timePeriod = new TimePeriod();

            var data = PracticeAggregateCoreDataSetProvider(grouping, timePeriod, ValueData.NullValue)
                .GetData(grouping, timePeriod, IndicatorMetadata());
            Assert.IsNull(data);
        }
        public override IList<CoreDataSet> AddMultipleAreaData(RowLabels rowLabels, Grouping grouping, 
            TimePeriod timePeriod, IndicatorMetadata metadata, Dictionary<string, Area> areaCodeToParentMap)
        {
            var dataList = GroupDataReader.GetCoreDataListForAllCategoryAreasOfCategoryAreaType(
                grouping, timePeriod, _categoryAreaTypeId, AreaCodes.England);

            ProfileDataWriter.AddCategorisedData(Worksheet, rowLabels, dataList, subnationalCategoryIdToCategoryAreaMap);

            return dataList;
        }
        public void GetData()
        {
            double val = 2;
            var grouping = new Grouping();
            var timePeriod = new TimePeriod();

            CoreDataSet data = CoreDataSetProvider(grouping, timePeriod, val)
                .GetData(grouping, timePeriod, null);
            Assert.AreEqual(val, data.Value);
        }
        public void TestMonthly()
        {
            var timePeriod = new TimePeriod
            {
                Year = 2001,
                Month = 1
            };

            Assert.AreEqual(CkanFrequency.Monthly, new CkanFrequency(timePeriod).Frequency);
        }
        public void TestQuarterly()
        {
            var timePeriod = new TimePeriod
            {
                Year = 2001,
                Quarter = 1
            };

            Assert.AreEqual(CkanFrequency.Quarterly, new CkanFrequency(timePeriod).Frequency);
        }
        public void GetData()
        {
            double val = 2;
            Grouping grouping = new Grouping();
            TimePeriod timePeriod = new TimePeriod();

            var data = PracticeAggregateCoreDataSetProvider(grouping, timePeriod, val)
                .GetData(grouping, timePeriod, IndicatorMetadata());
            Assert.AreEqual(val, data.Value);
        }
 protected void InitBuild(Grouping grouping)
 {
     if (grouping != null)
     {
         Grouping = grouping;
         IndicatorMetadata = IndicatorMetadataRepository.Instance.GetIndicatorMetadata(grouping.IndicatorId);
         Period = new DataPointOffsetCalculator(grouping, DataPointOffset, IndicatorMetadata.YearType).TimePeriod;
         Formatter = GetFormatter();
     }
 }
        private ShapeCoreDataSetProvider PracticeAggregateCoreDataSetProvider(Grouping grouping,
            TimePeriod timePeriod, double val)
        {
            var practiceDataAccess = new Mock<PracticeDataAccess>(MockBehavior.Strict);
            practiceDataAccess.Protected();
            practiceDataAccess.Setup(x => x
                .GetPracticeAggregateDataValue(grouping, timePeriod, AreaCode))
                .Returns(val);

            return new ShapeCoreDataSetProvider(area, practiceDataAccess.Object);
        }
        private GpDeprivationDecileCoreDataSetProvider CoreDataSetProvider(Grouping grouping,
            TimePeriod timePeriod, double val)
        {
            var practiceDataAccess = new Mock<PracticeDataAccess>(MockBehavior.Strict);
            practiceDataAccess.Protected();
            practiceDataAccess.Setup(x => x
                .GetGpDeprivationDecileDataValue(grouping, timePeriod, categoryArea))
                .Returns(val);

            return new GpDeprivationDecileCoreDataSetProvider(categoryArea, practiceDataAccess.Object);
        }
 public DataPointOffsetCalculator(Grouping grouping, int dataPointOffset, YearType yearType)
 {
     TimePeriod dataPointPeriod = TimePeriod.GetDataPoint(grouping);
     if (dataPointOffset == 0)
     {
         TimePeriod = TimePeriod.GetDataPoint(grouping);
     }
     else
     {
         SetOffsetTimePeriod(grouping, dataPointOffset, dataPointPeriod, yearType);
     }
 }
        public virtual IList<CoreDataSet> GetChildAreaData(Grouping grouping, IArea parentArea, TimePeriod period)
        {
            var categoryArea = parentArea as CategoryArea;
            if (categoryArea != null)
            {
                return groupDataReader.GetCoreDataListForChildrenOfCategoryArea(
                    grouping, period, categoryArea);
            }

            return parentArea.IsCountry
                ? groupDataReader.GetCoreDataForAllAreasOfType(grouping, period)
                : groupDataReader.GetCoreDataListForChildrenOfArea(grouping, period, parentArea.Code);
        }
        public void TestGetDataReturnsNullIfNoChildPracticeData()
        {
            Area area = Area;
            var timePeriod = new TimePeriod();
            var grouping = new Grouping();

            CcgCoreDataSetProvider provider = new CcgCoreDataSetProvider(area,
                ValidPopulationProvider(),
                EmptyDataListProvider(grouping, area, timePeriod),
                EmptyGroupDataReader(grouping, timePeriod));
            var coreDataSet = provider.GetData(grouping, timePeriod, new IndicatorMetadata());

            Assert.IsNull(coreDataSet);
        }
        public void TestGetDataReturnsNullIfInvalidGroupId()
        {
            var area = Area;
            var timePeriod = new TimePeriod();
            var grouping = new Grouping { GroupId = RuleShouldCcgAverageBeCalculatedForGroup.InvalidId };

            CcgCoreDataSetProvider provider = new CcgCoreDataSetProvider(area,
                ValidPopulationProvider(),
                ValidDataListProvider(grouping, area, timePeriod),
                EmptyGroupDataReader(grouping, timePeriod));
            var coreDataSet = provider.GetData(grouping, timePeriod, new IndicatorMetadata());

            Assert.IsNull(coreDataSet);
        }
        private CoreDataSet CalculateData(Grouping grouping, TimePeriod timePeriod, IndicatorMetadata indicatorMetadata)
        {
            if (RuleShouldCcgAverageBeCalculatedForGroup.Validates(grouping))
            {
                var dataList = coreDataSetListProvider.GetChildAreaData(grouping, Area, timePeriod);

                if (dataList.Any())
                {
                    var population = ccgPopulationProvider.GetPopulation(Area.Code);
                    return new CcgAverageCalculator(dataList, population, indicatorMetadata).Average;
                }
            }
            return null;
        }
 public CkanFrequency(TimePeriod timePeriod)
 {
     if (timePeriod.IsMonthly)
     {
         Frequency = Monthly;
     }
     else if (timePeriod.IsQuarterly)
     {
         Frequency = Quarterly;
     }
     else
     {
         Frequency = Annually;
     }
 } 
        public static string GetTimePeriodString(TimePeriod period, int yearTypeId)
        {
            // If need to format Baseline as well then pass in ad hoc 3 prop object

            if (period.IsQuarterly)
            {
                return FormatQuarter(period, yearTypeId);
            }
            if (period.IsMonthly)
            {
                return FormatMonth(period, yearTypeId);
            }

            return FormatYearly(period, yearTypeId);
        }
        public void TestGetData()
        {
            var categoryArea = CategoryArea.New(CategoryTypeIds.DeprivationDecileCountyAndUA2010, 1);
            var grouping = new Grouping();
            var timePeriod = new TimePeriod();

            var reader = new Mock<GroupDataReader>(MockBehavior.Strict);
            reader.Setup(x => x.GetCoreDataForCategoryArea(grouping, timePeriod, categoryArea))
                .Returns(new CoreDataSet());

            var provider = new CategoryAreaCoreDataSetProvider(categoryArea, reader.Object);

            Assert.IsNotNull(provider.GetData(grouping, timePeriod, null));

            reader.VerifyAll();
        }
        private CoreDataSet GetDataFromDatabase(Grouping grouping, TimePeriod timePeriod, IArea parentArea)
        {
            CoreDataSet benchmarkData;

            var categoryArea = parentArea as ICategoryArea;
            if (categoryArea != null)
            {
                benchmarkData = _groupDataReader.GetCoreDataForCategoryArea(grouping, timePeriod, categoryArea);
            }
            else
            {
                IList<CoreDataSet> data = _groupDataReader.GetCoreData(grouping, timePeriod, parentArea.Code);
                CheckOnlyOneCoreDataSetFound(grouping, parentArea, data);
                benchmarkData = data.FirstOrDefault();
            }
            return benchmarkData;
        }
        public TimePeriodIterator(TimePeriod start, TimePeriod end, YearType yearType)
        {
            this.start = start;
            this.end = end;

            if (start.IsQuarterly)
            {
                InitQuarterlyTimePeriods(yearType);
            }
            else if (start.IsMonthly)
            {
                InitMonthlyTimePeriods();
            }
            else
            {
                InitYearlyTimePeriods();
            }
        }
        public void TestCheckDatabaseForValueBeforeCalculating()
        {
            double valueInDatabase = 2;

            Area area = Area;
            var timePeriod = new TimePeriod();
            var grouping = new Grouping();

            var groupDataReader = new Mock<GroupDataReader>(MockBehavior.Strict);
            groupDataReader.Protected();
            groupDataReader.Setup(x => x
                .GetCoreData(grouping, timePeriod, areaCode))
                .Returns(new List<CoreDataSet> { new CoreDataSet { Value = valueInDatabase } });

            CcgCoreDataSetProvider provider = new CcgCoreDataSetProvider(area, null, null, groupDataReader.Object);
            var coreDataSet = provider.GetData(grouping, timePeriod, null);

            Assert.AreEqual(valueInDatabase, coreDataSet.Value);
        }
        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 InitMonthlyTimePeriods()
        {
            int range = start.YearRange;
            int startMonth = start.Month;
            for (int year = start.Year; year <= end.Year; year++)
            {
                for (int month = startMonth; month <= 12; month++)
                {
                    if (year == end.Year && month > end.Month)
                    {
                        break;
                    }

                    TimePeriod period = new TimePeriod { Year = year, YearRange = range, Month = month };
                    points.Add(period);
                }

                startMonth = 1;
            }
        }