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("onlineglom.properties not found.");
115 throw new IOException();
119 // check the configured glom file directory
120 String documentDirName = config.getProperty("glom.document.directory");
121 File documentDir = new File(documentDirName);
122 if (!documentDir.isDirectory()) {
123 Log.fatal(documentDirName + " is not a directory.");
124 throw new IOException();
126 if (!documentDir.canRead()) {
127 Log.fatal("Can't read the files in : " + documentDirName);
128 throw new IOException();
131 // get and check the glom files in the specified directory
132 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
134 public boolean accept(File dir, String name) {
135 return name.endsWith(".glom") ? true : false;
139 for (File glomFile : glomFiles) {
140 Document document = new Document();
141 document.set_file_uri("file://" + glomFile.getAbsolutePath());
143 boolean retval = document.load(error);
144 if (retval == false) {
146 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
147 message = "Could not find " + documentDir.getAbsolutePath();
149 message = "An unknown error occurred when trying to load " + documentDir.getAbsolutePath();
152 // continue with for loop because there may be other documents in the directory
156 // load the jdbc driver for the current glom document
157 ComboPooledDataSource cpds = new ComboPooledDataSource();
160 cpds.setDriverClass("org.postgresql.Driver");
161 } catch (PropertyVetoException e) {
162 Log.fatal("Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?");
166 // setup the JDBC driver for the current glom document
167 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
168 + document.get_connection_database());
170 // check if a username and password have been set and work for the current document
171 String documentTitle = document.get_database_title().trim();
172 ConfiguredDocument configuredDocument = new ConfiguredDocument();
173 String key = config.getKey(documentTitle);
175 String[] keyArray = key.split("\\.");
176 if (keyArray.length == 3 && "title".equals(keyArray[2])) {
177 // username/password could be set, let's check to see if it works
178 String usernameKey = key.replaceAll(keyArray[2], "username");
179 String passwordKey = key.replaceAll(keyArray[2], "password");
180 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
181 config.getProperty(usernameKey), config.getProperty(passwordKey)));
185 // check the if the global username and password have been set and work with this document
186 if (!configuredDocument.isAuthenticated()) {
187 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
188 config.getProperty("glom.document.username"), config.getProperty("glom.document.password")));
191 // add information to the hash table
192 configuredDocument.setDocument(document);
193 configuredDocument.setCpds(cpds);
194 documents.put(documentTitle, configuredDocument);
199 * Checks if the username and password works with the database configured with the specified ComboPooledDataSource.
201 * @return true if authentication works, false otherwise
203 private boolean checkAuthentication(String documentTitle, ComboPooledDataSource cpds, String username,
204 String password) throws SQLException {
205 cpds.setUser(username);
206 cpds.setPassword(password);
208 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
209 cpds.setAcquireRetryAttempts(1);
210 Connection conn = null;
212 // FIXME find a better way to check authentication
213 // it's possible that the connection could be failing for another reason
214 conn = cpds.getConnection();
216 } catch (SQLException e) {
217 Log.info("Username and password not correct for document: " + documentTitle);
221 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
227 * This is called when the servlet is stopped or restarted.
229 * @see javax.servlet.GenericServlet#destroy()
232 public void destroy() {
233 Glom.libglom_deinit();
235 for (String documenTitle : documents.keySet()) {
236 ConfiguredDocument configuredDoc = documents.get(documenTitle);
238 DataSources.destroy(configuredDoc.getCpds());
239 } catch (SQLException e) {
240 Log.error("Error cleaning up the ComboPooledDataSource for " + documenTitle, e);
246 public GlomDocument getGlomDocument(String documentTitle) {
248 Document document = documents.get(documentTitle).getDocument();
249 GlomDocument glomDocument = new GlomDocument();
251 // get arrays of table names and titles, and find the default table index
252 StringVector tablesVec = document.get_table_names();
254 int numTables = safeLongToInt(tablesVec.size());
255 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
257 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
258 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
259 boolean foundDefaultTable = false;
260 int visibleIndex = 0;
261 for (int i = 0; i < numTables; i++) {
262 String tableName = tablesVec.get(i);
263 if (!document.get_table_is_hidden(tableName)) {
264 tableNames.add(tableName);
265 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
266 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
267 glomDocument.setDefaultTableIndex(visibleIndex);
268 foundDefaultTable = true;
270 tableTitles.add(document.get_table_title(tableName));
275 // set everything we need
276 glomDocument.setTableNames(tableNames);
277 glomDocument.setTableTitles(tableTitles);
285 * @see org.glom.web.client.OnlineGlomService#getDefaultLayoutListTable(java.lang.String)
288 public LayoutListTable getDefaultLayoutListTable(String documentTitle) {
289 GlomDocument glomDocument = getGlomDocument(documentTitle);
290 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
291 LayoutListTable layoutListTable = getLayoutListTable(documentTitle, tableName);
292 layoutListTable.setTableName(tableName);
293 return layoutListTable;
296 public LayoutListTable getLayoutListTable(String documentTitle, String table) {
297 ConfiguredDocument configuredDoc = documents.get(documentTitle);
298 Document document = configuredDoc.getDocument();
299 LayoutListTable tableInfo = new LayoutListTable();
301 // access the layout list
302 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
303 ColumnInfo[] columns = null;
304 LayoutFieldVector layoutFields = new LayoutFieldVector();
305 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
306 if (listViewLayoutGroupSize > 0) {
307 // a layout list is defined, we can use it to for the LayoutListTable
308 if (listViewLayoutGroupSize > 1)
309 Log.warn(documentTitle + " - " + table + ": The size of the list view layout group for table " + table
310 + " is greater than 1. Attempting to use the first item for the layout list view.");
311 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
313 // find the defined layout list fields
314 int numItems = safeLongToInt(layoutItemsVec.size());
315 columns = new ColumnInfo[numItems];
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 layoutFields.add(layoutItemField);
322 columns[i] = new ColumnInfo(
323 layoutItemField.get_title_or_name(),
324 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
325 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
329 // no layout list is defined, use the table fields as the layout list
330 FieldVector fieldsVec = document.get_table_fields(table);
332 // find the fields to display in the layout list
333 int numItems = safeLongToInt(fieldsVec.size());
334 columns = new ColumnInfo[numItems];
335 for (int i = 0; i < numItems; i++) {
336 Field field = fieldsVec.get(i);
337 LayoutItem_Field layoutItemField = new LayoutItem_Field();
338 layoutItemField.set_full_field_details(field);
339 layoutFields.add(layoutItemField);
340 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
341 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
342 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
346 tableInfo.setColumns(columns);
348 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
350 if (!configuredDoc.isAuthenticated())
352 Connection conn = null;
356 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
357 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
358 // data. Here's the relevant PostgreSQL documentation:
359 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
360 ComboPooledDataSource cpds = configuredDoc.getCpds();
361 conn = cpds.getConnection();
362 conn.setAutoCommit(false);
363 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
364 String query = Glom.build_sql_select_count_simple(table, layoutFields);
365 // TODO Test execution time of this query with when the number of rows in the table is large (say >
366 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
367 rs = st.executeQuery(query);
369 // get the number of rows in the query
371 tableInfo.setNumRows(rs.getInt(1));
373 } catch (SQLException e) {
374 Log.error(documentTitle + " - " + table
375 + ": Error calculating number of rows in the query. Setting number of rows to 0.", e);
376 tableInfo.setNumRows(0);
378 // cleanup everything that has been used
386 } catch (Exception e) {
387 Log.error(documentTitle + " - " + table
388 + ": Error closing database resources. Subsequent database queries may not work.", e);
395 public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
396 return getTableData(documentTitle, tableName, start, length, false, 0, false);
399 public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
400 int sortColumnIndex, boolean isAscending) {
401 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
404 private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
405 boolean useSortClause, int sortColumnIndex, boolean isAscending) {
407 ConfiguredDocument configuredDoc = documents.get(documentTitle);
408 if (!configuredDoc.isAuthenticated())
409 return new ArrayList<GlomField[]>();
410 Document document = configuredDoc.getDocument();
412 // access the layout list using the defined layout list or the table fields if there's no layout list
413 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
414 LayoutFieldVector layoutFields = new LayoutFieldVector();
415 SortClause sortClause = new SortClause();
416 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
417 if (layoutListVec.size() > 0) {
418 // a layout list is defined, we can use it to for the LayoutListTable
419 if (listViewLayoutGroupSize > 1)
420 Log.warn(documentTitle + ": The size of the list view layout group for table " + tableName
421 + " is greater than 1. Attempting to use the first item for the layout list view.");
422 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
424 // find the defined layout list fields
425 int numItems = safeLongToInt(layoutItemsVec.size());
426 for (int i = 0; i < numItems; i++) {
427 // TODO add support for other LayoutItems (Text, Image, Button)
428 LayoutItem item = layoutItemsVec.get(i);
429 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
430 if (layoutItemfield != null) {
431 // use this field in the layout
432 layoutFields.add(layoutItemfield);
434 // create a sort clause if it's a primary key and we're not asked to sort a specific column
435 if (!useSortClause) {
436 Field details = layoutItemfield.get_full_field_details();
437 if (details != null && details.get_primary_key()) {
438 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
444 // no layout list is defined, use the table fields as the layout list
445 FieldVector fieldsVec = document.get_table_fields(tableName);
447 // find the fields to display in the layout list
448 int numItems = safeLongToInt(fieldsVec.size());
449 for (int i = 0; i < numItems; i++) {
450 Field field = fieldsVec.get(i);
451 LayoutItem_Field layoutItemField = new LayoutItem_Field();
452 layoutItemField.set_full_field_details(field);
453 layoutFields.add(layoutItemField);
455 // create a sort clause if it's a primary key and we're not asked to sort a specific column
456 if (!useSortClause) {
457 if (field.get_primary_key()) {
458 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
464 // create a sort clause for the column we've been asked to sort
466 LayoutItem item = layoutFields.get(sortColumnIndex);
467 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
469 sortClause.addLast(new SortFieldPair(field, isAscending));
471 Log.error(documentTitle + " - " + tableName + ": Error getting LayoutItem_Field for column index "
472 + sortColumnIndex + ". Cannot create a sort clause for this column.");
477 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
478 Connection conn = null;
482 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
483 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
484 // of data. Here's the relevant PostgreSQL documentation:
485 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
486 ComboPooledDataSource cpds = configuredDoc.getCpds();
487 conn = cpds.getConnection();
488 conn.setAutoCommit(false);
489 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
490 st.setFetchSize(length);
491 String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
492 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
493 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
494 // memory footprint. Check the difference between this value before and after the query:
495 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
496 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
497 rs = st.executeQuery(query);
499 // get the results from the ResultSet
500 rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
501 } catch (SQLException e) {
502 Log.error(documentTitle + " - " + tableName + ": Error executing database query.", e);
503 // TODO: somehow notify user of problem
505 // cleanup everything that has been used
513 } catch (Exception e) {
514 Log.error(documentTitle + " - " + tableName
515 + ": Error closing database resources. Subsequent database queries may not work.", e);
521 private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
522 LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
524 // get the data we've been asked for
526 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
527 while (rs.next() && rowCount <= length) {
528 int layoutFieldsSize = safeLongToInt(layoutFields.size());
529 GlomField[] rowArray = new GlomField[layoutFieldsSize];
530 for (int i = 0; i < layoutFieldsSize; i++) {
531 // make a new GlomField to set the text and colours
532 rowArray[i] = new GlomField();
534 // get foreground and background colours
535 LayoutItem_Field field = layoutFields.get(i);
536 FieldFormatting formatting = field.get_formatting_used();
537 String fgcolour = formatting.get_text_format_color_foreground();
538 if (!fgcolour.isEmpty())
539 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
540 String bgcolour = formatting.get_text_format_color_background();
541 if (!bgcolour.isEmpty())
542 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
544 // Convert the field value to a string based on the glom type. We're doing the formatting on the
545 // server side for now but it might be useful to move this to the client side.
546 switch (field.get_glom_type()) {
548 String text = rs.getString(i + 1);
549 rowArray[i].setText(text != null ? text : "");
552 rowArray[i].setBoolean(rs.getBoolean(i + 1));
555 // Take care of the numeric formatting before converting the number to a string.
556 NumericFormat numFormatGlom = formatting.getM_numeric_format();
557 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
558 // number should be formatted as a currency if the currency code string is not empty.
559 String currencyCode = numFormatGlom.getM_currency_symbol();
560 NumberFormat numFormatJava = null;
561 boolean useGlomCurrencyCode = false;
562 if (currencyCode.length() == 3) {
563 // Try to format the currency using the Java Locales system.
565 Currency currency = Currency.getInstance(currencyCode);
566 Log.info(documentTitle
569 + ": A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
570 int digits = currency.getDefaultFractionDigits();
571 numFormatJava = NumberFormat.getCurrencyInstance(locale);
572 numFormatJava.setCurrency(currency);
573 numFormatJava.setMinimumFractionDigits(digits);
574 numFormatJava.setMaximumFractionDigits(digits);
575 } catch (IllegalArgumentException e) {
576 Log.warn(documentTitle + " - " + tableName + ": " + currencyCode
577 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
578 // The currency code is not this is not an ISO 4217 currency code.
579 // We're going to manually set the currency code and use the glom numeric formatting.
580 useGlomCurrencyCode = true;
581 numFormatJava = getJavaNumberFormat(numFormatGlom);
583 } else if (currencyCode.length() > 0) {
584 Log.warn(documentTitle + " - " + tableName + ": " + currencyCode
585 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
586 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
587 // We're going to manually set the currency code and use the glom numeric formatting.
588 useGlomCurrencyCode = true;
589 numFormatJava = getJavaNumberFormat(numFormatGlom);
591 // The length of the currency code is 0; the number is not a currency.
592 numFormatJava = getJavaNumberFormat(numFormatGlom);
595 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
597 double number = rs.getDouble(i + 1);
599 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
600 // overrides the set foreground colour
601 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
602 .get_alternative_color_for_negatives()));
605 // Finally convert the number to text using the glom currency string if required.
606 if (useGlomCurrencyCode) {
607 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
609 rowArray[i].setText(numFormatJava.format(number));
613 Date date = rs.getDate(i + 1);
615 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
616 rowArray[i].setText(dateFormat.format(date));
618 rowArray[i].setText("");
622 Time time = rs.getTime(i + 1);
624 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
625 rowArray[i].setText(timeFormat.format(time));
627 rowArray[i].setText("");
631 byte[] image = rs.getBytes(i + 1);
633 // TODO implement field TYPE_IMAGE
634 rowArray[i].setText("Image (FIXME)");
636 rowArray[i].setText("");
641 Log.warn(documentTitle + " - " + tableName
642 + ": Invalid LayoutItem Field type. Using empty string for value.");
643 rowArray[i].setText("");
648 // add the row of GlomFields to the ArrayList we're going to return and update the row count
649 rowsList.add(rowArray);
656 public ArrayList<String> getDocumentTitles() {
657 ArrayList<String> documentTitles = new ArrayList<String>();
658 for (String title : documents.keySet()) {
659 documentTitles.add(title);
661 return documentTitles;
665 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
667 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
669 private int safeLongToInt(long value) {
670 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
671 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
676 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
677 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
678 if (numFormatGlom.getM_decimal_places_restricted()) {
679 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
680 numFormatJava.setMinimumFractionDigits(digits);
681 numFormatJava.setMaximumFractionDigits(digits);
683 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
684 return numFormatJava;
688 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
689 * significant 8-bits in each channel.
691 private String convertGdkColorToHtmlColour(String gdkColor) {
692 if (gdkColor.length() == 13)
693 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
694 else if (gdkColor.length() == 7) {
695 // This shouldn't happen but let's deal with it if it does.
696 Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
699 Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
705 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
706 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
707 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
708 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
710 private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
711 FieldFormatting.HorizontalAlignment alignment) {
713 case HORIZONTAL_ALIGNMENT_AUTO:
714 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
715 case HORIZONTAL_ALIGNMENT_LEFT:
716 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
717 case HORIZONTAL_ALIGNMENT_RIGHT:
718 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
720 Log.error("getColumnInfoGlomFieldType(): Recieved an alignment that I don't know about: "
721 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
722 + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
723 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
728 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
729 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
730 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
733 private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
736 return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
738 return ColumnInfo.GlomFieldType.TYPE_DATE;
740 return ColumnInfo.GlomFieldType.TYPE_IMAGE;
742 return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
744 return ColumnInfo.GlomFieldType.TYPE_TEXT;
746 return ColumnInfo.GlomFieldType.TYPE_TIME;
748 Log.info("getColumnInfoGlomFieldType(): Returning TYPE_INVALID.");
749 return ColumnInfo.GlomFieldType.TYPE_INVALID;
751 Log.error("getColumnInfoGlomFieldType(): Recieved a type that I don't know about: "
752 + Field.glom_field_type.class.getName() + "." + type.toString() + ". Returning "
753 + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
754 return ColumnInfo.GlomFieldType.TYPE_INVALID;
761 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
763 public boolean isAuthenticated(String documentTitle) {
764 return documents.get(documentTitle).isAuthenticated();
770 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
773 public boolean checkAuthentication(String documentTitle, String username, String password) {
774 ConfiguredDocument configuredDoc = documents.get(documentTitle);
775 boolean authenticated;
777 authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
778 } catch (SQLException e) {
782 configuredDoc.setAuthenticated(authenticated);
783 return authenticated;
789 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
791 public LayoutGroup getDetailsLayoutGroup(String documentTitle, String tableName) {
792 // FIXME not checking if authenticated
793 ConfiguredDocument configuredDoc = documents.get(documentTitle);
794 Document document = configuredDoc.getDocument();
795 LayoutGroupVector layoutGroupsVec = document.get_data_layout_groups("details", tableName);
796 org.glom.libglom.LayoutGroup libGlomLayoutGroup = layoutGroupsVec.get(0);
798 LayoutGroup layoutGroup = new LayoutGroup();
799 if (libGlomLayoutGroup == null)
802 layoutGroup.setTitle(libGlomLayoutGroup.get_title());
804 return getLayoutGroup(documentTitle, tableName, libGlomLayoutGroup);
808 * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
810 * @param libglomLayoutGroup
811 * <dt><b>Precondition:</b>
813 * libglomLayoutGroup must not be null
816 private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
817 org.glom.libglom.LayoutGroup libglomLayoutGroup) {
818 LayoutGroup layoutGroup = new LayoutGroup();
819 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
821 // look at each child item
822 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
823 for (int i = 0; i < layoutItemsVec.size(); i++) {
824 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
826 // just a safety check
827 if (libglomLayoutItem == null)
830 org.glom.web.shared.layout.LayoutItem layoutItem = null;
831 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
833 // recurse into child groups
834 layoutItem = getLayoutGroup(documentTitle, tableName, group);
836 // create GWT-Glom LayoutItem types based on the the libglom type
837 // FIXME use cast_dynamic methods to determine the libglom type
838 String partTypeName = libglomLayoutItem.get_part_type_name();
839 if ("Field".equals(partTypeName)) {
840 layoutItem = new LayoutItemField();
842 Log.warn(documentTitle + " - " + tableName
843 + "- getLayoutGroup(): Ignoring unknown LayoutItem of type: " + partTypeName);
848 layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
849 layoutGroup.addItem(layoutItem);
855 public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
857 ConfiguredDocument configuredDoc = documents.get(documentTitle);
858 Document document = configuredDoc.getDocument();
860 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName);
862 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
863 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
864 + tableName + ": Didn't find any fields to show. Returning null.");
868 // get primary key for the table to use in the SQL query
869 Field primaryKey = null;
870 FieldVector fieldsVec = document.get_table_fields(tableName);
871 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
872 Field field = fieldsVec.get(i);
873 if (field.get_primary_key()) {
879 // send back an empty GlomField array if can't find a primaryKey Field
880 if (primaryKey == null) {
881 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
882 + tableName + ": Couldn't find primary key in table. Returning null.");
886 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
887 Connection conn = null;
891 // Setup the JDBC driver and get the query.
892 ComboPooledDataSource cpds = configuredDoc.getCpds();
893 conn = cpds.getConnection();
894 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
895 String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
896 rs = st.executeQuery(query);
898 // get the results from the ResultSet
899 // using 2 as a length parameter so we can log a warning if the result set is greater than one
900 rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
901 } catch (SQLException e) {
902 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
903 + tableName + ": Error executing database query.", e);
904 // TODO: somehow notify user of problem
907 // cleanup everything that has been used
915 } catch (Exception e) {
916 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
917 + tableName + ": Error closing database resources. Subsequent database queries may not work.",
922 if (rowsList.size() == 0) {
923 Log.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
924 + tableName + ": The query returned an empty ResultSet. Returning null.");
926 } else if (rowsList.size() > 1) {
927 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": " + documentTitle + " - "
928 + tableName + ": The query did not return a unique result. Returning the first result in the set.");
931 return rowsList.get(0);
936 * Gets a LayoutFieldVector to use when generating an SQL query.
938 private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName) {
939 // TODO make general to be able to use "list" here - will need to change called methods
940 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
941 return getTableFieldsToShowForSequence(document, tableName, layoutGroupVec);
944 private LayoutFieldVector getTableFieldsToShowForSequence(Document document, String tableName,
945 LayoutGroupVector layoutGroupVec) {
947 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
948 // We will show the fields that the document says we should:
949 for (int i = 0; i < layoutGroupVec.size(); i++) {
950 org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
953 ArrayList<LayoutItem_Field> layoutItemsFields = getTableFieldsToShowForSequenceAddGroup(document,
954 tableName, layoutGroup);
955 for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
956 layoutFieldVector.add(layoutItem_Field);
959 return layoutFieldVector;
962 private ArrayList<LayoutItem_Field> getTableFieldsToShowForSequenceAddGroup(Document document, String tableName,
963 org.glom.libglom.LayoutGroup layoutGroup) {
965 ArrayList<LayoutItem_Field> layoutItemFields = new ArrayList<LayoutItem_Field>();
966 LayoutItemVector items = layoutGroup.get_items();
967 for (int i = 0; i < items.size(); i++) {
968 LayoutItem layoutItem = items.get(i);
970 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
971 if (layoutItemField != null) {
972 // the layoutItem is a LayoutItem_Field
974 if (layoutItemField.get_has_relationship_name()) {
975 // layoutItemField is a field in a related table
976 fields = document.get_table_fields(layoutItemField.get_table_used(tableName));
978 // layoutItemField is a field in this table
979 fields = document.get_table_fields(tableName);
982 // set the layoutItemFeild with details from its Field in the document and
983 // add it to the list to be returned
984 for (int j = 0; j < fields.size(); j++) {
985 // check the names to see if they're the same
986 // this works because we're using the field list from the related table if necessary
987 if (layoutItemField.get_name().equals(fields.get(j).get_name())) {
988 Field field = fields.get(j);
990 layoutItemField.set_full_field_details(field);
991 layoutItemFields.add(layoutItemField);
993 Log.warn(Thread.currentThread().getStackTrace()[1].getMethodName() + ": "
994 + document.get_database_title() + " - " + tableName + "LayoutItem_Field "
995 + layoutItemField.get_layout_display_name() + "not found in document field list.");
1002 // the layoutItem is not a LayoutItem_Field
1003 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
1004 if (subLayoutGroup != null) {
1005 // the layoutItem is a LayoutGroup
1006 LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
1007 if (layoutItemPortal == null) {
1008 // The subGroup is not a LayoutItem_Portal.
1009 // We're ignoring portals because they are filled by means of a separate SQL query.
1010 layoutItemFields.addAll(getTableFieldsToShowForSequenceAddGroup(document, tableName,
1016 return layoutItemFields;
1022 * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
1025 public LayoutGroup getDefaultDetailsLayoutGroup(String documentTitle) {
1026 GlomDocument glomDocument = getGlomDocument(documentTitle);
1027 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
1028 return getDetailsLayoutGroup(documentTitle, tableName);