Add server side logging with the gwt-log library.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / OnlineGlomServiceImpl.java
1 /*
2  * Copyright (C) 2010, 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.beans.PropertyVetoException;
23 import java.io.File;
24 import java.io.IOException;
25 import java.sql.Connection;
26 import java.sql.Date;
27 import java.sql.ResultSet;
28 import java.sql.SQLException;
29 import java.sql.Statement;
30 import java.sql.Time;
31 import java.text.DateFormat;
32 import java.text.NumberFormat;
33 import java.util.ArrayList;
34 import java.util.Currency;
35 import java.util.Locale;
36 import java.util.Properties;
37
38 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
39 import org.glom.libglom.Document;
40 import org.glom.libglom.Field;
41 import org.glom.libglom.FieldFormatting;
42 import org.glom.libglom.FieldVector;
43 import org.glom.libglom.Glom;
44 import org.glom.libglom.LayoutFieldVector;
45 import org.glom.libglom.LayoutGroupVector;
46 import org.glom.libglom.LayoutItem;
47 import org.glom.libglom.LayoutItemVector;
48 import org.glom.libglom.LayoutItem_Field;
49 import org.glom.libglom.NumericFormat;
50 import org.glom.libglom.SortClause;
51 import org.glom.libglom.SortFieldPair;
52 import org.glom.libglom.StringVector;
53 import org.glom.web.client.OnlineGlomService;
54 import org.glom.web.shared.ColumnInfo;
55 import org.glom.web.shared.GlomDocument;
56 import org.glom.web.shared.GlomField;
57 import org.glom.web.shared.LayoutListTable;
58
59 import com.allen_sauer.gwt.log.client.Log;
60 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
61 import com.mchange.v2.c3p0.ComboPooledDataSource;
62 import com.mchange.v2.c3p0.DataSources;
63
64 @SuppressWarnings("serial")
65 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
66         private Document document = null;
67         private ComboPooledDataSource cpds = null;
68         // TODO implement locale
69         private final Locale locale = Locale.ROOT;
70         private boolean configured = false;
71
72         /*
73          * This is called when the servlet is started or restarted.
74          */
75         public OnlineGlomServiceImpl() {
76                 Glom.libglom_init();
77         }
78
79         /*
80          * The properties file can't be loaded in the constructor because the servlet is not yet initialised and
81          * getServletContext() will return null. The work-around for this problem is to initialise the database and glom
82          * document object when makes its first request.
83          */
84         private void configureServlet() {
85                 document = new Document();
86
87                 Properties props = new Properties();
88                 String propFileName = "/WEB-INF/OnlineGlom.properties";
89                 try {
90                         props.load(getServletContext().getResourceAsStream(propFileName));
91                 } catch (IOException e) {
92                         Log.fatal("Error loading " + propFileName, e);
93                         // TODO can't continue, notify user of problem
94                 }
95
96                 File file = new File(props.getProperty("glomfile"));
97                 document.set_file_uri("file://" + file.getAbsolutePath());
98                 int error = 0;
99                 boolean retval = document.load(error);
100                 if (retval == false) {
101                         String message;
102                         if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
103                                 message = "Could not find " + file.getAbsolutePath();
104                         } else {
105                                 message = "An unknown error occurred when trying to load " + file.getAbsolutePath();
106                         }
107                         Log.fatal(message);
108                         // TODO can't continue, notify user of problem
109                 }
110
111                 // load the jdbc driver
112                 cpds = new ComboPooledDataSource();
113                 try {
114                         cpds.setDriverClass("org.postgresql.Driver");
115                 } catch (PropertyVetoException e) {
116                         Log.fatal(
117                                         "Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?",
118                                         e);
119                         // TODO can't continue, notify user of problem
120                 }
121
122                 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
123                                 + document.get_connection_database());
124                 cpds.setUser(props.getProperty("dbusername"));
125                 cpds.setPassword(props.getProperty("dbpassword"));
126                 // TODO notify user if dbusername or dbpassword are wrong
127                 configured = true;
128         }
129
130         /*
131          * This is called when the servlet is stopped or restarted.
132          * 
133          * @see javax.servlet.GenericServlet#destroy()
134          */
135         @Override
136         public void destroy() {
137                 Glom.libglom_deinit();
138                 try {
139                         if (configured)
140                                 DataSources.destroy(cpds);
141                 } catch (SQLException e) {
142                         Log.error("Error cleaning up the ComboPooledDataSource", e);
143                 }
144         }
145
146         /*
147          * FIXME I think Swig is generating long on 64-bit machines and int on 32-bit machines - need to keep this constant
148          * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
149          */
150         private static int safeLongToInt(long l) {
151                 if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
152                         throw new IllegalArgumentException(l + " cannot be cast to int without changing its value.");
153                 }
154                 return (int) l;
155         }
156
157         public GlomDocument getGlomDocument() {
158                 if (!configured)
159                         configureServlet();
160
161                 GlomDocument glomDocument = new GlomDocument();
162
163                 // get arrays of table names and titles, and find the default table index
164                 StringVector tablesVec = document.get_table_names();
165
166                 int numTables = safeLongToInt(tablesVec.size());
167                 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
168                 // of the ArrayList
169                 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
170                 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
171                 boolean foundDefaultTable = false;
172                 int visibleIndex = 0;
173                 for (int i = 0; i < numTables; i++) {
174                         String tableName = tablesVec.get(i);
175                         if (!document.get_table_is_hidden(tableName)) {
176                                 tableNames.add(tableName);
177                                 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
178                                 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
179                                         glomDocument.setDefaultTableIndex(visibleIndex);
180                                         foundDefaultTable = true;
181                                 }
182                                 tableTitles.add(document.get_table_title(tableName));
183                                 visibleIndex++;
184                         }
185                 }
186
187                 // set everything we need
188                 glomDocument.setTableNames(tableNames);
189                 glomDocument.setTableTitles(tableTitles);
190                 glomDocument.setTitle(document.get_database_title());
191
192                 return glomDocument;
193         }
194
195         public LayoutListTable getLayoutListTable(String tableName) {
196                 if (!configured)
197                         configureServlet();
198
199                 LayoutListTable tableInfo = new LayoutListTable();
200
201                 // access the layout list
202                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
203                 ColumnInfo[] columns = null;
204                 LayoutFieldVector layoutFields = new LayoutFieldVector();
205                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
206                 if (listViewLayoutGroupSize > 0) {
207                         // a layout list is defined, we can use it to for the LayoutListTable
208                         if (listViewLayoutGroupSize > 1)
209                                 Log.warn("The size of the list view layout group for table " + tableName
210                                                 + " is greater than 1. Attempting to use the first item for the layout list view.");
211                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
212
213                         // find the defined layout list fields
214                         int numItems = safeLongToInt(layoutItemsVec.size());
215                         columns = new ColumnInfo[numItems];
216                         for (int i = 0; i < numItems; i++) {
217                                 // TODO add support for other LayoutItems (Text, Image, Button)
218                                 LayoutItem item = layoutItemsVec.get(i);
219                                 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
220                                 if (layoutItemField != null) {
221                                         layoutFields.add(layoutItemField);
222                                         FieldFormatting.HorizontalAlignment alignment = layoutItemField
223                                                         .get_formatting_used_horizontal_alignment();
224                                         columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
225                                                         getColumnInfoHorizontalAlignment(alignment));
226                                 }
227                         }
228                 } else {
229                         // no layout list is defined, use the table fields as the layout list
230                         FieldVector fieldsVec = document.get_table_fields(tableName);
231
232                         // find the fields to display in the layout list
233                         int numItems = safeLongToInt(fieldsVec.size());
234                         columns = new ColumnInfo[numItems];
235                         for (int i = 0; i < numItems; i++) {
236                                 Field field = fieldsVec.get(i);
237                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
238                                 layoutItemField.set_full_field_details(field);
239                                 layoutFields.add(layoutItemField);
240                                 FieldFormatting.HorizontalAlignment alignment = layoutItemField
241                                                 .get_formatting_used_horizontal_alignment();
242                                 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
243                                                 getColumnInfoHorizontalAlignment(alignment));
244                         }
245                 }
246
247                 tableInfo.setColumns(columns);
248
249                 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
250                 // list view pager.
251                 Connection conn = null;
252                 Statement st = null;
253                 ResultSet rs = null;
254                 try {
255                         // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
256                         // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
257                         // data. Here's the relevant PostgreSQL documentation:
258                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
259                         conn = cpds.getConnection();
260                         conn.setAutoCommit(false);
261                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
262                         String query = Glom.build_sql_select_count_simple(tableName, layoutFields);
263                         // TODO Test execution time of this query with when the number of rows in the table is large (say >
264                         // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
265                         rs = st.executeQuery(query);
266
267                         // get the number of rows in the query
268                         rs.next();
269                         tableInfo.setNumRows(rs.getInt(1));
270
271                 } catch (SQLException e) {
272                         Log.error("Error calculating number of rows in the query. Setting number of rows to 0.", e);
273                         tableInfo.setNumRows(0);
274                 } finally {
275                         // cleanup everything that has been used
276                         try {
277                                 rs.close();
278                                 st.close();
279                                 conn.close();
280                         } catch (Exception e) {
281                                 Log.error("Error closing database resources. Subsequent database queries may not work.", e);
282                         }
283                 }
284
285                 return tableInfo;
286         }
287
288         public ArrayList<GlomField[]> getTableData(String table, int start, int length) {
289                 return getTableData(table, start, length, false, 0, false);
290         }
291
292         public ArrayList<GlomField[]> getSortedTableData(String table, int start, int length, int sortColumnIndex,
293                         boolean isAscending) {
294                 return getTableData(table, start, length, true, sortColumnIndex, isAscending);
295         }
296
297         private ArrayList<GlomField[]> getTableData(String table, int start, int length, boolean useSortClause,
298                         int sortColumnIndex, boolean isAscending) {
299                 if (!configured)
300                         configureServlet();
301
302                 // access the layout list using the defined layout list or the table fields if there's no layout list
303                 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
304                 LayoutFieldVector layoutFields = new LayoutFieldVector();
305                 SortClause sortClause = new SortClause();
306                 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
307                 if (layoutListVec.size() > 0) {
308                         // a layout list is defined, we can use it to for the LayoutListTable
309                         if (listViewLayoutGroupSize > 1)
310                                 Log.warn("The size of the list view layout group for table " + table
311                                                 + " is greater than 1. Attempting to use the first item for the layout list view.");
312                         LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
313
314                         // find the defined layout list fields
315                         int numItems = safeLongToInt(layoutItemsVec.size());
316                         for (int i = 0; i < numItems; i++) {
317                                 // TODO add support for other LayoutItems (Text, Image, Button)
318                                 LayoutItem item = layoutItemsVec.get(i);
319                                 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
320                                 if (layoutItemfield != null) {
321                                         // use this field in the layout
322                                         layoutFields.add(layoutItemfield);
323
324                                         // create a sort clause if it's a primary key and we're not asked to sort a specific column
325                                         if (!useSortClause) {
326                                                 Field details = layoutItemfield.get_full_field_details();
327                                                 if (details != null && details.get_primary_key()) {
328                                                         sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
329                                                 }
330                                         }
331                                 }
332                         }
333                 } else {
334                         // no layout list is defined, use the table fields as the layout list
335                         FieldVector fieldsVec = document.get_table_fields(table);
336
337                         // find the fields to display in the layout list
338                         int numItems = safeLongToInt(fieldsVec.size());
339                         for (int i = 0; i < numItems; i++) {
340                                 Field field = fieldsVec.get(i);
341                                 LayoutItem_Field layoutItemField = new LayoutItem_Field();
342                                 layoutItemField.set_full_field_details(field);
343                                 layoutFields.add(layoutItemField);
344
345                                 // create a sort clause if it's a primary key and we're not asked to sort a specific column
346                                 if (!useSortClause) {
347                                         if (field.get_primary_key()) {
348                                                 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
349                                         }
350                                 }
351                         }
352                 }
353
354                 // create a sort clause for the column we've been asked to sort
355                 if (useSortClause) {
356                         LayoutItem item = layoutFields.get(sortColumnIndex);
357                         LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
358                         if (field != null)
359                                 sortClause.addLast(new SortFieldPair(field, isAscending));
360                         else {
361                                 Log.error("Error getting LayoutItem_Field for column index " + sortColumnIndex
362                                                 + ". Cannot create a sort clause for this column.");
363                         }
364
365                 }
366
367                 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
368                 Connection conn = null;
369                 Statement st = null;
370                 ResultSet rs = null;
371                 try {
372                         // Setup and execute the query. Special care needs to be take to ensure that the results will be based on a
373                         // cursor so that large amounts of memory are not consumed when the query retrieve a large amount of data.
374                         // Here's the relevant PostgreSQL documentation:
375                         // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
376                         conn = cpds.getConnection();
377                         conn.setAutoCommit(false);
378                         st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
379                         st.setFetchSize(length);
380                         String query = Glom.build_sql_select_simple(table, layoutFields, sortClause) + " OFFSET " + start;
381                         // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
382                         // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
383                         // memory footprint. Check the difference between this value before and after the query:
384                         // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
385                         // Test the execution time at the same time (see the todo item in getLayoutListTable()).
386                         rs = st.executeQuery(query);
387
388                         // get the data we've been asked for
389                         int rowCount = 0;
390                         while (rs.next() && rowCount <= length) {
391                                 int layoutFieldsSize = safeLongToInt(layoutFields.size());
392                                 GlomField[] rowArray = new GlomField[layoutFieldsSize];
393                                 for (int i = 0; i < layoutFieldsSize; i++) {
394                                         // make a new GlomField to set the text and colours
395                                         rowArray[i] = new GlomField();
396
397                                         // get foreground and background colours
398                                         LayoutItem_Field field = layoutFields.get(i);
399                                         FieldFormatting formatting = field.get_formatting_used();
400                                         String fgcolour = formatting.get_text_format_color_foreground();
401                                         if (!fgcolour.isEmpty())
402                                                 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
403                                         String bgcolour = formatting.get_text_format_color_background();
404                                         if (!bgcolour.isEmpty())
405                                                 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
406
407                                         // convert field values are to strings based on the glom type
408                                         switch (field.get_glom_type()) {
409                                         case TYPE_TEXT:
410                                                 String text = rs.getString(i + 1);
411                                                 rowArray[i].setText(text != null ? text : "");
412                                                 break;
413                                         case TYPE_BOOLEAN:
414                                                 rowArray[i].setText(rs.getBoolean(i + 1) ? "TRUE" : "FALSE");
415                                                 break;
416                                         case TYPE_NUMERIC:
417                                                 // Take care of the numeric formatting before converting the number to a string.
418                                                 NumericFormat numFormatGlom = formatting.getM_numeric_format();
419                                                 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
420                                                 // number should be formatted as a currency if the currency code string is not empty.
421                                                 String currencyCode = numFormatGlom.getM_currency_symbol();
422                                                 NumberFormat numFormatJava = null;
423                                                 boolean useGlomCurrencyCode = false;
424                                                 if (currencyCode.length() == 3) {
425                                                         // Try to format the currency using the Java Locales system.
426                                                         try {
427                                                                 Currency currency = Currency.getInstance(currencyCode);
428                                                                 Log.info("A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
429                                                                 int digits = currency.getDefaultFractionDigits();
430                                                                 numFormatJava = NumberFormat.getCurrencyInstance(locale);
431                                                                 numFormatJava.setCurrency(currency);
432                                                                 numFormatJava.setMinimumFractionDigits(digits);
433                                                                 numFormatJava.setMaximumFractionDigits(digits);
434                                                         } catch (IllegalArgumentException e) {
435                                                                 Log.warn(currencyCode
436                                                                                 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
437                                                                 // The currency code is not this is not an ISO 4217 currency code.
438                                                                 // We're going to manually set the currency code and use the glom numeric formatting.
439                                                                 useGlomCurrencyCode = true;
440                                                                 numFormatJava = getJavaNumberFormat(numFormatGlom);
441                                                         }
442                                                 } else if (currencyCode.length() > 0) {
443                                                         Log.warn(currencyCode
444                                                                         + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
445                                                         // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
446                                                         // We're going to manually set the currency code and use the glom numeric formatting.
447                                                         useGlomCurrencyCode = true;
448                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
449                                                 } else {
450                                                         // The length of the currency code is 0; the number is not a currency.
451                                                         numFormatJava = getJavaNumberFormat(numFormatGlom);
452                                                 }
453
454                                                 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
455
456                                                 double number = rs.getDouble(i + 1);
457                                                 if (number < 0) {
458                                                         if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
459                                                                 // overrides the set foreground colour
460                                                                 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
461                                                                                 .get_alternative_color_for_negatives()));
462                                                 }
463
464                                                 // Finally convert the number to text using the glom currency string if required.
465                                                 if (useGlomCurrencyCode) {
466                                                         rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
467                                                 } else {
468                                                         rowArray[i].setText(numFormatJava.format(number));
469                                                 }
470                                                 break;
471                                         case TYPE_DATE:
472                                                 Date date = rs.getDate(i + 1);
473                                                 if (date != null) {
474                                                         DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
475                                                         rowArray[i].setText(dateFormat.format(rs.getDate(i + 1)));
476                                                 } else {
477                                                         rowArray[i].setText("");
478                                                 }
479                                                 break;
480                                         case TYPE_TIME:
481                                                 Time time = rs.getTime(i + 1);
482                                                 if (time != null) {
483                                                         DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
484                                                         rowArray[i].setText(timeFormat.format(time));
485                                                 } else {
486                                                         rowArray[i].setText("");
487                                                 }
488                                                 break;
489                                         case TYPE_IMAGE:
490                                                 byte[] image = rs.getBytes(i + 1);
491                                                 if (image != null) {
492                                                         // TODO implement field TYPE_IMAGE
493                                                         rowArray[i].setText("Image (FIXME)");
494                                                 } else {
495                                                         rowArray[i].setText("");
496                                                 }
497                                                 break;
498                                         case TYPE_INVALID:
499                                         default:
500                                                 Log.warn("Invalid LayoutItem Field type. Using empty string for value.");
501                                                 rowArray[i].setText("");
502                                                 break;
503                                         }
504                                 }
505
506                                 // add the row of GlomFields to the ArrayList we're going to return and update the row count
507                                 rowsList.add(rowArray);
508                                 rowCount++;
509                         }
510                 } catch (SQLException e) {
511                         Log.error("Error executing database query.", e);
512                         // TODO: somehow notify user of problem
513                 } finally {
514                         // cleanup everything that has been used
515                         try {
516                                 rs.close();
517                                 st.close();
518                                 conn.close();
519                         } catch (Exception e) {
520                                 Log.error("Error closing database resources. Subsequent database queries may not work.", e);
521                         }
522                 }
523                 return rowsList;
524         }
525
526         private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
527                 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
528                 if (numFormatGlom.getM_decimal_places_restricted()) {
529                         int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
530                         numFormatJava.setMinimumFractionDigits(digits);
531                         numFormatJava.setMaximumFractionDigits(digits);
532                 }
533                 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
534                 return numFormatJava;
535         }
536
537         /*
538          * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by disgarding the least
539          * significant 8-bits in each channel.
540          */
541         private String convertGdkColorToHtmlColour(String gdkColor) {
542                 if (gdkColor.length() == 13)
543                         return gdkColor.substring(0, 2) + gdkColor.substring(5, 6) + gdkColor.substring(9, 10);
544                 else if (gdkColor.length() == 7) {
545                         // FIXME will this happen in on 32-bit?
546                         Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
547                         return gdkColor;
548                 } else {
549                         Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
550                         return "#000000";
551                 }
552         }
553
554         /*
555          * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
556          * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
557          * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum indentical to
558          * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
559          */
560         private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
561                         FieldFormatting.HorizontalAlignment alignment) {
562                 int value = alignment.swigValue();
563                 ColumnInfo.HorizontalAlignment[] columnInfoValues = ColumnInfo.HorizontalAlignment.class.getEnumConstants();
564                 if (value < columnInfoValues.length && value >= 0)
565                         return columnInfoValues[value];
566                 Log.error("getColumnInfoHorizontalAlignment(): Mismatch between "
567                                 + FieldFormatting.HorizontalAlignment.class.getName() + " and "
568                                 + ColumnInfo.HorizontalAlignment.class.getName() + ". Returning HORIZONTAL_ALIGNMENT_RIGHT.");
569                 return columnInfoValues[FieldFormatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.swigValue()];
570         }
571 }