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