public ExecuteCommandPublishFilter(IActivateItems activator, IFilter filter, Catalogue targetCatalogue) : base(activator)
        {
            _filter    = filter;
            _catalogue = targetCatalogue;

            if (filter is ExtractionFilter)
            {
                SetImpossible("Filter is already a master Catalogue filter");
                return;
            }


            if (_catalogue == null)
            {
                SetImpossible("No Catalogue is associated with filter");
                return;
            }

            _allExtractionInformations = _catalogue.GetAllExtractionInformation(ExtractionCategory.Any);

            if (!_allExtractionInformations.Any())
            {
                SetImpossible("Cannot publish filter because Catalogue " + _catalogue + " does not have any ExtractionInformations (extractable columns) we could associate it with");
            }

            string reason;

            if (!FilterImporter.IsProperlyDocumented(filter, out reason))
            {
                SetImpossible("Filter is not properly documented:" + reason);
            }
        }
Beispiel #2
0
        private CohortIdentificationConfiguration CreateCohortIdentificationConfiguration(ExtractionFilter inclusionFilter1)
        {
            //Create the top level configuration object
            var cic = new CohortIdentificationConfiguration(_repos.CatalogueRepository, "Tayside Lung Cancer Cohort");

            //create a UNION container for Inclusion Criteria
            var container = new CohortAggregateContainer(_repos.CatalogueRepository, SetOperation.UNION);

            container.Name = "Inclusion Criteria";
            container.SaveToDatabase();

            cic.RootCohortAggregateContainer_ID = container.ID;
            cic.SaveToDatabase();

            //Create a new cohort set to the 'Inclusion Criteria' based on the filters Catalogue
            var cata = inclusionFilter1.ExtractionInformation.CatalogueItem.Catalogue;
            var ac   = cic.CreateNewEmptyConfigurationForCatalogue(cata, (a, b) => { throw new Exception("Problem encountered with chi column(s)"); }, false);

            container.AddChild(ac, 0);

            //Add the filter to the WHERE logic of the cohort set
            var whereContainer = new AggregateFilterContainer(_repos.CatalogueRepository, FilterContainerOperation.OR);

            ac.Name = "People with " + inclusionFilter1.Name;
            ac.RootFilterContainer_ID = whereContainer.ID;
            cic.EnsureNamingConvention(ac); //this will put cicx at the front and cause implicit SaveToDatabase

            FilterImporter filterImporter = new FilterImporter(new AggregateFilterFactory(_repos.CatalogueRepository), null);
            var            cloneFilter    = filterImporter.ImportFilter(inclusionFilter1, null);

            whereContainer.AddChild(cloneFilter);

            return(cic);
        }
        public void CloneWorks_AllPropertiesMatchIncludingParameters()
        {
            _filter.Description = "fish swim in the sea and make people happy to be";
            _filter.WhereSQL    = "LovelyCoconuts = @coconutCount";

            new ParameterCreator(new AggregateFilterFactory(CatalogueRepository), null, null).CreateAll(_filter, null);
            _filter.SaveToDatabase();

            Assert.IsNull(_filter.ClonedFromExtractionFilter_ID);

            var parameter = _filter.GetAllParameters().Single();

            parameter.ParameterSQL = "Declare @coconutCount int";
            parameter.Comment      = "It's coconut time!";
            parameter.Value        = "3";
            parameter.SaveToDatabase();

            var newMaster = new FilterImporter(new ExtractionFilterFactory(_chiExtractionInformation), null).ImportFilter(_filter, null);

            //we should now be a clone of the master we just created
            Assert.AreEqual(_filter.ClonedFromExtractionFilter_ID, newMaster.ID);
            Assert.IsTrue(newMaster.Description.StartsWith(_filter.Description)); //it adds some addendum stuff onto it
            Assert.AreEqual(_filter.WhereSQL, newMaster.WhereSQL);

            Assert.AreEqual(_filter.GetAllParameters().Single().ParameterName, newMaster.GetAllParameters().Single().ParameterName);
            Assert.AreEqual(_filter.GetAllParameters().Single().ParameterSQL, newMaster.GetAllParameters().Single().ParameterSQL);
            Assert.AreEqual(_filter.GetAllParameters().Single().Value, newMaster.GetAllParameters().Single().Value);
        }
        public void FilterCreated_CopyBecauseExistsAlready()
        {
            //The thing we will be importing
            var master = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            master.Name = "Space Odyssey";

            //An existing IFilter that is in the scope that is being imported into (e.g. a data extract configuration)
            var existing = Mock.Of <IFilter>(f =>
                                             f.Name == "Space Odyssey" &&
                                             f.GetAllParameters() == new ISqlParameter[0]);// has no parameters

            //The factory will return this value
            var constructed = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            //The factory Mock
            var factory = new Mock <IFilterFactory>();

            factory.Setup(m => m.CreateNewFilter("Copy of Space Odyssey")).Returns(constructed);

            //The thing we are testing
            var filterCreator = new FilterImporter(factory.Object, null);

            //The method we are testing
            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, new [] { existing });

            //Did the factory mock get ordered to create a filter called "Copy of Space Odyssey" (because there was already one called "Space Odyssey" in the same scope)
            factory.Verify();
        }
        public void FilterCreated_ParametersRenamingDueToExistingParameterInScopeWithSameName_MasterContainsMasterParameter()
        {
            //The filter we are cloning
            var master = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            master.Name     = "Space Odyssey";
            master.WhereSQL = "@hall = 'active'";

            //The existing parameter declared on the filter we are cloning
            var masterParameter = Mock.Of <ISqlParameter>(p => p.ParameterName == "@hall");

            masterParameter.Comment      = "SomeComment";
            masterParameter.Value        = "500";
            masterParameter.ParameterSQL = "DECLARE @hall AS int";

            //We expect that the filter we are cloning will be asked what it's parameters are once (and we tell them the param above)
            Mock.Get(master).Setup(m => m.GetAllParameters()).Returns(new[] { masterParameter });

            //An existing parameter that is in the scope that is being imported into
            var existingParameter = Mock.Of <ISqlParameter>(x => x.ParameterName == "@hall");

            //The filter to which the above existing parameter belongs
            var existing = Mock.Of <IFilter>(x =>
                                             x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper() &&
                                             x.GetAllParameters() == new[] { existingParameter });

            existing.Name = "Space Odyssey";

            //The return value for our Mock factory
            var constructed          = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());
            var constructedParameter = Mock.Of <ISqlParameter>();

            //The mocked factory
            var factory = new Mock <IFilterFactory>();

            factory.Setup(m => m.CreateNewFilter("Copy of Space Odyssey")).Returns(constructed);
            factory.Setup(m => m.CreateNewParameter(constructed, "DECLARE @hall2 AS int")).Returns(constructedParameter);

            //The thing we are testing
            var filterCreator = new FilterImporter(factory.Object, null);

            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, new[] { existing });

            Assert.AreEqual("@hall2 = 'active'", constructed.WhereSQL);

            //Master filter should have been asked what it's parameters are
            Mock.Get(master).Verify(m => m.GetAllParameters(), Times.Once);

            //Existing filter in the scope should have been asked what it's parameters are
            Mock.Get(existing).Verify();

            //The factory should have been asked to create a filter called "Copy of Space Odyssey" and a parameter "@hall2" (because @hall already exists in the import into scope) with type int because master parameter is type int
            factory.Verify();
        }
