2 * Copyright (C) 2010, 2011 Openismus GmbH
4 * This file is part of GWT-Glom.
6 * GWT-Glom is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
11 * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with GWT-Glom. If not, see <http://www.gnu.org/licenses/>.
20 package org.glom.web.server;
23 import java.io.FilenameFilter;
24 import java.io.IOException;
25 import java.io.InputStream;
26 import java.sql.Connection;
28 import java.sql.ResultSet;
29 import java.sql.SQLException;
30 import java.sql.Statement;
32 import java.text.DateFormat;
33 import java.text.NumberFormat;
34 import java.util.ArrayList;
35 import java.util.Currency;
36 import java.util.Hashtable;
37 import java.util.Locale;
38 import java.util.Properties;
40 import org.glom.libglom.BakeryDocument.LoadFailureCodes;
41 import org.glom.libglom.Document;
42 import org.glom.libglom.Field;
43 import org.glom.libglom.FieldFormatting;
44 import org.glom.libglom.FieldVector;
45 import org.glom.libglom.Glom;
46 import org.glom.libglom.LayoutFieldVector;
47 import org.glom.libglom.LayoutGroupVector;
48 import org.glom.libglom.LayoutItem;
49 import org.glom.libglom.LayoutItemVector;
50 import org.glom.libglom.LayoutItem_Field;
51 import org.glom.libglom.LayoutItem_Portal;
52 import org.glom.libglom.NumericFormat;
53 import org.glom.libglom.SortClause;
54 import org.glom.libglom.SortFieldPair;
55 import org.glom.libglom.StringVector;
56 import org.glom.web.client.OnlineGlomService;
57 import org.glom.web.shared.GlomDocument;
58 import org.glom.web.shared.GlomField;
59 import org.glom.web.shared.layout.Formatting;
60 import org.glom.web.shared.layout.LayoutGroup;
61 import org.glom.web.shared.layout.LayoutItemField;
63 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
64 import com.mchange.v2.c3p0.ComboPooledDataSource;
65 import com.mchange.v2.c3p0.DataSources;
67 @SuppressWarnings("serial")
68 public class OnlineGlomServiceImpl extends RemoteServiceServlet implements OnlineGlomService {
70 // convenience class to for dealing with the Online Glom configuration file
71 private class OnlineGlomProperties extends Properties {
72 public String getKey(String value) {
73 for (String key : stringPropertyNames()) {
74 if (getProperty(key).trim().equals(value))
81 private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
82 // TODO implement locale
83 private final Locale locale = Locale.ROOT;
86 * This is called when the servlet is started or restarted.
88 public OnlineGlomServiceImpl() throws Exception {
90 // Find the configuration file. See this thread for background info:
91 // http://stackoverflow.com/questions/2161054/where-to-place-properties-files-in-a-jsp-servlet-web-application
92 OnlineGlomProperties config = new OnlineGlomProperties();
93 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("onlineglom.properties");
95 Log.fatal("onlineglom.properties not found.");
96 throw new IOException();
100 // check the configured glom file directory
101 String documentDirName = config.getProperty("glom.document.directory");
102 File documentDir = new File(documentDirName);
103 if (!documentDir.isDirectory()) {
104 Log.fatal(documentDirName + " is not a directory.");
105 throw new IOException();
107 if (!documentDir.canRead()) {
108 Log.fatal("Can't read the files in : " + documentDirName);
109 throw new IOException();
112 // get and check the glom files in the specified directory
113 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
115 public boolean accept(File dir, String name) {
116 return name.endsWith(".glom");
120 for (File glomFile : glomFiles) {
121 Document document = new Document();
122 document.set_file_uri("file://" + glomFile.getAbsolutePath());
124 boolean retval = document.load(error);
125 if (retval == false) {
127 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
128 message = "Could not find file: " + glomFile.getAbsolutePath();
130 message = "An unknown error occurred when trying to load file: " + glomFile.getAbsolutePath();
133 // continue with for loop because there may be other documents in the directory
137 ConfiguredDocument configuredDocument = new ConfiguredDocument(document);
138 // check if a username and password have been set and work for the current document
139 String documentTitle = document.get_database_title().trim();
140 String key = config.getKey(documentTitle);
142 String[] keyArray = key.split("\\.");
143 if (keyArray.length == 3 && "title".equals(keyArray[2])) {
144 // username/password could be set, let's check to see if it works
145 String usernameKey = key.replaceAll(keyArray[2], "username");
146 String passwordKey = key.replaceAll(keyArray[2], "password");
147 configuredDocument.setUsernameAndPassword(config.getProperty(usernameKey),
148 config.getProperty(passwordKey));
152 // check the if the global username and password have been set and work with this document
153 if (!configuredDocument.isAuthenticated()) {
154 configuredDocument.setUsernameAndPassword(config.getProperty("glom.document.username"),
155 config.getProperty("glom.document.password"));
158 // add information to the hash table
159 documents.put(documentTitle, configuredDocument);
165 * This is called when the servlet is stopped or restarted.
167 * @see javax.servlet.GenericServlet#destroy()
170 public void destroy() {
171 Glom.libglom_deinit();
173 for (String documenTitle : documents.keySet()) {
174 ConfiguredDocument configuredDoc = documents.get(documenTitle);
176 DataSources.destroy(configuredDoc.getCpds());
177 } catch (SQLException e) {
178 Log.error(documenTitle, "Error cleaning up the ComboPooledDataSource.", e);
184 public GlomDocument getGlomDocument(String documentTitle) {
186 Document document = documents.get(documentTitle).getDocument();
187 GlomDocument glomDocument = new GlomDocument();
189 // get arrays of table names and titles, and find the default table index
190 StringVector tablesVec = document.get_table_names();
192 int numTables = safeLongToInt(tablesVec.size());
193 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
195 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
196 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
197 boolean foundDefaultTable = false;
198 int visibleIndex = 0;
199 for (int i = 0; i < numTables; i++) {
200 String tableName = tablesVec.get(i);
201 if (!document.get_table_is_hidden(tableName)) {
202 tableNames.add(tableName);
203 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
204 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
205 glomDocument.setDefaultTableIndex(visibleIndex);
206 foundDefaultTable = true;
208 tableTitles.add(document.get_table_title(tableName));
213 // set everything we need
214 glomDocument.setTableNames(tableNames);
215 glomDocument.setTableTitles(tableTitles);
223 * @see org.glom.web.client.OnlineGlomService#getDefaultLayoutListTable(java.lang.String)
226 public LayoutGroup getDefaultListLayout(String documentTitle) {
227 GlomDocument glomDocument = getGlomDocument(documentTitle);
228 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
229 LayoutGroup layoutGroup = getListLayout(documentTitle, tableName);
230 layoutGroup.setDefaultTableName(tableName);
234 public LayoutGroup getListLayout(String documentTitle, String tableName) {
235 ConfiguredDocument configuredDoc = documents.get(documentTitle);
236 Document document = configuredDoc.getDocument();
238 // access the layout list
239 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("list", tableName);
240 int listViewLayoutGroupSize = safeLongToInt(layoutGroupVec.size());
241 org.glom.libglom.LayoutGroup libglomLayoutGroup = null;
242 if (listViewLayoutGroupSize > 0) {
243 // a list layout group is defined; we can use the first group as the list
244 if (listViewLayoutGroupSize > 1)
245 Log.warn(documentTitle, tableName,
246 "The size of the list layout group is greater than 1. Attempting to use the first item for the layout list view.");
248 libglomLayoutGroup = layoutGroupVec.get(0);
250 // a list layout group is *not* defined; we are going make a libglom layout group from the list of fields
251 Log.info(documentTitle, tableName,
252 "A list layout is not defined for this table. Displaying a list layout based on the field list.");
254 FieldVector fieldsVec = document.get_table_fields(tableName);
255 libglomLayoutGroup = new org.glom.libglom.LayoutGroup();
256 for (int i = 0; i < fieldsVec.size(); i++) {
257 Field field = fieldsVec.get(i);
258 LayoutItem_Field layoutItemField = new LayoutItem_Field();
259 layoutItemField.set_full_field_details(field);
260 libglomLayoutGroup.add_item(layoutItemField);
264 // confirm the libglom LayoutGroup is not null as per the method's precondition
265 if (libglomLayoutGroup == null) {
266 Log.error(documentTitle, tableName, "A LayoutGroup was not found. Returning null.");
270 LayoutGroup layoutGroup = getLayoutGroup(documentTitle, tableName, libglomLayoutGroup);
272 // use the same fields list as will be used for the query
273 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName, "list");
274 layoutGroup.setExpectedResultSize(getResultSizeOfSQLQuery(documentTitle, tableName, fieldsToGet));
279 * Get the number of rows a query with the table name and layout fields would return. This is needed for the /* list
282 private int getResultSizeOfSQLQuery(String documentTitle, String tableName, LayoutFieldVector fieldsToGet) {
283 ConfiguredDocument configuredDoc = documents.get(documentTitle);
284 if (!configuredDoc.isAuthenticated())
286 Connection conn = null;
290 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
291 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
292 // data. Here's the relevant PostgreSQL documentation:
293 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
294 ComboPooledDataSource cpds = configuredDoc.getCpds();
295 conn = cpds.getConnection();
296 conn.setAutoCommit(false);
297 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
298 String query = Glom.build_sql_select_count_simple(tableName, fieldsToGet);
299 // TODO Test execution time of this query with when the number of rows in the table is large (say >
300 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
301 rs = st.executeQuery(query);
303 // get the number of rows in the query
307 } catch (SQLException e) {
308 Log.error(documentTitle, tableName,
309 "Error calculating number of rows in the query. Setting number of rows to 0.", e);
312 // cleanup everything that has been used
320 } catch (Exception e) {
321 Log.error(documentTitle, tableName,
322 "Error closing database resources. Subsequent database queries may not work.", e);
327 // FIXME Rename to getListData(), getSortedListDat() for consistency in method naming.
328 // FIXME Check if we can use getFieldsToShowForSQLQuery() in these methods
329 public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
330 return getTableData(documentTitle, tableName, start, length, false, 0, false);
333 public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
334 int sortColumnIndex, boolean isAscending) {
335 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
338 private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
339 boolean useSortClause, int sortColumnIndex, boolean isAscending) {
341 ConfiguredDocument configuredDoc = documents.get(documentTitle);
342 if (!configuredDoc.isAuthenticated())
343 return new ArrayList<GlomField[]>();
344 Document document = configuredDoc.getDocument();
346 // access the layout list using the defined layout list or the table fields if there's no layout list
347 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
348 LayoutFieldVector layoutFields = new LayoutFieldVector();
349 SortClause sortClause = new SortClause();
350 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
351 if (layoutListVec.size() > 0) {
352 // a layout list is defined, we can use it to for the LayoutListTable
353 if (listViewLayoutGroupSize > 1)
354 Log.warn(documentTitle, tableName,
355 "The size of the list view layout group for table is greater than 1. "
356 + "Attempting to use the first item for the layout list view.");
357 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
359 // find the defined layout list fields
360 int numItems = safeLongToInt(layoutItemsVec.size());
361 for (int i = 0; i < numItems; i++) {
362 // TODO add support for other LayoutItems (Text, Image, Button)
363 LayoutItem item = layoutItemsVec.get(i);
364 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
365 if (layoutItemfield != null) {
366 // use this field in the layout
367 layoutFields.add(layoutItemfield);
369 // create a sort clause if it's a primary key and we're not asked to sort a specific column
370 if (!useSortClause) {
371 Field details = layoutItemfield.get_full_field_details();
372 if (details != null && details.get_primary_key()) {
373 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
379 // no layout list is defined, use the table fields as the layout list
380 FieldVector fieldsVec = document.get_table_fields(tableName);
382 // find the fields to display in the layout list
383 int numItems = safeLongToInt(fieldsVec.size());
384 for (int i = 0; i < numItems; i++) {
385 Field field = fieldsVec.get(i);
386 LayoutItem_Field layoutItemField = new LayoutItem_Field();
387 layoutItemField.set_full_field_details(field);
388 layoutFields.add(layoutItemField);
390 // create a sort clause if it's a primary key and we're not asked to sort a specific column
391 if (!useSortClause) {
392 if (field.get_primary_key()) {
393 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
399 // create a sort clause for the column we've been asked to sort
401 LayoutItem item = layoutFields.get(sortColumnIndex);
402 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
404 sortClause.addLast(new SortFieldPair(field, isAscending));
406 Log.error(documentTitle, tableName, "Error getting LayoutItem_Field for column index "
407 + sortColumnIndex + ". Cannot create a sort clause for this column.");
412 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
413 Connection conn = null;
417 // Setup the JDBC driver and get the query. Special care needs to be take to ensure that the results will be
418 // based on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount
419 // of data. Here's the relevant PostgreSQL documentation:
420 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
421 ComboPooledDataSource cpds = configuredDoc.getCpds();
422 conn = cpds.getConnection();
423 conn.setAutoCommit(false);
424 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
425 st.setFetchSize(length);
426 String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
427 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
428 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
429 // memory footprint. Check the difference between this value before and after the query:
430 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
431 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
432 rs = st.executeQuery(query);
434 // get the results from the ResultSet
435 rowsList = getData(documentTitle, tableName, length, layoutFields, rs);
436 } catch (SQLException e) {
437 Log.error(documentTitle, tableName, "Error executing database query.", e);
438 // TODO: somehow notify user of problem
440 // cleanup everything that has been used
448 } catch (Exception e) {
449 Log.error(documentTitle, tableName,
450 "Error closing database resources. Subsequent database queries may not work.", e);
456 private ArrayList<GlomField[]> getData(String documentTitle, String tableName, int length,
457 LayoutFieldVector layoutFields, ResultSet rs) throws SQLException {
459 // get the data we've been asked for
461 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
462 while (rs.next() && rowCount <= length) {
463 int layoutFieldsSize = safeLongToInt(layoutFields.size());
464 GlomField[] rowArray = new GlomField[layoutFieldsSize];
465 for (int i = 0; i < layoutFieldsSize; i++) {
466 // make a new GlomField to set the text and colours
467 rowArray[i] = new GlomField();
469 // get foreground and background colours
470 LayoutItem_Field field = layoutFields.get(i);
471 FieldFormatting formatting = field.get_formatting_used();
472 String fgcolour = formatting.get_text_format_color_foreground();
473 if (!fgcolour.isEmpty())
474 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
475 String bgcolour = formatting.get_text_format_color_background();
476 if (!bgcolour.isEmpty())
477 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
479 // Convert the field value to a string based on the glom type. We're doing the formatting on the
480 // server side for now but it might be useful to move this to the client side.
481 switch (field.get_glom_type()) {
483 String text = rs.getString(i + 1);
484 rowArray[i].setText(text != null ? text : "");
487 rowArray[i].setBoolean(rs.getBoolean(i + 1));
490 // Take care of the numeric formatting before converting the number to a string.
491 NumericFormat numFormatGlom = formatting.getM_numeric_format();
492 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
493 // number should be formatted as a currency if the currency code string is not empty.
494 String currencyCode = numFormatGlom.getM_currency_symbol();
495 NumberFormat numFormatJava = null;
496 boolean useGlomCurrencyCode = false;
497 if (currencyCode.length() == 3) {
498 // Try to format the currency using the Java Locales system.
500 Currency currency = Currency.getInstance(currencyCode);
501 Log.info(documentTitle, tableName, "A valid ISO 4217 currency code is being used."
502 + " Overriding the numeric formatting with information from the locale.");
503 int digits = currency.getDefaultFractionDigits();
504 numFormatJava = NumberFormat.getCurrencyInstance(locale);
505 numFormatJava.setCurrency(currency);
506 numFormatJava.setMinimumFractionDigits(digits);
507 numFormatJava.setMaximumFractionDigits(digits);
508 } catch (IllegalArgumentException e) {
509 Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
510 + " Manually setting currency code with this value.");
511 // The currency code is not this is not an ISO 4217 currency code.
512 // We're going to manually set the currency code and use the glom numeric formatting.
513 useGlomCurrencyCode = true;
514 numFormatJava = getJavaNumberFormat(numFormatGlom);
516 } else if (currencyCode.length() > 0) {
517 Log.warn(documentTitle, tableName, currencyCode + " is not a valid ISO 4217 code."
518 + " Manually setting currency code with this value.");
519 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
520 // We're going to manually set the currency code and use the glom numeric formatting.
521 useGlomCurrencyCode = true;
522 numFormatJava = getJavaNumberFormat(numFormatGlom);
524 // The length of the currency code is 0; the number is not a currency.
525 numFormatJava = getJavaNumberFormat(numFormatGlom);
528 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
530 double number = rs.getDouble(i + 1);
532 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
533 // overrides the set foreground colour
534 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
535 .get_alternative_color_for_negatives()));
538 // Finally convert the number to text using the glom currency string if required.
539 if (useGlomCurrencyCode) {
540 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
542 rowArray[i].setText(numFormatJava.format(number));
546 Date date = rs.getDate(i + 1);
548 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
549 rowArray[i].setText(dateFormat.format(date));
551 rowArray[i].setText("");
555 Time time = rs.getTime(i + 1);
557 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
558 rowArray[i].setText(timeFormat.format(time));
560 rowArray[i].setText("");
564 byte[] image = rs.getBytes(i + 1);
566 // TODO implement field TYPE_IMAGE
567 rowArray[i].setText("Image (FIXME)");
569 rowArray[i].setText("");
574 Log.warn(documentTitle, tableName, "Invalid LayoutItem Field type. Using empty string for value.");
575 rowArray[i].setText("");
580 // add the row of GlomFields to the ArrayList we're going to return and update the row count
581 rowsList.add(rowArray);
588 public ArrayList<String> getDocumentTitles() {
589 ArrayList<String> documentTitles = new ArrayList<String>();
590 for (String title : documents.keySet()) {
591 documentTitles.add(title);
593 return documentTitles;
597 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
599 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
601 private int safeLongToInt(long value) {
602 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
603 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
608 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
609 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
610 if (numFormatGlom.getM_decimal_places_restricted()) {
611 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
612 numFormatJava.setMinimumFractionDigits(digits);
613 numFormatJava.setMaximumFractionDigits(digits);
615 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
616 return numFormatJava;
620 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
621 * significant 8-bits in each channel.
623 private String convertGdkColorToHtmlColour(String gdkColor) {
624 if (gdkColor.length() == 13)
625 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
626 else if (gdkColor.length() == 7) {
627 // This shouldn't happen but let's deal with it if it does.
628 Log.warn("Expected a 13 character string but received a 7 character string. Returning received string.");
631 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
637 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
638 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
639 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
640 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
642 private Formatting.HorizontalAlignment convertToGWTGlomHorizonalAlignment(
643 FieldFormatting.HorizontalAlignment alignment) {
645 case HORIZONTAL_ALIGNMENT_AUTO:
646 return Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
647 case HORIZONTAL_ALIGNMENT_LEFT:
648 return Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
649 case HORIZONTAL_ALIGNMENT_RIGHT:
650 return Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
652 Log.error("Recieved an alignment that I don't know about: "
653 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
654 + Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
655 return Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
660 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
661 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
662 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
665 private LayoutItemField.GlomFieldType convertToGWTGlomFieldType(Field.glom_field_type type) {
668 return LayoutItemField.GlomFieldType.TYPE_BOOLEAN;
670 return LayoutItemField.GlomFieldType.TYPE_DATE;
672 return LayoutItemField.GlomFieldType.TYPE_IMAGE;
674 return LayoutItemField.GlomFieldType.TYPE_NUMERIC;
676 return LayoutItemField.GlomFieldType.TYPE_TEXT;
678 return LayoutItemField.GlomFieldType.TYPE_TIME;
680 Log.info("Returning TYPE_INVALID.");
681 return LayoutItemField.GlomFieldType.TYPE_INVALID;
683 Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
684 + type.toString() + ". Returning " + LayoutItemField.GlomFieldType.TYPE_INVALID.toString() + ".");
685 return LayoutItemField.GlomFieldType.TYPE_INVALID;
692 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
694 public boolean isAuthenticated(String documentTitle) {
695 return documents.get(documentTitle).isAuthenticated();
701 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
704 public boolean checkAuthentication(String documentTitle, String username, String password) {
705 ConfiguredDocument configuredDoc = documents.get(documentTitle);
707 return configuredDoc.setUsernameAndPassword(username, password);
708 } catch (SQLException e) {
709 Log.error(documentTitle, "Unknown SQL Error checking the database authentication.", e);
717 * @see org.glom.web.client.OnlineGlomService#getDefaultDetailsLayoutGroup(java.lang.String)
720 public ArrayList<LayoutGroup> getDefaultDetailsLayout(String documentTitle) {
721 GlomDocument glomDocument = getGlomDocument(documentTitle);
722 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
723 return getDetailsLayout(documentTitle, tableName);
729 * @see org.glom.web.client.OnlineGlomService#getDetailsLayoutGroup(java.lang.String, java.lang.String)
731 public ArrayList<LayoutGroup> getDetailsLayout(String documentTitle, String tableName) {
732 ConfiguredDocument configuredDoc = documents.get(documentTitle);
733 Document document = configuredDoc.getDocument();
734 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
736 ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
737 for (int i = 0; i < layoutGroupVec.size(); i++) {
738 org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
740 if (libglomLayoutGroup == null)
743 layoutGroups.add(getLayoutGroup(documentTitle, tableName, libglomLayoutGroup));
749 * Gets a GWT-Glom LayoutGroup object for the specified libglom LayoutGroup object.
751 * @param libglomLayoutGroup
752 * <dt><b>Precondition:</b>
754 * libglomLayoutGroup must not be null
757 private LayoutGroup getLayoutGroup(String documentTitle, String tableName,
758 org.glom.libglom.LayoutGroup libglomLayoutGroup) {
759 LayoutGroup layoutGroup = new LayoutGroup();
760 layoutGroup.setColumnCount(safeLongToInt(libglomLayoutGroup.get_columns_count()));
762 // look at each child item
763 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
764 for (int i = 0; i < layoutItemsVec.size(); i++) {
765 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
767 // just a safety check
768 if (libglomLayoutItem == null)
771 org.glom.web.shared.layout.LayoutItem layoutItem = null;
772 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
774 // recurse into child groups
775 layoutItem = getLayoutGroup(documentTitle, tableName, group);
777 // create GWT-Glom LayoutItem types based on the the libglom type
778 // TODO add support for other LayoutItems (Text, Image, Button etc.)
779 LayoutItem_Field libglomLayoutField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
780 if (libglomLayoutField != null) {
781 LayoutItemField layoutItemField = new LayoutItemField();
782 layoutItemField.setType(convertToGWTGlomFieldType(libglomLayoutField.get_glom_type()));
783 Formatting formatting = new Formatting();
784 formatting.setHorizontalAlignment(convertToGWTGlomHorizonalAlignment(libglomLayoutField
785 .get_formatting_used_horizontal_alignment()));
786 layoutItemField.setFormatting(formatting);
787 layoutItem = layoutItemField;
789 Log.info(documentTitle, tableName,
790 "Ignoring unknown LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
796 layoutItem.setTitle(libglomLayoutItem.get_title_or_name());
797 layoutGroup.addItem(layoutItem);
803 public GlomField[] getDetailsData(String documentTitle, String tableName, String primaryKeyValue) {
805 ConfiguredDocument configuredDoc = documents.get(documentTitle);
806 Document document = configuredDoc.getDocument();
808 LayoutFieldVector fieldsToGet = getFieldsToShowForSQLQuery(document, tableName, "details");
810 if (fieldsToGet == null || fieldsToGet.size() <= 0) {
811 Log.warn(documentTitle, tableName, "Didn't find any fields to show. Returning null.");
815 // get primary key for the table to use in the SQL query
816 Field primaryKey = null;
817 FieldVector fieldsVec = document.get_table_fields(tableName);
818 for (int i = 0; i < safeLongToInt(fieldsVec.size()); i++) {
819 Field field = fieldsVec.get(i);
820 if (field.get_primary_key()) {
826 // send back an empty GlomField array if can't find a primaryKey Field
827 if (primaryKey == null) {
828 Log.error(documentTitle, tableName, "Couldn't find primary key in table. Returning null.");
832 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
833 Connection conn = null;
837 // Setup the JDBC driver and get the query.
838 ComboPooledDataSource cpds = configuredDoc.getCpds();
839 conn = cpds.getConnection();
840 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
841 String query = Glom.build_sql_select_with_key(tableName, fieldsToGet, primaryKey, primaryKeyValue);
842 rs = st.executeQuery(query);
844 // get the results from the ResultSet
845 // using 2 as a length parameter so we can log a warning if the result set is greater than one
846 rowsList = getData(documentTitle, tableName, 2, fieldsToGet, rs);
847 } catch (SQLException e) {
848 Log.error(documentTitle, tableName, "Error executing database query.", e);
849 // TODO: somehow notify user of problem
852 // cleanup everything that has been used
860 } catch (Exception e) {
861 Log.error(documentTitle, tableName,
862 "Error closing database resources. Subsequent database queries may not work.", e);
866 if (rowsList.size() == 0) {
867 Log.error(documentTitle, tableName, "The query returned an empty ResultSet. Returning null.");
869 } else if (rowsList.size() > 1) {
870 Log.warn(documentTitle, tableName,
871 "The query did not return a unique result. Returning the first result in the set.");
874 return rowsList.get(0);
879 * Gets a LayoutFieldVector to use when generating an SQL query.
881 private LayoutFieldVector getFieldsToShowForSQLQuery(Document document, String tableName, String layoutName) {
882 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups(layoutName, tableName);
883 LayoutFieldVector layoutFieldVector = new LayoutFieldVector();
885 // special case for list layouts that don't have a defined layout group
886 if ("list".equals(layoutName) && layoutGroupVec.size() == 0) {
887 FieldVector fieldsVec = document.get_table_fields(tableName);
888 for (int i = 0; i < fieldsVec.size(); i++) {
889 Field field = fieldsVec.get(i);
890 LayoutItem_Field layoutItemField = new LayoutItem_Field();
891 layoutItemField.set_full_field_details(field);
892 layoutFieldVector.add(layoutItemField);
894 return layoutFieldVector;
897 // We will show the fields that the document says we should:
898 for (int i = 0; i < layoutGroupVec.size(); i++) {
899 org.glom.libglom.LayoutGroup layoutGroup = layoutGroupVec.get(i);
902 ArrayList<LayoutItem_Field> layoutItemsFields = getFieldsToShowForSQLQueryAddGroup(document, tableName,
904 for (LayoutItem_Field layoutItem_Field : layoutItemsFields) {
905 layoutFieldVector.add(layoutItem_Field);
908 return layoutFieldVector;
911 private ArrayList<LayoutItem_Field> getFieldsToShowForSQLQueryAddGroup(Document document, String tableName,
912 org.glom.libglom.LayoutGroup layoutGroup) {
914 ArrayList<LayoutItem_Field> layoutItemFields = new ArrayList<LayoutItem_Field>();
915 LayoutItemVector items = layoutGroup.get_items();
916 for (int i = 0; i < items.size(); i++) {
917 LayoutItem layoutItem = items.get(i);
919 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(layoutItem);
920 if (layoutItemField != null) {
921 // the layoutItem is a LayoutItem_Field
923 if (layoutItemField.get_has_relationship_name()) {
924 // layoutItemField is a field in a related table
925 fields = document.get_table_fields(layoutItemField.get_table_used(tableName));
927 // layoutItemField is a field in this table
928 fields = document.get_table_fields(tableName);
931 // set the layoutItemFeild with details from its Field in the document and
932 // add it to the list to be returned
933 for (int j = 0; j < fields.size(); j++) {
934 // check the names to see if they're the same
935 // this works because we're using the field list from the related table if necessary
936 if (layoutItemField.get_name().equals(fields.get(j).get_name())) {
937 Field field = fields.get(j);
939 layoutItemField.set_full_field_details(field);
940 layoutItemFields.add(layoutItemField);
942 Log.warn(document.get_database_title(), tableName,
943 "LayoutItem_Field " + layoutItemField.get_layout_display_name()
944 + " not found in document field list.");
951 // the layoutItem is not a LayoutItem_Field
952 org.glom.libglom.LayoutGroup subLayoutGroup = org.glom.libglom.LayoutGroup.cast_dynamic(layoutItem);
953 if (subLayoutGroup != null) {
954 // the layoutItem is a LayoutGroup
955 LayoutItem_Portal layoutItemPortal = LayoutItem_Portal.cast_dynamic(layoutItem);
956 if (layoutItemPortal == null) {
957 // The subGroup is not a LayoutItem_Portal.
958 // We're ignoring portals because they are filled by means of a separate SQL query.
960 .addAll(getFieldsToShowForSQLQueryAddGroup(document, tableName, subLayoutGroup));
965 return layoutItemFields;