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