/** Create a properly-configured grid with default settings, on first load. */ public void loadTupleTableConfig(Database db, MolgenisRequest request, TupleTable tupleTable) throws TableException, IOException { tupleTable.setDb(db); final JQGridConfiguration config = new JQGridConfiguration(getId(), "Name", tupleTableBuilder.getUrl(), "test", tupleTable); final String jqJsonConfig = new Gson().toJson(config); request.getResponse().getOutputStream().println(jqJsonConfig); }
/** * Function to build a datastructure filled with rows from a {@link TupleTable}, to be serialised * by Gson and displayed from there by a jqGrid. * * @param rowCount The number of rows to select. * @param totalPages The total number of pages of data (ie. dependent on size of dataset and nr. * of rows per page) * @param page The selected page. * @param table The Tupletable from which to read the data. * @return */ public static JQGridResult buildJQGridResults( final int rowCount, final int totalPages, final int page, final TupleTable table) throws TableException { final JQGridResult result = new JQGridResult(page, totalPages, rowCount); for (final Tuple row : table.getRows()) { System.out.println("check: " + row); final LinkedHashMap<String, String> rowMap = new LinkedHashMap<String, String>(); final List<String> fieldNames = row.getFieldNames(); for (final String fieldName : fieldNames) { final String rowValue = !row.isNull(fieldName) ? row.getString(fieldName) : "null"; rowMap.put(fieldName, rowValue); // TODO encode to HTML } result.rows.add(rowMap); } return result; }
/** * Render a particular subset of data from a {@link TupleTable} to a particular {@link Renderer}. * * @param request The request encoding the particulars of the rendering to be done. * @param postData The selected page (only relevant for {@link JQGridRenderer} rendering) * @param totalPages The total number of pages (only relevant for {@link JQGridRenderer} * rendering) * @param tupleTable The table from which to render the data. */ private void renderData( MolgenisRequest request, JQGridPostData postData, int totalPages, final TupleTable tupleTable) throws TableException { tupleTable.setDb(request.getDatabase()); String strViewType = request.getString("viewType"); if (StringUtils.isEmpty(strViewType)) { strViewType = "JQ_GRID"; } try { final ViewFactory viewFactory = new ViewFactoryImpl(); final Renderers.Renderer view = viewFactory.createView(strViewType); view.export(request, request.getString("caption"), tupleTable, totalPages, postData.page); } catch (final Exception e) { throw new TableException(e); } }
/** * Handle a particular {@link MolgenisRequest}, and render into an {@link OutputStream}. * Particulars handled: * * <ul> * <li>Wrap the desired data source in the appropriate instantiation of {@link TupleTable}. * <li>Determine which {@link Operation} the request is asking to handle. * <li>Apply proper sorting and filter rules. * <li>Select the appropriate view towards which to export/render. * <li>Select and render the data. * </ul> * * @param db The database to connect to * @param request The {@link MolgenisRequest} tuple that encodes the request to handle * @param out The {@link OutputStream} to render to. */ public void handleRequest(Database db, Tuple request, OutputStream out) throws HandleRequestDelegationException { try { final TupleTable tupleTable = tupleTableBuilder.create(db, request); final Operation operation = StringUtils.isNotEmpty(request.getString(OPERATION)) ? Operation.valueOf(request.getString(OPERATION)) : Operation.RENDER_DATA; switch (operation) { case LOAD_CONFIG: loadTupleTableConfig(db, (MolgenisRequest) request, tupleTable); break; case LOAD_TREE: // risky: we give it all columns which would fail if there // are many final String treeNodes = JQueryUtil.getDynaTreeNodes(tupleTable.getAllColumns()); ((MolgenisRequest) request).getResponse().getOutputStream().print(treeNodes); break; case RENDER_DATA: final List<QueryRule> rules = new ArrayList<QueryRule>(); // parse the request into post data final JQGridPostData postData = new JQGridPostData(request); // convert any filters to query rules final List<QueryRule> filterRules = createQueryRulesFromJQGridRequest(postData.filters); if (CollectionUtils.isNotEmpty(filterRules)) { if (tupleTable instanceof FilterableTupleTable) { rules.addAll(filterRules); ((FilterableTupleTable) tupleTable).setFilters(rules); } } final int rowCount = tupleTable.getCount(); final int totalPages = (int) Math.ceil(rowCount / postData.rows); // update page postData.page = Math.min(postData.page, totalPages); final int offset = Math.max((postData.page - 1) * postData.rows, 0); final String exportSelection = request.getString("exportSelection"); if (!StringUtils.equalsIgnoreCase(exportSelection, "ALL")) { // data.rows == limit tupleTable.setLimit(postData.rows); // data.rows * data.page tupleTable.setOffset(offset); } if (StringUtils.isNotEmpty(postData.sidx) && tupleTable instanceof FilterableTupleTable) { final Operator sortOperator = StringUtils.equals(postData.sord, "asc") ? QueryRule.Operator.SORTASC : QueryRule.Operator.SORTDESC; rules.add(new QueryRule(sortOperator, postData.sidx)); } if (tupleTable instanceof FilterableTupleTable) { ((FilterableTupleTable) tupleTable).setFilters(rules); } renderData(((MolgenisRequest) request), postData, totalPages, tupleTable); } } catch (final Exception e) { throw new HandleRequestDelegationException(e); } }