Beispiel #6
0
        public void NotPopulated_ParameterNoComment()
        {
            _filter.Description = "fish swim in the sea and make people happy to be";
            _filter.WhereSQL    = "LovelyCoconuts = @coconutCount";
            _filter.SaveToDatabase();
            new ParameterCreator(new AggregateFilterFactory(CatalogueRepository), null, null).CreateAll(_filter, null);

            IFilter importedFilter = new FilterImporter(new ExtractionFilterFactory(_chiExtractionInformation), null).ImportFilter(null, _filter, null);

            Assert.AreEqual("folk", importedFilter.Name);
        }
Beispiel #7
0
        /// <summary>
        /// Makes the provided <paramref name="extractableDataSet"/> extractable in the current <see cref="IExtractionConfiguration"/>.  This
        /// includes selecting it (<see cref="ISelectedDataSets"/>) and replicating any mandatory filters.
        /// </summary>
        /// <param name="extractableDataSet"></param>
        public void AddDatasetToConfiguration(IExtractableDataSet extractableDataSet)
        {
            //it is already part of the configuration
            if (SelectedDataSets.Any(s => s.ExtractableDataSet_ID == extractableDataSet.ID))
            {
                return;
            }

            var dataExportRepo = (IDataExportRepository)Repository;

            var selectedDataSet = new SelectedDataSets(dataExportRepo, this, extractableDataSet, null);

            ExtractionFilter[] mandatoryExtractionFiltersToApplyToDataset = extractableDataSet.Catalogue.GetAllMandatoryFilters();

            //add mandatory filters
            if (mandatoryExtractionFiltersToApplyToDataset.Any())
            {
                //first we need a root container e.g. an AND container
                //add the AND container and set it as the root container for the dataset configuration
                FilterContainer rootFilterContainer = new FilterContainer(dataExportRepo);
                rootFilterContainer.Operation = FilterContainerOperation.AND;
                rootFilterContainer.SaveToDatabase();

                selectedDataSet.RootFilterContainer_ID = rootFilterContainer.ID;
                selectedDataSet.SaveToDatabase();

                var globals  = GlobalExtractionFilterParameters;
                var importer = new FilterImporter(new DeployedExtractionFilterFactory(dataExportRepo), globals);

                var mandatoryFilters = importer.ImportAllFilters(mandatoryExtractionFiltersToApplyToDataset, null);

                foreach (DeployedExtractionFilter filter in mandatoryFilters.Cast <DeployedExtractionFilter>())
                {
                    filter.FilterContainer_ID = rootFilterContainer.ID;
                    filter.SaveToDatabase();
                }
            }

            var legacyColumns = GetAllExtractableColumnsFor(extractableDataSet).Cast <ExtractableColumn>().ToArray();

            //add Core or ProjectSpecific columns
            foreach (var all in extractableDataSet.Catalogue.GetAllExtractionInformation(ExtractionCategory.Any))
            {
                if (all.ExtractionCategory == ExtractionCategory.Core || all.ExtractionCategory == ExtractionCategory.ProjectSpecific)
                {
                    if (legacyColumns.All(l => l.CatalogueExtractionInformation_ID != all.ID))
                    {
                        AddColumnToExtraction(extractableDataSet, all);
                    }
                }
            }
        }
        public void FilterCreated_ParametersWithMasterExplicitTyping()
        {
            //The filter we are cloning
            var master = Mock.Of <IFilter>(x =>
                                           x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper() &&
                                           x.Name == "Space Odyssey" &&
                                           x.WhereSQL == "@hall = 'active'");

            //The existing parameter declared on the filter we are cloning
            var masterParameter = Mock.Of <ISqlParameter>(
                x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper() &&
                x.ParameterName == "@hall" &&
                x.Comment == "SomeComment" &&
                x.Value == "500" &&
                x.ParameterSQL == "DECLARE @hall AS int"
                );

            Mock.Get(master).Setup(m => m.GetAllParameters()).Returns(new[] { masterParameter });
            //We expect that the filter we are cloning will be asked what it's parameters are once (and we tell them the param above)


            //The return values for our Mock factory
            var constructed          = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());
            var constructedParameter = Mock.Of <ISqlParameter>();

            constructedParameter.ParameterSQL = "DECLARE @hall AS int";

            //The mock factory will return the above instances for the new cloned objects
            var factory = Mock.Of <IFilterFactory>(m =>
                                                   m.CreateNewFilter("Space Odyssey") == constructed &&
                                                   m.CreateNewParameter(constructed, "DECLARE @hall AS int") == constructedParameter);

            //The thing we are actually testing
            var filterCreator = new FilterImporter(factory, null);

            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, null);//Import it brah

            //Master filter should have been asked what it's parameters are
            Mock.Get(master).Verify();

            //factory should have been asked to create a new filter called "Space Odyssey" and a parameter with a declaration that matches the master filter SQL (i.e. 'AS int')
            Mock.Get(factory).Verify(m => m.CreateNewFilter("Space Odyssey"), Times.Once);

            //The master filter parameters should have been copied to the child
            Assert.AreEqual(constructedParameter.Comment, masterParameter.Comment);
            Assert.AreEqual(constructedParameter.ParameterSQL, masterParameter.ParameterSQL); //We actually manually set this above because that's the contract with "CreateNewParameter"
            Assert.AreEqual(constructedParameter.Value, masterParameter.Value);
        }
