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.google.gwt.user.server.rpc.RemoteServiceServlet;
66 import com.mchange.v2.c3p0.ComboPooledDataSource;
67 import com.mchange.v2.c3p0.DataSources;
69 @SuppressWarnings("serial")
70 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
72 // class to hold configuration information for related to the glom document and db access
73 private class ConfiguredDocument {
74 private Document document;
75 private ComboPooledDataSource cpds;
76 private boolean authenticated = false;
79 public Document getDocument() { return document; }
80 public void setDocument(Document document) { this.document = document; }
81 public ComboPooledDataSource getCpds() { return cpds; }
82 public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds; }
83 public boolean isAuthenticated() { return authenticated; }
84 public void setAuthenticated(boolean authenticated) { this.authenticated = authenticated; }
88 // convenience class to for dealing with the Online Glom configuration file
89 private class OnlineGlomProperties extends Properties {
90 public String getKey(String value) {
91 for (String key : stringPropertyNames()) {
92 if (getProperty(key).trim().equals(value))
99 private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
100 // TODO implement locale
101 private final Locale locale = Locale.ROOT;
104 * This is called when the servlet is started or restarted.
106 public OnlineGlomServiceImpl() throws Exception {
108 // Find the configuration file. See this thread for background info:
109 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
110 OnlineGlomProperties config = new OnlineGlomProperties();
111 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
113 Log.fatal("onlineglom.properties not found.");
114 throw new IOException();
118 // check the configured glom file directory
119 String documentDirName = config.getProperty("glom.document.directory");
120 File documentDir = new File(documentDirName);
121 if (!documentDir.isDirectory()) {
122 Log.fatal(documentDirName + " is not a directory.");
123 throw new IOException();
125 if (!documentDir.canRead()) {
126 Log.fatal("Can't read the files in : " + documentDirName);
127 throw new IOException();
130 // get and check the glom files in the specified directory
131 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
133 public boolean accept(File dir, String name) {
134 return name.endsWith(".glom");
138 for (File glomFile : glomFiles) {
139 Document document = new Document();
140 document.set_file_uri("file://" + glomFile.getAbsolutePath());
142 boolean retval = document.load(error);
143 if (retval == false) {
145 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
146 message = "Could not find file: " + glomFile.getAbsolutePath();
148 message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
151 // continue with for loop because there may be other documents in the directory
155 // load the jdbc driver for the current glom document
156 ComboPooledDataSource cpds = new ComboPooledDataSource();
159 cpds.setDriverClass("org.postgresql.Driver");
160 } catch (PropertyVetoException e) {
161 Log.fatal("Error loading the PostgreSQL JDBC driver."
162 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
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(documentTitle, "Username and password not correct.");
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(documenTitle, "Error cleaning up the ComboPooledDataSource.", 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, tableName,
421 "The size of the list view layout group for table is greater than 1. "
422 + "Attempting to use the first item for the layout list view.");
423 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
425 // find the defined layout list fields
426 int numItems = safeLongToInt(layoutItemsVec.size());
427 for (int i = 0; i < numItems; i++) {
428 // TODO add support for other LayoutItems (Text, Image, Button)
429 LayoutItem item = layoutItemsVec.get(i);
430 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
431 if (layoutItemfield != null) {
432 // use this field in the layout
433 layoutFields.add(layoutItemfield);
435 // create a sort clause if it's a primary key and we're not asked to sort a specific column
436 if (!useSortClause) {
437 Field details = layoutItemfield.get_full_field_details();
438 if (details != null && details.get_primary_key()) {
439 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
445 // no layout list is defined, use the table fields as the layout list
446 FieldVector fieldsVec = document.get_table_fields(tableName);
448 // find the fields to display in the layout list
449 int numItems = safeLongToInt(fieldsVec.size());
450 for (int i = 0; i < numItems; i++) {
451 Field field = fieldsVec.get(i);
452 LayoutItem_Field layoutItemField = new LayoutItem_Field();
453 layoutItemField.set_full_field_details(field);
454 layoutFields.add(layoutItemField);
456 // create a sort clause if it's a primary key and we're not asked to sort a specific column
457 if (!useSortClause) {
458 if (field.get_primary_key()) {
459 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
465 // create a sort clause for the column we've been asked to sort
467 LayoutItem item = layoutFields.get(sortColumnIndex);
468 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
470 sortClause.addLast(new SortFieldPair(field, isAscending));
472 Log.error(documentTitle, tableName, "Error getting LayoutItem_Field for column index "
473 + sortColumnIndex + ". Cannot create a sort clause for this column.");
478 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
479 Connection conn = null;
483 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
484 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
485 // of data. Here's the relevant PostgreSQL documentation:
486 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
487 ComboPooledDataSource cpds = configuredDoc.getCpds();
488 conn = cpds.getConnection();
489 conn.setAutoCommit(false);
490 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
491 st.setFetchSize(length);
492 String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
493 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
494 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
495 // memory footprint. Check the difference between this value before and after the query:
496 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
497 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
498 rs = st.executeQuery(query);
500 // get the results from the ResultSet
501 rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
502 } catch (SQLException e) {
503 Log.error(documentTitle, tableName, "Error executing database query.", e);
504 // TODO: somehow notify user of problem
506 // cleanup everything that has been used
514 } catch (Exception e) {
515 Log.error(documentTitle, tableName,
516 "Error closing database resources. Subsequent database queries may not work.", e);
522 private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
523 LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
525 // get the data we've been asked for
527 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
528 while (rs.next() && rowCount <= length) {
529 int layoutFieldsSize = safeLongToInt(layoutFields.size());
530 GlomField[] rowArray = new GlomField[layoutFieldsSize];
531 for (int i = 0; i < layoutFieldsSize; i++) {
532 // make a new GlomField to set the text and colours
533 rowArray[i] = new GlomField();
535 // get foreground and background colours
536 LayoutItem_Field field = layoutFields.get(i);
537 FieldFormatting formatting = field.get_formatting_used();
538 String fgcolour = formatting.get_text_format_color_foreground();
539 if (!fgcolour.isEmpty())
540 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
541 String bgcolour = formatting.get_text_format_color_background();
542 if (!bgcolour.isEmpty())
543 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
545 // Convert the field value to a string based on the glom type. We're doing the formatting on the
546 // server side for now but it might be useful to move this to the client side.
547 switch (field.get_glom_type()) {
549 String text = rs.getString(i + 1);
550 rowArray[i].setText(text != null ? text : "");
553 rowArray[i].setBoolean(rs.getBoolean(i + 1));
556 // Take care of the numeric formatting before converting the number to a string.
557 NumericFormat numFormatGlom = formatting.getM_numeric_format();
558 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
559 // number should be formatted as a currency if the currency code string is not empty.
560 String currencyCode = numFormatGlom.getM_currency_symbol();
561 NumberFormat numFormatJava = null;
562 boolean useGlomCurrencyCode = false;
563 if (currencyCode.length() == 3) {
564 // Try to format the currency using the Java Locales system.
566 Currency currency = Currency.getInstance(currencyCode);
567 Log.info(documentTitle, tableName, "A valid ISO 4217 currency code is being used."
568 + " Overriding the numeric formatting with information from the locale.");
569 int digits = currency.getDefaultFractionDigits();
570 numFormatJava = NumberFormat.getCurrencyInstance(locale);
571 numFormatJava.setCurrency(currency);
572 numFormatJava.setMinimumFractionDigits(digits);
573 numFormatJava.setMaximumFractionDigits(digits);
574 } catch (IllegalArgumentException e) {
575 Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
576 + " Manually setting currency code with this value.");
577 // The currency code is not this is not an ISO 4217 currency code.
578 // We're going to manually set the currency code and use the glom numeric formatting.
579 useGlomCurrencyCode = true;
580 numFormatJava = getJavaNumberFormat(numFormatGlom);
582 } else if (currencyCode.length() > 0) {
583 Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
584 + " Manually setting currency code with this value.");
585 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
586 // We're going to manually set the currency code and use the glom numeric formatting.
587 useGlomCurrencyCode = true;
588 numFormatJava = getJavaNumberFormat(numFormatGlom);
590 // The length of the currency code is 0; the number is not a currency.
591 numFormatJava = getJavaNumberFormat(numFormatGlom);
594 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
596 double number = rs.getDouble(i + 1);
598 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
599 // overrides the set foreground colour
600 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
601 .get_alternative_color_for_negatives()));
604 // Finally convert the number to text using the glom currency string if required.
605 if (useGlomCurrencyCode) {
606 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
608 rowArray[i].setText(numFormatJava.format(number));
612 Date date = rs.getDate(i + 1);
614 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
615 rowArray[i].setText(dateFormat.format(date));
617 rowArray[i].setText("");
621 Time time = rs.getTime(i + 1);
623 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
624 rowArray[i].setText(timeFormat.format(time));
626 rowArray[i].setText("");
630 byte[] image = rs.getBytes(i + 1);
632 // TODO implement field TYPE_IMAGE
633 rowArray[i].setText("Image (FIXME)");
635 rowArray[i].setText("");
640 Log.warn(documentTitle, tableName, "Invalid LayoutItem Field type. Using empty string for value.");
641 rowArray[i].setText("");
646 // add the row of GlomFields to the ArrayList we're going to return and update the row count
647 rowsList.add(rowArray);
654 public ArrayList<String> getDocumentTitles() {
655 ArrayList<String> documentTitles = new ArrayList<String>();
656 for (String title : documents.keySet()) {
657 documentTitles.add(title);
659 return documentTitles;
663 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
665 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
667 private int safeLongToInt(long value) {
668 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
669 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
674 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
675 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
676 if (numFormatGlom.getM_decimal_places_restricted()) {
677 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
678 numFormatJava.setMinimumFractionDigits(digits);
679 numFormatJava.setMaximumFractionDigits(digits);
681 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
682 return numFormatJava;
686 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
687 * significant 8-bits in each channel.
689 private String convertGdkColorToHtmlColour(String gdkColor) {
690 if (gdkColor.length() == 13)
691 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
692 else if (gdkColor.length() == 7) {
693 // This shouldn't happen but let's deal with it if it does.
694 Log.warn("Expected a 13 character string but received a 7 character string. Returning received string.");
697 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
703 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
704 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
705 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
706 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
708 private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
709 FieldFormatting.HorizontalAlignment alignment) {
711 case HORIZONTAL_ALIGNMENT_AUTO:
712 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
713 case HORIZONTAL_ALIGNMENT_LEFT:
714 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
715 case HORIZONTAL_ALIGNMENT_RIGHT:
716 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
718 Log.error("Recieved an alignment that I don't know about: "
719 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
720 + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
721 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
726 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
727 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
728 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
731 private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
734 return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
736 return ColumnInfo.GlomFieldType.TYPE_DATE;
738 return ColumnInfo.GlomFieldType.TYPE_IMAGE;
740 return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
742 return ColumnInfo.GlomFieldType.TYPE_TEXT;
744 return ColumnInfo.GlomFieldType.TYPE_TIME;
746 Log.info("Returning TYPE_INVALID.");
747 return ColumnInfo.GlomFieldType.TYPE_INVALID;
749 Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
750 + type.toString() + ". Returning " + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
751 return ColumnInfo.GlomFieldType.TYPE_INVALID;
758 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
760 public boolean isAuthenticated(String documentTitle) {
761 return documents.get(documentTitle).isAuthenticated();
767 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
770 public boolean checkAuthentication(String documentTitle, String username, String password) {
771 ConfiguredDocument configuredDoc = documents.get(documentTitle);
772 boolean authenticated;
774 authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
775 } catch (SQLException e) {
776 Log.error(documentTitle, "Unknown SQL Error checking the database authentication.", e);
779 configuredDoc.setAuthenticated(authenticated);
780 return authenticated;
786 * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
789 public ArrayList<LayoutGroup> getDefaultDetailsLayout(String documentTitle) {
790 GlomDocument glomDocument = getGlomDocument(documentTitle);
791 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
792 return getDetailsLayout(documentTitle, tableName);
798 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
800 public ArrayList<LayoutGroup> getDetailsLayout(String documentTitle, String tableName) {
801 // FIXME not checking if authenticated
802 ConfiguredDocument configuredDoc = documents.get(documentTitle);
803 Document document = configuredDoc.getDocument();
804 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
806 ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
807 for (int i = 0; i < layoutGroupVec.size(); i++) {
808 org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
810 if (libglomLayoutGroup == null)
813 layoutGroups.add(getLayoutGroup(documentTitle, tableName, libglomLayoutGroup));
819 * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
821 * @param libglomLayoutGroup
822 * <dt><b>Precondition:</b>
824 * libglomLayoutGroup must not be null
827 private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
828 org.glom.libglom.LayoutGroup libglomLayoutGroup) {
829 LayoutGroup layoutGroup = new LayoutGroup();
830 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
832 // look at each child item
833 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
834 for (int i = 0; i < layoutItemsVec.size(); i++) {
835 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
837 // just a safety check
838 if (libglomLayoutItem == null)
841 org.glom.web.shared.layout.LayoutItem layoutItem = null;
842 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
844 // recurse into child groups
845 layoutItem = getLayoutGroup(documentTitle, tableName, group);
847 // create GWT-Glom LayoutItem types based on the the libglom type
848 LayoutItem_Field tempField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
849 if (tempField != null) {
850 layoutItem = new LayoutItemField();
852 Log.info(documentTitle, tableName,
853 "Ignoring unknown LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
858 layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
859 layoutGroup.addItem(layoutItem);
865 public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
867 ConfiguredDocument configuredDoc = documents.get(documentTitle);
868 Document document = configuredDoc.getDocument();
870 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName);
872 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
873 Log.warn(documentTitle, tableName, "Didn't find any fields to show. Returning null.");
877 // get primary key for the table to use in the SQL query
878 Field primaryKey = null;
879 FieldVector fieldsVec = document.get_table_fields(tableName);
880 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
881 Field field = fieldsVec.get(i);
882 if (field.get_primary_key()) {
888 // send back an empty GlomField array if can't find a primaryKey Field
889 if (primaryKey == null) {
890 Log.error(documentTitle, tableName, "Couldn't find primary key in table. Returning null.");
894 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
895 Connection conn = null;
899 // Setup the JDBC driver and get the query.
900 ComboPooledDataSource cpds = configuredDoc.getCpds();
901 conn = cpds.getConnection();
902 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
903 String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
904 rs = st.executeQuery(query);
906 // get the results from the ResultSet
907 // using 2 as a length parameter so we can log a warning if the result set is greater than one
908 rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
909 } catch (SQLException e) {
910 Log.error(documentTitle, tableName, "Error executing database query.", e);
911 // TODO: somehow notify user of problem
914 // cleanup everything that has been used
922 } catch (Exception e) {
923 Log.error(documentTitle, tableName,
924 "Error closing database resources. Subsequent database queries may not work.", e);
928 if (rowsList.size() == 0) {
929 Log.error(documentTitle, tableName, "The query returned an empty ResultSet. Returning null.");
931 } else if (rowsList.size() > 1) {
932 Log.warn(documentTitle, tableName,
933 "The query did not return a unique result. Returning the first result in the set.");
936 return rowsList.get(0);
941 * Gets a LayoutFieldVector to use when generating an SQL query.
943 private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName) {
944 // TODO make general to be able to use "list" here - will need to change called methods
945 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
947 // We will show the fields that the document says we should:
948 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
949 for (int i = 0; i < layoutGroupVec.size(); i++) {
950 org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
953 ArrayList<LayoutItem_Field> layoutItemsFields = getFieldsToShowForSQLQueryAddGroup(document, tableName,
955 for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
956 layoutFieldVector.add(layoutItem_Field);
959 return layoutFieldVector;
962 private ArrayList<LayoutItem_Field> getFieldsToShowForSQLQueryAddGroup(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(document.get_database_title(), tableName,
994 "LayoutItem_Field " + layoutItemField.get_layout_display_name()
995 + " 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.
1011 .addAll(getFieldsToShowForSQLQueryAddGroup(document, tableName, subLayoutGroup));
1016 return layoutItemFields;