public IList<TrendRoot> Build(IList<GroupRoot> groupRoots, ComparatorMap comparatorMap,
            int childAreaTypeId, int profileId, IList<IndicatorMetadata> indicatorMetadataList, bool isNearestNeighbour)
        {
            _isParentAreaNearestNeighbour = isNearestNeighbour;

            List<TrendRoot> trendRoots = new List<TrendRoot>();
            ITrendDataReader trendReader = ReaderFactory.GetTrendDataReader();

            var childAreaCodes = GetChildAreaCodes(comparatorMap, childAreaTypeId, profileId);

            if (groupRoots != null)
            {
                var metadataCollection = new IndicatorMetadataCollection(indicatorMetadataList);
                foreach (GroupRoot root in groupRoots)
                {
                    Grouping grouping = root.Grouping.FirstOrDefault();

                    if (grouping != null)
                    {
                        IndicatorMetadata indicatorMetadata = metadataCollection.GetIndicatorMetadataById(root.IndicatorId);
                        GroupRootTrendBuilderBase builder = GroupRootTrendBuilderBase.New(grouping, indicatorMetadata);
                        TrendRoot trendRoot = builder.BuildTrendRoot(comparatorMap, root, trendReader, childAreaCodes);
                        trendRoots.Add(trendRoot);
                    }
                }
            }

            return trendRoots;
        }
        public Limits GetLimits(IList<string> childAreaCodes, Grouping grouping, ComparatorMap comparatorMap)
        {
            areaCodes = new List<string>();

            AddRegionAndChildAreas(childAreaCodes, comparatorMap);
            AddNationalArea(comparatorMap);

            return GetLimitsOfSpecifiedAreas(grouping);
        }
        public ComparatorMapBuilder(IList<ParentArea> parentAreas)
        {
            ComparatorMap = new ComparatorMap();

            foreach (var parentArea in parentAreas)
            {
                AddNationalComparator(parentArea.ChildAreaTypeId);
                AddSubnationalComparator(parentArea);
            }
        }
 private void AddNationalArea(ComparatorMap comparatorMap)
 {
     if (ExcludeComparators == false)
     {
         Comparator nationalComparator = comparatorMap.GetNationalComparator();
         if (nationalComparator != null)
         {
             areaCodes.Add(nationalComparator.Area.Code);
         }
     }
 }
 public ComparatorMap LimitByParentArea(ParentArea parentArea)
 {
     ComparatorMap map = new ComparatorMap();
     IEnumerable<Comparator> result = comparators.Where(c => c.ChildAreaTypeId == parentArea.ChildAreaTypeId &&
         c.Area.Code == parentArea.AreaCode);
     foreach (var comparator in result)
     {
         map.Add(comparator);
     }
     return map;
 }
        private void AddRegionAndChildAreas(IList<string> childAreaCodes, ComparatorMap comparatorMap)
        {
            Comparator regionalComparator = comparatorMap.GetSubnationalComparator();
            if (regionalComparator != null)
            {
                areaCodes.AddRange(childAreaCodes);

                if (ExcludeComparators == false)
                {
                    areaCodes.Add(regionalComparator.Area.Code);
                }
            }
        }
        public ProfileDataBuilder(ComparatorMap comparatorMap, Profile profile, List<int> restrictSearchProfileIds,
            IList<ParentArea> parentAreas, IAreaType subnationalAreaType)
        {
            this._comparatorMap = comparatorMap;
            this._profile = profile;
            this._restrictSearchProfileIds = restrictSearchProfileIds;
            this._parentAreas = parentAreas;

            _profileDataWriter = new ProfileDataWriter(profile);

            _profileDataWriter.AddOrganisationDetails(profile.Id);

            _nationalArea = comparatorMap.GetNationalComparator().Area;

            this._subnationalAreaType = subnationalAreaType;
        }
        private IList<string> GetChildAreaCodes(ComparatorMap comparatorMap, int childAreaTypeId, int profileId)
        {
            var parentAreaCode = comparatorMap.GetSubnationalComparator().Area.Code;
            IList<string> childAreaCodes = new ChildAreaListBuilder(areasReader, parentAreaCode,childAreaTypeId)
                .ChildAreas
                .Select(x => x.Code)
                .ToList();

            var filter = IgnoredAreasFilterFactory.New(profileId);
            childAreaCodes = filter.RemoveAreaCodesIgnoredEverywhere(childAreaCodes).ToList();

            // Add parent areacode to child areas for nearest neighbours only
            if (_isParentAreaNearestNeighbour)
            {
                childAreaCodes.Insert(0, parentAreaCode.Substring(5));
            }
            return childAreaCodes;
        }
 public void TestCannotAddNullComparator()
 {
     ComparatorMap map = new ComparatorMap();
     map.Add(null);
     Assert.AreEqual(0, map.Count);
 }
 public ProfileDataBuilder(ComparatorMap comparatorMap, Profile profile, List<int> restrictSearchProfileIds,
     IList<int> indicatorIds, IList<ParentArea> parentAreas, IAreaType subnationalAreaType)
     : this(comparatorMap, profile, restrictSearchProfileIds, parentAreas, subnationalAreaType)
 {
     this._indicatorIds = indicatorIds;
 }
        private Limits GetLimits(Grouping grouping,  bool excludeComparators)
        {
            var areaCodes = new List<string>
            {
                AreaCodes.CountyUa_Cambridgeshire,
                AreaCodes.CountyUa_Bedford
            };

            var comparatorMap = new ComparatorMap();
            comparatorMap.Add(new Comparator
            {
                Area = AreaFactory.NewArea(areasReader, AreaCodes.Gor_EastOfEngland),
                ChildAreaTypeId = AreaTypeIds.CountyAndUnitaryAuthority,
                ComparatorId = ComparatorIds.Subnational
            });
            comparatorMap.Add(new Comparator
            {
                Area = AreaFactory.NewArea(areasReader, AreaCodes.England),
                ChildAreaTypeId = AreaTypeIds.CountyAndUnitaryAuthority,
                ComparatorId = ComparatorIds.England
            });

            var limitsBuilder = new LimitsBuilder()
            {
                ExcludeComparators = excludeComparators
            };
            return limitsBuilder.GetLimits(areaCodes, grouping, comparatorMap);
        }
        public TrendRoot BuildTrendRoot(ComparatorMap comparatorMap, GroupRoot root,
            ITrendDataReader trendReader, IList<string> childAreaCodes)
        {

            Init();

            TrendRoot trendRoot = new TrendRoot(root);

            periods = Grouping.GetTimePeriodIterator(IndicatorMetadata.YearType).TimePeriods;

            var formatter = NumericFormatterFactory.New(IndicatorMetadata, groupDataReader);
            dataProcessor = new CoreDataProcessor(formatter);

            // Get comparator trend data
            foreach (var comparator in comparatorMap.Comparators)
            {
                CategoryArea categoryArea = comparator.Area as CategoryArea;
                if (categoryArea != null)
                {
                    var categoryTypeId = categoryArea.CategoryTypeId;
                    //TODO: we we don't have data for trends calculate on the fly
                    var categoryAreaDataList = trendReader.GetTrendDataForSpecificCategory(Grouping,
                        AreaCodes.England, categoryTypeId, categoryArea.CategoryId);
                    comparatorIdToComparatorTrendData.Add(comparator.ComparatorId, categoryAreaDataList);
                }
                else
                {
                    var comparatorAreaDataList = trendReader.GetTrendData(Grouping, comparator.Area.Code);
                    comparatorIdToComparatorTrendData.Add(comparator.ComparatorId, comparatorAreaDataList);
                }
            }

            bool hasData = false;

            foreach (string areaCode in childAreaCodes)
            {
                dataList = trendReader.GetTrendData(Grouping, areaCode);

                // Do not include areas without data
                if (dataList.Count == 0)
                {
                    bool isData = comparatorIdToComparatorTrendData
                        .Any(keyValuePair => keyValuePair.Value.Count > 0);
                    if (isData == false)
                    {
                        continue;
                    }
                }
                hasData = true;

                // Create trend data points
                IList<TrendDataPoint> trendDataPoints = new List<TrendDataPoint>();
                foreach (var timePeriod in periods)
                {

                    var dataPoint = GetDataAtSpecificTimePeriod(dataList, timePeriod)
                                    ?? CoreDataSet.GetNullObject(areaCode);

                    var significances = AssignSignificanceToTrendDataPoint(dataPoint, Grouping, timePeriod);

                    // Need to assess count before data is truncated
                    var isCountValid = dataPoint.IsCountValid;

                    dataProcessor.FormatAndTruncate(dataPoint);
                    var trendDataPoint = new TrendDataPoint(dataPoint)
                    {
                        Significance = significances,
                        IsCountValid = isCountValid
                    };
                    trendDataPoints.Add(trendDataPoint);
                }
                trendRoot.DataPoints[areaCode] = trendDataPoints;
            }

            trendRoot.RecentTrends = root.RecentTrends;

            AssignPeriods(trendRoot);

            if (hasData)
            {
                AssignComparatorDataToTrendRoot(trendRoot, root.FirstGrouping, childAreaCodes);

                // Assign limits
                var limitBuilder = new LimitsBuilder()
                {
                    ExcludeComparators = IndicatorMetadata.ValueTypeId == ValueTypeIds.Count
                };
                trendRoot.Limits = limitBuilder.GetLimits(childAreaCodes, Grouping, comparatorMap);
            }

            return trendRoot;
        }