Beispiel #9
0
        public IFilter CreateFilter(IFilterFactory factory, IContainer filterContainer, IFilter[] alreadyExisting)
        {
            var importer  = new FilterImporter(factory, null);
            var newFilter = importer.ImportFilter(filterContainer, _filter, alreadyExisting);

            foreach (SimpleParameterUI parameterUi in parameterUis)
            {
                parameterUi.HandleSettingParameters(newFilter);
            }

            //if there are known good values
            if (ddKnownGoodValues.SelectedItem != null && ddKnownGoodValues.SelectedItem as string != string.Empty)
            {
                newFilter.Name += "_" + ddKnownGoodValues.SelectedItem;
            }


            newFilter.FilterContainer_ID = filterContainer.ID;
            newFilter.SaveToDatabase();

            return(newFilter);
        }
Beispiel #10
0
        public void FilterCreated_ParametersRenamingDueToExistingParameterInScopeWithSameName()
        {
            //The filter we are cloning
            var master = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            master.Name     = "Space Odyssey";
            master.WhereSQL = "@hall = 'active'";

            //An existing parameter that is in the scope that is being imported into
            var existingParameter = Mock.Of <ISqlParameter>(x => x.ParameterName == "@hall");

            //The filter to which the above existing parameter belongs
            var existing = Mock.Of <IFilter>(x =>
                                             x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper() &&
                                             x.GetAllParameters() == new[] { existingParameter });

            existing.Name = "Space Odyssey";

            //The return value for our Mock factory
            var constructed          = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());
            var constructedParameter = Mock.Of <ISqlParameter>();

            //The mocked factory
            var factory = new Mock <IFilterFactory>();

            factory.Setup(m => m.CreateNewFilter("Copy of Space Odyssey")).Returns(constructed);
            factory.Setup(m => m.CreateNewParameter(constructed, "DECLARE @hall2 AS varchar(50);")).Returns(constructedParameter);

            //The thing we are testing
            var filterCreator = new FilterImporter(factory.Object, null);

            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, new [] { existing });

            //Existing filter in the scope should have been asked what it's parameters are
            Mock.Get(existing).Verify(x => x.GetAllParameters(), Times.Once);

            //The factory should have been asked to create a filter called "Copy of Space Odyssey" and a parameter "@hall2" (because @hall already exists in the import into scope)
            factory.Verify();
        }
