public IList<TrendRoot> GetTrendData()
        {
            int profileId = _parameters.ProfileId;

            var parentArea = new ParentArea(_parameters.ParentAreaCode, _parameters.AreaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            // Do not repository as do not want results cached like this (need to be
            // cached by ID and areatype, i.e. repository by roots)
            GroupData data = new GroupDataBuilderByIndicatorIds
            {
                IndicatorIds = _parameters.IndicatorIds,
                ComparatorMap = comparatorMap,
                AreaTypeId = _parameters.AreaTypeId,
                RestrictSearchProfileIds = _parameters.RestrictResultsToProfileIdList,
                ProfileId = profileId
            }.Build();

            if (data.IsDataOk)
            {
                var groupDataReader = ReaderFactory.GetGroupDataReader();
                data.GroupRoots = new GroupRootFilter(groupDataReader).RemoveRootsWithoutChildAreaData(data.GroupRoots);
            }

            bool isParentAreaCodeNearestNeighbour = Area.IsNearestNeighbour(_parameters.ParentAreaCode);

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap,
                _parameters.AreaTypeId, profileId, data.IndicatorMetadata, isParentAreaCodeNearestNeighbour);

            return trendRoots;
        }
        public void TestBuild()
        {
            var areaTypeId = AreaTypeIds.CountyAndUnitaryAuthority;
            var profileId = ProfileIds.Phof;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);

            var parentArea = new ParentArea(AreaCodes.Gor_NorthEast, areaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            GroupData data = new GroupDataBuilderByGroupings
            {
                GroupId = profile.GroupIds[0],
                ChildAreaTypeId = areaTypeId,
                ProfileId = profile.Id,
                ComparatorMap = comparatorMap,
                AssignData = true
            }.Build();

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap, areaTypeId, profileId,
                data.IndicatorMetadata, false);
            Assert.IsTrue(trendRoots.Count > 0);
            Assert.IsTrue(trendRoots[0].Periods.Count > 0);
            Assert.IsNotNull(trendRoots[0].Limits);
            Assert.IsTrue(trendRoots[0].ComparatorValueFs.Count > 0);

            string s = trendRoots[0].ComparatorValueFs[0][ComparatorIds.Subnational];
            Assert.AreNotEqual("-", s);
        }
        public IList<GroupRoot> GetGroupRoots()
        {
            int profileId = _parameters.ProfileId;

            var parentArea = new ParentArea(_parameters.ParentAreaCode, _parameters.AreaTypeId);

            // Do not repository as do not want results cached like this (need to be
            // cached by ID and areatype, i.e. repository by roots)
            GroupData data = new GroupDataBuilderByIndicatorIds
            {
                IndicatorIds = _parameters.IndicatorIds,
                ProfileId = profileId,
                RestrictSearchProfileIds = _parameters.RestrictResultsToProfileIdList,
                ComparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap,
                AreaTypeId = _parameters.AreaTypeId
            }.Build();

            new GroupDataProcessor().Process(data);

            if (data.IsDataOk)
            {
                var groupDataReader = ReaderFactory.GetGroupDataReader();
                data.GroupRoots = new GroupRootFilter(groupDataReader).RemoveRootsWithoutChildAreaData(data.GroupRoots);
            }

            return data.GroupRoots ?? new List<GroupRoot>();
        }
 private void AddSubnationalComparator(ParentArea parentArea)
 {
     ComparatorMap.Add(new Comparator
     {
         ComparatorId = ComparatorIds.Subnational,
         ChildAreaTypeId = parentArea.ChildAreaTypeId,
         Area = AreaFactory.NewArea(areasReader, parentArea.AreaCode)
     });
 }
 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;
 }
        public static IWorkbook GetWorkbookForProfile(int profileId)
        {
            var region = ReaderFactory.GetAreasReader().GetAreaFromCode(AreaCodes.Gor_EastOfEngland);
            var parentArea = new ParentArea(region.Code, AreaTypeIds.CountyAndUnitaryAuthority);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = AreaTypeIds.GoRegion;

            return new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook();
        }
        private static void InitWorksheets()
        {
            var region = ReaderFactory.GetAreasReader().GetAreaFromCode(AreaCodes.England);
            var parentArea = new ParentArea(region.Code, AreaTypeIds.StrategicClinicalNetwork);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profileId = ProfileIds.CardioVascularDisease;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = AreaTypeIds.Country;

            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)
                ).BuildWorkbook();
        }
        public GroupData GetGroupData(string parentAreaCode, int childAreaTypeId, int profileId, int groupId)
        {
            var parentArea = new ParentArea(parentAreaCode, childAreaTypeId);

            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            return new GroupDataBuilderByGroupings
                {
                    AssignAreas = AssignAreas,
                    AssignChildAreaData = AssignChildAreaData,
                    GroupId = groupId,
                    ProfileId = profileId,
                    ComparatorMap = comparatorMap,
                    ChildAreaTypeId = childAreaTypeId
                }.Build();
        }
        private static void InitWorksheets()
        {
            // Parameters
            var profileId = ProfileIds.Phof;
            var childAreaType = AreaTypeIds.GoRegion;
            var parentArea = new ParentArea(AreaCodes.England, childAreaType);
            var parentAreaTypeId = AreaTypeIds.Country;

            // Create workbook
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);
            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)
                ).BuildWorkbook();
        }
        private static void InitDeprivationDecilesForAllEnglandWorksheets()
        {
            // Parameters
            var profileId = ProfileIds.Phof;
            var childAreaType = AreaTypeIds.DistrictAndUnitaryAuthority;
            var parentArea = new ParentArea(AreaCodes.England, childAreaType);
            var parentAreaTypeId =
                CategoryAreaType.GetAreaTypeIdFromCategoryTypeId(
                    CategoryTypeIds.DeprivationDecileDistrictAndUA2015);

            // Create workbook
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);
            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            _workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook(); ;
        }
        public IList<TrendRoot> GetTrendData()
        {
            var parentArea = new ParentArea(_parameters.ParentAreaCode, _parameters.AreaTypeId);

            bool isParentCodeNearestNeighbour = Area.IsNearestNeighbour(_parameters.ParentAreaCode);

            var groupData = new GroupDataAtDataPointRepository().GetGroupData(_parameters.ParentAreaCode,
                _parameters.AreaTypeId, _parameters.ProfileId, _parameters.GroupId);

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(
               groupData.GroupRoots,
                new ComparatorMapBuilder(parentArea).ComparatorMap,
                _parameters.AreaTypeId,
                _parameters.ProfileId,
                groupData.IndicatorMetadata, isParentCodeNearestNeighbour);

            return trendRoots;
        }
        private static void InitOneDecileWorksheets()
        {
            var category = new Category
            {
                Id = 5,
                CategoryTypeId = CategoryTypeIds.DeprivationDecileCountyAndUA2010,
                Name = "",
                ShortName = ""
            };
            var categoryArea = CategoryArea.New(category);

            var parentArea = new ParentArea(categoryArea.Code, AreaTypeIds.CountyAndUnitaryAuthority);
            IList<ParentArea> parentAreas = new List<ParentArea>();
            parentAreas.Add(parentArea);

            var map = new ComparatorMapBuilder(parentAreas).ComparatorMap;
            var profileId = ProfileIds.Phof;
            var profile = ReaderFactory.GetProfileReader().GetProfile(profileId);
            var parentAreaTypeId = CategoryAreaType.GetAreaTypeIdFromCategoryTypeId(
                CategoryTypeIds.DeprivationDecileCountyAndUA2010);
            workbook = new ProfileDataBuilder(map, profile, new List<int> { profileId },
                parentAreas, AreaTypeFactory.New(ReaderFactory.GetAreasReader(), parentAreaTypeId)).BuildWorkbook();
        }
        public void TestDiabetesPrevAndRiskHighestQuintileExists()
        {
            var areaTypeId = AreaTypeIds.Ccg;
            var profileId = ProfileIds.Diabetes;

            var parentArea = new ParentArea(AreaCodes.CommissioningRegionLondon, areaTypeId);
            ComparatorMap comparatorMap = new ComparatorMapBuilder(parentArea).ComparatorMap;

            GroupData data = new GroupDataBuilderByGroupings
            {
                GroupId = GroupIds.Diabetes_PrevalenceAndRisk,
                ChildAreaTypeId = areaTypeId,
                ProfileId = ProfileIds.Diabetes,
                ComparatorMap = comparatorMap,
                AssignData = true
            }.Build();

            IList<TrendRoot> trendRoots = new TrendRootBuilder().Build(data.GroupRoots, comparatorMap, areaTypeId, profileId,
                data.IndicatorMetadata, false);

            var highestQuintileCount = trendRoots.Select(x => x.DataPoints.Values.FirstOrDefault()[0].Significance).Count(significances => significances.ContainsValue((Significance)5));

            Assert.AreNotEqual(highestQuintileCount, 0);
        }
        private void BuildCategoryAreaMap(CategoryAreaType categoryAreaType, ParentArea parentArea)
        {
            var categories = _areasReader.GetCategories(categoryAreaType.CategoryTypeId);
            var subnationalCategoryIdToCategoryAreaMap = categories
                .ToDictionary<Category, int, IArea>(
                    category => category.Id,
                    CategoryArea.New
                );

            foreach (
                var categorisedArea in
                    _areasReader.GetCategorisedAreasForAllCategories(AreaTypeIds.Country, parentArea.ChildAreaTypeId,
                        categoryAreaType.CategoryTypeId))
            {
                var area = new Area
                {
                    Code = categorisedArea.CategoryId.ToString(),
                    Name = subnationalCategoryIdToCategoryAreaMap[categorisedArea.CategoryId].Name
                };

                _areaCodeToParentMap.Add(categorisedArea.AreaCode, area);
            }
        }
 public Comparator GetRegionalComparatorByRegion(ParentArea parentArea)
 {
     return comparators.FirstOrDefault(comparator => comparator.ComparatorId == ComparatorIds.Subnational &&
                                                     comparator.Area.Code == parentArea.AreaCode &&
                                                     comparator.ChildAreaTypeId == parentArea.ChildAreaTypeId);
 }
 private static ComparatorMap GetComparatorMap(ParentArea parentArea)
 {
     return new ComparatorMapBuilder(parentArea).ComparatorMap;
 }
        private void WriteCoreData(GroupData data, ParentArea parentArea)
        {
            if (data.IsDataOk)
            {
                var valueNoteLookUp = ValueNotes.ToDictionary(x => x.Id, x => x.Text);

                // Child areas
                var childAreaCodes = data.Areas.Select(x => x.Code).ToArray();
                var childAreaCodeToAreaMap = data.Areas.ToDictionary(area => area.Code);

                // Worksheets
                WorksheetInfo childAreaWorksheet = _profileDataWriter.GetWorksheetInfo(_childAreaTypeSheetName);
                WorksheetInfo subnationalWorksheet = _profileDataWriter.GetWorksheetInfo(_subnationalAreaTypeSheetName);
                WorksheetInfo nationalWorksheet = _profileDataWriter.GetWorksheetInfo(NationalLabel);

                // Subnational areas
                ParentDataWriter parentDataWriter = ParentDataWriterFactory.New(_areasReader, _groupDataReader,
                    subnationalWorksheet, _profileDataWriter, _subnationalAreaType);
                var parentAreaDataProvider = new CoreDataSetProviderFactory().New(_parentArea);

                // No subnational sheet if direct parent area is country
                var isSubnationalSheet = subnationalWorksheet != null;

                var areMultipleSubnationalAreasRequired = AreMultipleSubnationalAreasRequired();

                foreach (var groupRoot in data.GroupRoots)
                {
                    // Check this data has not already been writen
                    var key = new GroupRootUniqueKey(groupRoot).Key + parentArea.AreaCode;
                    if (_groupRootKeys.Contains(key))
                    {
                        // This added has already been written
                        continue;
                    }
                    _groupRootKeys.Add(key);

                    var indicatorMetadata = data.GetIndicatorMetadataById(groupRoot.IndicatorId);

                    // Adding here means order in metadata sheet is same as in data sheet
                    AddMetadata(indicatorMetadata);

                    var timePeriodFormatter = new TimePeriodTextFormatter(indicatorMetadata);
                    var coreDataCollector = new CoreDataCollector();

                    var grouping = groupRoot.Grouping.FirstOrDefault();
                    if (grouping != null)
                    {
                        var sex = _labelReader.LookUpSexLabel(grouping.SexId);
                        var age = _labelReader.LookUpAgeLabel(grouping.AgeId);

                        var timePeriods = grouping.GetTimePeriodIterator(indicatorMetadata.YearType).TimePeriods;

                        // Write core data
                        foreach (TimePeriod timePeriod in timePeriods)
                        {
                            string timeString = timePeriodFormatter.Format(timePeriod);

                            var rowLabels = new RowLabels
                            {
                                Age = age,
                                Sex = sex,
                                TimePeriod = timeString,
                                IndicatorName = indicatorMetadata.Descriptive[IndicatorMetadataTextColumnNames.Name],
                                ValueNoteLookUp = valueNoteLookUp
                            };

                            // Write child area data
                            var coreDataList = _groupDataReader.GetCoreData(grouping, timePeriod, childAreaCodes);
                            _profileDataWriter.AddData(childAreaWorksheet, rowLabels, coreDataList, childAreaCodeToAreaMap, _areaCodeToParentMap);

                            // Subnational data
                            if (isSubnationalSheet)
                            {
                                if (areMultipleSubnationalAreasRequired)
                                {
                                    // Multiple parent areas
                                    var dataList = parentDataWriter.AddMultipleAreaData(rowLabels, grouping, timePeriod,
                                        indicatorMetadata, _areaCodeToParentMap);
                                    coreDataCollector.AddDataList(dataList);
                                }
                                else
                                {
                                    // One parent area
                                    var coreData = parentAreaDataProvider.GetData(grouping, timePeriod, indicatorMetadata);
                                    _profileDataWriter.AddData(subnationalWorksheet, rowLabels, coreData, _parentArea);
                                    coreDataCollector.AddData(coreData);
                                }
                            }

                            // Write national data
                            var nationalData = new CoreDataSetProviderFactory().New(_nationalArea).GetData(grouping, timePeriod, indicatorMetadata);
                            _profileDataWriter.AddData(nationalWorksheet, rowLabels, nationalData, _nationalArea);
                        }

                        // Write trend data
                        var trendMarkerWriter = TrendMarkerWriterFactory.New(_profileDataWriter, groupRoot.PolarityId, timePeriods, _profile.HasTrendMarkers);

                        // Child area trend markers
                        trendMarkerWriter.WriteChildTrendMarkers(childAreaWorksheet, groupRoot.RecentTrends, childAreaCodes);

                        // Subnational trend markers
                        if (isSubnationalSheet)
                        {
                            if (areMultipleSubnationalAreasRequired)
                            {
                                trendMarkerWriter.WriteMultipleSubnationalTrendMarkers(subnationalWorksheet, grouping,
                                    indicatorMetadata, coreDataCollector, parentDataWriter.CategoryIdAndAreaCodes);
                            }
                            else
                            {
                                trendMarkerWriter.WriteSingleSubnationalTrendMarker(subnationalWorksheet, grouping,
                                    indicatorMetadata, coreDataCollector);
                            }
                        }

                        // National trend markers
                        trendMarkerWriter.WriteNationalTrendMarkers(nationalWorksheet, groupRoot.RecentTrends,
                            _nationalArea.Code);
                    }
                }
            }
        }
        private void InitParentArea(ParentArea parentArea)
        {
            Comparator comparator = _comparatorMap.GetRegionalComparatorByRegion(parentArea);

            // Add child area worksheet
            var areaType = _areasReader.GetAreaType(parentArea.ChildAreaTypeId);
            _childAreaTypeSheetName = _sheetNamer.GetSheetName(areaType.ShortName);
            _profileDataWriter.AddSheet(_childAreaTypeSheetName);
            _parentArea = comparator.Area;

            // Add parent area(s) worksheet
            if (_subnationalAreaType.Id != AreaTypeIds.Country)
            {
                _subnationalAreaTypeSheetName = _sheetNamer.GetSheetName(_subnationalAreaType.ShortName);
                _profileDataWriter.AddSheet(_subnationalAreaTypeSheetName);
            }

            // Add England Worksheet
            _profileDataWriter.AddSheet(NationalLabel);

            //Get all possible parent areas for the child area type
            _areaCodeToParentMap = _areasReader.GetParentAreasFromChildAreaId(_subnationalAreaType.Id);
        }
 public ComparatorMapBuilder(ParentArea parentArea) :
     this(new List<ParentArea> { parentArea }) { }