/// <summary> /// Converts JSON Data to List /// </summary> /// <param name="jsondata">JSON Object Data</param> /// <returns>List of Recall Data</returns> /// <remarks></remarks> public static List <ResultRecall> CnvJsonDataToList(JObject jsondata) { var result = new List <ResultRecall>(); foreach (var obj in jsondata.GetValue("results")) { var tmp = new ResultRecall(); tmp.City = (obj["city"]).ToString(); tmp.Classification = (obj["classification"]).ToString(); tmp.Code_info = (obj["code_info"]).ToString(); tmp.Country = (obj["country"]).ToString(); tmp.Distribution_Pattern = (obj["distribution_pattern"]).ToString(); tmp.Event_Id = (obj["event_id"]).ToString(); tmp.Initial_Firm_Notification = (obj["initial_firm_notification"]).ToString(); //tmp.openfda = zz("city") tmp.Product_Description = (obj["product_description"]).ToString(); tmp.Product_Quantity = (obj["product_quantity"]).ToString(); tmp.Product_Type = (obj["product_type"]).ToString(); tmp.Reason_For_Recall = (obj["reason_for_recall"]).ToString(); tmp.Recall_Initiation_Date = (obj["recall_initiation_date"]).ToString(); tmp.Recall_Number = (obj["recall_number"]).ToString(); tmp.Recalling_Firm = (obj["recalling_firm"]).ToString(); tmp.Report_Date = (obj["report_date"]).ToString(); tmp.State = (obj["state"]).ToString(); tmp.Status = (obj["status"]).ToString(); tmp.Voluntary_Mandated = (obj["voluntary_mandated"]).ToString(); result.Add(tmp); } return(result); }
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 }
/// <summary> /// Converts JSON Data to List /// </summary> /// <param name="jsondata">JSON Object Data</param> /// <returns>List of Recall Data</returns> /// <remarks></remarks> public static List<ResultRecall> CnvJsonDataToList(JObject jsondata) { var result = new List<ResultRecall>(); foreach (var obj in jsondata.GetValue("results")) { var tmp = new ResultRecall(); tmp.City = (obj["city"]).ToString(); tmp.Classification = (obj["classification"]).ToString(); tmp.Code_info = (obj["code_info"]).ToString(); tmp.Country = (obj["country"]).ToString(); tmp.Distribution_Pattern = (obj["distribution_pattern"]).ToString(); tmp.Event_Id = (obj["event_id"]).ToString(); tmp.Initial_Firm_Notification = (obj["initial_firm_notification"]).ToString(); //tmp.openfda = zz("city") tmp.Product_Description = (obj["product_description"]).ToString(); tmp.Product_Quantity = (obj["product_quantity"]).ToString(); tmp.Product_Type = (obj["product_type"]).ToString(); tmp.Reason_For_Recall = (obj["reason_for_recall"]).ToString(); tmp.Recall_Initiation_Date = (obj["recall_initiation_date"]).ToString(); tmp.Recall_Number = (obj["recall_number"]).ToString(); tmp.Recalling_Firm = (obj["recalling_firm"]).ToString(); tmp.Report_Date = (obj["report_date"]).ToString(); tmp.State = (obj["state"]).ToString(); tmp.Status = (obj["status"]).ToString(); tmp.Voluntary_Mandated = (obj["voluntary_mandated"]).ToString(); result.Add(tmp); } return result; }