Beispiel #11
0
        private IFilter Import(IContainer containerToImportOneInto, IFilter filterToImport, ISqlParameter[] globalParameters, IFilter[] otherFiltersInScope)
        {
            //Sometimes filters have some recommended parameter values which the user can pick from (e.g. filter Condition could have parameter value sets for 'Dementia', 'Alzheimers' etc
            var chosenParameterValues = AdvertiseAvailableFilterParameterSetsIfAny(filterToImport as ExtractionFilter);

            FilterImporter importer = null;

            if (containerToImportOneInto is AggregateFilterContainer)
            {
                importer = new FilterImporter(new AggregateFilterFactory((ICatalogueRepository)containerToImportOneInto.Repository), globalParameters);
            }
            else if (containerToImportOneInto is FilterContainer)
            {
                importer =
                    new FilterImporter(new DeployedExtractionFilterFactory((IDataExportRepository)containerToImportOneInto.Repository), globalParameters);
            }
            else
            {
                throw new ArgumentException("Cannot import into IContainer of type " + containerToImportOneInto.GetType().Name, "containerToImportOneInto");
            }

            //if there is a parameter value set then tell the importer to use these parameter values instead of the IFilter's default ones
            if (chosenParameterValues != null)
            {
                importer.AlternateValuesToUseForNewParameters = chosenParameterValues.GetAllParameters();
            }

            //create the filter
            var newFilter = importer.ImportFilter(filterToImport, otherFiltersInScope);

            //if we used custom parameter values we should update the filter name so the user is reminded that the concept of the filter includes both 'Condition' and the value they selected e.g. 'Dementia'
            if (chosenParameterValues != null)
            {
                newFilter.Name += "_" + chosenParameterValues.Name;
                newFilter.SaveToDatabase();
            }

            return(newFilter);
        }
