/// <summary>
        ///     Gets a count of issues. Issues are enforcements (Recalls) of food, drug and device (identified by classification)
        ///     and events of drug and devices
        /// </summary>
        /// <param name="keyWord"></param>
        /// <param name="state"></param>
        /// <returns></returns>
        /// <remarks></remarks>
        public SearchSummary GetSearchSummary(string keyWord, string state)
        {
            SearchSummary results = null;

            results = GetRecallInfoCounts(keyWord, state);

            return(results);
        }
        private SearchSummary GetRecallInfoCounts(string keyWord, string state)
        {
            _fda = new OpenFda(_restClient);

            var searchSummaryForKeyword = new SearchSummary()
            {
                Keyword = keyWord,
                State   = state
            };
            var filterType = default(FdaFilterTypes);

            filterType = FdaFilterTypes.RecallReason;

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

            const int maxresultsize = 0;

            var filterList = new List <string>();

            filterList.Add(keyWord);

            foreach (var endPoint in endPointList)
            {
                var endpointSearchSummary = ExecuteSearchCounts(endPoint, filterType, filterList, maxresultsize, state, "classification");

                if (endpointSearchSummary != null)
                {
                    searchSummaryForKeyword.ClassICount   += endpointSearchSummary.ClassICount;
                    searchSummaryForKeyword.ClassIICount  += endpointSearchSummary.ClassIICount;
                    searchSummaryForKeyword.ClassIIICount += endpointSearchSummary.ClassIIICount;
                }
            }

            searchSummaryForKeyword.EventCount  = 0;
            searchSummaryForKeyword.EventCount += _fda.GetDrugEventsByDrugNameCount(keyWord);
            searchSummaryForKeyword.EventCount += _fda.GetDeviceEventsByDescriptionCount(keyWord);

            return(searchSummaryForKeyword);
        }
        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 SearchSummary GetRecallInfoCounts(string keyWord, string state)
        {
            _fda = new OpenFda(_restClient);

            var searchSummaryForKeyword = new SearchSummary()
                                          {
                                              Keyword = keyWord,
                                              State = state
                                          };
            var filterType = default(FdaFilterTypes);
            filterType = FdaFilterTypes.RecallReason;

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

            const int maxresultsize = 0;

            var filterList = new List<string>();
            filterList.Add(keyWord);

            foreach (var endPoint in endPointList)
            {
                var endpointSearchSummary = ExecuteSearchCounts(endPoint, filterType, filterList, maxresultsize, state, "classification");

                if (endpointSearchSummary != null)
                {
                    searchSummaryForKeyword.ClassICount += endpointSearchSummary.ClassICount;
                    searchSummaryForKeyword.ClassIICount += endpointSearchSummary.ClassIICount;
                    searchSummaryForKeyword.ClassIIICount += endpointSearchSummary.ClassIIICount;
                }
            }

            searchSummaryForKeyword.EventCount = 0;
            searchSummaryForKeyword.EventCount += _fda.GetDrugEventsByDrugNameCount(keyWord);
            searchSummaryForKeyword.EventCount += _fda.GetDeviceEventsByDescriptionCount(keyWord);

            return searchSummaryForKeyword;
        }
        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;
        }