2 * Copyright (C) 2010, 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.server;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.sql.Connection;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
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;
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;
65 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
66 import com.mchange.v2.c3p0.ComboPooledDataSource;
67 import com.mchange.v2.c3p0.DataSources;
70 * The servlet for retrieving layout information from libglom and data from the underlying PostgreSQL database.
72 * TODO: move methods that that require a glom document object to the ConfiguredDocument class.
74 * @author Ben Konrath <ben@bagu.org>
76 @SuppressWarnings("serial")
77 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
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))
90 private final Hashtable<String, ConfiguredDocument> documentMapping = new Hashtable<String, ConfiguredDocument>();
91 // TODO implement locale
92 private final Locale locale = Locale.ROOT;
95 * This is called when the servlet is started or restarted.
97 public OnlineGlomServiceImpl() throws Exception {
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");
104 Log.fatal("onlineglom.properties not found.");
105 throw new IOException();
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();
116 if (!documentDir.canRead()) {
117 Log.fatal("Can't read the files in : " + documentDirName);
118 throw new IOException();
121 // get and check the glom files in the specified directory
122 final String glomFileExtension = ".glom";
123 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
125 public boolean accept(File dir, String name) {
126 return name.endsWith(glomFileExtension);
130 for (File glomFile : glomFiles) {
131 Document document = new Document();
132 document.set_file_uri("file://" + glomFile.getAbsolutePath());
134 boolean retval = document.load(error);
135 if (retval == false) {
137 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
138 message = "Could not find file: " + glomFile.getAbsolutePath();
140 message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
143 // continue with for loop because there may be other documents in the directory
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);
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));
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"));
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(
172 documentMapping.put(hashKey, configuredDocument);
178 * This is called when the servlet is stopped or restarted.
180 * @see javax.servlet.GenericServlet#destroy()
183 public void destroy() {
184 Glom.libglom_deinit();
186 for (String documenTitle : documentMapping.keySet()) {
187 ConfiguredDocument configuredDoc = documentMapping.get(documenTitle);
189 DataSources.destroy(configuredDoc.getCpds());
190 } catch (SQLException e) {
191 Log.error(documenTitle, "Error cleaning up the ComboPooledDataSource.", e);
197 public GlomDocument getGlomDocument(String documentID) {
199 Document document = documentMapping.get(documentID).getDocument();
200 GlomDocument glomDocument = new GlomDocument();
202 // get arrays of table names and titles, and find the default table index
203 StringVector tablesVec = document.get_table_names();
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
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;
221 tableTitles.add(document.get_table_title(tableName));
226 // set everything we need
227 glomDocument.setTableNames(tableNames);
228 glomDocument.setTableTitles(tableTitles);
229 glomDocument.setTitle(document.get_database_title());
234 public LayoutGroup getListLayout(String documentID, String tableName) {
235 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
236 Document document = configuredDoc.getDocument();
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();
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.");
254 libglomLayoutGroup = layoutGroupVec.get(0);
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.");
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);
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.");
276 LayoutGroup layoutGroup = getLayoutGroup(documentID, tableName, libglomLayoutGroup);
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));
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);
293 layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
296 layoutGroup.setTableName(tableName);
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()) {
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
317 LayoutItem_Field libglomLayoutItemField = new LayoutItem_Field();
318 libglomLayoutItemField.set_full_field_details(primaryKey);
319 return libglomLayoutItemField;
323 * Gets the primary key index of the LayoutFieldVector.
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())
336 * Get the number of rows a query with the table name and layout fields would return. This is needed for the /* list
339 private int getResultSizeOfSQLQuery(String documentID, String tableName, LayoutFieldVector fieldsToGet) {
340 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
341 if (!configuredDoc.isAuthenticated())
343 Connection conn = null;
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);
360 // get the number of rows in the query
364 } catch (SQLException e) {
365 Log.error(documentID, tableName, "Error calculating number of rows in the query.", e);
368 // cleanup everything that has been used
376 } catch (Exception e) {
377 Log.error(documentID, tableName,
378 "Error closing database resources. Subsequent database queries may not work.", e);
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);
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);
393 private ArrayList<GlomField[]> getListData(String documentID, String tableName, int start, int length,
394 boolean useSortClause, int sortColumnIndex, boolean isAscending) {
396 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
397 if (!configuredDoc.isAuthenticated()) {
398 return new ArrayList<GlomField[]>();
401 Document document = configuredDoc.getDocument();
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();
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();
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);
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
441 // no layout list is defined, use the table fields as the layout list
442 FieldVector fieldsVec = document.get_table_fields(tableName);
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);
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
461 // create a sort clause for the column we've been asked to sort
463 LayoutItem item = layoutFields.get(sortColumnIndex);
464 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
466 sortClause.addLast(new SortFieldPair(field, isAscending));
468 Log.error(documentID, tableName, "Error getting LayoutItem_Field for column index " + sortColumnIndex
469 + ". Cannot create a sort clause for this column.");
473 // Add a LayoutItem_Field for the primary key to the end of the LayoutFieldVector if it doesn't already contain
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));
481 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
482 Connection conn = null;
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);
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
509 // cleanup everything that has been used
517 } catch (Exception e) {
518 Log.error(documentID, tableName,
519 "Error closing database resources. Subsequent database queries may not work.", e);
525 private ArrayList<GlomField[]> getData(String documentID, String tableName, int length,
526 LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
528 // get the data we've been asked for
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();
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));
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()) {
552 String text = rs.getString(i + 1);
553 rowArray[i].setText(text != null ? text : "");
556 rowArray[i].setBoolean(rs.getBoolean(i + 1));
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.
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);
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);
593 // The length of the currency code is 0; the number is not a currency.
594 numFormatJava = convertToJavaNumberFormat(numFormatGlom);
597 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
599 double number = rs.getDouble(i + 1);
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()));
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));
611 rowArray[i].setText(numFormatJava.format(number));
615 Date date = rs.getDate(i + 1);
617 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
618 rowArray[i].setText(dateFormat.format(date));
620 rowArray[i].setText("");
624 Time time = rs.getTime(i + 1);
626 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
627 rowArray[i].setText(timeFormat.format(time));
629 rowArray[i].setText("");
633 byte[] image = rs.getBytes(i + 1);
635 // TODO implement field TYPE_IMAGE
636 rowArray[i].setText("Image (FIXME)");
638 rowArray[i].setText("");
643 Log.warn(documentID, tableName, "Invalid LayoutItem Field type. Using empty string for value.");
644 rowArray[i].setText("");
649 // add the row of GlomFields to the ArrayList we're going to return and update the row count
650 rowsList.add(rowArray);
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());
667 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
669 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
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.");
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);
685 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
686 return numFormatJava;
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.
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.");
701 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
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.
712 private Formatting.HorizontalAlignment convertToGWTGlomHorizonalAlignment(
713 FieldFormatting.HorizontalAlignment 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;
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;
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
735 private LayoutItemField.GlomFieldType convertToGWTGlomFieldType(Field.glom_field_type type) {
738 return LayoutItemField.GlomFieldType.TYPE_BOOLEAN;
740 return LayoutItemField.GlomFieldType.TYPE_DATE;
742 return LayoutItemField.GlomFieldType.TYPE_IMAGE;
744 return LayoutItemField.GlomFieldType.TYPE_NUMERIC;
746 return LayoutItemField.GlomFieldType.TYPE_TEXT;
748 return LayoutItemField.GlomFieldType.TYPE_TIME;
750 Log.info("Returning TYPE_INVALID.");
751 return LayoutItemField.GlomFieldType.TYPE_INVALID;
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;
762 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
764 public boolean isAuthenticated(String documentID) {
765 return documentMapping.get(documentID).isAuthenticated();
771 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
774 public boolean checkAuthentication(String documentID, String username, String password) {
775 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
777 return configuredDoc.setUsernameAndPassword(username, password);
778 } catch (SQLException e) {
779 Log.error(documentID, "Unknown SQL Error checking the database authentication.", e);
787 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
789 public ArrayList<LayoutGroup> getDetailsLayout(String documentID, String tableName) {
790 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
791 Document document = configuredDoc.getDocument();
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();
799 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
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);
805 if (libglomLayoutGroup == null)
808 layoutGroups.add(getLayoutGroup(documentID, tableName, libglomLayoutGroup));
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.
818 * @param documentID Glom document identifier
820 * @param tableName table name in the specified Glom document
822 * @param libglomLayoutGroup libglom LayoutGroup to convert
824 * @precondition libglomLayoutGroup must not be null
826 * @return {@link LayoutGroup} object that represents the layout for the specified {@link
827 * org.glom.libglom.LayoutGroup}
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());
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);
840 // just a safety check
841 if (libglomLayoutItem == null)
844 org.glom.web.shared.layout.LayoutItem layoutItem = null;
845 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
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();
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;
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);
867 Log.info(documentID, tableName,
868 "Ignoring unknown LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
874 layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
875 layoutGroup.addItem(layoutItem);
881 private LayoutItemField convertToGWTGlomLayoutItemField(LayoutItem_Field libglomLayoutField) {
882 LayoutItemField layoutItemField = new LayoutItemField();
885 layoutItemField.setType(convertToGWTGlomFieldType(libglomLayoutField.get_glom_type()));
888 Formatting formatting = new Formatting();
889 formatting.setHorizontalAlignment(convertToGWTGlomHorizonalAlignment(libglomLayoutField
890 .get_formatting_used_horizontal_alignment()));
891 layoutItemField.setFormatting(formatting);
893 return layoutItemField;
896 public GlomField[] getDetailsData(String documentID, String tableName, String primaryKeyValue) {
898 ConfiguredDocument configuredDoc = documentMapping.get(documentID);
899 Document document = configuredDoc.getDocument();
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();
907 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName, "details");
909 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
910 Log.warn(documentID, tableName, "Didn't find any fields to show. Returning null.");
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()) {
925 if (primaryKey == null) {
926 Log.error(documentID, tableName, "Couldn't find primary key in table. Returning null.");
930 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
931 Connection conn = null;
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);
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
950 // cleanup everything that has been used
958 } catch (Exception e) {
959 Log.error(documentID, tableName,
960 "Error closing database resources. Subsequent database queries may not work.", e);
964 if (rowsList.size() == 0) {
965 Log.error(documentID, tableName, "The query returned an empty ResultSet. Returning 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.");
973 return rowsList.get(0);
978 * Gets a LayoutFieldVector to use when generating an SQL query.
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();
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);
993 return layoutFieldVector;
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);
1001 ArrayList<LayoutItem_Field> layoutItemsFields = getFieldsToShowForSQLQueryAddGroup(document, tableName,
1003 for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
1004 layoutFieldVector.add(layoutItem_Field);
1007 return layoutFieldVector;
1010 private ArrayList<LayoutItem_Field> getFieldsToShowForSQLQueryAddGroup(Document document, String tableName,
1011 org.glom.libglom.LayoutGroup layoutGroup) {
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);
1018 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
1019 if (layoutItemField != null) {
1020 // the layoutItem is a LayoutItem_Field
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));
1026 // layoutItemField is a field in this table
1027 fields = document.get_table_fields(tableName);
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);
1041 Log.warn(document.get_database_title(), tableName,
1042 "LayoutItem_Field " + layoutItemField.get_layout_display_name()
1043 + " not found in document field list.");
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.
1059 .addAll(getFieldsToShowForSQLQueryAddGroup(document, tableName, subLayoutGroup));
1064 return layoutItemFields;