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;
        }
        protected override void ReadGroupings(IGroupDataReader groupDataReader)
        {
            var profileIds = RestrictSearchProfileIds != null && RestrictSearchProfileIds.Any() ?
                RestrictSearchProfileIds :
                new List<int> { ProfileId };

            var profileReader = ReaderFactory.GetProfileReader();

            Groupings = new GroupingListProvider(groupDataReader, profileReader)
                .GetGroupings(profileIds, IndicatorIds, AreaTypeId);
        }
        public void TestGetGroupingsUniquifiesGroupings()
        {
            var groupingsFromReader = new List<Grouping>
            {
                new Grouping {IndicatorId = 4},
                new Grouping {IndicatorId = 4}
            };

            var groupings = new GroupingListProvider(GroupDataReader(groupingsFromReader).Object, ProfileReader().Object)
                 .GetGroupings(profileIds, indicatorIds, areaTypeId);

            Assert.AreEqual(1, groupings.Count);
        }
        public Dictionary<int, IndicatorMetadata> GetIndicatorMetadatas()
        {
            IndicatorMetadataRepository indicatorMetadataRepository = IndicatorMetadataRepository.Instance;
            IGroupDataReader groupDataReader = ReaderFactory.GetGroupDataReader();
            IProfileReader profileReader = ReaderFactory.GetProfileReader();

            IList<IndicatorMetadata> indicatorMetadataList;
            if (_parameters.UseIndicatorIds)
            {
                var profileIds = _parameters.RestrictResultsToProfileIds;

                var indicatorIds = _parameters.IndicatorIds;

                if (profileIds.Any())
                {
                    IList<Grouping> groupings = new GroupingListProvider(groupDataReader, profileReader)
                        .GetGroupings(profileIds, indicatorIds);

                    var matchedGroupings = groupings
                        .GroupBy(x => x.IndicatorId)
                        .Select(grp => grp.First());

                    indicatorMetadataList = indicatorMetadataRepository.GetIndicatorMetadata(matchedGroupings.ToList());
                }
                else
                {
                    indicatorMetadataList = indicatorMetadataRepository.GetIndicatorMetadata(indicatorIds);
                }
            }
            else
            {
                IList<Grouping> groupings = groupDataReader.GetGroupingsByGroupIds(_parameters.GroupIds);
                indicatorMetadataList = indicatorMetadataRepository.GetIndicatorMetadata(groupings);
            }

            Dictionary<int, IndicatorMetadata> metadataMap = indicatorMetadataList.ToDictionary(
                indicatorMetadata => indicatorMetadata.IndicatorId);

            if (_parameters.IncludeDefinition == false)
            {
                indicatorMetadataRepository.ReduceDescriptiveMetadata(indicatorMetadataList);
            }

            if (_parameters.IncludeSystemContent == false)
            {
                //Remove the system content fields (This is the default)
                indicatorMetadataRepository.RemoveSystemContentFields(indicatorMetadataList);
            }

            return metadataMap;
        }
        public void TestGetGroupings()
        {
            var groupDataReader = GroupDataReader(new List<Grouping>());
            var profileReader = ProfileReader();

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

            // Only 1 grouping ensures Groupings have been uniquified
            Assert.IsNotNull(groupings);

            groupDataReader.Verify(x => x.GetGroupingsByGroupIdsAndIndicatorIdsAndAreaType(
                groupIds, indicatorIds, areaTypeId));

            profileReader.Verify((x => x.GetGroupIdsFromSpecificProfiles(profileIds)));
        }
        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);
        }