Display an error if no glom documents are found in the specified directory.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / OnlineGlomServiceImpl.java
1 /*
2  * Copyright (C) 2010, 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.beans.PropertyVetoException;
23 import java.io.File;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.sql.Connection;
27 import java.sql.Date;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
31 import java.sql.Time;
32 import java.text.DateFormat;
33 import java.text.NumberFormat;
34 import java.util.ArrayList;
35 import java.util.Currency;
36 import java.util.Hashtable;
37 import java.util.Locale;
38 import java.util.Properties;
39
40 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
41 import org.glom.libglom.Document;
42 import org.glom.libglom.Field;
43 import org.glom.libglom.FieldFormatting;
44 import org.glom.libglom.FieldVector;
45 import org.glom.libglom.Glom;
46 import org.glom.libglom.LayoutFieldVector;
47 import org.glom.libglom.LayoutGroupVector;
48 import org.glom.libglom.LayoutItem;
49 import org.glom.libglom.LayoutItemVector;
50 import org.glom.libglom.LayoutItem_Field;
51 import org.glom.libglom.NumericFormat;
52 import org.glom.libglom.SortClause;
53 import org.glom.libglom.SortFieldPair;
54 import org.glom.libglom.StringVector;
55 import org.glom.web.client.OnlineGlomService;
56 import org.glom.web.shared.ColumnInfo;
57 import org.glom.web.shared.GlomDocument;
58 import org.glom.web.shared.GlomField;
59 import org.glom.web.shared.LayoutListTable;
60
61 import com.allen_sauer.gwt.log.client.Log;
62 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
63 import com.mchange.v2.c3p0.ComboPooledDataSource;
64 import com.mchange.v2.c3p0.DataSources;
65
66 @SuppressWarnings("serial")
67 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
68         private class ConfiguredDocument {
69                 private Document document;
70                 private ComboPooledDataSource cpds;
71
72                 public ConfiguredDocument(Document document, ComboPooledDataSource cpds) {
73                         this.cpds = cpds;
74                         this.document = document;
75                 }
76
77                 public Document getDocument() {
78                         return document;
79                 }
80
81                 public ComboPooledDataSource getCpds() {
82                         return cpds;
83                 }
84
85         }
86
87         private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
88
89         // TODO implement locale
90         private final Locale locale = Locale.ROOT;
91         private boolean configured = false;
92
93         /*
94          * This is called when the servlet is started or restarted.
95          */
96         public OnlineGlomServiceImpl() {
97                 Glom.libglom_init();
98
99         }
100
101         /*
102          * The properties file can't be loaded in the constructor because the servlet is not yet initialised and
103          * getServletContext() will return null. The work-around for this problem is to initialise the database and glom
104          * document object when makes its first request.
105          */
106         private void configureServlet() {
107                 // TODO get this from a config file
108                 String glomDemoDir = "/home/ben";
109
110                 File file = new File(glomDemoDir);
111
112                 if (!file.isDirectory()) {
113                         // TODO notify user of error
114                         Log.fatal(glomDemoDir + " is not a directory.");
115                         return;
116                 }
117
118                 if (!file.canRead()) {
119                         // TODO notify user of error
120                         Log.fatal("Can't read the files in : " + glomDemoDir);
121                         return;
122                 }
123
124                 File[] glomFiles = file.listFiles(new FilenameFilter() {
125                         @Override
126                         public boolean accept(File dir, String name) {
127                                 return name.endsWith(".glom") ? true : false;
128                         }
129                 });
130
131                 for (File glomFile : glomFiles) {
132                         Document document = new Document();
133                         document.set_file_uri("file://" + glomFile.getAbsolutePath());
134                         int error = 0;
135                         boolean retval = document.load(error);
136                         if (retval == false) {
137                                 String message;
138                                 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
139                                         message = "Could not find " + file.getAbsolutePath();
140                                 } else {
141                                         message = "An unknown error occurred when trying to load " + file.getAbsolutePath();
142                                 }
143                                 Log.error(message);
144                                 continue;
145                         }
146
147                         Properties props = new Properties();
148                         String propFileName = "/WEB-INF/OnlineGlom.properties";
149                         try {
150                                 props.load(getServletContext().getResourceAsStream(propFileName));
151                         } catch (IOException e) {
152                                 Log.fatal("Error loading " + propFileName, e);
153                                 // TODO can't continue, notify user of problem
154                         }
155
156                         // load the jdbc driver for this document
157                         ComboPooledDataSource cpds = new ComboPooledDataSource();
158                         try {
159                                 cpds.setDriverClass("org.postgresql.Driver");
160                         } catch (PropertyVetoException e) {
161                                 Log.fatal(
162                                                 "Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?",
163                                                 e);
164                                 // TODO can't continue, notify user of problem
165                                 break;
166                         }
167
168                         cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
169                                         + document.get_connection_database());
170                         // TODO notify user if dbusername or dbpassword are wrong
171                         cpds.setUser(props.getProperty("dbusername"));
172                         cpds.setPassword(props.getProperty("dbpassword"));
173
174                         String documentTitle = document.get_database_title().trim();
175                         ConfiguredDocument configuredDocument = new ConfiguredDocument(document, cpds);
176                         documents.put(documentTitle, configuredDocument);
177                 }
178
179                 configured = true;
180         }
181
182         /*
183          * This is called when the servlet is stopped or restarted.
184          * 
185          * @see javax.servlet.GenericServlet#destroy()
186          */
187         @Override
188         public void destroy() {
189                 Glom.libglom_deinit();
190
191                 if (configured) {
192                         for (String documenTitle : documents.keySet()) {
193                                 ConfiguredDocument configuredDoc = documents.get(documenTitle);
194                                 try {
195                                         DataSources.destroy(configuredDoc.getCpds());
196                                 } catch (SQLException e) {
197                                         Log.error("Error cleaning up the ComboPooledDataSource for " + documenTitle, e);
198                                 }
199                         }
200                 }
201
202         }
203
204         /*
205          * FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
206          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
207          */
208         private static int safeLongToInt(long l) {
209                 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
210                         throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
211                 }
212                 return (int) l;
213         }
214
215         public GlomDocument getGlomDocument(String documentTitle) {
216                 if (!configured)
217                         configureServlet();
218
219                 Document document = documents.get(documentTitle).getDocument();
220                 GlomDocument glomDocument = new GlomDocument();
221
222                 // get arrays of table names and titles, and find the default table index
223                 StringVector tablesVec = document.get_table_names();
224
225                 int numTables = safeLongToInt(tablesVec.size());
226                 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
227                 // of the ArrayList
228                 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
229                 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
230                 boolean foundDefaultTable = false;
231                 int visibleIndex = 0;
232                 for (int i = 0; i < numTables; i++) {
233                         String tableName = tablesVec.get(i);
234                         if (!document.get_table_is_hidden(tableName)) {
235                                 tableNames.add(tableName);
236                                 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
237                                 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
238                                         glomDocument.setDefaultTableIndex(visibleIndex);
239                                         foundDefaultTable = true;
240                                 }
241                                 tableTitles.add(document.get_table_title(tableName));
242                                 visibleIndex++;
243                         }
244                 }
245
246                 // set everything we need
247                 glomDocument.setTableNames(tableNames);
248                 glomDocument.setTableTitles(tableTitles);
249
250                 return glomDocument;
251         }
252
253         public LayoutListTable getLayoutListTable(String documentTitle, String table) {
254                 if (!configured)
255                         configureServlet();
256
257                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
258                 Document document = configuredDoc.getDocument();
259                 LayoutListTable tableInfo = new LayoutListTable();
260
261                 // access the layout list
262                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
263                 ColumnInfo[] columns = null;
264                 LayoutFieldVector layoutFields = new LayoutFieldVector();
265                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
266                 if (listViewLayoutGroupSize > 0) {
267                         // a layout list is defined, we can use it to for the LayoutListTable
268                         if (listViewLayoutGroupSize > 1)
269                                 Log.warn(documentTitle + " " + table + ": The size of the list view layout group for table " + table
270                                                 + " is greater than 1. Attempting to use the first item for the layout list view.");
271                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
272
273                         // find the defined layout list fields
274                         int numItems = safeLongToInt(layoutItemsVec.size());
275                         columns = new ColumnInfo[numItems];
276                         for (int i = 0; i < numItems; i++) {
277                                 // TODO add support for other LayoutItems (Text, Image, Button)
278                                 LayoutItem item = layoutItemsVec.get(i);
279                                 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
280                                 if (layoutItemField != null) {
281                                         layoutFields.add(layoutItemField);
282                                         columns[i] = new ColumnInfo(
283                                                         layoutItemField.get_title_or_name(),
284                                                         getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
285                                                         getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
286                                 }
287                         }
288                 } else {
289                         // no layout list is defined, use the table fields as the layout list
290                         FieldVector fieldsVec = document.get_table_fields(table);
291
292                         // find the fields to display in the layout list
293                         int numItems = safeLongToInt(fieldsVec.size());
294                         columns = new ColumnInfo[numItems];
295                         for (int i = 0; i < numItems; i++) {
296                                 Field field = fieldsVec.get(i);
297                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
298                                 layoutItemField.set_full_field_details(field);
299                                 layoutFields.add(layoutItemField);
300                                 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
301                                                 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
302                                                 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
303                         }
304                 }
305
306                 tableInfo.setColumns(columns);
307
308                 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
309                 // list view pager.
310                 Connection conn = null;
311                 Statement st = null;
312                 ResultSet rs = null;
313                 try {
314                         // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
315                         // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
316                         // data. Here's the relevant PostgreSQL documentation:
317                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
318                         ComboPooledDataSource cpds = configuredDoc.getCpds();
319                         conn = cpds.getConnection();
320                         conn.setAutoCommit(false);
321                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
322                         String query = Glom.build_sql_select_count_simple(table, layoutFields);
323                         // TODO Test execution time of this query with when the number of rows in the table is large (say >
324                         // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
325                         rs = st.executeQuery(query);
326
327                         // get the number of rows in the query
328                         rs.next();
329                         tableInfo.setNumRows(rs.getInt(1));
330
331                 } catch (SQLException e) {
332                         Log.error(documentTitle + " " + table
333                                         + ": Error calculating number of rows in the query. Setting number of rows to 0.", e);
334                         tableInfo.setNumRows(0);
335                 } finally {
336                         // cleanup everything that has been used
337                         try {
338                                 rs.close();
339                                 st.close();
340                                 conn.close();
341                         } catch (Exception e) {
342                                 Log.error(documentTitle + " " + table
343                                                 + ": Error closing database resources. Subsequent database queries may not work.", e);
344                         }
345                 }
346
347                 return tableInfo;
348         }
349
350         public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
351                 return getTableData(documentTitle, tableName, start, length, false, 0, false);
352         }
353
354         public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
355                         int sortColumnIndex, boolean isAscending) {
356                 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
357         }
358
359         private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
360                         boolean useSortClause, int sortColumnIndex, boolean isAscending) {
361
362                 // FIXME fix LayoutListView to not call this method with empty table or document title
363                 if (documentTitle.isEmpty() || tableName.isEmpty())
364                         return new ArrayList<GlomField[]>();
365
366                 if (!configured)
367                         configureServlet();
368
369                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
370                 Document document = configuredDoc.getDocument();
371
372                 // access the layout list using the defined layout list or the table fields if there's no layout list
373                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
374                 LayoutFieldVector layoutFields = new LayoutFieldVector();
375                 SortClause sortClause = new SortClause();
376                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
377                 if (layoutListVec.size() > 0) {
378                         // a layout list is defined, we can use it to for the LayoutListTable
379                         if (listViewLayoutGroupSize > 1)
380                                 Log.warn(documentTitle + ": The size of the list view layout group for table " + tableName
381                                                 + " is greater than 1. Attempting to use the first item for the layout list view.");
382                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
383
384                         // find the defined layout list fields
385                         int numItems = safeLongToInt(layoutItemsVec.size());
386                         for (int i = 0; i < numItems; i++) {
387                                 // TODO add support for other LayoutItems (Text, Image, Button)
388                                 LayoutItem item = layoutItemsVec.get(i);
389                                 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
390                                 if (layoutItemfield != null) {
391                                         // use this field in the layout
392                                         layoutFields.add(layoutItemfield);
393
394                                         // create a sort clause if it's a primary key and we're not asked to sort a specific column
395                                         if (!useSortClause) {
396                                                 Field details = layoutItemfield.get_full_field_details();
397                                                 if (details != null && details.get_primary_key()) {
398                                                         sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
399                                                 }
400                                         }
401                                 }
402                         }
403                 } else {
404                         // no layout list is defined, use the table fields as the layout list
405                         FieldVector fieldsVec = document.get_table_fields(tableName);
406
407                         // find the fields to display in the layout list
408                         int numItems = safeLongToInt(fieldsVec.size());
409                         for (int i = 0; i < numItems; i++) {
410                                 Field field = fieldsVec.get(i);
411                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
412                                 layoutItemField.set_full_field_details(field);
413                                 layoutFields.add(layoutItemField);
414
415                                 // create a sort clause if it's a primary key and we're not asked to sort a specific column
416                                 if (!useSortClause) {
417                                         if (field.get_primary_key()) {
418                                                 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
419                                         }
420                                 }
421                         }
422                 }
423
424                 // create a sort clause for the column we've been asked to sort
425                 if (useSortClause) {
426                         LayoutItem item = layoutFields.get(sortColumnIndex);
427                         LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
428                         if (field != null)
429                                 sortClause.addLast(new SortFieldPair(field, isAscending));
430                         else {
431                                 Log.error(documentTitle + " " + tableName + ": Error getting LayoutItem_Field for column index "
432                                                 + sortColumnIndex + ". Cannot create a sort clause for this column.");
433                         }
434
435                 }
436
437                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
438                 Connection conn = null;
439                 Statement st = null;
440                 ResultSet rs = null;
441                 try {
442                         // Setup and execute the query. Special care needs to be take to ensure that the results will be based on a
443                         // cursor so that large amounts of memory are not consumed when the query retrieve a large amount of data.
444                         // Here's the relevant PostgreSQL documentation:
445                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
446                         ComboPooledDataSource cpds = configuredDoc.getCpds();
447                         conn = cpds.getConnection();
448                         conn.setAutoCommit(false);
449                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
450                         st.setFetchSize(length);
451                         String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
452                         // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
453                         // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
454                         // memory footprint. Check the difference between this value before and after the query:
455                         // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
456                         // Test the execution time at the same time (see the todo item in getLayoutListTable()).
457                         rs = st.executeQuery(query);
458
459                         // get the data we've been asked for
460                         int rowCount = 0;
461                         while (rs.next() && rowCount <= length) {
462                                 int layoutFieldsSize = safeLongToInt(layoutFields.size());
463                                 GlomField[] rowArray = new GlomField[layoutFieldsSize];
464                                 for (int i = 0; i < layoutFieldsSize; i++) {
465                                         // make a new GlomField to set the text and colours
466                                         rowArray[i] = new GlomField();
467
468                                         // get foreground and background colours
469                                         LayoutItem_Field field = layoutFields.get(i);
470                                         FieldFormatting formatting = field.get_formatting_used();
471                                         String fgcolour = formatting.get_text_format_color_foreground();
472                                         if (!fgcolour.isEmpty())
473                                                 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
474                                         String bgcolour = formatting.get_text_format_color_background();
475                                         if (!bgcolour.isEmpty())
476                                                 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
477
478                                         // Convert the field value to a string based on the glom type. We're doing the formatting on the
479                                         // server side for now but it might be useful to move this to the client side.
480                                         switch (field.get_glom_type()) {
481                                         case TYPE_TEXT:
482                                                 String text = rs.getString(i + 1);
483                                                 rowArray[i].setText(text != null ? text : "");
484                                                 break;
485                                         case TYPE_BOOLEAN:
486                                                 rowArray[i].setBoolean(rs.getBoolean(i + 1));
487                                                 break;
488                                         case TYPE_NUMERIC:
489                                                 // Take care of the numeric formatting before converting the number to a string.
490                                                 NumericFormat numFormatGlom = formatting.getM_numeric_format();
491                                                 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
492                                                 // number should be formatted as a currency if the currency code string is not empty.
493                                                 String currencyCode = numFormatGlom.getM_currency_symbol();
494                                                 NumberFormat numFormatJava = null;
495                                                 boolean useGlomCurrencyCode = false;
496                                                 if (currencyCode.length() == 3) {
497                                                         // Try to format the currency using the Java Locales system.
498                                                         try {
499                                                                 Currency currency = Currency.getInstance(currencyCode);
500                                                                 Log.info(documentTitle
501                                                                                 + " "
502                                                                                 + tableName
503                                                                                 + ": A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
504                                                                 int digits = currency.getDefaultFractionDigits();
505                                                                 numFormatJava = NumberFormat.getCurrencyInstance(locale);
506                                                                 numFormatJava.setCurrency(currency);
507                                                                 numFormatJava.setMinimumFractionDigits(digits);
508                                                                 numFormatJava.setMaximumFractionDigits(digits);
509                                                         } catch (IllegalArgumentException e) {
510                                                                 Log.warn(documentTitle
511                                                                                 + " "
512                                                                                 + tableName
513                                                                                 + ": "
514                                                                                 + currencyCode
515                                                                                 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
516                                                                 // The currency code is not this is not an ISO 4217 currency code.
517                                                                 // We're going to manually set the currency code and use the glom numeric formatting.
518                                                                 useGlomCurrencyCode = true;
519                                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
520                                                         }
521                                                 } else if (currencyCode.length() > 0) {
522                                                         Log.warn(documentTitle + " " + tableName + ": " + currencyCode
523                                                                         + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
524                                                         // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
525                                                         // We're going to manually set the currency code and use the glom numeric formatting.
526                                                         useGlomCurrencyCode = true;
527                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
528                                                 } else {
529                                                         // The length of the currency code is 0; the number is not a currency.
530                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
531                                                 }
532
533                                                 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
534
535                                                 double number = rs.getDouble(i + 1);
536                                                 if (number < 0) {
537                                                         if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
538                                                                 // overrides the set foreground colour
539                                                                 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
540                                                                                 .get_alternative_color_for_negatives()));
541                                                 }
542
543                                                 // Finally convert the number to text using the glom currency string if required.
544                                                 if (useGlomCurrencyCode) {
545                                                         rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
546                                                 } else {
547                                                         rowArray[i].setText(numFormatJava.format(number));
548                                                 }
549                                                 break;
550                                         case TYPE_DATE:
551                                                 Date date = rs.getDate(i + 1);
552                                                 if (date != null) {
553                                                         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
554                                                         rowArray[i].setText(dateFormat.format(date));
555                                                 } else {
556                                                         rowArray[i].setText("");
557                                                 }
558                                                 break;
559                                         case TYPE_TIME:
560                                                 Time time = rs.getTime(i + 1);
561                                                 if (time != null) {
562                                                         DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
563                                                         rowArray[i].setText(timeFormat.format(time));
564                                                 } else {
565                                                         rowArray[i].setText("");
566                                                 }
567                                                 break;
568                                         case TYPE_IMAGE:
569                                                 byte[] image = rs.getBytes(i + 1);
570                                                 if (image != null) {
571                                                         // TODO implement field TYPE_IMAGE
572                                                         rowArray[i].setText("Image (FIXME)");
573                                                 } else {
574                                                         rowArray[i].setText("");
575                                                 }
576                                                 break;
577                                         case TYPE_INVALID:
578                                         default:
579                                                 Log.warn(documentTitle + " " + tableName
580                                                                 + ": Invalid LayoutItem Field type. Using empty string for value.");
581                                                 rowArray[i].setText("");
582                                                 break;
583                                         }
584                                 }
585
586                                 // add the row of GlomFields to the ArrayList we're going to return and update the row count
587                                 rowsList.add(rowArray);
588                                 rowCount++;
589                         }
590                 } catch (SQLException e) {
591                         Log.error(documentTitle + " " + tableName + ": Error executing database query.", e);
592                         // TODO: somehow notify user of problem
593                 } finally {
594                         // cleanup everything that has been used
595                         try {
596                                 rs.close();
597                                 st.close();
598                                 conn.close();
599                         } catch (Exception e) {
600                                 Log.error(documentTitle + " " + tableName
601                                                 + ": Error closing database resources. Subsequent database queries may not work.", e);
602                         }
603                 }
604                 return rowsList;
605         }
606
607         public ArrayList<String> getDemoDatabaseTitles() {
608                 if (!configured)
609                         configureServlet();
610
611                 ArrayList<String> documentTitles = new ArrayList<String>();
612                 for (String title : documents.keySet()) {
613                         documentTitles.add(title);
614                 }
615                 return documentTitles;
616         }
617
618         private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
619                 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
620                 if (numFormatGlom.getM_decimal_places_restricted()) {
621                         int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
622                         numFormatJava.setMinimumFractionDigits(digits);
623                         numFormatJava.setMaximumFractionDigits(digits);
624                 }
625                 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
626                 return numFormatJava;
627         }
628
629         /*
630          * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
631          * significant 8-bits in each channel.
632          */
633         private String convertGdkColorToHtmlColour(String gdkColor) {
634                 if (gdkColor.length() == 13)
635                         return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
636                 else if (gdkColor.length() == 7) {
637                         // FIXME will this happen in on 32-bit?
638                         Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
639                         return gdkColor;
640                 } else {
641                         Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
642                         return "#000000";
643                 }
644         }
645
646         /*
647          * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
648          * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
649          * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
650          * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
651          */
652         private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
653                         FieldFormatting.HorizontalAlignment alignment) {
654                 switch (alignment) {
655                 case HORIZONTAL_ALIGNMENT_AUTO:
656                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
657                 case HORIZONTAL_ALIGNMENT_LEFT:
658                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
659                 case HORIZONTAL_ALIGNMENT_RIGHT:
660                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
661                 default:
662                         Log.error("getColumnInfoGlomFieldType(): Recieved an alignment that I don't know about: "
663                                         + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
664                                         + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
665                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
666                 }
667         }
668
669         /*
670          * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
671          * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
672          * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
673          * ColumnInfo class.
674          */
675         private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
676                 switch (type) {
677                 case TYPE_BOOLEAN:
678                         return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
679                 case TYPE_DATE:
680                         return ColumnInfo.GlomFieldType.TYPE_DATE;
681                 case TYPE_IMAGE:
682                         return ColumnInfo.GlomFieldType.TYPE_IMAGE;
683                 case TYPE_NUMERIC:
684                         return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
685                 case TYPE_TEXT:
686                         return ColumnInfo.GlomFieldType.TYPE_TEXT;
687                 case TYPE_TIME:
688                         return ColumnInfo.GlomFieldType.TYPE_TIME;
689                 case TYPE_INVALID:
690                         Log.info("getColumnInfoGlomFieldType(): Returning TYPE_INVALID.");
691                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
692                 default:
693                         Log.error("getColumnInfoGlomFieldType(): Recieved a type that I don't know about: "
694                                         + Field.glom_field_type.class.getName() + "." + type.toString() + ". Returning "
695                                         + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
696                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
697                 }
698         }
699
700 }