2 * Copyright (C) 2010, 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server;
22 import java.beans.PropertyVetoException;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.sql.Connection;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
32 import java.text.DateFormat;
33 import java.text.NumberFormat;
34 import java.util.ArrayList;
35 import java.util.Currency;
36 import java.util.Locale;
37 import java.util.Properties;
39 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
40 import org.glom.libglom.Document;
41 import org.glom.libglom.Field;
42 import org.glom.libglom.FieldFormatting;
43 import org.glom.libglom.FieldVector;
44 import org.glom.libglom.Glom;
45 import org.glom.libglom.LayoutFieldVector;
46 import org.glom.libglom.LayoutGroupVector;
47 import org.glom.libglom.LayoutItem;
48 import org.glom.libglom.LayoutItemVector;
49 import org.glom.libglom.LayoutItem_Field;
50 import org.glom.libglom.NumericFormat;
51 import org.glom.libglom.SortClause;
52 import org.glom.libglom.SortFieldPair;
53 import org.glom.libglom.StringVector;
54 import org.glom.web.client.OnlineGlomService;
55 import org.glom.web.shared.ColumnInfo;
56 import org.glom.web.shared.GlomDocument;
57 import org.glom.web.shared.GlomField;
58 import org.glom.web.shared.LayoutListTable;
60 import com.allen_sauer.gwt.log.client.Log;
61 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
62 import com.mchange.v2.c3p0.ComboPooledDataSource;
63 import com.mchange.v2.c3p0.DataSources;
65 @SuppressWarnings("serial")
66 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
67 private Document document = null;
68 private ComboPooledDataSource cpds = null;
69 // TODO implement locale
70 private final Locale locale = Locale.ROOT;
71 private boolean configured = false;
74 * This is called when the servlet is started or restarted.
76 public OnlineGlomServiceImpl() {
81 * The properties file can't be loaded in the constructor because the servlet is not yet initialised and
82 * getServletContext() will return null. The work-around for this problem is to initialise the database and glom
83 * document object when makes its first request.
85 private void configureServlet() {
86 document = new Document();
88 Properties props = new Properties();
89 String propFileName = "/WEB-INF/OnlineGlom.properties";
91 props.load(getServletContext().getResourceAsStream(propFileName));
92 } catch (IOException e) {
93 Log.fatal("Error loading " + propFileName, e);
94 // TODO can't continue, notify user of problem
97 File file = new File(props.getProperty("glomfile"));
98 document.set_file_uri("file://" + file.getAbsolutePath());
100 boolean retval = document.load(error);
101 if (retval == false) {
103 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
104 message = "Could not find " + file.getAbsolutePath();
106 message = "An unknown error occurred when trying to load " + file.getAbsolutePath();
109 // TODO can't continue, notify user of problem
112 // load the jdbc driver
113 cpds = new ComboPooledDataSource();
115 cpds.setDriverClass("org.postgresql.Driver");
116 } catch (PropertyVetoException e) {
117 Log.fatal("Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?",
119 // TODO can't continue, notify user of problem
122 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
123 + document.get_connection_database());
124 cpds.setUser(props.getProperty("dbusername"));
125 cpds.setPassword(props.getProperty("dbpassword"));
126 // TODO notify user if dbusername or dbpassword are wrong
131 * This is called when the servlet is stopped or restarted.
133 * @see javax.servlet.GenericServlet#destroy()
136 public void destroy() {
137 Glom.libglom_deinit();
140 DataSources.destroy(cpds);
141 } catch (SQLException e) {
142 Log.error("Error cleaning up the ComboPooledDataSource", e);
147 * FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
148 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
150 private static int safeLongToInt(long l) {
151 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
152 throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
157 public GlomDocument getGlomDocument() {
161 GlomDocument glomDocument = new GlomDocument();
163 // get arrays of table names and titles, and find the default table index
164 StringVector tablesVec = document.get_table_names();
166 int numTables = safeLongToInt(tablesVec.size());
167 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
169 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
170 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
171 boolean foundDefaultTable = false;
172 int visibleIndex = 0;
173 for (int i = 0; i < numTables; i++) {
174 String tableName = tablesVec.get(i);
175 if (!document.get_table_is_hidden(tableName)) {
176 tableNames.add(tableName);
177 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
178 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
179 glomDocument.setDefaultTableIndex(visibleIndex);
180 foundDefaultTable = true;
182 tableTitles.add(document.get_table_title(tableName));
187 // set everything we need
188 glomDocument.setTableNames(tableNames);
189 glomDocument.setTableTitles(tableTitles);
190 glomDocument.setTitle(document.get_database_title());
195 public LayoutListTable getLayoutListTable(String table) {
199 LayoutListTable tableInfo = new LayoutListTable();
201 // access the layout list
202 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
203 ColumnInfo[] columns = null;
204 LayoutFieldVector layoutFields = new LayoutFieldVector();
205 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
206 if (listViewLayoutGroupSize > 0) {
207 // a layout list is defined, we can use it to for the LayoutListTable
208 if (listViewLayoutGroupSize > 1)
209 Log.warn("The size of the list view layout group for table " + table
210 + " is greater than 1. Attempting to use the first item for the layout list view.");
211 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
213 // find the defined layout list fields
214 int numItems = safeLongToInt(layoutItemsVec.size());
215 columns = new ColumnInfo[numItems];
216 for (int i = 0; i < numItems; i++) {
217 // TODO add support for other LayoutItems (Text, Image, Button)
218 LayoutItem item = layoutItemsVec.get(i);
219 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
220 if (layoutItemField != null) {
221 layoutFields.add(layoutItemField);
222 columns[i] = new ColumnInfo(
223 layoutItemField.get_title_or_name(),
224 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
225 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
229 // no layout list is defined, use the table fields as the layout list
230 FieldVector fieldsVec = document.get_table_fields(table);
232 // find the fields to display in the layout list
233 int numItems = safeLongToInt(fieldsVec.size());
234 columns = new ColumnInfo[numItems];
235 for (int i = 0; i < numItems; i++) {
236 Field field = fieldsVec.get(i);
237 LayoutItem_Field layoutItemField = new LayoutItem_Field();
238 layoutItemField.set_full_field_details(field);
239 layoutFields.add(layoutItemField);
240 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
241 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
242 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
246 tableInfo.setColumns(columns);
248 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
250 Connection conn = null;
254 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
255 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
256 // data. Here's the relevant PostgreSQL documentation:
257 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
258 conn = cpds.getConnection();
259 conn.setAutoCommit(false);
260 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
261 String query = Glom.build_sql_select_count_simple(table, layoutFields);
262 // TODO Test execution time of this query with when the number of rows in the table is large (say >
263 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
264 rs = st.executeQuery(query);
266 // get the number of rows in the query
268 tableInfo.setNumRows(rs.getInt(1));
270 } catch (SQLException e) {
271 Log.error("Error calculating number of rows in the query. Setting number of rows to 0.", e);
272 tableInfo.setNumRows(0);
274 // cleanup everything that has been used
279 } catch (Exception e) {
280 Log.error("Error closing database resources. Subsequent database queries may not work.", e);
287 public ArrayList<GlomField[]> getTableData(String table, int start, int length) {
288 return getTableData(table, start, length, false, 0, false);
291 public ArrayList<GlomField[]> getSortedTableData(String table, int start, int length, int sortColumnIndex,
292 boolean isAscending) {
293 return getTableData(table, start, length, true, sortColumnIndex, isAscending);
296 private ArrayList<GlomField[]> getTableData(String table, int start, int length, boolean useSortClause,
297 int sortColumnIndex, boolean isAscending) {
301 // access the layout list using the defined layout list or the table fields if there's no layout list
302 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
303 LayoutFieldVector layoutFields = new LayoutFieldVector();
304 SortClause sortClause = new SortClause();
305 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
306 if (layoutListVec.size() > 0) {
307 // a layout list is defined, we can use it to for the LayoutListTable
308 if (listViewLayoutGroupSize > 1)
309 Log.warn("The size of the list view layout group for table " + table
310 + " is greater than 1. Attempting to use the first item for the layout list view.");
311 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
313 // find the defined layout list fields
314 int numItems = safeLongToInt(layoutItemsVec.size());
315 for (int i = 0; i < numItems; i++) {
316 // TODO add support for other LayoutItems (Text, Image, Button)
317 LayoutItem item = layoutItemsVec.get(i);
318 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
319 if (layoutItemfield != null) {
320 // use this field in the layout
321 layoutFields.add(layoutItemfield);
323 // create a sort clause if it's a primary key and we're not asked to sort a specific column
324 if (!useSortClause) {
325 Field details = layoutItemfield.get_full_field_details();
326 if (details != null && details.get_primary_key()) {
327 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
333 // no layout list is defined, use the table fields as the layout list
334 FieldVector fieldsVec = document.get_table_fields(table);
336 // find the fields to display in the layout list
337 int numItems = safeLongToInt(fieldsVec.size());
338 for (int i = 0; i < numItems; i++) {
339 Field field = fieldsVec.get(i);
340 LayoutItem_Field layoutItemField = new LayoutItem_Field();
341 layoutItemField.set_full_field_details(field);
342 layoutFields.add(layoutItemField);
344 // create a sort clause if it's a primary key and we're not asked to sort a specific column
345 if (!useSortClause) {
346 if (field.get_primary_key()) {
347 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
353 // create a sort clause for the column we've been asked to sort
355 LayoutItem item = layoutFields.get(sortColumnIndex);
356 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
358 sortClause.addLast(new SortFieldPair(field, isAscending));
360 Log.error("Error getting LayoutItem_Field for column index " + sortColumnIndex
361 + ". Cannot create a sort clause for this column.");
366 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
367 Connection conn = null;
371 // Setup and execute the query. Special care needs to be take to ensure that the results will be based on a
372 // cursor so that large amounts of memory are not consumed when the query retrieve a large amount of data.
373 // Here's the relevant PostgreSQL documentation:
374 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
375 conn = cpds.getConnection();
376 conn.setAutoCommit(false);
377 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
378 st.setFetchSize(length);
379 String query = Glom.build_sql_select_simple(table, layoutFields, sortClause) + " OFFSET " + start;
380 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
381 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
382 // memory footprint. Check the difference between this value before and after the query:
383 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
384 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
385 rs = st.executeQuery(query);
387 // get the data we've been asked for
389 while (rs.next() && rowCount <= length) {
390 int layoutFieldsSize = safeLongToInt(layoutFields.size());
391 GlomField[] rowArray = new GlomField[layoutFieldsSize];
392 for (int i = 0; i < layoutFieldsSize; i++) {
393 // make a new GlomField to set the text and colours
394 rowArray[i] = new GlomField();
396 // get foreground and background colours
397 LayoutItem_Field field = layoutFields.get(i);
398 FieldFormatting formatting = field.get_formatting_used();
399 String fgcolour = formatting.get_text_format_color_foreground();
400 if (!fgcolour.isEmpty())
401 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
402 String bgcolour = formatting.get_text_format_color_background();
403 if (!bgcolour.isEmpty())
404 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
406 // Convert the field value to a string based on the glom type. We're doing the formatting on the
407 // server side for now but it might be useful to move this to the client side.
408 switch (field.get_glom_type()) {
410 String text = rs.getString(i + 1);
411 rowArray[i].setText(text != null ? text : "");
414 rowArray[i].setBoolean(rs.getBoolean(i + 1));
417 // Take care of the numeric formatting before converting the number to a string.
418 NumericFormat numFormatGlom = formatting.getM_numeric_format();
419 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
420 // number should be formatted as a currency if the currency code string is not empty.
421 String currencyCode = numFormatGlom.getM_currency_symbol();
422 NumberFormat numFormatJava = null;
423 boolean useGlomCurrencyCode = false;
424 if (currencyCode.length() == 3) {
425 // Try to format the currency using the Java Locales system.
427 Currency currency = Currency.getInstance(currencyCode);
428 Log.info("A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
429 int digits = currency.getDefaultFractionDigits();
430 numFormatJava = NumberFormat.getCurrencyInstance(locale);
431 numFormatJava.setCurrency(currency);
432 numFormatJava.setMinimumFractionDigits(digits);
433 numFormatJava.setMaximumFractionDigits(digits);
434 } catch (IllegalArgumentException e) {
435 Log.warn(currencyCode
436 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
437 // The currency code is not this is not an ISO 4217 currency code.
438 // We're going to manually set the currency code and use the glom numeric formatting.
439 useGlomCurrencyCode = true;
440 numFormatJava = getJavaNumberFormat(numFormatGlom);
442 } else if (currencyCode.length() > 0) {
443 Log.warn(currencyCode
444 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
445 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
446 // We're going to manually set the currency code and use the glom numeric formatting.
447 useGlomCurrencyCode = true;
448 numFormatJava = getJavaNumberFormat(numFormatGlom);
450 // The length of the currency code is 0; the number is not a currency.
451 numFormatJava = getJavaNumberFormat(numFormatGlom);
454 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
456 double number = rs.getDouble(i + 1);
458 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
459 // overrides the set foreground colour
460 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
461 .get_alternative_color_for_negatives()));
464 // Finally convert the number to text using the glom currency string if required.
465 if (useGlomCurrencyCode) {
466 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
468 rowArray[i].setText(numFormatJava.format(number));
472 Date date = rs.getDate(i + 1);
474 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
475 rowArray[i].setText(dateFormat.format(date));
477 rowArray[i].setText("");
481 Time time = rs.getTime(i + 1);
483 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
484 rowArray[i].setText(timeFormat.format(time));
486 rowArray[i].setText("");
490 byte[] image = rs.getBytes(i + 1);
492 // TODO implement field TYPE_IMAGE
493 rowArray[i].setText("Image (FIXME)");
495 rowArray[i].setText("");
500 Log.warn("Invalid LayoutItem Field type. Using empty string for value.");
501 rowArray[i].setText("");
506 // add the row of GlomFields to the ArrayList we're going to return and update the row count
507 rowsList.add(rowArray);
510 } catch (SQLException e) {
511 Log.error("Error executing database query.", e);
512 // TODO: somehow notify user of problem
514 // cleanup everything that has been used
519 } catch (Exception e) {
520 Log.error("Error closing database resources. Subsequent database queries may not work.", e);
526 public ArrayList<String> getDemoDatabaseTitles() {
527 String glomDemoDir = "/home/ben";
529 File file = new File(glomDemoDir);
530 if (!file.isDirectory()) {
531 // TODO notify user of error
532 Log.fatal(glomDemoDir + " is not a directory.");
533 return new ArrayList<String>(0);
536 if (!file.canRead()) {
537 // TODO notify user of error
538 Log.fatal("Can't read the files in : " + glomDemoDir);
539 return new ArrayList<String>(0);
542 File[] glomFiles = file.listFiles(new FilenameFilter() {
544 public boolean accept(File dir, String name) {
545 return name.endsWith(".glom") ? true : false;
549 ArrayList<String> dbTitles = new ArrayList<String>();
550 for (File glomFile : glomFiles) {
551 Document curDoc = new Document();
552 curDoc.set_file_uri("file://" + glomFile.getAbsolutePath());
554 boolean retval = curDoc.load(error);
555 if (retval != false) {
556 dbTitles.add(curDoc.get_database_title());
562 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
563 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
564 if (numFormatGlom.getM_decimal_places_restricted()) {
565 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
566 numFormatJava.setMinimumFractionDigits(digits);
567 numFormatJava.setMaximumFractionDigits(digits);
569 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
570 return numFormatJava;
574 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
575 * significant 8-bits in each channel.
577 private String convertGdkColorToHtmlColour(String gdkColor) {
578 if (gdkColor.length() == 13)
579 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
580 else if (gdkColor.length() == 7) {
581 // FIXME will this happen in on 32-bit?
582 Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
585 Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
591 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
592 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
593 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
594 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
596 private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
597 FieldFormatting.HorizontalAlignment alignment) {
599 case HORIZONTAL_ALIGNMENT_AUTO:
600 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
601 case HORIZONTAL_ALIGNMENT_LEFT:
602 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
603 case HORIZONTAL_ALIGNMENT_RIGHT:
604 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
606 Log.error("getColumnInfoGlomFieldType(): Recieved an alignment that I don't know about: "
607 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
608 + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
609 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
614 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
615 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
616 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
619 private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
622 return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
624 return ColumnInfo.GlomFieldType.TYPE_DATE;
626 return ColumnInfo.GlomFieldType.TYPE_IMAGE;
628 return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
630 return ColumnInfo.GlomFieldType.TYPE_TEXT;
632 return ColumnInfo.GlomFieldType.TYPE_TIME;
634 Log.info("getColumnInfoGlomFieldType(): Returning TYPE_INVALID.");
635 return ColumnInfo.GlomFieldType.TYPE_INVALID;
637 Log.error("getColumnInfoGlomFieldType(): Recieved a type that I don't know about: "
638 + Field.glom_field_type.class.getName() + "." + type.toString() + ". Returning "
639 + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
640 return ColumnInfo.GlomFieldType.TYPE_INVALID;