public void SetProperties(CkanPackage unsavedPackage, CkanGroup ckanGroup, ProfileParameters parameters,
     IndicatorMetadata indicatorMetadata, TimeRange timeRange)
 {
     SetMetadataProperties(unsavedPackage, indicatorMetadata);
     SetPackageProperties(unsavedPackage, ckanGroup, parameters);
     SetDateProperties(unsavedPackage, timeRange);
 }
        public void TestNew()
        {
            var areaTypeListProvider = new Mock<IAreaTypeListProvider>();
            areaTypeListProvider.Setup(x => x.GetAllAreaTypeIdsUsedInProfile(ProfileId))
                .Returns(new List<int>{
                    AreaTypeIds.CountyAndUnitaryAuthority,
                    AreaTypeIds.DistrictAndUnitaryAuthority});
            areaTypeListProvider.Setup(x => x.GetCategoryTypeIdsUsedInProfile(ProfileId))
                .Returns(new List<int>{CategoryTypeIds.EthnicGroups5});

            var parameters = new ProfileParameters(areaTypeListProvider.Object, ProfileId,
                GroupName);

            // Assert: properties
            Assert.AreEqual(ProfileId, parameters.ProfileId);
            Assert.AreEqual(GroupName, parameters.CkanGroupName);
            Assert.AreEqual(1, parameters.CategoryTypeIds.Count);

            // Assert: area type ids are split and distinct
            var areaTypeIds = parameters.AreaTypeIds;
            Assert.AreEqual(3, areaTypeIds.Count);
            Assert.IsTrue(areaTypeIds.Contains(AreaTypeIds.District));
            Assert.IsTrue(areaTypeIds.Contains(AreaTypeIds.UnitaryAuthority));
            Assert.IsTrue(areaTypeIds.Contains(AreaTypeIds.County));
        }
 private static void SetPackageProperties(CkanPackage package, 
     CkanGroup group, ProfileParameters parameters)
 {
     string homePage = parameters.ProfileUrl;
     package.Source = homePage;
     package.Homepage = homePage;
     package.LicenseId = parameters.LicenceId;
     string emailAddress = AuthorDetails.GetEmailAddress(parameters.ProfileId);
     package.Author = emailAddress;
     package.AuthorEmail = emailAddress;
     package.Maintainer = emailAddress;
     package.MaintainerEmail = emailAddress;
     package.OwnerOrganization = parameters.OrganisationId;
     package.Resources = new List<CkanResource>();
     package.Groups = new List<CkanGroup> { @group.GetMinimalGroupForSendingToCkan() };
 }
        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);
                    }
                }
            }
        }
        public void TestUpdatePackage()
        {
            var ckan = CkanApi();

            var group = ckan.GetGroup(GroupName).GetMinimalGroupForSendingToCkan();

            // Set up package
            var package = ckan.GetPackage(PackageName);
            var parameters = new ProfileParameters();
            package.Title = PackageTitle;
            package.Source = parameters.ProfileUrl;
            package.Homepage = parameters.ProfileUrl;
            package.Origin = parameters.OrganisationTitle; // source
            package.OwnerOrganization = OrganisationNames.PublicHealthEngland;
            package.Groups.Add(group);
            package.Frequency.Add(CkanFrequency.Annually);
            package.CoverageStartDate = "1999-11-01";
            package.CoverageEndDate = "2014-12-27";
            package.Resources.Clear();

            // Update package
            var updatedPackage = ckan.UpdatePackage(package);

            // Check package
            Assert.IsNotNull(updatedPackage);
            Assert.AreEqual(PackageTitle, updatedPackage.Title);
        }