private SearchSummary ExecuteSearchCounts(OpenFdaApiEndPoints endPointType,
                                                  FdaFilterTypes filterType,
                                                  List <string> filterList,
                                                  int maxresultsize,
                                                  string state,
                                                  string cntField)
        {
            var apiUrl = string.Empty;
            var tmpRecallResultList = new List <ResultRecall>();

            var searchSummary = new SearchSummary()
            {
                Keyword = filterList[0],
                State   = state
            };
            var searchResults = "";

            //Limit first query to a 1 year window
            var beginDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddDays(1));
            var endDate   = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-1));

            _fda.ResetSearch();
            _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, new List <string>(new[]
            {
                state
            }), FilterCompairType.And);
            _fda.AddSearchFilter(endPointType, filterType, filterList, FilterCompairType.And);
            _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List <string>(new[]
            {
                beginDate,
                endDate
            }), FilterCompairType.And);

            _fda.AddCountField(string.Format("{0}.exact", cntField.ToLower()));
            apiUrl = _fda.BuildUrl(endPointType);

            searchResults = _fda.Execute(apiUrl);

            // If there was not data in the 1 yr window the get all results.
            // Check a 2 yr window for results.
            if (string.IsNullOrEmpty(searchResults))
            {
                endDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-2));

                _fda.ResetSearch();
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, new List <string>(new[]
                {
                    state
                }), FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, filterType, filterList, FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List <string>(new[]
                {
                    beginDate,
                    endDate
                }), FilterCompairType.And);

                _fda.AddCountField(string.Format("{0}.exact", cntField.ToLower()));
                apiUrl = _fda.BuildUrl(endPointType);

                searchResults = _fda.Execute(apiUrl);
            }

            // If there was not data in the 2 yr window the get all results.
            if (string.IsNullOrEmpty(searchResults))
            {
                _fda.ResetSearch();
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, new List <string>(new[]
                {
                    state
                }), FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, filterType, filterList, FilterCompairType.And);

                _fda.AddCountField(string.Format("{0}.exact", cntField.ToLower()));
                apiUrl = _fda.BuildUrl(endPointType);

                searchResults = _fda.Execute(apiUrl);
            }

            if (!string.IsNullOrEmpty(searchResults))
            {
                var jo           = JObject.Parse(searchResults);
                var countResults = (JArray)(jo["results"]);

                var termCountFound = false;
                var termCount      = 0;

                foreach (var itm in countResults)
                {
                    termCount = (int)itm["count"];

                    var termClassification = (itm["term"]).ToString();

                    switch (termClassification)
                    {
                    case "Class I":

                        searchSummary.ClassICount = termCount;
                        termCountFound            = true;
                        break;

                    case "Class II":

                        searchSummary.ClassIICount = termCount;
                        termCountFound             = true;
                        break;

                    case "Class III":
                        searchSummary.ClassIIICount = termCount;
                        termCountFound = true;
                        break;
                    }
                }

                if (!termCountFound)
                {
                    searchSummary = null;
                }
            }

            return(searchSummary);
        }
        private IEnumerable <ResultRecall> GetRecallInfo(string keyWord, string state)
        {
            _fda = new OpenFda(_restClient);

            OpenFdaApiHits = 0;

            var resultList   = new List <ResultRecall>();
            var endPointList = new List <OpenFdaApiEndPoints>(new[]
            {
                OpenFdaApiEndPoints.FoodRecall,
                OpenFdaApiEndPoints.DrugRecall,
                OpenFdaApiEndPoints.DeviceRecall
            });

            foreach (var endPointType in endPointList)
            {
                var filterList = new List <string>(new[]
                {
                    state
                });

                //Limit first query to a 1 year window
                var beginDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddDays(1));
                var endDate   = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-1));

                _fda.ResetSearch();
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List <string>(new[]
                {
                    keyWord
                }), FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List <string>(new[]
                {
                    beginDate,
                    endDate
                }), FilterCompairType.And);

                var apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                var searchResults = _fda.Execute(apiUrl);
                OpenFdaApiHits++;

                var dataSetSize = _fda.GetMetaResults().
                                  Total;

                // If there was not data in the 1 yr window the get all results.
                // Check a 2 yr window for results.
                if (dataSetSize == 0)
                {
                    endDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-2));

                    _fda.ResetSearch();
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List <string>(new[]
                    {
                        keyWord
                    }), FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List <string>(new[]
                    {
                        beginDate,
                        endDate
                    }), FilterCompairType.And);

                    apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                    searchResults = _fda.Execute(apiUrl);
                    OpenFdaApiHits++;

                    dataSetSize = _fda.GetMetaResults().
                                  Total;
                }

                // If there was not data in the 2 yr window the get all results.
                if (dataSetSize == 0)
                {
                    _fda.ResetSearch();
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List <string>(new[]
                    {
                        keyWord
                    }), FilterCompairType.And);

                    apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                    searchResults = _fda.Execute(apiUrl);
                    OpenFdaApiHits++;

                    dataSetSize = _fda.GetMetaResults().
                                  Total;
                }

                // if total records int the Search request exceeds the max of 100 records per request
                // then page through the data
                // LIMIT the number of page request to a MAX of 5
                var pageLimit = (int)(decimal.Ceiling((decimal)((double)dataSetSize / 100)));
                if (pageLimit > 5)
                {
                    pageLimit = 5;
                }

                var skipValue = 0;
                if (dataSetSize > 0)
                {
                    do
                    {
                        pageLimit--;

                        if (!string.IsNullOrEmpty(searchResults))
                        {
                            var result = ResultRecall.CnvJsonDataToList(searchResults);
                            resultList.AddRange(result);
                        }

                        if (pageLimit > 0)
                        {
                            skipValue += 100;
                            var newApiUrl = apiUrl.Replace("&limit=100", string.Format("&limit=100&skip={0}", skipValue));
                            searchResults = _fda.Execute(apiUrl);
                            OpenFdaApiHits++;
                        }
                    }while (!(pageLimit == 0));
                }
            }

            var sortedResultList = ((from el in resultList
                                     select el).OrderByDescending(el => el.Recall_Initiation_Date)).ToList();

            return(sortedResultList);

            //Return resultList
        }
        private IEnumerable<ResultRecall> GetRecallInfo(string keyWord, string state)
        {
            _fda = new OpenFda(_restClient);

            OpenFdaApiHits = 0;

            var resultList = new List<ResultRecall>();
            var endPointList = new List<OpenFdaApiEndPoints>(new[]
                                                             {
                                                                 OpenFdaApiEndPoints.FoodRecall,
                                                                 OpenFdaApiEndPoints.DrugRecall,
                                                                 OpenFdaApiEndPoints.DeviceRecall
                                                             });

            foreach (var endPointType in endPointList)
            {
                var filterList = new List<string>(new[]
                                                  {
                                                      state
                                                  });

                //Limit first query to a 1 year window
                var beginDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddDays(1));
                var endDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-1));

                _fda.ResetSearch();
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List<string>(new[]
                                                                                                 {
                                                                                                     keyWord
                                                                                                 }), FilterCompairType.And);
                _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List<string>(new[]
                                                                                         {
                                                                                             beginDate,
                                                                                             endDate
                                                                                         }), FilterCompairType.And);

                var apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                var searchResults = _fda.Execute(apiUrl);
                OpenFdaApiHits++;

                var dataSetSize = _fda.GetMetaResults().
                                       Total;

                // If there was not data in the 1 yr window the get all results.
                // Check a 2 yr window for results.
                if (dataSetSize == 0)
                {
                    endDate = string.Format("{0:yyyyMMdd}", DateTime.Now.AddYears(-2));

                    _fda.ResetSearch();
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List<string>(new[]
                                                                                                     {
                                                                                                         keyWord
                                                                                                     }), FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Date, new List<string>(new[]
                                                                                             {
                                                                                                 beginDate,
                                                                                                 endDate
                                                                                             }), FilterCompairType.And);

                    apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                    searchResults = _fda.Execute(apiUrl);
                    OpenFdaApiHits++;

                    dataSetSize = _fda.GetMetaResults().
                                       Total;
                }

                // If there was not data in the 2 yr window the get all results.
                if (dataSetSize == 0)
                {
                    _fda.ResetSearch();
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.Region, filterList, FilterCompairType.And);
                    _fda.AddSearchFilter(endPointType, FdaFilterTypes.RecallReason, new List<string>(new[]
                                                                                                     {
                                                                                                         keyWord
                                                                                                     }), FilterCompairType.And);

                    apiUrl = _fda.BuildUrl(endPointType, MaxResultSetSize);

                    searchResults = _fda.Execute(apiUrl);
                    OpenFdaApiHits++;

                    dataSetSize = _fda.GetMetaResults().
                                       Total;
                }

                // if total records int the Search request exceeds the max of 100 records per request
                // then page through the data
                // LIMIT the number of page request to a MAX of 5
                var pageLimit = (int) (decimal.Ceiling((decimal) ((double) dataSetSize/100)));
                if (pageLimit > 5)
                {
                    pageLimit = 5;
                }

                var skipValue = 0;
                if (dataSetSize > 0)
                {
                    do
                    {
                        pageLimit--;

                        if (!string.IsNullOrEmpty(searchResults))
                        {
                            var result = ResultRecall.CnvJsonDataToList(searchResults);
                            resultList.AddRange(result);
                        }

                        if (pageLimit > 0)
                        {
                            skipValue += 100;
                            var newApiUrl = apiUrl.Replace("&limit=100", string.Format("&limit=100&skip={0}", skipValue));
                            searchResults = _fda.Execute(apiUrl);
                            OpenFdaApiHits++;
                        }
                    }
                    while (!(pageLimit == 0));
                }
            }

            var sortedResultList = ((from el in resultList
                                     select el).OrderByDescending(el => el.Recall_Initiation_Date)).ToList();

            return sortedResultList;

            //Return resultList
        }