public void SetProperties(CkanPackage unsavedPackage, CkanGroup ckanGroup, ProfileParameters parameters,
     IndicatorMetadata indicatorMetadata, TimeRange timeRange)
 {
     SetMetadataProperties(unsavedPackage, indicatorMetadata);
     SetPackageProperties(unsavedPackage, ckanGroup, parameters);
     SetDateProperties(unsavedPackage, timeRange);
 }
        private static void SetDateProperties(CkanPackage package, TimeRange timeRange)
        {
            package.CoverageStartDate = timeRange.FirstTimePeriod.Year + "-01-01";

            TimePeriod timePeriod = timeRange.LastTimePeriod;
            package.CoverageEndDate = (timePeriod.Year + timePeriod.YearRange - 1) + "-12-31";
            package.Frequency = new List<string> { new CkanFrequency(timePeriod).Frequency };
        }
        public void TestTimeRange()
        {
            var grouping = new Grouping
            {
                YearRange = 3,
                BaselineYear = 2001,
                DataPointYear = 2003
            };

            var timeRange = new TimeRange(grouping);

            // Check first time period
            var firstTimePeriod = timeRange.FirstTimePeriod;
            Assert.AreEqual(2001, firstTimePeriod.Year);
            Assert.AreEqual(3, firstTimePeriod.YearRange);

            // Check last time period
            var lastTimePeriod = timeRange.LastTimePeriod;
            Assert.AreEqual(2003, lastTimePeriod.Year);
            Assert.AreEqual(3, lastTimePeriod.YearRange);
        }
        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);
                    }
                }
            }
        }