public IDictionary<int, List<int>> GetAreaTypeIdToIndicatorIdsWithData()
        {
            var indicatorIds = new IndicatorSearch().SearchIndicators(_parameters.SearchText);
            var profileIds = _parameters.RestrictResultsToProfileIdList;

            var areaTypes = new AreaTypeListProvider(new GroupIdProvider(profileReader), areasReader, groupDataReader)
                .GetChildAreaTypesUsedInProfiles(profileIds);

            IDictionary<int, List<int>> areaTypeIdToIndicatorIdsWithData = new Dictionary<int, List<int>>();
            foreach (var areaType in areaTypes)
            {
                List<int> indicatorIdsWithData = new List<int>();
                int areaTypeId = areaType.Id;

                var groupings = new GroupingListProvider(groupDataReader, profileReader)
                    .GetGroupings(profileIds, indicatorIds, areaTypeId);

                var groupRoots = new GroupRootBuilder().BuildGroupRoots(groupings);

                foreach (var groupRoot in groupRoots)
                {
                    var grouping = groupRoot.FirstGrouping;
                    var count = groupDataReader.GetCoreDataCountAtDataPoint(grouping);
                    if (count > 0)
                    {
                        indicatorIdsWithData.Add(grouping.IndicatorId);
                    }
                }

                areaTypeIdToIndicatorIdsWithData.Add(areaTypeId, indicatorIdsWithData);
            }

            return areaTypeIdToIndicatorIdsWithData;
        }
        public void TestGroupingsAnnualAndMonthlyGroupingsGiveTwoGroupRoots()
        {
            List<Grouping> grouping = new List<Grouping>();

            grouping.Add(new Grouping
            {
                IndicatorId = 1,
                SexId = 1,
                BaselineYear = 2001,
                BaselineQuarter = -1,
                DataPointYear = 2001,
                DataPointQuarter = -1
            });

            grouping.Add(new Grouping
            {
                IndicatorId = 1,
                SexId = 1,
                BaselineYear = 2001,
                BaselineQuarter = -1,
                BaselineMonth = 1,
                DataPointYear = 2001,
                DataPointQuarter = -1,
                DataPointMonth = 1
            });

            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            Assert.AreEqual(2, roots.Count);
        }
        public void TestAgeIdAssignedFromGrouping()
        {
            var ageId = 200;
            List<Grouping> grouping = new List<Grouping>();
            grouping.Add(new Grouping { AgeId = ageId });

            var root = new GroupRootBuilder().BuildGroupRoots(grouping).First();

            Assert.AreEqual(ageId, root.AgeId);
        }
        public void TestAgeLabelSetCorrectly()
        {
            List<Grouping> grouping = new List<Grouping>();

            grouping.Add(new Grouping { IndicatorId = 1, SexId = 1 });
            grouping.Add(new Grouping { IndicatorId = 2, SexId = 1, AgeId = AgeIds.AllAges });
            grouping.Add(new Grouping { IndicatorId = 2, SexId = 1, AgeId = AgeIds.From0To4 });

            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            Assert.AreEqual(3, roots.Count);
            Assert.IsFalse(roots[0].StateAge);
            Assert.IsTrue(roots[1].StateAge);
            Assert.IsTrue(roots[2].StateAge);
        }
        public IList<GroupRootSummary> Build(int profileId, int areaTypeId)
        {
            IndicatorMetadataRepository metadataRepo = IndicatorMetadataRepository.Instance;
            IList<int> groupIds = ReaderFactory.GetGroupDataReader().GetGroupingIds(profileId);

            IList<GroupRootSummary> groupRootSummaries = new List<GroupRootSummary>();
            foreach (int groupId in groupIds)
            {
                IList<Grouping> groupings =
                    ReaderFactory.GetGroupDataReader()
                        .GetGroupingsByGroupIdAndAreaTypeIdOrderedBySequence(groupId, areaTypeId);
                GroupRootBuilder rootBuilder = new GroupRootBuilder();
                IList<GroupRoot> roots = rootBuilder.BuildGroupRoots(groupings);

                IndicatorMetadataCollection metadataCollection = metadataRepo.GetIndicatorMetadataCollection(groupings);
                foreach (var groupRoot in roots)
                {
                    IndicatorMetadata indicatorMetadata =
                        metadataCollection.GetIndicatorMetadataById(groupRoot.IndicatorId);

                    var duplicate = groupRootSummaries.FirstOrDefault(
                        x =>
                            x.Age.Id == groupRoot.AgeId &&
                            x.Sex.Id == groupRoot.SexId &&
                            x.IndicatorId == groupRoot.IndicatorId);

                    if (duplicate == null)
                    {
                        groupRootSummaries.Add(
                            new GroupRootSummary
                            {
                                GroupId = groupRoot.FirstGrouping.GroupId,
                                Age = groupRoot.Age,
                                Sex = groupRoot.Sex,
                                IndicatorId = groupRoot.IndicatorId,
                                IndicatorName = indicatorMetadata.Descriptive[IndicatorMetadataTextColumnNames.Name],
                                StateSex = groupRoot.StateSex,
                                StateAge = groupRoot.StateAge,
                                IndicatorUnit = indicatorMetadata.Unit
                            });
                    }
                }
            }
            return groupRootSummaries;
        }
        public void TestTwoGroupings()
        {
            List<Grouping> grouping = new List<Grouping>();

            grouping.Add(new Grouping { IndicatorId = 1, SexId = 1 });
            grouping.Add(new Grouping { IndicatorId = 2, SexId = 1 });

            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            Assert.AreEqual(2, roots.Count);
        }
        public void TestStateSexSetCorrectly()
        {
            List<Grouping> grouping = new List<Grouping>();

            grouping.Add(new Grouping { IndicatorId = 1, SexId = 1 });
            grouping.Add(new Grouping { IndicatorId = 2, SexId = 1 });
            grouping.Add(new Grouping { IndicatorId = 2, SexId = 4 });

            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            Assert.AreEqual(3, roots.Count);
            Assert.IsFalse(roots[0].StateSex);
            Assert.IsTrue(roots[1].StateSex);
            Assert.IsTrue(roots[2].StateSex);
        }
        public void TestSexIdAssignedFromGrouping()
        {
            List<Grouping> grouping = new List<Grouping>();
            grouping.Add(new Grouping { SexId = SexIds.Male });
            grouping.Add(new Grouping { SexId = SexIds.Female });

            var roots = new GroupRootBuilder().BuildGroupRoots(grouping);

            Assert.AreEqual(SexIds.Male, roots[0].SexId);
            Assert.AreEqual(SexIds.Female, roots[1].SexId);
        }
        public void TestPolarityAssignedFromGrouping()
        {
            var polarity = PolarityIds.BlueOrangeBlue;
            List<Grouping> grouping = new List<Grouping>();
            grouping.Add(new Grouping { PolarityId = polarity });

            var root = new GroupRootBuilder().BuildGroupRoots(grouping).First();

            Assert.AreEqual(polarity, root.PolarityId);
        }
        public void TestOrderOfRootsIsSameAsGroupings()
        {
            List<Grouping> grouping = new List<Grouping>();

            grouping.Add(new Grouping { IndicatorId = 43, Sequence = 1 });
            grouping.Add(new Grouping { IndicatorId = 2, Sequence = 2 });
            grouping.Add(new Grouping { IndicatorId = 9, Sequence = 3 });

            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            // Do not want any reordering, e.g. by indicator ID
            Assert.AreEqual(43, roots[0].IndicatorId);
            Assert.AreEqual(2, roots[1].IndicatorId);
            Assert.AreEqual(9, roots[2].IndicatorId);
        }
        public void TestNoGroupingsSomeIndicatorIds()
        {
            List<Grouping> grouping = new List<Grouping>();
            GroupRootBuilder builder = new GroupRootBuilder();
            IList<GroupRoot> roots = builder.BuildGroupRoots(grouping);

            Assert.AreEqual(0, roots.Count);
        }
 private void BuildGroupRoots()
 {
     var rootBuilder = new GroupRootBuilder();
     GroupData.GroupRoots = rootBuilder.BuildGroupRoots(Groupings);
 }
        private IList<IndicatorMetadata> AddTopicData(Area area, IEnumerable<Area> parentAreas)
        {
            IndicatorMetadataCollection allMetadata = new IndicatorMetadataCollection();

            foreach (var groupId in GroupIds)
            {
                IList<Grouping> groupings = groupDataReader.GetGroupingsByGroupId(groupId);

                IList<GroupRoot> roots = new GroupRootBuilder().BuildGroupRoots(groupings);

                IndicatorMetadataCollection metadataCollection =
                    IndicatorMetadataRepository.Instance.GetIndicatorMetadataCollection(groupings);

                allMetadata.AddIndicatorMetadata(metadataCollection.IndicatorMetadata);

                foreach (GroupRoot root in roots)
                {
                    Grouping grouping = root.FirstGrouping;

                    // Add indicator information
                    IndicatorMetadata metadata = metadataCollection.GetIndicatorMetadataById(grouping.IndicatorId);
                    IList<TimePeriod> periods = grouping.GetTimePeriodIterator(metadata.YearType).TimePeriods;
                    writer.AddPracticeIndicatorTitles(metadata, periods);
                    writer.AddCcgIndicatorTitles(metadata, periods);

                    foreach (TimePeriod timePeriod in periods)
                    {
                        // Add data
                        writer.AddPracticeIndicatorData(GetPracticeDataMap(area, grouping, timePeriod));
                        writer.AddAreaIndicatorData(GetParentAreaDataMap(grouping, timePeriod, parentAreas, metadata));
                    }
                }
            }
            return allMetadata.IndicatorMetadata;
        }
        public Dictionary<string, IList<SimpleAreaData>> Build()
        {
            IndicatorMetadataRepository indicatorMetadataRepository = IndicatorMetadataRepository.Instance;
            IList<Grouping> groupings = groupDataReader.GetGroupingsByGroupIdAndAreaTypeIdOrderedBySequence(GroupId, AreaTypeId);
            GroupRootBuilder rootBuilder = new GroupRootBuilder();
            IList<GroupRoot> roots = rootBuilder.BuildGroupRoots(groupings);

            CoreDataSetProviderFactory coreDataSetProviderFactory = new CoreDataSetProviderFactory();

            Dictionary<string, IList<SimpleAreaData>> responseObjects = new Dictionary<string, IList<SimpleAreaData>>();

            foreach (IArea area in Areas)
            {
                List<SimpleAreaData> dataObjects = new List<SimpleAreaData>();
                responseObjects.Add(area.Code, dataObjects);

                var isAreaCcg = area.IsCcg;

                bool isAreaAggregate = isAreaCcg || area.IsGpDeprivationDecile || area.IsShape;

                CoreDataSetProvider coreDataSetProvider = coreDataSetProviderFactory.New(area);

                var indicatorComparerFactory =
                    new IndicatorComparerFactory { PholioReader = pholioReader };
                foreach (GroupRoot root in roots)
                {
                    Grouping grouping = root.FirstGrouping;

                    IndicatorComparer comparer = indicatorComparerFactory.New(grouping);
                    IndicatorMetadata metadata = indicatorMetadataRepository.GetIndicatorMetadata(grouping.IndicatorId);

                    var formatter = NumericFormatterFactory.New(metadata, groupDataReader);
                    var dataProcessor = new ValueWithCIsDataProcessor(formatter);

                    List<ValueData> dataList = new List<ValueData>();

                    ITimePeriodTextListBuilder timePeriodTextListBuilder =
                        TimePeriodTextListBuilderFactory.New(IncludeTimePeriods, metadata);

                    Dictionary<string, List<int?>> significanceHash = null;
                    if (isAreaAggregate == false || isAreaCcg)
                    {
                        significanceHash = GetSignificanceHash(ComparatorAreaCodes);
                    }

                    var timePeriods = GetTimePeriods(grouping, metadata.YearType);
                    foreach (TimePeriod timePeriod in timePeriods)
                    {
                        timePeriodTextListBuilder.Add(timePeriod);

                        CoreDataSet areaData = coreDataSetProvider.GetData(grouping, timePeriod, metadata);

                        if (areaData != null)
                        {
                            ValueWithCIsData data = areaData.GetValueWithCIsData();
                            dataProcessor.FormatAndTruncate(data);

                            if (isAreaAggregate && isAreaCcg == false)
                            {
                                dataList.Add(data.GetValueData());
                            }
                            else
                            {
                                dataList.Add(data);

                                foreach (var comparatorAreaCode in ComparatorAreaCodes)
                                {
                                    CoreDataSet comparatorData =
                                        groupDataReader.GetCoreData(grouping, timePeriod, comparatorAreaCode)
                                            .FirstOrDefault();
                                    try
                                    {
                                        int significance;
                                        if (comparer is ICategoryComparer)
                                        {
                                            var d = new ChildAreaValuesBuilder(indicatorComparerFactory,
                                                groupDataReader, areasReader, profileReader)
                                            {
                                                ParentAreaCode = comparatorAreaCode,
                                                AreaTypeId = AreaTypeId,
                                                ComparatorId = grouping.ComparatorId
                                            }.Build(grouping);
                                            var coreData = d.First(x => x.AreaCode.Equals(area.Code));
                                            significance = coreData.Significance.Values.First();
                                        }
                                        else
                                        {
                                            significance =
                                                (int)comparer.Compare(areaData, comparatorData, metadata);
                                        }

                                        var significanceList = significanceHash[comparatorAreaCode];
                                        significanceList.Add(significance);
                                    }
                                    catch (Exception ex)
                                    {
                                        ExceptionLog.LogException(ex, string.Empty);
                                    }
                                }
                            }
                        }
                        else
                        {
                            // Placeholders for missing data
                            dataList.Add(null);
                            if (significanceHash != null)
                            {
                                foreach (var comparatorAreaCode in ComparatorAreaCodes)
                                {
                                    significanceHash[comparatorAreaCode].Add(null);
                                }
                            }
                        }
                    }

                    SimpleAreaData dataObject;
                    if (IncludeTimePeriods)
                    {
                        // Only attach metadata when requested (hybrid of GroupRoot & CoreDataSet)
                        //TODO this is difficult to keep aligned with Grouping class
                        dataObject = new FullAreaData
                        {
                            IndicatorId = grouping.IndicatorId,
                            Significances = significanceHash,
                            Data = dataList,
                            StateSex = root.StateSex,
                            Sex = grouping.Sex,
                            Age = grouping.Age,
                            ComparatorConfidence = grouping.ComparatorConfidence,
                            ComparatorMethodId = grouping.ComparatorMethodId,
                            Periods = timePeriodTextListBuilder.GetTimePeriodStrings()
                        };
                    }
                    else
                    {
                        dataObject = new SimpleAreaData
                        {
                            IndicatorId = grouping.IndicatorId,
                            Significances = significanceHash,
                            Data = dataList
                        };
                    }
                    dataObjects.Add(dataObject);
                }
            }

            return responseObjects;
        }
        private IEnumerable<GroupRoot> GetRoots()
        {
            IGroupDataReader reader = ReaderFactory.GetGroupDataReader();
            IList<Grouping> groupings;
            if (_parameters.UseIndicatorIds)
            {
                groupings = new GroupingListProvider(reader, profileReader).GetGroupings(
                    profileIds,
                    _parameters.IndicatorIds,
                    _parameters.ChildAreaTypeId);

                var roots = new GroupRootBuilder().BuildGroupRoots(groupings);
                return new GroupRootFilter(reader).RemoveRootsWithoutChildAreaData(roots);
            }

            groupings = reader.GetGroupingsByGroupIdAndAreaTypeIdOrderedBySequence(_parameters.GroupId, _parameters.ChildAreaTypeId);
            return new GroupRootBuilder().BuildGroupRoots(groupings);
        }