/// <summary> /// Performs a search against the Clinical Trials API /// </summary> /// <param name="searchParams">Search paramesters</param> /// <param name="dynamicFilterParams">Deserialized dynamic search parameters</param> /// <returns>Clinical Trials collection</returns> public ClinicalTrialsCollection Search(CTSSearchParams searchParams, int pageNumber = 0, int itemsPerPage = 10) { int from = GetPageOffset(pageNumber, itemsPerPage); Dictionary <string, object> filterCriteria = searchParams.ToFilterCriteria(); //Get our list of trials from the API client ClinicalTrialsCollection rtnResults = new ClinicalTrialsCollection(); //Fetch results rtnResults = Client.List( searchParams: filterCriteria, size: itemsPerPage, from: from, includeFields: CTSConstants.IncludeFields ); //Remove all the inactive sites from all the trials. foreach (ClinicalTrial trial in rtnResults.Trials) { RemoveNonRecruitingSites(trial); } return(rtnResults); }
/// <summary> /// Gets a selection of trials based on a query /// </summary> /// <param name="query">The Query without paging or include/exclude fields</param> /// <param name="pageNumber">The current page number</param> /// <param name="itemsPerPage">Items per page</param> /// <param name="includeFields">A list of fields to include (DEFAULT: Return all fields)</param> /// <returns></returns> public ClinicalTrialsCollection GetClinicalTrials(JObject query, int pageNumber = 0, int itemsPerPage = 10, string[] includeFields = null) { int from = GetPageOffset(pageNumber, itemsPerPage); ClinicalTrialsCollection rtnResults = new ClinicalTrialsCollection(); //Fetch results rtnResults = Client.List( searchParams: query, size: itemsPerPage, from: from, includeFields: includeFields ); //Remove all the inactive sites from all the trials. foreach (ClinicalTrial trial in rtnResults.Trials) { RemoveNonRecruitingSites(trial); } return(rtnResults); }
/// <summary> /// Gets a collection of trials based on trial ids, batchVal at a time. /// </summary> /// <param name="ids">An array of trial IDs to fetch</param> /// <param name="batchVal">The number of trials to retrieve at a time</param> /// <returns>An enumerable list of ClinicalTrial objects</returns> public IEnumerable <ClinicalTrial> GetMultipleTrials(List <String> ids, int batchVal = 5) { foreach (IEnumerable <string> batch in ids.Batch(batchVal)) { Dictionary <string, object> filterCriteria = new Dictionary <string, object>(); filterCriteria.Add("nci_id", batch.ToArray()); ClinicalTrialsCollection ctColl = new ClinicalTrialsCollection(); ctColl = Client.List( size: 100, //from: 0, searchParams: filterCriteria ); foreach (ClinicalTrial c in ctColl.Trials) { //Remove all the inactive sites from the trial. RemoveNonRecruitingSites(c); yield return(c); } } }
/// <summary> /// Gets a collection of trials based on trial ids, batchVal at a time. /// </summary> /// <param name="ids">An array of trial IDs to fetch</param> /// <param name="batchVal">The number of trials to retrieve at a time</param> /// <returns>An enumerable list of ClinicalTrial objects</returns> public IEnumerable <string> GetActiveTrialIDs(List <String> ids, int batchVal = 50) { foreach (IEnumerable <string> batch in ids.Batch(batchVal)) { Dictionary <string, object> filterCriteria = new Dictionary <string, object>(); filterCriteria.Add("nci_id", batch.ToArray()); filterCriteria.Add("current_trial_status", CTSConstants.ActiveTrialStatuses); ClinicalTrialsCollection ctColl = new ClinicalTrialsCollection(); string[] fieldsToInclude = { "nci_id" }; ctColl = Client.List( size: 100, includeFields: fieldsToInclude, searchParams: filterCriteria ); foreach (ClinicalTrial c in ctColl.Trials) { yield return(c.NCIID); } } }
/// <summary> /// CTSSearchParams -> filterCriterea mapping tests /// </summary> /// <param name="searchParams">An instance of a CTSSearchParams object</param> /// <param name="expectedCriteria">The expected criteria for the search</param> private void MappingTest(CTSSearchParams searchParams, Dictionary <string, object> expectedCriteria) { Dictionary <string, object> actualCriteria = null; //When search gets called trap the criteria and set the actualCriteria var mockClient = GetClientMock( (filterCriteria, size, from, include, exclude) => actualCriteria = filterCriteria, new ClinicalTrialsCollection() { TotalResults = 0, Trials = new ClinicalTrial[] { } } ); //Create a new instance of the factory, passing in the Mock's version of an implementation //of our IClinicalTrialsAPIClient interface. BasicCTSManager manager = new BasicCTSManager(mockClient.Object); //Get the results of parsing the URL ClinicalTrialsCollection returnedCol = manager.Search(searchParams); //Test the actual result to the expected. NOTE: If you add fields to the CTSSearchParams, you need //to also modify the comparer Assert.Equal(expectedCriteria, actualCriteria); }
/// <summary> /// Gets a mock that can be used for a IClinicalTrialsAPIClient /// See https://github.com/moq/moq4 for more details on the mock library. /// (You can do cool thinks like make sure a method was called a certain number of times too...) /// (You can pretend to throw an exception if this are not right...) /// </summary> /// /// <returns>A mock to be used as the service.</returns> private Mock <IClinicalTrialsAPIClient> GetClientMock( Action <Dictionary <string, object>, int, int, string[], string[]> criteriaIntercept, ClinicalTrialsCollection rtnCollection) { Mock <IClinicalTrialsAPIClient> rtnMock = new Mock <IClinicalTrialsAPIClient>(); //Handle the case when a string of C4872 is passed in to GetTitleCase and return the label "Breast Cancer" //This makes it so that we do not have to create a fake class that returns fake data. rtnMock.Setup(client => client.List( It.IsAny <Dictionary <string, object> >(), It.IsAny <int>(), It.IsAny <int>(), It.IsAny <string[]>(), It.IsAny <string[]>())) .Callback(criteriaIntercept) //This should be fleshed out to accept more params .Returns(rtnCollection); return(rtnMock); }
/// <summary> /// Goes and fetches the data from the API & Returns the results to base class to be bound to the template. /// </summary> /// <returns></returns> protected override object GetDataForTemplate() { //TODO: Don't do a search if there are param errors. if (SearchParams.HasInvalidParams() == true) { _results = new ClinicalTrialsCollection(); _results.TotalResults = 0; } else { _results = CTSManager.Search(SearchParams, this.PageNum, this.ItemsPerPage); } //Let's setup some helpful items for the template, so they do not need to be helper functions //The start is either 0 if there are no results, or a 1 based offset based on Page number and items per page. int startItemNumber = _results.TotalResults == 0 ? _results.TotalResults : ((this.PageNum - 1) * this.ItemsPerPage) + 1; //Determine the last item. long lastItemNumber = (this.PageNum * this.ItemsPerPage); if (lastItemNumber > _results.TotalResults) { lastItemNumber = _results.TotalResults; } //Determine the max page int maxPage = (int)Math.Ceiling((double)_results.TotalResults / (double)this.ItemsPerPage); // Add URL filters PageInstruction.AddUrlFilter("CurrentUrl", (name, url) => { //Convert the current search parameters into a NciUrl NciUrl paramsUrl = CTSSearchParamFactory.ConvertParamsToUrl(this.SearchParams); //Add or replace the currentURL params based on the *validated* query params. foreach (KeyValuePair <string, string> qp in paramsUrl.QueryParameters) { if (!url.QueryParameters.ContainsKey(qp.Key)) { url.QueryParameters.Add(qp.Key, qp.Value); } else { url.QueryParameters[qp.Key] = qp.Value; } } url.QueryParameters.Add("ni", this.ItemsPerPage.ToString()); }); //Return the object for binding. return(new { Results = _results, Control = this, Parameters = SearchParams, PageInfo = new { CurrentPage = this.PageNum, MaxPage = maxPage, ItemsPerPage = this.ItemsPerPage, StartItemNumber = startItemNumber, LastItemNumber = lastItemNumber }, TrialTools = new TrialVelocityTools() }); }