Beispiel #12
0
        public void FilterCreated_Parameters()
        {
            var master = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            master.Name     = "Space Odyssey";
            master.WhereSQL = "@hall = 'active'";

            var constructed          = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());
            var constructedParameter = Mock.Of <ISqlParameter>();

            var factory = new Mock <IFilterFactory>();

            factory.Setup(m => m.CreateNewFilter("Space Odyssey")).Returns(constructed);
            factory.Setup(m => m.CreateNewParameter(constructed, "DECLARE @hall AS varchar(50);")).Returns(constructedParameter);

            var filterCreator = new FilterImporter(factory.Object, null);

            //Returns constructed
            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, null);

            factory.Verify(m => m.CreateNewFilter("Space Odyssey"), Times.Once);
            factory.Verify(m => m.CreateNewParameter(constructed, "DECLARE @hall AS varchar(50);"), Times.Once);
        }
Beispiel #13
0
        private void ImportMandatoryFilters(Catalogue catalogue, AggregateConfiguration configuration, ISqlParameter[] globalParameters)
        {
            var filterImporter = new FilterImporter(new AggregateFilterFactory((ICatalogueRepository)catalogue.Repository), globalParameters);

            //Find any Mandatory Filters
            var mandatoryFilters = catalogue.GetAllMandatoryFilters();

            List <AggregateFilter> createdSoFar = new List <AggregateFilter>();

            if (mandatoryFilters.Any())
            {
                var container = new AggregateFilterContainer((ICatalogueRepository)Repository, FilterContainerOperation.AND);
                configuration.RootFilterContainer_ID = container.ID;
                configuration.SaveToDatabase();

                foreach (ExtractionFilter filter in mandatoryFilters)
                {
                    var newFilter = (AggregateFilter)filterImporter.ImportFilter(filter, createdSoFar.ToArray());

                    container.AddChild(newFilter);
                    createdSoFar.Add(newFilter);
                }
            }
        }
Beispiel #14
0
        public void FilterCreated_NewFilterGetsSameName()
        {
            //Thing we will be cloning
            var master = Mock.Of <IFilter>(x =>
                                           x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper() &&
                                           x.Name == "Space Odyssey");

            //The factory will return this value
            var constructed = Mock.Of <IFilter>(x => x.GetQuerySyntaxHelper() == new MicrosoftQuerySyntaxHelper());

            //The factory Mock
            var factory = new Mock <IFilterFactory>();

            factory.Setup(m => m.CreateNewFilter("Space Odyssey")).Returns(constructed);

            //The thing we are testing
            var filterCreator = new FilterImporter(factory.Object, null);

            //The method we are testing
            filterCreator.ImportFilter(WhenIHaveA <AggregateFilterContainer>(), master, null);

            //Did the factory mock get ordered to create a filter called "Space Odyssey"?
            factory.Verify(f => f.CreateNewFilter(It.IsAny <string>()), Times.Once);
        }
