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;
22 import java.beans.PropertyVetoException;
24 import java.io.FilenameFilter;
25 import java.io.IOException;
26 import java.io.InputStream;
27 import java.sql.Connection;
29 import java.sql.ResultSet;
30 import java.sql.SQLException;
31 import java.sql.Statement;
33 import java.text.DateFormat;
34 import java.text.NumberFormat;
35 import java.util.ArrayList;
36 import java.util.Currency;
37 import java.util.Hashtable;
38 import java.util.Locale;
39 import java.util.Properties;
41 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
42 import org.glom.libglom.Document;
43 import org.glom.libglom.Field;
44 import org.glom.libglom.FieldFormatting;
45 import org.glom.libglom.FieldVector;
46 import org.glom.libglom.Glom;
47 import org.glom.libglom.LayoutFieldVector;
48 import org.glom.libglom.LayoutGroupVector;
49 import org.glom.libglom.LayoutItem;
50 import org.glom.libglom.LayoutItemVector;
51 import org.glom.libglom.LayoutItem_Field;
52 import org.glom.libglom.LayoutItem_Portal;
53 import org.glom.libglom.NumericFormat;
54 import org.glom.libglom.SortClause;
55 import org.glom.libglom.SortFieldPair;
56 import org.glom.libglom.StringVector;
57 import org.glom.web.client.OnlineGlomService;
58 import org.glom.web.shared.ColumnInfo;
59 import org.glom.web.shared.GlomDocument;
60 import org.glom.web.shared.GlomField;
61 import org.glom.web.shared.LayoutListTable;
62 import org.glom.web.shared.layout.LayoutGroup;
63 import org.glom.web.shared.layout.LayoutItemField;
65 import com.allen_sauer.gwt.log.client.Log;
66 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
67 import com.mchange.v2.c3p0.ComboPooledDataSource;
68 import com.mchange.v2.c3p0.DataSources;
70 @SuppressWarnings("serial")
71 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
73 // class to hold configuration information for related to the glom document and db access
74 private class ConfiguredDocument {
75 private Document document;
76 private ComboPooledDataSource cpds;
77 private boolean authenticated = false;
80 public Document getDocument() { return document; }
81 public void setDocument(Document document) { this.document = document; }
82 public ComboPooledDataSource getCpds() { return cpds; }
83 public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds; }
84 public boolean isAuthenticated() { return authenticated; }
85 public void setAuthenticated(boolean authenticated) { this.authenticated = authenticated; }
89 // convenience class to for dealing with the Online Glom configuration file
90 private class OnlineGlomProperties extends Properties {
91 public String getKey(String value) {
92 for (String key : stringPropertyNames()) {
93 if (getProperty(key).trim().equals(value))
100 private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
101 // TODO implement locale
102 private final Locale locale = Locale.ROOT;
105 * This is called when the servlet is started or restarted.
107 public OnlineGlomServiceImpl() throws Exception {
109 // Find the configuration file. See this thread for background info:
110 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
111 OnlineGlomProperties config = new OnlineGlomProperties();
112 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
114 Log.fatal(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
115 + "onlineglom.properties not found.");
116 throw new IOException();
120 // check the configured glom file directory
121 String documentDirName = config.getProperty("glom.document.directory");
122 File documentDir = new File(documentDirName);
123 if (!documentDir.isDirectory()) {
124 Log.fatal(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentDirName
125 + " is not a directory.");
126 throw new IOException();
128 if (!documentDir.canRead()) {
129 Log.fatal(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + "Can't read the files in : "
131 throw new IOException();
134 // get and check the glom files in the specified directory
135 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
137 public boolean accept(File dir, String name) {
138 return name.endsWith(".glom") ? true : false;
142 for (File glomFile : glomFiles) {
143 Document document = new Document();
144 document.set_file_uri("file://" + glomFile.getAbsolutePath());
146 boolean retval = document.load(error);
147 if (retval == false) {
149 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
150 message = "Could not find " + documentDir.getAbsolutePath();
152 message = "An unknown error occurred when trying to load " + documentDir.getAbsolutePath();
154 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + message);
155 // continue with for loop because there may be other documents in the directory
159 // load the jdbc driver for the current glom document
160 ComboPooledDataSource cpds = new ComboPooledDataSource();
163 cpds.setDriverClass("org.postgresql.Driver");
164 } catch (PropertyVetoException e) {
165 Log.fatal(Thread.currentThread().getStackTrace()[1].getMethodName()
167 + "Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?");
171 // setup the JDBC driver for the current glom document
172 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
173 + document.get_connection_database());
175 // check if a username and password have been set and work for the current document
176 String documentTitle = document.get_database_title().trim();
177 ConfiguredDocument configuredDocument = new ConfiguredDocument();
178 String key = config.getKey(documentTitle);
180 String[] keyArray = key.split("\\.");
181 if (keyArray.length == 3 && "title".equals(keyArray[2])) {
182 // username/password could be set, let's check to see if it works
183 String usernameKey = key.replaceAll(keyArray[2], "username");
184 String passwordKey = key.replaceAll(keyArray[2], "password");
185 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
186 config.getProperty(usernameKey), config.getProperty(passwordKey)));
190 // check the if the global username and password have been set and work with this document
191 if (!configuredDocument.isAuthenticated()) {
192 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
193 config.getProperty("glom.document.username"), config.getProperty("glom.document.password")));
196 // add information to the hash table
197 configuredDocument.setDocument(document);
198 configuredDocument.setCpds(cpds);
199 documents.put(documentTitle, configuredDocument);
204 * Checks if the username and password works with the database configured with the specified ComboPooledDataSource.
206 * @return true if authentication works, false otherwise
208 private boolean checkAuthentication(String documentTitle, ComboPooledDataSource cpds, String username,
209 String password) throws SQLException {
210 cpds.setUser(username);
211 cpds.setPassword(password);
213 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
214 cpds.setAcquireRetryAttempts(1);
215 Connection conn = null;
217 // FIXME find a better way to check authentication
218 // it's possible that the connection could be failing for another reason
219 conn = cpds.getConnection();
221 } catch (SQLException e) {
222 Log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
223 + "Username and password not correct for document: " + documentTitle);
227 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
233 * This is called when the servlet is stopped or restarted.
235 * @see javax.servlet.GenericServlet#destroy()
238 public void destroy() {
239 Glom.libglom_deinit();
241 for (String documenTitle : documents.keySet()) {
242 ConfiguredDocument configuredDoc = documents.get(documenTitle);
244 DataSources.destroy(configuredDoc.getCpds());
245 } catch (SQLException e) {
246 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
247 + "Error cleaning up the ComboPooledDataSource for " + documenTitle, e);
253 public GlomDocument getGlomDocument(String documentTitle) {
255 Document document = documents.get(documentTitle).getDocument();
256 GlomDocument glomDocument = new GlomDocument();
258 // get arrays of table names and titles, and find the default table index
259 StringVector tablesVec = document.get_table_names();
261 int numTables = safeLongToInt(tablesVec.size());
262 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
264 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
265 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
266 boolean foundDefaultTable = false;
267 int visibleIndex = 0;
268 for (int i = 0; i < numTables; i++) {
269 String tableName = tablesVec.get(i);
270 if (!document.get_table_is_hidden(tableName)) {
271 tableNames.add(tableName);
272 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
273 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
274 glomDocument.setDefaultTableIndex(visibleIndex);
275 foundDefaultTable = true;
277 tableTitles.add(document.get_table_title(tableName));
282 // set everything we need
283 glomDocument.setTableNames(tableNames);
284 glomDocument.setTableTitles(tableTitles);
292 * @see org.glom.web.client.OnlineGlomService#getDefaultLayoutListTable(java.lang.String)
295 public LayoutListTable getDefaultLayoutListTable(String documentTitle) {
296 GlomDocument glomDocument = getGlomDocument(documentTitle);
297 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
298 LayoutListTable layoutListTable = getLayoutListTable(documentTitle, tableName);
299 layoutListTable.setTableName(tableName);
300 return layoutListTable;
303 public LayoutListTable getLayoutListTable(String documentTitle, String table) {
304 ConfiguredDocument configuredDoc = documents.get(documentTitle);
305 Document document = configuredDoc.getDocument();
306 LayoutListTable tableInfo = new LayoutListTable();
308 // access the layout list
309 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
310 ColumnInfo[] columns = null;
311 LayoutFieldVector layoutFields = new LayoutFieldVector();
312 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
313 if (listViewLayoutGroupSize > 0) {
314 // a layout list is defined, we can use it to for the LayoutListTable
315 if (listViewLayoutGroupSize > 1)
316 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
317 + table + ": The size of the list view layout group for table " + table
318 + " is greater than 1. Attempting to use the first item for the layout list view.");
319 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
321 // find the defined layout list fields
322 int numItems = safeLongToInt(layoutItemsVec.size());
323 columns = new ColumnInfo[numItems];
324 for (int i = 0; i < numItems; i++) {
325 // TODO add support for other LayoutItems (Text, Image, Button)
326 LayoutItem item = layoutItemsVec.get(i);
327 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
328 if (layoutItemField != null) {
329 layoutFields.add(layoutItemField);
330 columns[i] = new ColumnInfo(
331 layoutItemField.get_title_or_name(),
332 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
333 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
337 // no layout list is defined, use the table fields as the layout list
338 FieldVector fieldsVec = document.get_table_fields(table);
340 // find the fields to display in the layout list
341 int numItems = safeLongToInt(fieldsVec.size());
342 columns = new ColumnInfo[numItems];
343 for (int i = 0; i < numItems; i++) {
344 Field field = fieldsVec.get(i);
345 LayoutItem_Field layoutItemField = new LayoutItem_Field();
346 layoutItemField.set_full_field_details(field);
347 layoutFields.add(layoutItemField);
348 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
349 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
350 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
354 tableInfo.setColumns(columns);
356 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
358 if (!configuredDoc.isAuthenticated())
360 Connection conn = null;
364 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
365 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
366 // data. Here's the relevant PostgreSQL documentation:
367 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
368 ComboPooledDataSource cpds = configuredDoc.getCpds();
369 conn = cpds.getConnection();
370 conn.setAutoCommit(false);
371 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
372 String query = Glom.build_sql_select_count_simple(table, layoutFields);
373 // TODO Test execution time of this query with when the number of rows in the table is large (say >
374 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
375 rs = st.executeQuery(query);
377 // get the number of rows in the query
379 tableInfo.setNumRows(rs.getInt(1));
381 } catch (SQLException e) {
382 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - " + table
383 + ": Error calculating number of rows in the query. Setting number of rows to 0.", e);
384 tableInfo.setNumRows(0);
386 // cleanup everything that has been used
394 } catch (Exception e) {
395 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
396 + table + ": Error closing database resources. Subsequent database queries may not work.", e);
403 public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
404 return getTableData(documentTitle, tableName, start, length, false, 0, false);
407 public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
408 int sortColumnIndex, boolean isAscending) {
409 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
412 private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
413 boolean useSortClause, int sortColumnIndex, boolean isAscending) {
415 ConfiguredDocument configuredDoc = documents.get(documentTitle);
416 if (!configuredDoc.isAuthenticated())
417 return new ArrayList<GlomField[]>();
418 Document document = configuredDoc.getDocument();
420 // access the layout list using the defined layout list or the table fields if there's no layout list
421 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
422 LayoutFieldVector layoutFields = new LayoutFieldVector();
423 SortClause sortClause = new SortClause();
424 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
425 if (layoutListVec.size() > 0) {
426 // a layout list is defined, we can use it to for the LayoutListTable
427 if (listViewLayoutGroupSize > 1)
428 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle
429 + ": The size of the list view layout group for table " + tableName
430 + " is greater than 1. Attempting to use the first item for the layout list view.");
431 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
433 // find the defined layout list fields
434 int numItems = safeLongToInt(layoutItemsVec.size());
435 for (int i = 0; i < numItems; i++) {
436 // TODO add support for other LayoutItems (Text, Image, Button)
437 LayoutItem item = layoutItemsVec.get(i);
438 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
439 if (layoutItemfield != null) {
440 // use this field in the layout
441 layoutFields.add(layoutItemfield);
443 // create a sort clause if it's a primary key and we're not asked to sort a specific column
444 if (!useSortClause) {
445 Field details = layoutItemfield.get_full_field_details();
446 if (details != null && details.get_primary_key()) {
447 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
453 // no layout list is defined, use the table fields as the layout list
454 FieldVector fieldsVec = document.get_table_fields(tableName);
456 // find the fields to display in the layout list
457 int numItems = safeLongToInt(fieldsVec.size());
458 for (int i = 0; i < numItems; i++) {
459 Field field = fieldsVec.get(i);
460 LayoutItem_Field layoutItemField = new LayoutItem_Field();
461 layoutItemField.set_full_field_details(field);
462 layoutFields.add(layoutItemField);
464 // create a sort clause if it's a primary key and we're not asked to sort a specific column
465 if (!useSortClause) {
466 if (field.get_primary_key()) {
467 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
473 // create a sort clause for the column we've been asked to sort
475 LayoutItem item = layoutFields.get(sortColumnIndex);
476 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
478 sortClause.addLast(new SortFieldPair(field, isAscending));
480 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
481 + tableName + ": Error getting LayoutItem_Field for column index " + sortColumnIndex
482 + ". Cannot create a sort clause for this column.");
487 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
488 Connection conn = null;
492 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
493 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
494 // of data. Here's the relevant PostgreSQL documentation:
495 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
496 ComboPooledDataSource cpds = configuredDoc.getCpds();
497 conn = cpds.getConnection();
498 conn.setAutoCommit(false);
499 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
500 st.setFetchSize(length);
501 String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
502 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
503 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
504 // memory footprint. Check the difference between this value before and after the query:
505 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
506 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
507 rs = st.executeQuery(query);
509 // get the results from the ResultSet
510 rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
511 } catch (SQLException e) {
512 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
513 + tableName + ": Error executing database query.", e);
514 // TODO: somehow notify user of problem
516 // cleanup everything that has been used
524 } catch (Exception e) {
525 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
526 + tableName + ": Error closing database resources. Subsequent database queries may not work.",
533 private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
534 LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
536 // get the data we've been asked for
538 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
539 while (rs.next() && rowCount <= length) {
540 int layoutFieldsSize = safeLongToInt(layoutFields.size());
541 GlomField[] rowArray = new GlomField[layoutFieldsSize];
542 for (int i = 0; i < layoutFieldsSize; i++) {
543 // make a new GlomField to set the text and colours
544 rowArray[i] = new GlomField();
546 // get foreground and background colours
547 LayoutItem_Field field = layoutFields.get(i);
548 FieldFormatting formatting = field.get_formatting_used();
549 String fgcolour = formatting.get_text_format_color_foreground();
550 if (!fgcolour.isEmpty())
551 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
552 String bgcolour = formatting.get_text_format_color_background();
553 if (!bgcolour.isEmpty())
554 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
556 // Convert the field value to a string based on the glom type. We're doing the formatting on the
557 // server side for now but it might be useful to move this to the client side.
558 switch (field.get_glom_type()) {
560 String text = rs.getString(i + 1);
561 rowArray[i].setText(text != null ? text : "");
564 rowArray[i].setBoolean(rs.getBoolean(i + 1));
567 // Take care of the numeric formatting before converting the number to a string.
568 NumericFormat numFormatGlom = formatting.getM_numeric_format();
569 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
570 // number should be formatted as a currency if the currency code string is not empty.
571 String currencyCode = numFormatGlom.getM_currency_symbol();
572 NumberFormat numFormatJava = null;
573 boolean useGlomCurrencyCode = false;
574 if (currencyCode.length() == 3) {
575 // Try to format the currency using the Java Locales system.
577 Currency currency = Currency.getInstance(currencyCode);
578 Log.info(Thread.currentThread().getStackTrace()[1].getMethodName()
583 + ": A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
584 int digits = currency.getDefaultFractionDigits();
585 numFormatJava = NumberFormat.getCurrencyInstance(locale);
586 numFormatJava.setCurrency(currency);
587 numFormatJava.setMinimumFractionDigits(digits);
588 numFormatJava.setMaximumFractionDigits(digits);
589 } catch (IllegalArgumentException e) {
590 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle
591 + " - " + tableName + ": " + currencyCode
592 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
593 // The currency code is not this is not an ISO 4217 currency code.
594 // We're going to manually set the currency code and use the glom numeric formatting.
595 useGlomCurrencyCode = true;
596 numFormatJava = getJavaNumberFormat(numFormatGlom);
598 } else if (currencyCode.length() > 0) {
599 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle
600 + " - " + tableName + ": " + currencyCode
601 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
602 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
603 // We're going to manually set the currency code and use the glom numeric formatting.
604 useGlomCurrencyCode = true;
605 numFormatJava = getJavaNumberFormat(numFormatGlom);
607 // The length of the currency code is 0; the number is not a currency.
608 numFormatJava = getJavaNumberFormat(numFormatGlom);
611 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
613 double number = rs.getDouble(i + 1);
615 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
616 // overrides the set foreground colour
617 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
618 .get_alternative_color_for_negatives()));
621 // Finally convert the number to text using the glom currency string if required.
622 if (useGlomCurrencyCode) {
623 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
625 rowArray[i].setText(numFormatJava.format(number));
629 Date date = rs.getDate(i + 1);
631 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
632 rowArray[i].setText(dateFormat.format(date));
634 rowArray[i].setText("");
638 Time time = rs.getTime(i + 1);
640 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
641 rowArray[i].setText(timeFormat.format(time));
643 rowArray[i].setText("");
647 byte[] image = rs.getBytes(i + 1);
649 // TODO implement field TYPE_IMAGE
650 rowArray[i].setText("Image (FIXME)");
652 rowArray[i].setText("");
657 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
658 + tableName + ": Invalid LayoutItem Field type. Using empty string for value.");
659 rowArray[i].setText("");
664 // add the row of GlomFields to the ArrayList we're going to return and update the row count
665 rowsList.add(rowArray);
672 public ArrayList<String> getDocumentTitles() {
673 ArrayList<String> documentTitles = new ArrayList<String>();
674 for (String title : documents.keySet()) {
675 documentTitles.add(title);
677 return documentTitles;
681 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
683 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
685 private int safeLongToInt(long value) {
686 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
687 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
692 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
693 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
694 if (numFormatGlom.getM_decimal_places_restricted()) {
695 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
696 numFormatJava.setMinimumFractionDigits(digits);
697 numFormatJava.setMaximumFractionDigits(digits);
699 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
700 return numFormatJava;
704 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
705 * significant 8-bits in each channel.
707 private String convertGdkColorToHtmlColour(String gdkColor) {
708 if (gdkColor.length() == 13)
709 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
710 else if (gdkColor.length() == 7) {
711 // This shouldn't happen but let's deal with it if it does.
712 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName()
713 + ": Expected a 13 character string but received a 7 character string. Returning received string.");
716 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName()
717 + ": Did not receive a 13 or 7 character string. Returning black HTML colour code.");
723 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
724 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
725 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
726 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
728 private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
729 FieldFormatting.HorizontalAlignment alignment) {
731 case HORIZONTAL_ALIGNMENT_AUTO:
732 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
733 case HORIZONTAL_ALIGNMENT_LEFT:
734 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
735 case HORIZONTAL_ALIGNMENT_RIGHT:
736 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
738 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName()
739 + ": Recieved an alignment that I don't know about: "
740 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
741 + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
742 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
747 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
748 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
749 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
752 private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
755 return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
757 return ColumnInfo.GlomFieldType.TYPE_DATE;
759 return ColumnInfo.GlomFieldType.TYPE_IMAGE;
761 return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
763 return ColumnInfo.GlomFieldType.TYPE_TEXT;
765 return ColumnInfo.GlomFieldType.TYPE_TIME;
767 Log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ": Returning TYPE_INVALID.");
768 return ColumnInfo.GlomFieldType.TYPE_INVALID;
770 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName()
771 + ": Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
772 + type.toString() + ". Returning " + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
773 return ColumnInfo.GlomFieldType.TYPE_INVALID;
780 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
782 public boolean isAuthenticated(String documentTitle) {
783 return documents.get(documentTitle).isAuthenticated();
789 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
792 public boolean checkAuthentication(String documentTitle, String username, String password) {
793 ConfiguredDocument configuredDoc = documents.get(documentTitle);
794 boolean authenticated;
796 authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
797 } catch (SQLException e) {
798 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle, e);
801 configuredDoc.setAuthenticated(authenticated);
802 return authenticated;
808 * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
811 public ArrayList<LayoutGroup> getDefaultDetailsLayout(String documentTitle) {
812 GlomDocument glomDocument = getGlomDocument(documentTitle);
813 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
814 return getDetailsLayout(documentTitle, tableName);
820 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
822 public ArrayList<LayoutGroup> getDetailsLayout(String documentTitle, String tableName) {
823 // FIXME not checking if authenticated
824 ConfiguredDocument configuredDoc = documents.get(documentTitle);
825 Document document = configuredDoc.getDocument();
826 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
828 ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
829 for (int i = 0; i < layoutGroupVec.size(); i++) {
830 org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
832 if (libglomLayoutGroup == null)
835 layoutGroups.add(getLayoutGroup(documentTitle, tableName, libglomLayoutGroup));
841 * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
843 * @param libglomLayoutGroup
844 * <dt><b>Precondition:</b>
846 * libglomLayoutGroup must not be null
849 private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
850 org.glom.libglom.LayoutGroup libglomLayoutGroup) {
851 LayoutGroup layoutGroup = new LayoutGroup();
852 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
854 // look at each child item
855 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
856 for (int i = 0; i < layoutItemsVec.size(); i++) {
857 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
859 // just a safety check
860 if (libglomLayoutItem == null)
863 org.glom.web.shared.layout.LayoutItem layoutItem = null;
864 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
866 // recurse into child groups
867 layoutItem = getLayoutGroup(documentTitle, tableName, group);
869 // create GWT-Glom LayoutItem types based on the the libglom type
870 LayoutItem_Field tempField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
871 if (tempField != null) {
872 layoutItem = new LayoutItemField();
874 Log.info(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
875 + tableName + ": Ignoring unknown LayoutItem of type "
876 + libglomLayoutItem.get_part_type_name() + ".");
881 layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
882 layoutGroup.addItem(layoutItem);
888 public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
890 ConfiguredDocument configuredDoc = documents.get(documentTitle);
891 Document document = configuredDoc.getDocument();
893 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName);
895 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
896 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
897 + tableName + ": Didn't find any fields to show. Returning null.");
901 // get primary key for the table to use in the SQL query
902 Field primaryKey = null;
903 FieldVector fieldsVec = document.get_table_fields(tableName);
904 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
905 Field field = fieldsVec.get(i);
906 if (field.get_primary_key()) {
912 // send back an empty GlomField array if can't find a primaryKey Field
913 if (primaryKey == null) {
914 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
915 + tableName + ": Couldn't find primary key in table. Returning null.");
919 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
920 Connection conn = null;
924 // Setup the JDBC driver and get the query.
925 ComboPooledDataSource cpds = configuredDoc.getCpds();
926 conn = cpds.getConnection();
927 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
928 String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
929 rs = st.executeQuery(query);
931 // get the results from the ResultSet
932 // using 2 as a length parameter so we can log a warning if the result set is greater than one
933 rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
934 } catch (SQLException e) {
935 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
936 + tableName + ": Error executing database query.", e);
937 // TODO: somehow notify user of problem
940 // cleanup everything that has been used
948 } catch (Exception e) {
949 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
950 + tableName + ": Error closing database resources. Subsequent database queries may not work.",
955 if (rowsList.size() == 0) {
956 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
957 + tableName + ": The query returned an empty ResultSet. Returning null.");
959 } else if (rowsList.size() > 1) {
960 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
961 + tableName + ": The query did not return a unique result. Returning the first result in the set.");
964 return rowsList.get(0);
969 * Gets a LayoutFieldVector to use when generating an SQL query.
971 private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName) {
972 // TODO make general to be able to use "list" here - will need to change called methods
973 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
975 // We will show the fields that the document says we should:
976 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
977 for (int i = 0; i < layoutGroupVec.size(); i++) {
978 org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
981 ArrayList<LayoutItem_Field> layoutItemsFields = getFieldsToShowForSQLQueryAddGroup(document,
982 tableName, layoutGroup);
983 for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
984 layoutFieldVector.add(layoutItem_Field);
987 return layoutFieldVector;
990 private ArrayList<LayoutItem_Field> getFieldsToShowForSQLQueryAddGroup(Document document, String tableName,
991 org.glom.libglom.LayoutGroup layoutGroup) {
993 ArrayList<LayoutItem_Field> layoutItemFields = new ArrayList<LayoutItem_Field>();
994 LayoutItemVector items = layoutGroup.get_items();
995 for (int i = 0; i < items.size(); i++) {
996 LayoutItem layoutItem = items.get(i);
998 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
999 if (layoutItemField != null) {
1000 // the layoutItem is a LayoutItem_Field
1002 if (layoutItemField.get_has_relationship_name()) {
1003 // layoutItemField is a field in a related table
1004 fields = document.get_table_fields(layoutItemField.get_table_used(tableName));
1006 // layoutItemField is a field in this table
1007 fields = document.get_table_fields(tableName);
1010 // set the layoutItemFeild with details from its Field in the document and
1011 // add it to the list to be returned
1012 for (int j = 0; j < fields.size(); j++) {
1013 // check the names to see if they're the same
1014 // this works because we're using the field list from the related table if necessary
1015 if (layoutItemField.get_name().equals(fields.get(j).get_name())) {
1016 Field field = fields.get(j);
1017 if (field != null) {
1018 layoutItemField.set_full_field_details(field);
1019 layoutItemFields.add(layoutItemField);
1021 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
1022 + document.get_database_title() + " - " + tableName + "LayoutItem_Field "
1023 + layoutItemField.get_layout_display_name() + "not found in document field list.");
1030 // the layoutItem is not a LayoutItem_Field
1031 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
1032 if (subLayoutGroup != null) {
1033 // the layoutItem is a LayoutGroup
1034 LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
1035 if (layoutItemPortal == null) {
1036 // The subGroup is not a LayoutItem_Portal.
1037 // We're ignoring portals because they are filled by means of a separate SQL query.
1038 layoutItemFields.addAll(getFieldsToShowForSQLQueryAddGroup(document, tableName,
1044 return layoutItemFields;