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