Beispiel #15
0
        public void CloneWithFilters(bool introduceOrphanExtractionInformation)
        {
            if (introduceOrphanExtractionInformation)
            {
                IntroduceOrphan();
            }

            Assert.IsEmpty(_configuration.ReleaseLog);

            var filter = new ExtractionFilter(CatalogueRepository, "FilterByFish", _extractionInformations[0]);

            try
            {
                //setup a filter with a parameter
                filter.WhereSQL = "Fish = @fish";

                new ParameterCreator(new ExtractionFilterFactory(_extractionInformations[0]), null, null).CreateAll(filter, null);
                filter.SaveToDatabase();

                Assert.IsTrue(filter.ExtractionFilterParameters.Count() == 1);

                //create a root container
                var container = new FilterContainer(DataExportRepository);
                _selectedDataSet.RootFilterContainer_ID = container.ID;
                _selectedDataSet.SaveToDatabase();

                //create a deployed filter
                var importer       = new FilterImporter(new DeployedExtractionFilterFactory(DataExportRepository), null);
                var deployedFilter = (DeployedExtractionFilter)importer.ImportFilter(filter, null);
                deployedFilter.FilterContainer_ID = container.ID;
                deployedFilter.Name = "FilterByFishDeployed";
                deployedFilter.SaveToDatabase();

                var param = deployedFilter.ExtractionFilterParameters[0];
                param.Value = "'jormungander'";
                param.SaveToDatabase();

                ExtractDatasetCommand request = new ExtractDatasetCommand(_configuration, new ExtractableDatasetBundle(_extractableDataSet));
                request.GenerateQueryBuilder();
                Assert.AreEqual(
                    CollapseWhitespace(
                        string.Format(
                            @"DECLARE @fish AS varchar(50);
SET @fish='jormungander';
/*The ID of the cohort in [{0}CohortDatabase]..[Cohort]*/
DECLARE @CohortDefinitionID AS int;
SET @CohortDefinitionID=-599;
/*The project number of project {0}ExtractionConfiguration*/
DECLARE @ProjectNumber AS int;
SET @ProjectNumber=1;

SELECT DISTINCT 
[{0}CohortDatabase]..[Cohort].[ReleaseID] AS ReleaseID,
[{0}ScratchArea].[dbo].[TestTable].[Name],
[{0}ScratchArea].[dbo].[TestTable].[DateOfBirth]
FROM 
[{0}ScratchArea].[dbo].[TestTable] INNER JOIN [{0}CohortDatabase]..[Cohort] ON [{0}ScratchArea].[dbo].[TestTable].[PrivateID]=[{0}CohortDatabase]..[Cohort].[PrivateID]

WHERE
(
/*FilterByFishDeployed*/
Fish = @fish
)
AND
[{0}CohortDatabase]..[Cohort].[cohortDefinition_id]=-599
"
                            , TestDatabaseNames.Prefix))
                    , CollapseWhitespace(request.QueryBuilder.SQL));

                ExtractionConfiguration deepClone = _configuration.DeepCloneWithNewIDs();
                Assert.AreEqual(deepClone.Cohort_ID, _configuration.Cohort_ID);
                Assert.AreNotEqual(deepClone.ID, _configuration.ID);
                try
                {
                    ExtractDatasetCommand request2 = new ExtractDatasetCommand(deepClone, new ExtractableDatasetBundle(_extractableDataSet));
                    request2.GenerateQueryBuilder();

                    Assert.AreEqual(request.QueryBuilder.SQL, request2.QueryBuilder.SQL);
                }
                finally
                {
                    deepClone.DeleteInDatabase();
                }
            }
            finally
            {
                filter.DeleteInDatabase();
            }
        }