public Stream SearchData(string tablename, string dataview, string query, string limit, string offset) { var request = WebOperationContext.Current.IncomingRequest; var headers = request.Headers; var JSONString = string.Empty; try { var httpToken = headers["Authorization"].Trim().Replace("Bearer ", ""); using (SecureData sd = new SecureData(false, httpToken)) { if (string.IsNullOrEmpty(offset)) { offset = "0"; } if (string.IsNullOrEmpty(limit)) { limit = "0"; } //sd.Search(query, ignoreCase, andTermsTogether, indexList, resolverName, offset, limit, 0, 0, null, options, null, null) DataSet ds = sd.Search(query, true, true, null, tablename, int.Parse(offset), int.Parse(limit), 0, 0, null, "", null, null); if (ds.Tables["SearchResult"].Rows.Count > 0) { var ids = new List <string>(); foreach (DataRow row in ds.Tables["SearchResult"].Rows) { ids.Add(row.ItemArray[0].ToString()); } if (string.IsNullOrEmpty(dataview)) { var response = sd.GetData("table_mob_" + tablename, ":" + tablename.Replace("_", "") + "id=" + string.Join(",", ids.ToArray()), int.Parse(offset), int.Parse(limit), ""); JSONString = JsonConvert.SerializeObject(response.Tables["table_mob_" + tablename]); } else { var response = sd.GetData(dataview, ":" + tablename.Replace("_", "") + "id= " + string.Join(",", ids.ToArray()), int.Parse(offset), int.Parse(limit), ""); JSONString = JsonConvert.SerializeObject(response.Tables[dataview]); } } else { WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.NoContent; //204 return(null); } } } catch (Exception e) { WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.InternalServerError; //500 JSONString = JsonConvert.SerializeObject(e.Message); } WebOperationContext.Current.OutgoingResponse.ContentType = "application/json; charset=utf-8"; return(new MemoryStream(Encoding.UTF8.GetBytes(JSONString))); }
protected void Page_Load(object sender, EventArgs e) { string action = (Request.Params["action"] + "").ToLower(); string fmt = (Request.Params["format"] + "").ToLower(); if (action == "bounce" || action == "export") { string file = "~/uploads/exports/export_" + DateTime.Now.ToString("yyyy_MM_dd_hh_mm_ss"); string contentType = fmt; // pull data from body, spit out to a file, return url to that file if (fmt == "csv") { contentType = "text/csv"; file += ".csv"; } else if (fmt == "tab") { contentType = "text/plain"; file += ".txt"; } else if (fmt == "") { // TODO: add more cases here } else { // assume html otherwise contentType = "text/html"; file += ".html"; } try { string physicalPath = Server.MapPath(file); File.WriteAllText(physicalPath, Request.Params["data"]); } catch (Exception ex) { Response.Clear(); Response.Write(" { errmsg: '" + ex.Message.Replace("'", @"\'") + "'}"); Response.End(); } Response.Clear(); if (action == "export") { Response.Redirect(Page.ResolveUrl(file)); } else if (action == "bounce") { Response.ContentType = contentType; string url = Page.ResolveUrl(file); Response.Write("{ url : '" + url + "' }"); } else { throw new NotImplementedException("action='" + action + "' not implemented yet."); } Response.End(); } else { // cheesy web service simulator until I get the WCF / login token stuff squared around... string dataview = Request.QueryString["dataview"]; if (String.IsNullOrEmpty(dataview)) { dataview = "web_pivot_test2"; } DataSet ds = new DataSet(); try { using (var sd = new SecureData(false, UserManager.GetLoginToken(true))) { ds = sd.Search( Request.QueryString["q"], String.Compare(Request.QueryString["ic"], "true", true) == 0, String.Compare(Request.QueryString["ma"], "true", true) == 0, null, "accession", 0, Toolkit.ToInt32(Request.QueryString["lim"], 500), 0, 0, dataview, null, null, null); DataTable dt = ds.Tables[dataview]; // hide all '_id' fields, assume they're needed only for internal purposes foreach (DataColumn dc in dt.Columns) { if (dc.ColumnName.ToLower().EndsWith("_id")) { dc.ExtendedProperties["is_hidden"] = true; } } } } catch (Exception ex) { Response.Clear(); Response.Write(" { errmsg: '" + ex.Message.Replace("'", @"\'") + "'}"); Response.End(); } Response.Clear(); Response.Write(ds.Tables[dataview].ToJson("accession_id", "pi_number")); Response.End(); } }
public DataSet Search(bool suppressExceptions, string userName, string password, string query, bool ignoreCase, bool andTermsTogether, string indexList, string resolverName, int offset, int limit, string options) { using (SecureData sd = new SecureData(suppressExceptions, Login(userName, password))) { return(sd.Search(query, ignoreCase, andTermsTogether, indexList, resolverName, offset, limit, 0, 0, null, options, null, null)); } }
void doSearch(string advanceQry) { try { using (HighPrecisionTimer hpt = new HighPrecisionTimer("timer", true)) { List <DataTable> tables = new List <DataTable>(); int limit = Toolkit.ToInt32(lim.SelectedValue, 0); string query = ""; if (ml.Checked) { query = q2.Text; } else { query = q.Text; } q.Text = q2.Text = query; string sDv = ""; string advancedQuery = ""; if (advanceQry == "") { advancedQuery = getAdvancedQuery(); } else { advancedQuery = advanceQry; } var searchString = query; query = query + advancedQuery; string op = ma.Checked ? " and " : " or "; if (query.Length >= op.Length) { if (query.Substring(0, op.Length) == op) { query = query.Substring(op.Length, query.Length - op.Length); } } //if (ed.Checked && query != "") // query = query + " AND (@inventory.is_distributable = 'Y' and @inventory.is_available = 'Y')"; if (query == "") { Master.ShowError(Page.GetDisplayMember("Search", "doSearch{noCriteria}", "Please enter search string or other search criteria and try again.")); } else { var searchIndexes = Toolkit.GetSetting("WebSearchableIndexes", "accession, accession_name, taxonomy_species, inventory, accession_source, cooperator"); DataSet ds = null; using (var sd = new SecureData(true, UserManager.GetLoginToken(true))) { //ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, view.SelectedValue, "passthru=always;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null); if (exp.Checked) { sDv = "web_search_overview_export"; } else { sDv = view.SelectedValue; } //if (ed.Checked) // ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, 0, 0, 0, "", "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null); //else // ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, sDv, "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null); query = query + " and @accession.is_web_visible = 'Y' "; query = Utils.Sanitize2(query); ds = sd.Search(query, ic.Checked, ma.Checked, searchIndexes, "accession", 0, limit, 0, 0, sDv, "passthru=nonindexedorcomparison;OrMultipleLines=" + (ml.Checked ? "true" : "false"), null, null); var dtError = ds.Tables["ExceptionTable"]; if (dtError != null && dtError.Rows.Count > 0) { if (dtError.Rows[0]["Message"].ToString().ToLower().StartsWith("bad search string")) { Master.ShowError(Page.GetDisplayMember("Search", "doSearch{invalidquery}", "Invalid query. Please alter your criteria and try again.")); } else { var type = dtError.Rows[0]["ExceptionType"].ToString().ToLower(); if (type.Contains("endpointnotfoundexception")) { throw new InvalidOperationException(dtError.Rows[0]["Message"].ToString()); } else { Master.ShowError(dtError.Rows[0]["Message"].ToString()); } } } /* * else * { * if (ed.Checked) * { * List<int> IDs = new List<int>(); * int id; * if (ds != null) * { * foreach (DataRow dr in ds.Tables["SearchResult"].Rows) * { * if (int.TryParse(dr[0].ToString(), out id)) * IDs.Add(id); * } * } * * DataTable dtID = null; * List<int> IDa = new List<int>(); * using (var dm = DataManager.Create(sd.DataConnectionSpec)) * { * dtID = dm.Read("select distinct accession_id from inventory where is_distributable = 'Y' and is_available = 'Y'"); * if (dtID != null) * { * foreach (DataRow dr in dtID.Rows) * { * if (int.TryParse(dr[0].ToString(), out id)) * IDa.Add(id); * } * } * } * * List<int> IDavail = IDs.Intersect(IDa).ToList(); * * if (limit < IDavail.Count) * IDavail = IDavail.GetRange(0, limit); * * ds = sd.GetData(sDv, ":idlist=" + string.Join(",", IDavail.Select(n => n.ToString()).ToArray()), 0, 0, ""); * } * } */ } hpt.Stop(); //TimeSpan ts = TimeSpan.FromMilliseconds(hpt.ElapsedMilliseconds); //string elapsed = ts.Seconds.ToString("00") + "." + ts.Milliseconds.ToString("000"); // lblTimer.Text = "Search time: " + elapsed + " seconds"; DataTable dt = ds.Tables[sDv]; if (exp.Checked) { pnlSearchResults.Visible = false; if (dt == null) { Master.ShowError(Page.GetDisplayMember("Search", "doSearch{errorout}", "Query limit exceeded capacity. Please reduce your return accessions number and try again.")); } else { Toolkit.OutputData(dt, Response, "csv", false, true); //exportToExcel(dt); } } else { pnlSearchResults.Visible = true; //if (rs.SelectedValue.Contains("obs")) { // // special case: obs data is stored row-wise, // // we need it column-wise // // need to pivot by the 'trait_value' column // dt = dt.transform(new string[] { "accession_id", "pi_number" }, "trait_name", null, "trait_value"); //} if (dt == null || dt.Rows.Count == 0) { pnlSearchResultsHeader.Visible = false; ggPivotView.DataSource = dt; ggPivotView.DataBind(); } else { pnlSearchResultsHeader.Visible = true; ggPivotView.DataSource = dt; ggPivotView.DataBind(); } var display = "<b>Query Criteria:</b><br /> " + " Search String: " + searchString + "<br />" + getAdvancedQueryDisplay(); Master.ShowMore(display); } } } } catch (Exception ex) { pnlSearch.Visible = false; pnlSearchDown.Visible = true; if (Page.User.IsInRole("Admins")) { pnlError.Visible = true; SearchError = ex.ToString(); } else { pnlError.Visible = false; } } }