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 IList<AreaType> GetResponse(IList<int> profileIds)
        {
            var areaTypeListProvider = new AreaTypeListProvider(new GroupIdProvider(profileReader), areasReader, groupDataReader);

            if (profileIds.Any())
            {
                return areaTypeListProvider.GetChildAreaTypesUsedInProfiles(profileIds);
            }
            return areaTypeListProvider.GetAllAreaTypes();
        }
        public void TestGetAreaTypesUsedInProfiles()
        {
            var profileIds = new List<int> { 1, 2 };

            var groupIdProvider = new Mock<GroupIdProvider>();
            groupIdProvider.Setup(x => x.GetGroupIds(1)).Returns(new List<int> { 3, 4 });
            groupIdProvider.Setup(x => x.GetGroupIds(2)).Returns(new List<int> { 5, 6 });

            var areasReader = new Mock<AreasReader>();
            areasReader.Setup(x => x.GetAreaTypes(It.IsAny<IList<int>>())).Returns(new List<AreaType> { new AreaType() });

            var groupDataReader = new Mock<GroupDataReader>();
            var distinctGroupIds = new List<int> { 10, 11 };
            groupDataReader.Setup(x => x.GetDistinctGroupingAreaTypeIds(It.IsAny<IList<int>>())).Returns(distinctGroupIds);

            var areaTypes = new AreaTypeListProvider(groupIdProvider.Object, areasReader.Object, groupDataReader.Object)
                .GetChildAreaTypesUsedInProfiles(profileIds);

            Assert.IsNotNull(areaTypes[0]);
        }
        public void UploadProfile(int profileId)
        {
            // Ensure a group exists
            var profile = profileReader.GetProfile(profileId);
            var ckanGroup = GetCkanGroup(profile);

            // Ckan parameters
            var groupIdProvider = new GroupIdProvider(profileReader);
            var areaTypeListProvider = new AreaTypeListProvider(groupIdProvider,
                areasReader, groupDataReader);
            var parameters = new ProfileParameters(areaTypeListProvider, profileId, ckanGroup.Name);

            var metadataRepo = IndicatorMetadataRepository.Instance;
            var areaCodesToIgnore = profileReader.GetAreaCodesToIgnore(profileId).AreaCodesIgnoredEverywhere;

            var lookUpManager = new LookUpManager(pholioReader, areasReader,
                parameters.AreaTypeIds, parameters.CategoryTypeIds);

            var groupIds = groupIdProvider.GetGroupIds(profileId);
            foreach (var groupId in groupIds)
            {
                var groupings = groupDataReader.GetGroupingsByGroupId(groupId);

                var metadataCollection = metadataRepo.GetIndicatorMetadataCollection(groupings);
                metadataRepo.RemoveSystemContentFields(metadataCollection.IndicatorMetadata);

                foreach (var grouping in groupings)
                {
                    var indicatorId = grouping.IndicatorId;
                    if (indicatorIdsAlreadyUploaded.Contains(indicatorId) == false)
                    {
                        indicatorIdsAlreadyUploaded.Add(indicatorId);

                        var timeRange = new TimeRange(grouping);

                        var metadata = metadataCollection.GetIndicatorMetadataById(indicatorId);

                        Console.WriteLine("#UPLOADING: [{0}] {1} \"{2}\"",
                            indicatorIdsAlreadyUploaded.Count, indicatorId,
                            metadata.Descriptive[IndicatorMetadataTextColumnNames.Name]
                            );

                        // Create/update package
                        var packageIdProvider = new PackageIdProvider(metadata.IndicatorId);
                        var ckanPackage = _ckanPackageRepository.RetrieveOrGetNew(packageIdProvider);
                        CkanGroup group = _ckanGroupRepository.GetExistingGroup(parameters.CkanGroupName);
                        new CkanPackagePropertySetter()
                            .SetProperties(ckanPackage, group, parameters, metadata, timeRange);
                        var savedPackage = _ckanPackageRepository.CreateOrUpdate(ckanPackage);

                        // Create metadata resource
                        var textMetadataResource = new MetadataResourceBuilder()
                            .GetUnsavedResource(savedPackage.Id, metadata, metadataRepo.IndicatorMetadataTextProperties);

                        // Create data resource
                        var allGroupingsForIndicator = groupings
                            .Where(x => x.IndicatorId == indicatorId).ToList();

                        var sexIds = allGroupingsForIndicator
                            .Select(x => x.SexId).Distinct().ToList();

                        var ageIds = groupDataReader.GetAllAgeIdsForIndicator(indicatorId);
                        var allDataList = new CoreDataListBuilder { GroupDataReader = groupDataReader }
                            .GetData(metadata.YearType, grouping, sexIds, ageIds,
                            parameters.AreaTypeIds, parameters.CategoryTypeIds, areaCodesToIgnore);

                        var dataResource = new CoreDataResourceBuilder(lookUpManager)
                            .GetUnsavedResource(savedPackage.Id, metadata, allDataList);

                        // Upload resources
                        new CkanResourceUploader { CkanApi = ckanApi }.AddResourcesToPackage(
                            savedPackage.Id, textMetadataResource, dataResource);
                    }
                }
            }
        }