Create wrapper class to create consistent log messages.
[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.google.gwt.user.server.rpc.RemoteServiceServlet;
66 import com.mchange.v2.c3p0.ComboPooledDataSource;
67 import com.mchange.v2.c3p0.DataSources;
68
69 @SuppressWarnings("serial")
70 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
71
72         // class to hold configuration information for related to the glom document and db access
73         private class ConfiguredDocument {
74                 private Document document;
75                 private ComboPooledDataSource cpds;
76                 private boolean authenticated = false;
77
78                 // @formatter:off
79                 public Document getDocument() { return document; }
80                 public void setDocument(Document document) { this.document = document; }
81                 public ComboPooledDataSource getCpds() { return cpds; }
82                 public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds;     }
83                 public boolean isAuthenticated() { return authenticated; }
84                 public void setAuthenticated(boolean authenticated) { this.authenticated = authenticated; }
85                 // @formatter:on
86         }
87
88         // convenience class to for dealing with the Online Glom configuration file
89         private class OnlineGlomProperties extends Properties {
90                 public String getKey(String value) {
91                         for (String key : stringPropertyNames()) {
92                                 if (getProperty(key).trim().equals(value))
93                                         return key;
94                         }
95                         return null;
96                 }
97         }
98
99         private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
100         // TODO implement locale
101         private final Locale locale = Locale.ROOT;
102
103         /*
104          * This is called when the servlet is started or restarted.
105          */
106         public OnlineGlomServiceImpl() throws Exception {
107
108                 // Find the configuration file. See this thread for background info:
109                 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
110                 OnlineGlomProperties config = new OnlineGlomProperties();
111                 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
112                 if (is == null) {
113                         Log.fatal("onlineglom.properties not found.");
114                         throw new IOException();
115                 }
116                 config.load(is);
117
118                 // check the configured glom file directory
119                 String documentDirName = config.getProperty("glom.document.directory");
120                 File documentDir = new File(documentDirName);
121                 if (!documentDir.isDirectory()) {
122                         Log.fatal(documentDirName + " is not a directory.");
123                         throw new IOException();
124                 }
125                 if (!documentDir.canRead()) {
126                         Log.fatal("Can't read the files in : " + documentDirName);
127                         throw new IOException();
128                 }
129
130                 // get and check the glom files in the specified directory
131                 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
132                         @Override
133                         public boolean accept(File dir, String name) {
134                                 return name.endsWith(".glom");
135                         }
136                 });
137                 Glom.libglom_init();
138                 for (File glomFile : glomFiles) {
139                         Document document = new Document();
140                         document.set_file_uri("file://" + glomFile.getAbsolutePath());
141                         int error = 0;
142                         boolean retval = document.load(error);
143                         if (retval == false) {
144                                 String message;
145                                 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
146                                         message = "Could not find file: " + glomFile.getAbsolutePath();
147                                 } else {
148                                         message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
149                                 }
150                                 Log.error(message);
151                                 // continue with for loop because there may be other documents in the directory
152                                 continue;
153                         }
154
155                         // load the jdbc driver for the current glom document
156                         ComboPooledDataSource cpds = new ComboPooledDataSource();
157
158                         try {
159                                 cpds.setDriverClass("org.postgresql.Driver");
160                         } catch (PropertyVetoException e) {
161                                 Log.fatal("Error loading the PostgreSQL JDBC driver."
162                                                 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
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(documentTitle, "Username and password not correct.");
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(documenTitle, "Error cleaning up the ComboPooledDataSource.", 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, tableName,
421                                                 "The size of the list view layout group for table is greater than 1. "
422                                                                 + "Attempting to use the first item for the layout list view.");
423                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
424
425                         // find the defined layout list fields
426                         int numItems = safeLongToInt(layoutItemsVec.size());
427                         for (int i = 0; i < numItems; i++) {
428                                 // TODO add support for other LayoutItems (Text, Image, Button)
429                                 LayoutItem item = layoutItemsVec.get(i);
430                                 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
431                                 if (layoutItemfield != null) {
432                                         // use this field in the layout
433                                         layoutFields.add(layoutItemfield);
434
435                                         // create a sort clause if it's a primary key and we're not asked to sort a specific column
436                                         if (!useSortClause) {
437                                                 Field details = layoutItemfield.get_full_field_details();
438                                                 if (details != null && details.get_primary_key()) {
439                                                         sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
440                                                 }
441                                         }
442                                 }
443                         }
444                 } else {
445                         // no layout list is defined, use the table fields as the layout list
446                         FieldVector fieldsVec = document.get_table_fields(tableName);
447
448                         // find the fields to display in the layout list
449                         int numItems = safeLongToInt(fieldsVec.size());
450                         for (int i = 0; i < numItems; i++) {
451                                 Field field = fieldsVec.get(i);
452                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
453                                 layoutItemField.set_full_field_details(field);
454                                 layoutFields.add(layoutItemField);
455
456                                 // create a sort clause if it's a primary key and we're not asked to sort a specific column
457                                 if (!useSortClause) {
458                                         if (field.get_primary_key()) {
459                                                 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
460                                         }
461                                 }
462                         }
463                 }
464
465                 // create a sort clause for the column we've been asked to sort
466                 if (useSortClause) {
467                         LayoutItem item = layoutFields.get(sortColumnIndex);
468                         LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
469                         if (field != null)
470                                 sortClause.addLast(new SortFieldPair(field, isAscending));
471                         else {
472                                 Log.error(documentTitle, tableName, "Error getting LayoutItem_Field for column index "
473                                                 + sortColumnIndex + ". Cannot create a sort clause for this column.");
474                         }
475
476                 }
477
478                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
479                 Connection conn = null;
480                 Statement st = null;
481                 ResultSet rs = null;
482                 try {
483                         // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
484                         // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
485                         // of data. Here's the relevant PostgreSQL documentation:
486                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
487                         ComboPooledDataSource cpds = configuredDoc.getCpds();
488                         conn = cpds.getConnection();
489                         conn.setAutoCommit(false);
490                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
491                         st.setFetchSize(length);
492                         String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
493                         // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
494                         // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
495                         // memory footprint. Check the difference between this value before and after the query:
496                         // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
497                         // Test the execution time at the same time (see the todo item in getLayoutListTable()).
498                         rs = st.executeQuery(query);
499
500                         // get the results from the ResultSet
501                         rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
502                 } catch (SQLException e) {
503                         Log.error(documentTitle, tableName, "Error executing database query.", e);
504                         // TODO: somehow notify user of problem
505                 } finally {
506                         // cleanup everything that has been used
507                         try {
508                                 if (rs != null)
509                                         rs.close();
510                                 if (st != null)
511                                         st.close();
512                                 if (conn != null)
513                                         conn.close();
514                         } catch (Exception e) {
515                                 Log.error(documentTitle, tableName,
516                                                 "Error closing database resources. Subsequent database queries may not work.", e);
517                         }
518                 }
519                 return rowsList;
520         }
521
522         private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
523                         LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
524
525                 // get the data we've been asked for
526                 int rowCount = 0;
527                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
528                 while (rs.next() && rowCount <= length) {
529                         int layoutFieldsSize = safeLongToInt(layoutFields.size());
530                         GlomField[] rowArray = new GlomField[layoutFieldsSize];
531                         for (int i = 0; i < layoutFieldsSize; i++) {
532                                 // make a new GlomField to set the text and colours
533                                 rowArray[i] = new GlomField();
534
535                                 // get foreground and background colours
536                                 LayoutItem_Field field = layoutFields.get(i);
537                                 FieldFormatting formatting = field.get_formatting_used();
538                                 String fgcolour = formatting.get_text_format_color_foreground();
539                                 if (!fgcolour.isEmpty())
540                                         rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
541                                 String bgcolour = formatting.get_text_format_color_background();
542                                 if (!bgcolour.isEmpty())
543                                         rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
544
545                                 // Convert the field value to a string based on the glom type. We're doing the formatting on the
546                                 // server side for now but it might be useful to move this to the client side.
547                                 switch (field.get_glom_type()) {
548                                 case TYPE_TEXT:
549                                         String text = rs.getString(i + 1);
550                                         rowArray[i].setText(text != null ? text : "");
551                                         break;
552                                 case TYPE_BOOLEAN:
553                                         rowArray[i].setBoolean(rs.getBoolean(i + 1));
554                                         break;
555                                 case TYPE_NUMERIC:
556                                         // Take care of the numeric formatting before converting the number to a string.
557                                         NumericFormat numFormatGlom = formatting.getM_numeric_format();
558                                         // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
559                                         // number should be formatted as a currency if the currency code string is not empty.
560                                         String currencyCode = numFormatGlom.getM_currency_symbol();
561                                         NumberFormat numFormatJava = null;
562                                         boolean useGlomCurrencyCode = false;
563                                         if (currencyCode.length() == 3) {
564                                                 // Try to format the currency using the Java Locales system.
565                                                 try {
566                                                         Currency currency = Currency.getInstance(currencyCode);
567                                                         Log.info(documentTitle, tableName, "A valid ISO 4217 currency code is being used."
568                                                                         + " Overriding the numeric formatting with information from the locale.");
569                                                         int digits = currency.getDefaultFractionDigits();
570                                                         numFormatJava = NumberFormat.getCurrencyInstance(locale);
571                                                         numFormatJava.setCurrency(currency);
572                                                         numFormatJava.setMinimumFractionDigits(digits);
573                                                         numFormatJava.setMaximumFractionDigits(digits);
574                                                 } catch (IllegalArgumentException e) {
575                                                         Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
576                                                                         + " Manually setting currency code with this value.");
577                                                         // The currency code is not this is not an ISO 4217 currency code.
578                                                         // We're going to manually set the currency code and use the glom numeric formatting.
579                                                         useGlomCurrencyCode = true;
580                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
581                                                 }
582                                         } else if (currencyCode.length() > 0) {
583                                                 Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
584                                                                 + " Manually setting currency code with this value.");
585                                                 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
586                                                 // We're going to manually set the currency code and use the glom numeric formatting.
587                                                 useGlomCurrencyCode = true;
588                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
589                                         } else {
590                                                 // The length of the currency code is 0; the number is not a currency.
591                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
592                                         }
593
594                                         // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
595
596                                         double number = rs.getDouble(i + 1);
597                                         if (number < 0) {
598                                                 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
599                                                         // overrides the set foreground colour
600                                                         rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
601                                                                         .get_alternative_color_for_negatives()));
602                                         }
603
604                                         // Finally convert the number to text using the glom currency string if required.
605                                         if (useGlomCurrencyCode) {
606                                                 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
607                                         } else {
608                                                 rowArray[i].setText(numFormatJava.format(number));
609                                         }
610                                         break;
611                                 case TYPE_DATE:
612                                         Date date = rs.getDate(i + 1);
613                                         if (date != null) {
614                                                 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
615                                                 rowArray[i].setText(dateFormat.format(date));
616                                         } else {
617                                                 rowArray[i].setText("");
618                                         }
619                                         break;
620                                 case TYPE_TIME:
621                                         Time time = rs.getTime(i + 1);
622                                         if (time != null) {
623                                                 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
624                                                 rowArray[i].setText(timeFormat.format(time));
625                                         } else {
626                                                 rowArray[i].setText("");
627                                         }
628                                         break;
629                                 case TYPE_IMAGE:
630                                         byte[] image = rs.getBytes(i + 1);
631                                         if (image != null) {
632                                                 // TODO implement field TYPE_IMAGE
633                                                 rowArray[i].setText("Image (FIXME)");
634                                         } else {
635                                                 rowArray[i].setText("");
636                                         }
637                                         break;
638                                 case TYPE_INVALID:
639                                 default:
640                                         Log.warn(documentTitle, tableName, "Invalid LayoutItem Field type. Using empty string for value.");
641                                         rowArray[i].setText("");
642                                         break;
643                                 }
644                         }
645
646                         // add the row of GlomFields to the ArrayList we're going to return and update the row count
647                         rowsList.add(rowArray);
648                         rowCount++;
649                 }
650
651                 return rowsList;
652         }
653
654         public ArrayList<String> getDocumentTitles() {
655                 ArrayList<String> documentTitles = new ArrayList<String>();
656                 for (String title : documents.keySet()) {
657                         documentTitles.add(title);
658                 }
659                 return documentTitles;
660         }
661
662         /*
663          * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
664          * 
665          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
666          */
667         private int safeLongToInt(long value) {
668                 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
669                         throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
670                 }
671                 return (int) value;
672         }
673
674         private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
675                 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
676                 if (numFormatGlom.getM_decimal_places_restricted()) {
677                         int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
678                         numFormatJava.setMinimumFractionDigits(digits);
679                         numFormatJava.setMaximumFractionDigits(digits);
680                 }
681                 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
682                 return numFormatJava;
683         }
684
685         /*
686          * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
687          * significant 8-bits in each channel.
688          */
689         private String convertGdkColorToHtmlColour(String gdkColor) {
690                 if (gdkColor.length() == 13)
691                         return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
692                 else if (gdkColor.length() == 7) {
693                         // This shouldn't happen but let's deal with it if it does.
694                         Log.warn("Expected a 13 character string but received a 7 character string. Returning received string.");
695                         return gdkColor;
696                 } else {
697                         Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
698                         return "#000000";
699                 }
700         }
701
702         /*
703          * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
704          * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
705          * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
706          * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
707          */
708         private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
709                         FieldFormatting.HorizontalAlignment alignment) {
710                 switch (alignment) {
711                 case HORIZONTAL_ALIGNMENT_AUTO:
712                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
713                 case HORIZONTAL_ALIGNMENT_LEFT:
714                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
715                 case HORIZONTAL_ALIGNMENT_RIGHT:
716                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
717                 default:
718                         Log.error("Recieved an alignment that I don't know about: "
719                                         + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
720                                         + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
721                         return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
722                 }
723         }
724
725         /*
726          * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
727          * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
728          * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
729          * ColumnInfo class.
730          */
731         private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
732                 switch (type) {
733                 case TYPE_BOOLEAN:
734                         return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
735                 case TYPE_DATE:
736                         return ColumnInfo.GlomFieldType.TYPE_DATE;
737                 case TYPE_IMAGE:
738                         return ColumnInfo.GlomFieldType.TYPE_IMAGE;
739                 case TYPE_NUMERIC:
740                         return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
741                 case TYPE_TEXT:
742                         return ColumnInfo.GlomFieldType.TYPE_TEXT;
743                 case TYPE_TIME:
744                         return ColumnInfo.GlomFieldType.TYPE_TIME;
745                 case TYPE_INVALID:
746                         Log.info("Returning TYPE_INVALID.");
747                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
748                 default:
749                         Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
750                                         + type.toString() + ". Returning " + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
751                         return ColumnInfo.GlomFieldType.TYPE_INVALID;
752                 }
753         }
754
755         /*
756          * (non-Javadoc)
757          * 
758          * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
759          */
760         public boolean isAuthenticated(String documentTitle) {
761                 return documents.get(documentTitle).isAuthenticated();
762         }
763
764         /*
765          * (non-Javadoc)
766          * 
767          * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
768          * java.lang.String)
769          */
770         public boolean checkAuthentication(String documentTitle, String username, String password) {
771                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
772                 boolean authenticated;
773                 try {
774                         authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
775                 } catch (SQLException e) {
776                         Log.error(documentTitle, "Unknown SQL Error checking the database authentication.", e);
777                         return false;
778                 }
779                 configuredDoc.setAuthenticated(authenticated);
780                 return authenticated;
781         }
782
783         /*
784          * (non-Javadoc)
785          * 
786          * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
787          */
788         @Override
789         public ArrayList<LayoutGroup> getDefaultDetailsLayout(String documentTitle) {
790                 GlomDocument glomDocument = getGlomDocument(documentTitle);
791                 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
792                 return getDetailsLayout(documentTitle, tableName);
793         }
794
795         /*
796          * (non-Javadoc)
797          * 
798          * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
799          */
800         public ArrayList<LayoutGroup> getDetailsLayout(String documentTitle, String tableName) {
801                 // FIXME not checking if authenticated
802                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
803                 Document document = configuredDoc.getDocument();
804                 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
805
806                 ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
807                 for (int i = 0; i < layoutGroupVec.size(); i++) {
808                         org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
809
810                         if (libglomLayoutGroup == null)
811                                 continue;
812
813                         layoutGroups.add(getLayoutGroup(documentTitle, tableName, libglomLayoutGroup));
814                 }
815                 return layoutGroups;
816         }
817
818         /**
819          * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
820          * 
821          * @param libglomLayoutGroup
822          *            <dt><b>Precondition:</b>
823          *            <dd>
824          *            libglomLayoutGroup must not be null
825          * @return
826          */
827         private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
828                         org.glom.libglom.LayoutGroup libglomLayoutGroup) {
829                 LayoutGroup layoutGroup = new LayoutGroup();
830                 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
831
832                 // look at each child item
833                 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
834                 for (int i = 0; i < layoutItemsVec.size(); i++) {
835                         org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
836
837                         // just a safety check
838                         if (libglomLayoutItem == null)
839                                 continue;
840
841                         org.glom.web.shared.layout.LayoutItem layoutItem = null;
842                         org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
843                         if (group != null) {
844                                 // recurse into child groups
845                                 layoutItem = getLayoutGroup(documentTitle, tableName, group);
846                         } else {
847                                 // create GWT-Glom LayoutItem types based on the the libglom type
848                                 LayoutItem_Field tempField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
849                                 if (tempField != null) {
850                                         layoutItem = new LayoutItemField();
851                                 } else {
852                                         Log.info(documentTitle, tableName,
853                                                         "Ignoring unknown LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
854                                         continue;
855                                 }
856                         }
857
858                         layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
859                         layoutGroup.addItem(layoutItem);
860                 }
861
862                 return layoutGroup;
863         }
864
865         public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
866
867                 ConfiguredDocument configuredDoc = documents.get(documentTitle);
868                 Document document = configuredDoc.getDocument();
869
870                 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName);
871
872                 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
873                         Log.warn(documentTitle, tableName, "Didn't find any fields to show. Returning null.");
874                         return null;
875                 }
876
877                 // get primary key for the table to use in the SQL query
878                 Field primaryKey = null;
879                 FieldVector fieldsVec = document.get_table_fields(tableName);
880                 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
881                         Field field = fieldsVec.get(i);
882                         if (field.get_primary_key()) {
883                                 primaryKey = field;
884                                 break;
885                         }
886                 }
887
888                 // send back an empty GlomField array if can't find a primaryKey Field
889                 if (primaryKey == null) {
890                         Log.error(documentTitle, tableName, "Couldn't find primary key in table. Returning null.");
891                         return null;
892                 }
893
894                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
895                 Connection conn = null;
896                 Statement st = null;
897                 ResultSet rs = null;
898                 try {
899                         // Setup the JDBC driver and get the query.
900                         ComboPooledDataSource cpds = configuredDoc.getCpds();
901                         conn = cpds.getConnection();
902                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
903                         String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
904                         rs = st.executeQuery(query);
905
906                         // get the results from the ResultSet
907                         // using 2 as a length parameter so we can log a warning if the result set is greater than one
908                         rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
909                 } catch (SQLException e) {
910                         Log.error(documentTitle, tableName, "Error executing database query.", e);
911                         // TODO: somehow notify user of problem
912                         return null;
913                 } finally {
914                         // cleanup everything that has been used
915                         try {
916                                 if (rs != null)
917                                         rs.close();
918                                 if (st != null)
919                                         st.close();
920                                 if (conn != null)
921                                         conn.close();
922                         } catch (Exception e) {
923                                 Log.error(documentTitle, tableName,
924                                                 "Error closing database resources. Subsequent database queries may not work.", e);
925                         }
926                 }
927
928                 if (rowsList.size() == 0) {
929                         Log.error(documentTitle, tableName, "The query returned an empty ResultSet. Returning null.");
930                         return null;
931                 } else if (rowsList.size() > 1) {
932                         Log.warn(documentTitle, tableName,
933                                         "The query did not return a unique result. Returning the first result in the set.");
934                 }
935
936                 return rowsList.get(0);
937
938         }
939
940         /*
941          * Gets a LayoutFieldVector to use when generating an SQL query.
942          */
943         private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName) {
944                 // TODO make general to be able to use "list" here - will need to change called methods
945                 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
946
947                 // We will show the fields that the document says we should:
948                 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
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 = getFieldsToShowForSQLQueryAddGroup(document, tableName,
954                                         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> getFieldsToShowForSQLQueryAddGroup(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(document.get_database_title(), tableName,
994                                                                         "LayoutItem_Field " + layoutItemField.get_layout_display_name()
995                                                                                         + " 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
1011                                                                 .addAll(getFieldsToShowForSQLQueryAddGroup(document, tableName, subLayoutGroup));
1012                                         }
1013                                 }
1014                         }
1015                 }
1016                 return layoutItemFields;
1017         }
1018
1019 }