public void TestCsvExport() { PdbResultsWithMetadata values = _helper.Query(null, 10, null, _publicRoles, 2000, 1); string csv = _helper.ResultsAsCsv(values, false); Assert.IsNotEmpty(csv, "CSV Export should be populated."); }
protected override void InternalGET(HttpContext context, HandlerTimedCache cache) { IList <SecurityRole> roles = UserHelper.GetUserRoles(context.User.Identity.Name); //Get the paging parameters... int page = WebUtil.ParseIntParam(context, "page"); int pageSize = WebUtil.ParseIntParam(context, "pageSize"); // Check to see if this is a csv export request. Runs the normal query (with no paging). bool csv = false; WebUtil.ParseOptionalBoolParam(context, "csv", ref csv); // If this is csv, we want all data - override any paging if (csv) { page = -1; pageSize = -1; } IList <IExpression> expressions = ParseExpressions(context); // Now get the ordering parameters, if specified. int sortCol = -1; WebUtil.ParseOptionalIntParam(context, "sortBy", ref sortCol); SortType?sortDir = null; if (sortCol >= 0) { // Default is ascending sort, passing false means descending. bool ascending = true; WebUtil.ParseOptionalBoolParam(context, "sortasc", ref ascending); sortDir = ascending ? SortType.Asc : SortType.Desc; } PdbTwoTableHelper dataHelper = new PdbTwoTableHelper(Config.GetConfig("PDP.Data"), "Properties", PdbEntityType.Properties); // Now get the grouping parameters, if specified. IList <string> groupBys = WebUtil.GetJsonStringArrayParam(context, "groupby", true); PdbResultsWithMetadata list; if ((groupBys != null) && (groupBys.Count > 0)) { list = dataHelper.GroupedQuery(expressions, groupBys, sortCol, sortDir, roles, pageSize, page); } else { list = dataHelper.Query(expressions, sortCol, sortDir, roles, pageSize, page); } // If this was a csv request, format it and return it instead if (csv) { // Generate actual csv data, determine if this is groupby'd or not string export = dataHelper.ResultsAsCsv(list, ((groupBys != null) && (groupBys.Count > 0))); // Setup the response to handle this type of request context.Response.AddHeader("Content-Disposition", "attachment;filename=Furman_Center_SHIP_Properties.csv"); context.Response.ContentType = "text/csv"; context.Response.Write(export); return; } context.Response.Write(WebUtil.ObjectToJson(list)); }