Add data to the DetailsView using a hard-coded primary key value.
[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.io.InputStream;
27 import java.sql.Connection;
28 import java.sql.Date;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
32 import java.sql.Time;
33 import java.text.DateFormat;
34 import java.text.NumberFormat;
35 import java.util.ArrayList;
36 import java.util.Currency;
37 import java.util.Hashtable;
38 import java.util.Locale;
39 import java.util.Properties;
40
41 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
42 import org.glom.libglom.Document;
43 import org.glom.libglom.Field;
44 import org.glom.libglom.FieldFormatting;
45 import org.glom.libglom.FieldVector;
46 import org.glom.libglom.Glom;
47 import org.glom.libglom.LayoutFieldVector;
48 import org.glom.libglom.LayoutGroupVector;
49 import org.glom.libglom.LayoutItem;
50 import org.glom.libglom.LayoutItemVector;
51 import org.glom.libglom.LayoutItem_Field;
52 import org.glom.libglom.LayoutItem_Portal;
53 import org.glom.libglom.NumericFormat;
54 import org.glom.libglom.SortClause;
55 import org.glom.libglom.SortFieldPair;
56 import org.glom.libglom.StringVector;
57 import org.glom.web.client.OnlineGlomService;
58 import org.glom.web.shared.ColumnInfo;
59 import org.glom.web.shared.GlomDocument;
60 import org.glom.web.shared.GlomField;
61 import org.glom.web.shared.LayoutListTable;
62 import org.glom.web.shared.layout.LayoutGroup;
63 import org.glom.web.shared.layout.LayoutItemField;
64
65 import com.allen_sauer.gwt.log.client.Log;
66 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
67 import com.mchange.v2.c3p0.ComboPooledDataSource;
68 import com.mchange.v2.c3p0.DataSources;
69
70 @SuppressWarnings("serial")
71 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
72
73         // class to hold configuration information for related to the glom document and db access
74         private class ConfiguredDocument {
75                 private Document document;
76                 private ComboPooledDataSource cpds;
77                 private boolean authenticated = false;
78
79                 // @formatter:off
80                 public Document getDocument() { return document; }
81                 public void setDocument(Document document) { this.document = document; }
82                 public ComboPooledDataSource getCpds() { return cpds; }
83                 public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds;     }
84                 public boolean isAuthenticated() { return authenticated; }
85                 public void setAuthenticated(boolean authenticated) { this.authenticated = authenticated; }
86                 // @formatter:on
87         }
88
89         // convenience class to for dealing with the Online Glom configuration file
90         private class OnlineGlomProperties extends Properties {
91                 public String getKey(String value) {
92                         for (String key : stringPropertyNames()) {
93                                 if (getProperty(key).trim().equals(value))
94                                         return key;
95                         }
96                         return null;
97                 }
98         }
99
100         private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
101         // TODO implement locale
102         private final Locale locale = Locale.ROOT;
103
104         /*
105          * This is called when the servlet is started or restarted.
106          */
107         public OnlineGlomServiceImpl() throws Exception {
108
109                 // Find the configuration file. See this thread for background info:
110                 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
111                 OnlineGlomProperties config = new OnlineGlomProperties();
112                 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
113                 if (is == null) {
114                         Log.fatal("onlineglom.properties not found.");
115                         throw new IOException();
116                 }
117                 config.load(is);
118
119                 // check the configured glom file directory
120                 String documentDirName = config.getProperty("glom.document.directory");
121                 File documentDir = new File(documentDirName);
122                 if (!documentDir.isDirectory()) {
123                         Log.fatal(documentDirName + " is not a directory.");
124                         throw new IOException();
125                 }
126                 if (!documentDir.canRead()) {
127                         Log.fatal("Can't read the files in : " + documentDirName);
128                         throw new IOException();
129                 }
130
131                 // get and check the glom files in the specified directory
132                 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
133                         @Override
134                         public boolean accept(File dir, String name) {
135                                 return name.endsWith(".glom") ? true : false;
136                         }
137                 });
138                 Glom.libglom_init();
139                 for (File glomFile : glomFiles) {
140                         Document document = new Document();
141                         document.set_file_uri("file://" + glomFile.getAbsolutePath());
142                         int error = 0;
143                         boolean retval = document.load(error);
144                         if (retval == false) {
145                                 String message;
146                                 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
147                                         message = "Could not find " + documentDir.getAbsolutePath();
148                                 } else {
149                                         message = "An unknown error occurred when trying to load " + documentDir.getAbsolutePath();
150                                 }
151                                 Log.error(message);
152                                 // continue with for loop because there may be other documents in the directory
153                                 continue;
154                         }
155
156                         // load the jdbc driver for the current glom document
157                         ComboPooledDataSource cpds = new ComboPooledDataSource();
158
159                         try {
160                                 cpds.setDriverClass("org.postgresql.Driver");
161                         } catch (PropertyVetoException e) {
162                                 Log.fatal("Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?");
163                                 throw e;
164                         }
165
166                         // setup the JDBC driver for the current glom document
167                         cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
168                                         + document.get_connection_database());
169
170                         // check if a username and password have been set and work for the current document
171                         String documentTitle = document.get_database_title().trim();
172                         ConfiguredDocument configuredDocument = new ConfiguredDocument();
173                         String key = config.getKey(documentTitle);
174                         if (key != null) {
175                                 String[] keyArray = key.split("\\.");
176                                 if (keyArray.length == 3 && "title".equals(keyArray[2])) {
177                                         // username/password could be set, let's check to see if it works
178                                         String usernameKey = key.replaceAll(keyArray[2], "username");
179                                         String passwordKey = key.replaceAll(keyArray[2], "password");
180                                         configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
181                                                         config.getProperty(usernameKey), config.getProperty(passwordKey)));
182                                 }
183                         }
184
185                         // check the if the global username and password have been set and work with this document
186                         if (!configuredDocument.isAuthenticated()) {
187                                 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
188                                                 config.getProperty("glom.document.username"), config.getProperty("glom.document.password")));
189                         }
190
191                         // add information to the hash table
192                         configuredDocument.setDocument(document);
193                         configuredDocument.setCpds(cpds);
194                         documents.put(documentTitle, configuredDocument);
195                 }
196         }
197
198         /*
199          * Checks if the username and password works with the database configured with the specified ComboPooledDataSource.
200          * 
201          * @return true if authentication works, false otherwise
202          */
203         private boolean checkAuthentication(String documentTitle, ComboPooledDataSource cpds, String username,
204                         String password) throws SQLException {
205                 cpds.setUser(username);
206                 cpds.setPassword(password);
207
208                 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
209                 cpds.setAcquireRetryAttempts(1);
210                 Connection conn = null;
211                 try {
212                         // FIXME find a better way to check authentication
213                         // it's possible that the connection could be failing for another reason
214                         conn = cpds.getConnection();
215                         return true;
216                 } catch (SQLException e) {
217                         Log.info("Username and password not correct for document: " + documentTitle);
218                 } finally {
219                         if (conn != null)
220                                 conn.close();
221                         cpds.setAcquireRetryAttempts(acquireRetryAttempts);
222                 }
223                 return false;
224         }
225
226         /*
227          * This is called when the servlet is stopped or restarted.
228          * 
229          * @see javax.servlet.GenericServlet#destroy()
230          */
231         @Override
232         public void destroy() {
233                 Glom.libglom_deinit();
234
235                 for (String documenTitle : documents.keySet()) {
236                         ConfiguredDocument configuredDoc = documents.get(documenTitle);
237                         try {
238                                 DataSources.destroy(configuredDoc.getCpds());
239                         } catch (SQLException e) {
240                                 Log.error("Error cleaning up the ComboPooledDataSource for " + documenTitle, e);
241                         }
242                 }
243
244         }
245
246         public GlomDocument getGlomDocument(String documentTitle) {
247
248                 Document document = documents.get(documentTitle).getDocument();
249                 GlomDocument glomDocument = new GlomDocument();
250
251                 // get arrays of table names and titles, and find the default table index
252                 StringVector tablesVec = document.get_table_names();
253
254                 int numTables = safeLongToInt(tablesVec.size());
255                 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
256                 // of the ArrayList
257                 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
258                 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
259                 boolean foundDefaultTable = false;
260                 int visibleIndex = 0;
261                 for (int i = 0; i < numTables; i++) {
262                         String tableName = tablesVec.get(i);
263                         if (!document.get_table_is_hidden(tableName)) {
264                                 tableNames.add(tableName);
265                                 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
266                                 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
267                                         glomDocument.setDefaultTableIndex(visibleIndex);
268                                         foundDefaultTable = true;
269                                 }
270                                 tableTitles.add(document.get_table_title(tableName));
271                                 visibleIndex++;
272                         }
273                 }
274
275                 // set everything we need
276                 glomDocument.setTableNames(tableNames);
277                 glomDocument.setTableTitles(tableTitles);
278
279                 return glomDocument;
280         }
281
282         /*
283          * (non-Javadoc)
284          * 
285          * @see org.glom.web.client.OnlineGlomService#getDefaultLayoutListTable(java.lang.String)
286          */
287         @Override
288         public LayoutListTable getDefaultLayoutListTable(String documentTitle) {
289                 GlomDocument glomDocument = getGlomDocument(documentTitle);
290                 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
291                 LayoutListTable layoutListTable = getLayoutListTable(documentTitle, tableName);
292                 layoutListTable.setTableName(tableName);
293                 return layoutListTable;
294         }
295
296         public LayoutListTable getLayoutListTable(String documentTitle, String table) {
297                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
298                 Document document = configuredDoc.getDocument();
299                 LayoutListTable tableInfo = new LayoutListTable();
300
301                 // access the layout list
302                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
303                 ColumnInfo[] columns = null;
304                 LayoutFieldVector layoutFields = new LayoutFieldVector();
305                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
306                 if (listViewLayoutGroupSize > 0) {
307                         // a layout list is defined, we can use it to for the LayoutListTable
308                         if (listViewLayoutGroupSize > 1)
309                                 Log.warn(documentTitle + " - " + table + ": 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();
312
313                         // find the defined layout list fields
314                         int numItems = safeLongToInt(layoutItemsVec.size());
315                         columns = new ColumnInfo[numItems];
316                         for (int i = 0; i < numItems; i++) {
317                                 // TODO add support for other LayoutItems (Text, Image, Button)
318                                 LayoutItem item = layoutItemsVec.get(i);
319                                 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
320                                 if (layoutItemField != null) {
321                                         layoutFields.add(layoutItemField);
322                                         columns[i] = new ColumnInfo(
323                                                         layoutItemField.get_title_or_name(),
324                                                         getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
325                                                         getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
326                                 }
327                         }
328                 } else {
329                         // no layout list is defined, use the table fields as the layout list
330                         FieldVector fieldsVec = document.get_table_fields(table);
331
332                         // find the fields to display in the layout list
333                         int numItems = safeLongToInt(fieldsVec.size());
334                         columns = new ColumnInfo[numItems];
335                         for (int i = 0; i < numItems; i++) {
336                                 Field field = fieldsVec.get(i);
337                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
338                                 layoutItemField.set_full_field_details(field);
339                                 layoutFields.add(layoutItemField);
340                                 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
341                                                 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
342                                                 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
343                         }
344                 }
345
346                 tableInfo.setColumns(columns);
347
348                 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
349                 // list view pager.
350                 if (!configuredDoc.isAuthenticated())
351                         return tableInfo;
352                 Connection conn = null;
353                 Statement st = null;
354                 ResultSet rs = null;
355                 try {
356                         // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
357                         // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
358                         // data. Here's the relevant PostgreSQL documentation:
359                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
360                         ComboPooledDataSource cpds = configuredDoc.getCpds();
361                         conn = cpds.getConnection();
362                         conn.setAutoCommit(false);
363                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
364                         String query = Glom.build_sql_select_count_simple(table, layoutFields);
365                         // TODO Test execution time of this query with when the number of rows in the table is large (say >
366                         // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
367                         rs = st.executeQuery(query);
368
369                         // get the number of rows in the query
370                         rs.next();
371                         tableInfo.setNumRows(rs.getInt(1));
372
373                 } catch (SQLException e) {
374                         Log.error(documentTitle + " - " + table
375                                         + ": Error calculating number of rows in the query. Setting number of rows to 0.", e);
376                         tableInfo.setNumRows(0);
377                 } finally {
378                         // cleanup everything that has been used
379                         try {
380                                 if (rs != null)
381                                         rs.close();
382                                 if (st != null)
383                                         st.close();
384                                 if (conn != null)
385                                         conn.close();
386                         } catch (Exception e) {
387                                 Log.error(documentTitle + " - " + table
388                                                 + ": Error closing database resources. Subsequent database queries may not work.", e);
389                         }
390                 }
391
392                 return tableInfo;
393         }
394
395         public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
396                 return getTableData(documentTitle, tableName, start, length, false, 0, false);
397         }
398
399         public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
400                         int sortColumnIndex, boolean isAscending) {
401                 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
402         }
403
404         private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
405                         boolean useSortClause, int sortColumnIndex, boolean isAscending) {
406
407                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
408                 if (!configuredDoc.isAuthenticated())
409                         return new ArrayList<GlomField[]>();
410                 Document document = configuredDoc.getDocument();
411
412                 // access the layout list using the defined layout list or the table fields if there's no layout list
413                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
414                 LayoutFieldVector layoutFields = new LayoutFieldVector();
415                 SortClause sortClause = new SortClause();
416                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
417                 if (layoutListVec.size() > 0) {
418                         // a layout list is defined, we can use it to for the LayoutListTable
419                         if (listViewLayoutGroupSize > 1)
420                                 Log.warn(documentTitle + ": The size of the list view layout group for table " + tableName
421                                                 + " is greater than 1. Attempting to use the first item for the layout list view.");
422                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
423
424                         // find the defined layout list fields
425                         int numItems = safeLongToInt(layoutItemsVec.size());
426                         for (int i = 0; i < numItems; i++) {
427                                 // TODO add support for other LayoutItems (Text, Image, Button)
428                                 LayoutItem item = layoutItemsVec.get(i);
429                                 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
430                                 if (layoutItemfield != null) {
431                                         // use this field in the layout
432                                         layoutFields.add(layoutItemfield);
433
434                                         // create a sort clause if it's a primary key and we're not asked to sort a specific column
435                                         if (!useSortClause) {
436                                                 Field details = layoutItemfield.get_full_field_details();
437                                                 if (details != null && details.get_primary_key()) {
438                                                         sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
439                                                 }
440                                         }
441                                 }
442                         }
443                 } else {
444                         // no layout list is defined, use the table fields as the layout list
445                         FieldVector fieldsVec = document.get_table_fields(tableName);
446
447                         // find the fields to display in the layout list
448                         int numItems = safeLongToInt(fieldsVec.size());
449                         for (int i = 0; i < numItems; i++) {
450                                 Field field = fieldsVec.get(i);
451                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
452                                 layoutItemField.set_full_field_details(field);
453                                 layoutFields.add(layoutItemField);
454
455                                 // create a sort clause if it's a primary key and we're not asked to sort a specific column
456                                 if (!useSortClause) {
457                                         if (field.get_primary_key()) {
458                                                 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
459                                         }
460                                 }
461                         }
462                 }
463
464                 // create a sort clause for the column we've been asked to sort
465                 if (useSortClause) {
466                         LayoutItem item = layoutFields.get(sortColumnIndex);
467                         LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
468                         if (field != null)
469                                 sortClause.addLast(new SortFieldPair(field, isAscending));
470                         else {
471                                 Log.error(documentTitle + " - " + tableName + ": Error getting LayoutItem_Field for column index "
472                                                 + sortColumnIndex + ". Cannot create a sort clause for this column.");
473                         }
474
475                 }
476
477                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
478                 Connection conn = null;
479                 Statement st = null;
480                 ResultSet rs = null;
481                 try {
482                         // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
483                         // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
484                         // of data. Here's the relevant PostgreSQL documentation:
485                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
486                         ComboPooledDataSource cpds = configuredDoc.getCpds();
487                         conn = cpds.getConnection();
488                         conn.setAutoCommit(false);
489                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
490                         st.setFetchSize(length);
491                         String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
492                         // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
493                         // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
494                         // memory footprint. Check the difference between this value before and after the query:
495                         // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
496                         // Test the execution time at the same time (see the todo item in getLayoutListTable()).
497                         rs = st.executeQuery(query);
498
499                         // get the results from the ResultSet
500                         rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
501                 } catch (SQLException e) {
502                         Log.error(documentTitle + " - " + tableName + ": Error executing database query.", e);
503                         // TODO: somehow notify user of problem
504                 } finally {
505                         // cleanup everything that has been used
506                         try {
507                                 if (rs != null)
508                                         rs.close();
509                                 if (st != null)
510                                         st.close();
511                                 if (conn != null)
512                                         conn.close();
513                         } catch (Exception e) {
514                                 Log.error(documentTitle + " - " + tableName
515                                                 + ": Error closing database resources. Subsequent database queries may not work.", e);
516                         }
517                 }
518                 return rowsList;
519         }
520
521         private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
522                         LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
523
524                 // get the data we've been asked for
525                 int rowCount = 0;
526                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
527                 while (rs.next() && rowCount <= length) {
528                         int layoutFieldsSize = safeLongToInt(layoutFields.size());
529                         GlomField[] rowArray = new GlomField[layoutFieldsSize];
530                         for (int i = 0; i < layoutFieldsSize; i++) {
531                                 // make a new GlomField to set the text and colours
532                                 rowArray[i] = new GlomField();
533
534                                 // get foreground and background colours
535                                 LayoutItem_Field field = layoutFields.get(i);
536                                 FieldFormatting formatting = field.get_formatting_used();
537                                 String fgcolour = formatting.get_text_format_color_foreground();
538                                 if (!fgcolour.isEmpty())
539                                         rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
540                                 String bgcolour = formatting.get_text_format_color_background();
541                                 if (!bgcolour.isEmpty())
542                                         rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
543
544                                 // Convert the field value to a string based on the glom type. We're doing the formatting on the
545                                 // server side for now but it might be useful to move this to the client side.
546                                 switch (field.get_glom_type()) {
547                                 case TYPE_TEXT:
548                                         String text = rs.getString(i + 1);
549                                         rowArray[i].setText(text != null ? text : "");
550                                         break;
551                                 case TYPE_BOOLEAN:
552                                         rowArray[i].setBoolean(rs.getBoolean(i + 1));
553                                         break;
554                                 case TYPE_NUMERIC:
555                                         // Take care of the numeric formatting before converting the number to a string.
556                                         NumericFormat numFormatGlom = formatting.getM_numeric_format();
557                                         // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
558                                         // number should be formatted as a currency if the currency code string is not empty.
559                                         String currencyCode = numFormatGlom.getM_currency_symbol();
560                                         NumberFormat numFormatJava = null;
561                                         boolean useGlomCurrencyCode = false;
562                                         if (currencyCode.length() == 3) {
563                                                 // Try to format the currency using the Java Locales system.
564                                                 try {
565                                                         Currency currency = Currency.getInstance(currencyCode);
566                                                         Log.info(documentTitle
567                                                                         + " "
568                                                                         + tableName
569                                                                         + ": A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
570                                                         int digits = currency.getDefaultFractionDigits();
571                                                         numFormatJava = NumberFormat.getCurrencyInstance(locale);
572                                                         numFormatJava.setCurrency(currency);
573                                                         numFormatJava.setMinimumFractionDigits(digits);
574                                                         numFormatJava.setMaximumFractionDigits(digits);
575                                                 } catch (IllegalArgumentException e) {
576                                                         Log.warn(documentTitle + " - " + tableName + ": " + currencyCode
577                                                                         + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
578                                                         // The currency code is not this is not an ISO 4217 currency code.
579                                                         // We're going to manually set the currency code and use the glom numeric formatting.
580                                                         useGlomCurrencyCode = true;
581                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
582                                                 }
583                                         } else if (currencyCode.length() > 0) {
584                                                 Log.warn(documentTitle + " - " + tableName + ": " + currencyCode
585                                                                 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
586                                                 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
587                                                 // We're going to manually set the currency code and use the glom numeric formatting.
588                                                 useGlomCurrencyCode = true;
589                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
590                                         } else {
591                                                 // The length of the currency code is 0; the number is not a currency.
592                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
593                                         }
594
595                                         // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
596
597                                         double number = rs.getDouble(i + 1);
598                                         if (number < 0) {
599                                                 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
600                                                         // overrides the set foreground colour
601                                                         rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
602                                                                         .get_alternative_color_for_negatives()));
603                                         }
604
605                                         // Finally convert the number to text using the glom currency string if required.
606                                         if (useGlomCurrencyCode) {
607                                                 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
608                                         } else {
609                                                 rowArray[i].setText(numFormatJava.format(number));
610                                         }
611                                         break;
612                                 case TYPE_DATE:
613                                         Date date = rs.getDate(i + 1);
614                                         if (date != null) {
615                                                 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
616                                                 rowArray[i].setText(dateFormat.format(date));
617                                         } else {
618                                                 rowArray[i].setText("");
619                                         }
620                                         break;
621                                 case TYPE_TIME:
622                                         Time time = rs.getTime(i + 1);
623                                         if (time != null) {
624                                                 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
625                                                 rowArray[i].setText(timeFormat.format(time));
626                                         } else {
627                                                 rowArray[i].setText("");
628                                         }
629                                         break;
630                                 case TYPE_IMAGE:
631                                         byte[] image = rs.getBytes(i + 1);
632                                         if (image != null) {
633                                                 // TODO implement field TYPE_IMAGE
634                                                 rowArray[i].setText("Image (FIXME)");
635                                         } else {
636                                                 rowArray[i].setText("");
637                                         }
638                                         break;
639                                 case TYPE_INVALID:
640                                 default:
641                                         Log.warn(documentTitle + " - " + tableName
642                                                         + ": Invalid LayoutItem Field type. Using empty string for value.");
643                                         rowArray[i].setText("");
644                                         break;
645                                 }
646                         }
647
648                         // add the row of GlomFields to the ArrayList we're going to return and update the row count
649                         rowsList.add(rowArray);
650                         rowCount++;
651                 }
652
653                 return rowsList;
654         }
655
656         public ArrayList<String> getDocumentTitles() {
657                 ArrayList<String> documentTitles = new ArrayList<String>();
658                 for (String title : documents.keySet()) {
659                         documentTitles.add(title);
660                 }
661                 return documentTitles;
662         }
663
664         /*
665          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
666          * 
667          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
668          */
669         private int safeLongToInt(long value) {
670                 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
671                         throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
672                 }
673                 return (int) value;
674         }
675
676         private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
677                 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
678                 if (numFormatGlom.getM_decimal_places_restricted()) {
679                         int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
680                         numFormatJava.setMinimumFractionDigits(digits);
681                         numFormatJava.setMaximumFractionDigits(digits);
682                 }
683                 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
684                 return numFormatJava;
685         }
686
687         /*
688          * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
689          * significant 8-bits in each channel.
690          */
691         private String convertGdkColorToHtmlColour(String gdkColor) {
692                 if (gdkColor.length() == 13)
693                         return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
694                 else if (gdkColor.length() == 7) {
695                         // This shouldn't happen but let's deal with it if it does.
696                         Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
697                         return gdkColor;
698                 } else {
699                         Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
700                         return "#000000";
701                 }
702         }
703
704         /*
705          * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
706          * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
707          * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
708          * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
709          */
710         private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
711                         FieldFormatting.HorizontalAlignment alignment) {
712                 switch (alignment) {
713                 case HORIZONTAL_ALIGNMENT_AUTO:
714                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
715                 case HORIZONTAL_ALIGNMENT_LEFT:
716                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
717                 case HORIZONTAL_ALIGNMENT_RIGHT:
718                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
719                 default:
720                         Log.error("getColumnInfoGlomFieldType(): Recieved an alignment that I don't know about: "
721                                         + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
722                                         + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
723                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
724                 }
725         }
726
727         /*
728          * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
729          * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
730          * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
731          * ColumnInfo class.
732          */
733         private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
734                 switch (type) {
735                 case TYPE_BOOLEAN:
736                         return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
737                 case TYPE_DATE:
738                         return ColumnInfo.GlomFieldType.TYPE_DATE;
739                 case TYPE_IMAGE:
740                         return ColumnInfo.GlomFieldType.TYPE_IMAGE;
741                 case TYPE_NUMERIC:
742                         return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
743                 case TYPE_TEXT:
744                         return ColumnInfo.GlomFieldType.TYPE_TEXT;
745                 case TYPE_TIME:
746                         return ColumnInfo.GlomFieldType.TYPE_TIME;
747                 case TYPE_INVALID:
748                         Log.info("getColumnInfoGlomFieldType(): Returning TYPE_INVALID.");
749                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
750                 default:
751                         Log.error("getColumnInfoGlomFieldType(): Recieved a type that I don't know about: "
752                                         + Field.glom_field_type.class.getName() + "." + type.toString() + ". Returning "
753                                         + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
754                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
755                 }
756         }
757
758         /*
759          * (non-Javadoc)
760          * 
761          * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
762          */
763         public boolean isAuthenticated(String documentTitle) {
764                 return documents.get(documentTitle).isAuthenticated();
765         }
766
767         /*
768          * (non-Javadoc)
769          * 
770          * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
771          * java.lang.String)
772          */
773         public boolean checkAuthentication(String documentTitle, String username, String password) {
774                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
775                 boolean authenticated;
776                 try {
777                         authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
778                 } catch (SQLException e) {
779                         Log.error("", e);
780                         return false;
781                 }
782                 configuredDoc.setAuthenticated(authenticated);
783                 return authenticated;
784         }
785
786         /*
787          * (non-Javadoc)
788          * 
789          * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
790          */
791         public LayoutGroup getDetailsLayoutGroup(String documentTitle, String tableName) {
792                 // FIXME not checking if authenticated
793                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
794                 Document document = configuredDoc.getDocument();
795                 LayoutGroupVector layoutGroupsVec = document.get_data_layout_groups("details", tableName);
796                 org.glom.libglom.LayoutGroup libGlomLayoutGroup = layoutGroupsVec.get(0);
797
798                 LayoutGroup layoutGroup = new LayoutGroup();
799                 if (libGlomLayoutGroup == null)
800                         return layoutGroup;
801
802                 layoutGroup.setTitle(libGlomLayoutGroup.get_title());
803
804                 return getLayoutGroup(documentTitle, tableName, libGlomLayoutGroup);
805         }
806
807         /**
808          * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
809          * 
810          * @param libglomLayoutGroup
811          *            <dt><b>Precondition:</b>
812          *            <dd>
813          *            libglomLayoutGroup must not be null
814          * @return
815          */
816         private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
817                         org.glom.libglom.LayoutGroup libglomLayoutGroup) {
818                 LayoutGroup layoutGroup = new LayoutGroup();
819                 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
820
821                 // look at each child item
822                 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
823                 for (int i = 0; i < layoutItemsVec.size(); i++) {
824                         org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
825
826                         // just a safety check
827                         if (libglomLayoutItem == null)
828                                 continue;
829
830                         org.glom.web.shared.layout.LayoutItem layoutItem = null;
831                         org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
832                         if (group != null) {
833                                 // recurse into child groups
834                                 layoutItem = getLayoutGroup(documentTitle, tableName, group);
835                         } else {
836                                 // create GWT-Glom LayoutItem types based on the the libglom type
837                                 // FIXME use cast_dynamic methods to determine the libglom type
838                                 String partTypeName = libglomLayoutItem.get_part_type_name();
839                                 if ("Field".equals(partTypeName)) {
840                                         layoutItem = new LayoutItemField();
841                                 } else {
842                                         Log.warn(documentTitle + " - " + tableName
843                                                         + "- getLayoutGroup(): Ignoring unknown LayoutItem of type: " + partTypeName);
844                                         continue;
845                                 }
846                         }
847
848                         layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
849                         layoutGroup.addItem(layoutItem);
850                 }
851
852                 return layoutGroup;
853         }
854
855         public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
856
857                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
858                 Document document = configuredDoc.getDocument();
859
860                 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName);
861
862                 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
863                         Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
864                                         + tableName + ": Didn't find any fields to show. Returning null.");
865                         return null;
866                 }
867
868                 // get primary key for the table to use in the SQL query
869                 Field primaryKey = null;
870                 FieldVector fieldsVec = document.get_table_fields(tableName);
871                 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
872                         Field field = fieldsVec.get(i);
873                         if (field.get_primary_key()) {
874                                 primaryKey = field;
875                                 break;
876                         }
877                 }
878
879                 // send back an empty GlomField array if can't find a primaryKey Field
880                 if (primaryKey == null) {
881                         Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
882                                         + tableName + ": Couldn't find primary key in table. Returning null.");
883                         return null;
884                 }
885
886                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
887                 Connection conn = null;
888                 Statement st = null;
889                 ResultSet rs = null;
890                 try {
891                         // Setup the JDBC driver and get the query.
892                         ComboPooledDataSource cpds = configuredDoc.getCpds();
893                         conn = cpds.getConnection();
894                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
895                         String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
896                         rs = st.executeQuery(query);
897
898                         // get the results from the ResultSet
899                         // using 2 as a length parameter so we can log a warning if the result set is greater than one
900                         rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
901                 } catch (SQLException e) {
902                         Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
903                                         + tableName + ": Error executing database query.", e);
904                         // TODO: somehow notify user of problem
905                         return null;
906                 } finally {
907                         // cleanup everything that has been used
908                         try {
909                                 if (rs != null)
910                                         rs.close();
911                                 if (st != null)
912                                         st.close();
913                                 if (conn != null)
914                                         conn.close();
915                         } catch (Exception e) {
916                                 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
917                                                 + tableName + ": Error closing database resources. Subsequent database queries may not work.",
918                                                 e);
919                         }
920                 }
921
922                 if (rowsList.size() == 0) {
923                         Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
924                                         + tableName + ": The query returned an empty ResultSet. Returning null.");
925                         return null;
926                 } else if (rowsList.size() > 1) {
927                         Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
928                                         + tableName + ": The query did not return a unique result. Returning the first result in the set.");
929                 }
930
931                 return rowsList.get(0);
932
933         }
934
935         /*
936          * Gets a LayoutFieldVector to use when generating an SQL query.
937          */
938         private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName) {
939                 // TODO make general to be able to use "list" here - will need to change called methods
940                 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
941                 return getTableFieldsToShowForSequence(document, tableName, layoutGroupVec);
942         }
943
944         private LayoutFieldVector getTableFieldsToShowForSequence(Document document, String tableName,
945                         LayoutGroupVector layoutGroupVec) {
946
947                 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
948                 // We will show the fields that the document says we should:
949                 for (int i = 0; i < layoutGroupVec.size(); i++) {
950                         org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
951
952                         // Get the fields:
953                         ArrayList<LayoutItem_Field> layoutItemsFields = getTableFieldsToShowForSequenceAddGroup(document,
954                                         tableName, layoutGroup);
955                         for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
956                                 layoutFieldVector.add(layoutItem_Field);
957                         }
958                 }
959                 return layoutFieldVector;
960         }
961
962         private ArrayList<LayoutItem_Field> getTableFieldsToShowForSequenceAddGroup(Document document, String tableName,
963                         org.glom.libglom.LayoutGroup layoutGroup) {
964
965                 ArrayList<LayoutItem_Field> layoutItemFields = new ArrayList<LayoutItem_Field>();
966                 LayoutItemVector items = layoutGroup.get_items();
967                 for (int i = 0; i < items.size(); i++) {
968                         LayoutItem layoutItem = items.get(i);
969
970                         LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
971                         if (layoutItemField != null) {
972                                 // the layoutItem is a LayoutItem_Field
973                                 FieldVector fields;
974                                 if (layoutItemField.get_has_relationship_name()) {
975                                         // layoutItemField is a field in a related table
976                                         fields = document.get_table_fields(layoutItemField.get_table_used(tableName));
977                                 } else {
978                                         // layoutItemField is a field in this table
979                                         fields = document.get_table_fields(tableName);
980                                 }
981
982                                 // set the layoutItemFeild with details from its Field in the document and
983                                 // add it to the list to be returned
984                                 for (int j = 0; j < fields.size(); j++) {
985                                         // check the names to see if they're the same
986                                         // this works because we're using the field list from the related table if necessary
987                                         if (layoutItemField.get_name().equals(fields.get(j).get_name())) {
988                                                 Field field = fields.get(j);
989                                                 if (field != null) {
990                                                         layoutItemField.set_full_field_details(field);
991                                                         layoutItemFields.add(layoutItemField);
992                                                 } else {
993                                                         Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
994                                                                         + document.get_database_title() + " - " + tableName + "LayoutItem_Field "
995                                                                         + layoutItemField.get_layout_display_name() + "not found in document field list.");
996                                                 }
997                                                 break;
998                                         }
999                                 }
1000
1001                         } else {
1002                                 // the layoutItem is not a LayoutItem_Field
1003                                 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
1004                                 if (subLayoutGroup != null) {
1005                                         // the layoutItem is a LayoutGroup
1006                                         LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
1007                                         if (layoutItemPortal == null) {
1008                                                 // The subGroup is not a LayoutItem_Portal.
1009                                                 // We're ignoring portals because they are filled by means of a separate SQL query.
1010                                                 layoutItemFields.addAll(getTableFieldsToShowForSequenceAddGroup(document, tableName,
1011                                                                 subLayoutGroup));
1012                                         }
1013                                 }
1014                         }
1015                 }
1016                 return layoutItemFields;
1017         }
1018
1019         /*
1020          * (non-Javadoc)
1021          * 
1022          * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
1023          */
1024         @Override
1025         public LayoutGroup getDefaultDetailsLayoutGroup(String documentTitle) {
1026                 GlomDocument glomDocument = getGlomDocument(documentTitle);
1027                 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
1028                 return getDetailsLayoutGroup(documentTitle, tableName);
1029         }
1030 }