From 7b5e1cbc595266bf9631f9718c4534db565f1c9e Mon Sep 17 00:00:00 2001 From: Ben Konrath Date: Fri, 18 Feb 2011 16:02:34 +0100 Subject: [PATCH] Enable the CellTable Pager when more than 20 rows need to be viewed. The Pager will automatically become active when the results are larger than the CellTable size which is currently set to 20 lines. * src/main/java/org/glom/web/client/LayoutListView.java: Correct class name on debug statment in RPC call in LayoutListDataProvider, add numRows parameter to LayoutListView constructor, propperly set rowCount in CellTable. * src/main/java/org/glom/web/client/OnlineGlom.java: Correct class name on debug statment in RPC call, use LayoutListTable object in RPC calls, pass rowCount to LayoutListView. * src/main/java/org/glom/web/client/OnlineGlomService.java: Change getLayoutListHeaders to getLayoutListTable and return LayoutListTable object. * src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java: Update interface for changes in OnlineGlomService. * src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java: Change getLayoutListHeaders() to getLayoutListTable() and return LayoutListTable. Using this object allows me to pass other information about the LayoutList like the expected number of rows in the result set. The Connection object from the connection pool is now propperly closed. Only the requested number of lines are returned to the client in getTableData(). * src/main/java/org/glom/web/shared/LayoutListTable.java: Move from GlomTable and add columnTitles and numRows. --- ChangeLog | 29 ++++++ .../java/org/glom/web/client/LayoutListView.java | 9 +- src/main/java/org/glom/web/client/OnlineGlom.java | 13 +-- .../org/glom/web/client/OnlineGlomService.java | 3 +- .../glom/web/client/OnlineGlomServiceAsync.java | 3 +- .../org/glom/web/server/OnlineGlomServiceImpl.java | 109 +++++++++++++++++---- src/main/java/org/glom/web/shared/GlomTable.java | 47 --------- .../java/org/glom/web/shared/LayoutListTable.java | 52 ++++++++++ 8 files changed, 187 insertions(+), 78 deletions(-) delete mode 100644 src/main/java/org/glom/web/shared/GlomTable.java create mode 100644 src/main/java/org/glom/web/shared/LayoutListTable.java diff --git a/ChangeLog b/ChangeLog index 8533fb8..9b39f93 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,34 @@ 2011-02-18 Ben Konrath + Enable the CellTable Pager when more than 20 rows need to be viewed. + + The Pager will automatically become active when the results are larger + than the CellTable size which is currently set to 20 lines. + + * src/main/java/org/glom/web/client/LayoutListView.java: Correct class + name on debug statment in RPC call in LayoutListDataProvider, add + numRows parameter to LayoutListView constructor, propperly set rowCount + in CellTable. + * src/main/java/org/glom/web/client/OnlineGlom.java: Correct class + name on debug statment in RPC call, use LayoutListTable object in RPC + calls, pass rowCount to LayoutListView. + * src/main/java/org/glom/web/client/OnlineGlomService.java: Change + getLayoutListHeaders to getLayoutListTable and return LayoutListTable + object. + * src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java: Update + interface for changes in OnlineGlomService. + * src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java: Change + getLayoutListHeaders() to getLayoutListTable() and return + LayoutListTable. Using this object allows me to pass other information + about the LayoutList like the expected number of rows in the result set. + The Connection object from the connection pool is now propperly closed. + Only the requested number of lines are returned to the client in + getTableData(). + * src/main/java/org/glom/web/shared/LayoutListTable.java: Move from + GlomTable and add columnTitles and numRows. + +2011-02-18 Ben Konrath + Use String arrays instead of GlomTable objects in GlomDocument GWT-RPC object. This is a small performance boost. I'll use GlomTable to get the required diff --git a/src/main/java/org/glom/web/client/LayoutListView.java b/src/main/java/org/glom/web/client/LayoutListView.java index 081d796..562f094 100644 --- a/src/main/java/org/glom/web/client/LayoutListView.java +++ b/src/main/java/org/glom/web/client/LayoutListView.java @@ -42,7 +42,7 @@ class LayoutListDataProvider extends AsyncDataProvider { AsyncCallback> callback = new AsyncCallback>() { public void onFailure(Throwable caught) { // FIXME: need to deal with failure - System.out.println("AsyncCallback Failed: LibGlomService.getTableData()"); + System.out.println("AsyncCallback Failed: OnlineGlomService.getTableData()"); } public void onSuccess(ArrayList result) { @@ -58,12 +58,11 @@ public class LayoutListView extends Composite { private CellTable table = null; - public LayoutListView(String[] headers) { - table = new CellTable(); + public LayoutListView(String[] headers, int numRows) { + table = new CellTable(20); LayoutListDataProvider dataProvider = new LayoutListDataProvider(); dataProvider.addDataDisplay(table); - // TODO wire up the pager SimplePager pager = new SimplePager(SimplePager.TextLocation.CENTER); pager.setDisplay(table); @@ -87,7 +86,7 @@ public class LayoutListView extends Composite { } - table.setRowCount(0); + table.setRowCount(numRows); // take care of the necessary stuff required for composite widgets initWidget(panel); diff --git a/src/main/java/org/glom/web/client/OnlineGlom.java b/src/main/java/org/glom/web/client/OnlineGlom.java index ba305ce..d5ee7be 100644 --- a/src/main/java/org/glom/web/client/OnlineGlom.java +++ b/src/main/java/org/glom/web/client/OnlineGlom.java @@ -20,6 +20,7 @@ package org.glom.web.client; import org.glom.web.shared.GlomDocument; +import org.glom.web.shared.LayoutListTable; import com.google.gwt.core.client.EntryPoint; import com.google.gwt.event.dom.client.ChangeEvent; @@ -62,7 +63,7 @@ public class OnlineGlom implements EntryPoint { AsyncCallback callback = new AsyncCallback() { public void onFailure(Throwable caught) { // FIXME: need to deal with failure - System.out.println("AsyncCallback Failed: LibGlomService"); + System.out.println("AsyncCallback Failed: OnlineGlomService.getGlomDocument()"); } public void onSuccess(GlomDocument result) { @@ -84,23 +85,23 @@ public class OnlineGlom implements EntryPoint { private void updateTable() { // set up the callback object. - AsyncCallback callback = new AsyncCallback() { + AsyncCallback callback = new AsyncCallback() { public void onFailure(Throwable caught) { // FIXME: need to deal with failure - System.out.println("AsyncCallback Failed: LibGlomService.updateTable()"); + System.out.println("AsyncCallback Failed: OnlineGlomService.getLayoutListTable()"); } - public void onSuccess(String[] result) { + public void onSuccess(LayoutListTable result) { if (table != null) mainVPanel.remove(table); - table = new LayoutListView(result); + table = new LayoutListView(result.getColumnTitles(), result.getNumRows()); mainVPanel.add(table); Window.setTitle("OnlineGlom - " + documentName + ": " + dropBox.getItemText(dropBox.getSelectedIndex())); } }; String selectedTable = dropBox.getValue(dropBox.getSelectedIndex()); - OnlineGlomServiceAsync.Util.getInstance().getLayoutListHeaders(selectedTable, callback); + OnlineGlomServiceAsync.Util.getInstance().getLayoutListTable(selectedTable, callback); } diff --git a/src/main/java/org/glom/web/client/OnlineGlomService.java b/src/main/java/org/glom/web/client/OnlineGlomService.java index 3694aa4..da5a157 100644 --- a/src/main/java/org/glom/web/client/OnlineGlomService.java +++ b/src/main/java/org/glom/web/client/OnlineGlomService.java @@ -22,6 +22,7 @@ package org.glom.web.client; import java.util.ArrayList; import org.glom.web.shared.GlomDocument; +import org.glom.web.shared.LayoutListTable; import com.google.gwt.user.client.rpc.RemoteService; import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; @@ -31,7 +32,7 @@ public interface OnlineGlomService extends RemoteService { GlomDocument getGlomDocument(); - String[] getLayoutListHeaders(String table); + LayoutListTable getLayoutListTable(String tableName); ArrayList getTableData(int start, int length, String table); diff --git a/src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java b/src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java index 8fe085b..be72a60 100644 --- a/src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java +++ b/src/main/java/org/glom/web/client/OnlineGlomServiceAsync.java @@ -3,6 +3,7 @@ package org.glom.web.client; import java.util.ArrayList; import org.glom.web.shared.GlomDocument; +import org.glom.web.shared.LayoutListTable; import com.google.gwt.core.client.GWT; import com.google.gwt.user.client.rpc.AsyncCallback; @@ -11,7 +12,7 @@ public interface OnlineGlomServiceAsync { void getGlomDocument(AsyncCallback callback); - void getLayoutListHeaders(String table, AsyncCallback callback); + void getLayoutListTable(String tableName, AsyncCallback callback); void getTableData(int start, int length, String table, AsyncCallback> callback); diff --git a/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java b/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java index 3c099ab..44c6118 100644 --- a/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java +++ b/src/main/java/org/glom/web/server/OnlineGlomServiceImpl.java @@ -39,6 +39,7 @@ import org.glom.libglom.SortFieldPair; import org.glom.libglom.StringVector; import org.glom.web.client.OnlineGlomService; import org.glom.web.shared.GlomDocument; +import org.glom.web.shared.LayoutListTable; import com.google.gwt.user.server.rpc.RemoteServiceServlet; import com.mchange.v2.c3p0.ComboPooledDataSource; @@ -115,14 +116,67 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin return glomDocument; } - public String[] getLayoutListHeaders(String table) { - LayoutGroupVector layoutList = document.get_data_layout_groups("list", table); - LayoutItemVector layoutItems = layoutList.get(0).get_items(); - String[] headers = new String[safeLongToInt(layoutItems.size())]; - for (int i = 0; i < layoutItems.size(); i++) { - headers[i] = layoutItems.get(i).get_title_or_name(); + public LayoutListTable getLayoutListTable(String tableName) { + + LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName); + LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items(); + int numItems = safeLongToInt(layoutItemsVec.size()); + String[] columnTitles = new String[numItems]; + for (int i = 0; i < numItems; i++) { + columnTitles[i] = layoutItemsVec.get(i).get_title_or_name(); + } + + LayoutFieldVector layoutFields = new LayoutFieldVector(); + for (int i = 0; i < numItems; i++) { + LayoutItem item = layoutItemsVec.get(i); + columnTitles[i] = item.get_title_or_name(); + LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item); + if (field != null) { + layoutFields.add(field); + } } - return headers; + + // get the size of the returned query for the pager + // TODO since we're executing a query anyway, maybe we should return the rows that will be displayed on the + // first page + // TODO this code is really similar to code in getTableData, find a way to not duplicate the code + int numRows; + Connection conn = null; + Statement st = null; + ResultSet rs = null; + try { + // setup and execute the query + conn = cpds.getConnection(); + st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); + String query = Glom.build_sql_select_simple(tableName, layoutFields); + rs = st.executeQuery(query); + + // get the number of rows in the query + rs.setFetchDirection(ResultSet.FETCH_FORWARD); + rs.last(); + numRows = rs.getRow(); + + } catch (SQLException e) { + // TODO log error + // we don't know how many rows are in the query + e.printStackTrace(); + numRows = 0; + } finally { + // this is a little awkward but we want to make we're cleaning everything up that has been used + try { + if (rs != null) + rs.close(); + if (st != null) + st.close(); + if (conn != null) + conn.close(); + } catch (SQLException e) { + // TODO log error + e.printStackTrace(); + } + } + + return new LayoutListTable(tableName, document.get_table_title(tableName), columnTitles, numRows); } public ArrayList getTableData(int start, int length, String table) { @@ -131,7 +185,8 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin LayoutFieldVector layoutFields = new LayoutFieldVector(); SortClause sortClause = new SortClause(); - for (int i = 0; i < layoutItems.size(); i++) { + int numItems = safeLongToInt(layoutItems.size()); + for (int i = 0; i < numItems; i++) { LayoutItem item = layoutItems.get(i); LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item); if (field != null) { @@ -144,28 +199,46 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin } ArrayList rowsList = new ArrayList(); + Connection conn = null; + Statement st = null; + ResultSet rs = null; try { - Connection conn = cpds.getConnection(); - Statement st = conn.createStatement(); - + // setup and execute the query + conn = cpds.getConnection(); + st = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); String query = Glom.build_sql_select_simple(table, layoutFields, sortClause); - ResultSet rs = st.executeQuery(query); + rs = st.executeQuery(query); - while (rs.next()) { + // get data we're asked for + // TODO get the correct range with an sql query + rs.setFetchDirection(ResultSet.FETCH_FORWARD); + rs.absolute(start); + int rowCount = 0; + while (rs.next() && rowCount <= length) { String[] rowArray = new String[safeLongToInt(layoutItems.size())]; for (int i = 0; i < layoutItems.size(); i++) { rowArray[i] = rs.getString(i + 1); } rowsList.add(rowArray); + rowCount++; } - - rs.close(); - st.close(); } catch (SQLException e) { // TODO: log error, notify user of problem e.printStackTrace(); + } finally { + // this is a little awkward but we want to make we're cleaning everything up that has been used + try { + if (rs != null) + rs.close(); + if (st != null) + st.close(); + if (conn != null) + conn.close(); + } catch (SQLException e) { + // TODO log error + e.printStackTrace(); + } } - return rowsList; } @@ -180,4 +253,4 @@ public class OnlineGlomServiceImpl extends RemoteServiceServlet implements Onlin } } -} +} \ No newline at end of file diff --git a/src/main/java/org/glom/web/shared/GlomTable.java b/src/main/java/org/glom/web/shared/GlomTable.java deleted file mode 100644 index e430ed7..0000000 --- a/src/main/java/org/glom/web/shared/GlomTable.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright (C) 2010, 2011 Openismus GmbH - * - * This file is part of GWT-Glom. - * - * GWT-Glom is free software: you can redistribute it and/or modify it - * under the terms of the GNU Lesser General Public License as published by the - * Free Software Foundation, either version 3 of the License, or (at your - * option) any later version. - * - * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or - * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License - * for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with GWT-Glom. If not, see . - */ - -package org.glom.web.shared; - -import java.io.Serializable; - -@SuppressWarnings("serial") -public class GlomTable implements Serializable { - private String name; - private String title; - - public GlomTable() { - } - - public void setName(String name) { - this.name = name; - } - - public String getName() { - return name; - } - - public void setTitle(String title) { - this.title = title; - } - - public String getTitle() { - return title; - } -} \ No newline at end of file diff --git a/src/main/java/org/glom/web/shared/LayoutListTable.java b/src/main/java/org/glom/web/shared/LayoutListTable.java new file mode 100644 index 0000000..c309e1a --- /dev/null +++ b/src/main/java/org/glom/web/shared/LayoutListTable.java @@ -0,0 +1,52 @@ +/* + * Copyright (C) 2010, 2011 Openismus GmbH + * + * This file is part of GWT-Glom. + * + * GWT-Glom is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with GWT-Glom. If not, see . + */ + +package org.glom.web.shared; + +import java.io.Serializable; + +@SuppressWarnings("serial") +public class LayoutListTable implements Serializable { + private String name; + private String title; + private String[] columnTitles; + private int numRows; + + public LayoutListTable() { + new LayoutListTable("", "", new String[] {}, 0); + } + + public LayoutListTable(String name, String title, String[] columnTitles, int numRows) { + this.name = name; + this.title = title; + this.columnTitles = columnTitles; + this.numRows = numRows; + } + + // @formatter:off + public String getName() { return name;} + public void setName(String name) { this.name = name;} + public String getTitle() { return title; } + public void setTitle(String title) { this.title = title; } + public String[] getColumnTitles() { return columnTitles; } + public void setColumnTitles(String[] columns) { this.columnTitles = columns; } + public int getNumRows() { return numRows; } + public void setColumns(int numRows) { this.numRows = numRows; } + // @formatter:on +} \ No newline at end of file -- 2.1.4