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;
             }
         }
     }
 }
        public void TestQuarterlyFinancialMultiYearCumulativeQuarter()
        {
            var yearType = new YearType {Id = YearTypeIds.FinancialMultiYearCumulativeQuarter};

            TimePeriodIterator iterator = new TimePeriodIterator(
                new TimePeriod { Year = 2013, YearRange = 2, Quarter = 2 },
                new TimePeriod { Year = 2014, YearRange = 2, Quarter = 6 },
                yearType);

            Assert.AreEqual(13, iterator.TimePeriods.Count);

            AssertTimePeriod(iterator.TimePeriods[0], 2013, 2, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[1], 2013, 3, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[2], 2013, 4, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[3], 2013, 5, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[4], 2013, 6, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[5], 2013, 7, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[6], 2013, 8, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[7], 2014, 1, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[8], 2014, 2, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[9], 2014, 3, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[10], 2014, 4, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[11], 2014, 5, TimePoint.Undefined);
            AssertTimePeriod(iterator.TimePeriods[12], 2014, 6, TimePoint.Undefined);
        }
 private static int GetMaximumNumberOfQuarters(YearType yearType, int range)
 {
     if (yearType.Id == YearTypeIds.FinancialMultiYearCumulativeQuarter)
     {
         return QuartersInOneYear * range;
     }
     return QuartersInOneYear;
 }
 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 List<CkanCoreDataSet> GetData(YearType yearType, Grouping grouping,
            IList<int> sexIds, IList<int> ageIds, IList<int> areaTypeIds,
            IList<int> categoryTypeIds, IList<string> areaCodesToIgnore)
        {
            var timePeriods = new TimePeriodIterator(
                TimePeriod.GetBaseline(grouping),
                TimePeriod.GetDataPoint(grouping), yearType).TimePeriods;

            var allDataList = new List<CkanCoreDataSet>();
            foreach (var sexId in sexIds)
            {
                foreach (var ageId in ageIds)
                {
                    foreach (var timePeriod in timePeriods)
                    {
                        var periodString = TimePeriodFormatter.GetTimePeriodString(timePeriod, yearType.Id);

                        grouping.SexId = sexId;
                        grouping.AgeId = ageId;

                        foreach (var areaTypeId in areaTypeIds)
                        {
                            grouping.AreaTypeId = areaTypeId;
                            var dataList = GroupDataReader
                                .GetCoreDataForAllAreasOfType(grouping, timePeriod);

                            var ckanDataList = FilterAndConvert(areaCodesToIgnore, dataList, periodString);

                            allDataList.AddRange(ckanDataList);
                        }

                        foreach (var categoryTypeId in categoryTypeIds)
                        {
                            var dataList = GroupDataReader.GetCoreDataForCategoryTypeId(
                                grouping, timePeriod, categoryTypeId);

                            var ckanDataList = FilterAndConvert(areaCodesToIgnore, dataList, periodString);

                            allDataList.AddRange(ckanDataList);
                        }
                    }
                }
            }
            return allDataList;
        }
        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 QofListSizeProvider(IGroupDataReader groupDataReader, Area area, int groupId, int dataPointOffset,
            YearType yearType)
        {
            Grouping grouping = groupDataReader.GetGroupingsByGroupIdAndIndicatorId(groupId, IndicatorId);
            var period = new DataPointOffsetCalculator(grouping, dataPointOffset, yearType).TimePeriod;

            if (area.IsCcg)
            {
                //Note: zeroes may occur and should be included
                Value = groupDataReader
                    .GetCoreDataListForChildrenOfArea(grouping, period, area.Code)
                    .Where(x => x.IsValueValid)
                    .Average(x => x.Value);
            }
            else
            {
                SetSingleAreaValue(groupDataReader, area, grouping, period);
            }
        }
        private void InitQuarterlyTimePeriods(YearType yearType)
        {
            int range = start.YearRange;
            var maxQuarter = GetMaximumNumberOfQuarters(yearType, range);
            int startQuarter = start.Quarter;
            for (int year = start.Year; year <= end.Year; year++)
            {
                for (int quarter = startQuarter; quarter <= maxQuarter; quarter++)
                {
                    if (year == end.Year && quarter > end.Quarter)
                    {
                        break;
                    }

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

                startQuarter = 1;
            }
        }
        private IEnumerable<TimePeriod> GetTimePeriods(Grouping grouping, YearType yearType)
        {
            if (LatestDataOnly)
            {
                return new List<TimePeriod> {TimePeriod.GetDataPoint(grouping)};
            }

            TimePeriodIterator timePeriodIterator = grouping.GetTimePeriodIterator(yearType);
            return timePeriodIterator.TimePeriods;
        }
 public TimePeriodIterator GetTimePeriodIterator(YearType yearType)
 {
     return new TimePeriodIterator(TimePeriod.GetBaseline(this),
         TimePeriod.GetDataPoint(this), yearType);
 }