/// <summary> /// Publishing LinkChecker Search /// </summary> /// <returns></returns> public ActionResult LinkCheckerSearch() { var result = new JsonResult(); try { #region According to Datatables.net, Server side parameters //global search var search = Request.Form["search[value]"]; //what is draw?? var draw = Request.Form["draw"]; var orderBy = string.Empty; //column index var order = int.Parse(Request.Form["order[0][column]"]); //sort direction var orderDir = Request.Form["order[0][dir]"]; int startRec = int.Parse(Request.Form["start"]); int pageSize = int.Parse(Request.Form["length"]); int pageNbr = (startRec / pageSize) + 1; #endregion #region Where filter //individual column wise search var columnSearch = new List <string>(); var globalSearch = new List <string>(); DateTime dt = new DateTime(); //Get all keys starting with columns foreach (var index in Request.Form.AllKeys.Where(x => Regex.Match(x, @"columns\[(\d+)]").Success).Select(x => int.Parse(Regex.Match(x, @"\d+").Value)).Distinct().ToList()) { //get individual columns search value var value = Request.Form[string.Format("columns[{0}][search][value]", index)]; if (!string.IsNullOrWhiteSpace(value.Trim())) { value = value.Trim(); string colName = Request.Form[string.Format("columns[{0}][data]", index)]; if (colName == "DisplayDate" || colName == "CheckDate") { if (DateTime.TryParse(value, out dt)) { columnSearch.Add(string.Format(" (convert(varchar(10),CheckDate,120) = '{0}') ", dt.ToString("yyyy-MM-dd"))); } } else { if (value.Length > 1 && value.IndexOf("!") == 0) { columnSearch.Add(string.Format("({0} NOT LIKE '%{1}%')", Request.Form[string.Format("columns[{0}][data]", index)], value.Substring(1))); } else { //check for OR, or || //should watch for incomplete typing if (value.IndexOf(" OR ") > 0) { var itemList = value.Split(new string[] { " OR " }, StringSplitOptions.None); string filter = ""; string OR = ""; foreach (var item in itemList) { filter = OR + string.Format("({0} LIKE '%{1}%')", Request.Form[string.Format("columns[{0}][data]", index)], item); OR = " OR "; } columnSearch.Add(filter); } else if (value.IndexOf("||") > 0) { var itemList = value.Split(new string[] { "||" }, StringSplitOptions.None); string filter = ""; string OR = ""; foreach (var item in itemList) { filter = OR + string.Format("({0} LIKE '%{1}%')", Request.Form[string.Format("columns[{0}][data]", index)], item); OR = " OR "; } columnSearch.Add(filter); } else { columnSearch.Add(string.Format("({0} LIKE '%{1}%')", Request.Form[string.Format("columns[{0}][data]", index)], value)); } } } } //get column filter for global search if (!string.IsNullOrWhiteSpace(search)) { if (index > 0) //skip date { if (Request.Form[string.Format("columns[{0}][data]", index)] != "Totals") { globalSearch.Add(string.Format("({0} LIKE '%{1}%')", Request.Form[string.Format("columns[{0}][data]", index)], search)); } } } //get order by from order index if (order == index) { orderBy = Request.Form[string.Format("columns[{0}][data]", index)]; } } var where = string.Empty; //concat all filters for global search if (globalSearch.Any()) { where = globalSearch.Aggregate((current, next) => current + " OR " + next); } if (columnSearch.Any()) { if (!string.IsNullOrEmpty(where)) { where = string.Format("({0}) AND ({1})", where, columnSearch.Aggregate((current, next) => current + " AND " + next)); } else { where = columnSearch.Aggregate((current, next) => current + " AND " + next); } } #endregion if (orderBy == "DisplayDate") { orderBy = "CheckDate"; } SearchRequest parms = new SearchRequest() { OrderBy = orderBy, OrderDescending = orderDir == "desc" ? true : false, PageNumber = pageNbr, PageSize = pageSize }; parms.Filter = where; var totalRecords = 0; //var list = ActivityServices.PublishHistorySearch( parms, ref totalRecords ); var list = new List <LinkCheckActivity>(); parms.Token = UtilityManager.GetAppKeyValue("apiPublisherIdentifier", ""); var getUrl = UtilityManager.GetAppKeyValue("ceServicesLinkCheckerSearch"); SearchResponse response = new SearchResponse(); try { var content = new StringContent(Newtonsoft.Json.JsonConvert.SerializeObject(parms), System.Text.Encoding.UTF8, "application/json"); var rawData = new HttpClient().PostAsync(getUrl, content).Result.Content.ReadAsStringAsync().Result; if (rawData == null || rawData.IndexOf("The resource cannot be found") > 0 ) { //messages.Add( "Error: the concepts schemes were not found using fallback?????? " ); LoggingHelper.DoTrace(2, string.Format("LinkCheckerSearch. ??????")); //return false; } else { response = new JavaScriptSerializer().Deserialize <SearchResponse>(rawData); } if (response != null && response.History != null && response.History.Count > 0) { list = response.History; } else { } } catch (Exception ex) { LoggingHelper.LogError(ex, ".LinkCheckerSearch"); string message = LoggingHelper.FormatExceptions(ex); list.Add(new LinkCheckActivity() { OrganizationName = "Error encountered.", OrganizationCTID = message }); } result = Json(new { data = list, draw = int.Parse(draw), recordsTotal = response.TotalRecords, recordsFiltered = response }, JsonRequestBehavior.AllowGet); } catch (Exception ex) { } return(result); }
/// <summary> /// Retrieve a resource from the registry by resourceId /// </summary> /// <param name="resourceId">Url to a resource in the registry</param> /// <param name="statusMessage"></param> /// <returns></returns> public static string GetResourceByUrl(string resourceUrl, ref string ctdlType, ref string statusMessage) { string payload = ""; statusMessage = ""; ctdlType = ""; string ceApiKey = UtilityManager.GetAppKeyValue("ceApiKey"); try { using (var client = new HttpClient()) { client.DefaultRequestHeaders. Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); if (!string.IsNullOrWhiteSpace(ceApiKey)) { client.DefaultRequestHeaders.Add("Authorization", "Token " + ceApiKey); } var task = client.GetAsync(resourceUrl); task.Wait(); var response1 = task.Result; payload = task.Result.Content.ReadAsStringAsync().Result; //just in case, likely the caller knows the context if (!string.IsNullOrWhiteSpace(payload) && payload.Length > 100 //&& payload.IndexOf("\"errors\":") == -1 ) { ctdlType = RegistryServices.GetResourceType(payload); } else { //nothing found, or error/not found LoggingHelper.DoTrace(1, "RegistryServices.GetResourceByUrl. Did not find: " + resourceUrl); statusMessage = "The referenced resource was not found in the credential registry: " + resourceUrl; payload = ""; } // } } catch (Exception exc) { if (exc.Message.IndexOf("(404) Not Found") > 0) { //need to surface these better statusMessage = "The referenced resource was not found in the credential registry: " + resourceUrl; } else { var msg = LoggingHelper.FormatExceptions(exc); if (msg.IndexOf("remote name could not be resolved: 'sandbox.credentialengineregistry.org'") > 0) { //retry? statusMessage = "retry"; } LoggingHelper.LogError(exc, "RegistryServices.GetResource"); statusMessage = exc.Message; } } return(payload); }