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.FileInputStream;
25 import java.io.FilenameFilter;
26 import java.io.IOException;
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.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.ColumnInfo;
58 import org.glom.web.shared.GlomDocument;
59 import org.glom.web.shared.GlomField;
60 import org.glom.web.shared.LayoutListTable;
62 import com.allen_sauer.gwt.log.client.Log;
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 // class to hold configuration information for related to the glom document and db access
71 private class ConfiguredDocument {
72 private Document document;
73 private ComboPooledDataSource cpds;
74 private boolean authenticated = false;
77 public Document getDocument() { return document; }
78 public void setDocument(Document document) { this.document = document; }
79 public ComboPooledDataSource getCpds() { return cpds; }
80 public void setCpds(ComboPooledDataSource cpds) { this.cpds = cpds; }
81 public boolean isAuthenticated() { return authenticated; }
82 public void setAuthenticated(boolean authenticated) { this.authenticated = authenticated; }
86 // convenience class to for dealing with the Online Glom configuration file
87 private class OnlineGlomProperties extends Properties {
88 public String getKey(String value) {
89 for (String key : stringPropertyNames()) {
90 if (getProperty(key).trim().equals(value))
97 private final Hashtable<String, ConfiguredDocument> documents = new Hashtable<String, ConfiguredDocument>();
98 // TODO implement locale
99 private final Locale locale = Locale.ROOT;
102 * This is called when the servlet is started or restarted.
104 public OnlineGlomServiceImpl() throws Exception {
106 // This retrieves configuration values from the onlineglom properties file located on the classpath for this
108 String classpath = System.getProperty("java.class.path");
109 String[] paths = classpath.split(File.pathSeparator);
110 File propFile = null;
111 boolean configFound = false;
112 for (String path : paths) {
113 propFile = new File(path, "onlineglom.properties");
114 if (propFile.exists() && !propFile.isDirectory()) {
116 Log.info("Using configuration file: " + propFile.getAbsolutePath());
121 Log.fatal("onlineglom.properties not found on the classpath.");
122 throw new IOException();
124 OnlineGlomProperties config = new OnlineGlomProperties();
125 config.load(new FileInputStream(propFile));
127 // check the configured glom file directory
128 String documentDirName = config.getProperty("glom.document.directory");
129 File documentDir = new File(documentDirName);
130 if (!documentDir.isDirectory()) {
131 Log.fatal(documentDirName + " is not a directory.");
132 throw new IOException();
134 if (!documentDir.canRead()) {
135 Log.fatal("Can't read the files in : " + documentDirName);
136 throw new IOException();
139 // get and check the glom files in the specified directory
140 File[] glomFiles = documentDir.listFiles(new FilenameFilter() {
142 public boolean accept(File dir, String name) {
143 return name.endsWith(".glom") ? true : false;
147 for (File glomFile : glomFiles) {
148 Document document = new Document();
149 document.set_file_uri("file://" + glomFile.getAbsolutePath());
151 boolean retval = document.load(error);
152 if (retval == false) {
154 if (LoadFailureCodes.LOAD_FAILURE_CODE_NOT_FOUND == LoadFailureCodes.swigToEnum(error)) {
155 message = "Could not find " + documentDir.getAbsolutePath();
157 message = "An unknown error occurred when trying to load " + documentDir.getAbsolutePath();
160 // continue with for loop because there may be other documents in the directory
164 // load the jdbc driver for the current glom document
165 ComboPooledDataSource cpds = new ComboPooledDataSource();
168 cpds.setDriverClass("org.postgresql.Driver");
169 } catch (PropertyVetoException e) {
170 Log.fatal("Error loading the PostgreSQL JDBC driver. Is the PostgreSQL JDBC jar available to the servlet?");
174 // setup the JDBC driver for the current glom document
175 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + "/"
176 + document.get_connection_database());
178 // check if a username and password have been set and work for the current document
179 String documentTitle = document.get_database_title().trim();
180 ConfiguredDocument configuredDocument = new ConfiguredDocument();
181 String key = config.getKey(documentTitle);
183 String[] keyArray = key.split("\\.");
184 if (keyArray.length == 3 && "title".equals(keyArray[2])) {
185 // username/password could be set, let's check to see if it works
186 String usernameKey = key.replaceAll(keyArray[2], "username");
187 String passwordKey = key.replaceAll(keyArray[2], "password");
188 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
189 config.getProperty(usernameKey), config.getProperty(passwordKey)));
193 // check the if the global username and password have been set and work with this document
194 if (!configuredDocument.isAuthenticated()) {
195 configuredDocument.setAuthenticated(checkAuthentication(documentTitle, cpds,
196 config.getProperty("glom.document.username"), config.getProperty("glom.document.password")));
199 // add information to the hash table
200 configuredDocument.setDocument(document);
201 configuredDocument.setCpds(cpds);
202 documents.put(documentTitle, configuredDocument);
207 * Checks if the username and password works with the database configured with the specified ComboPooledDataSource.
209 * @return true if authentication works, false otherwise
211 private boolean checkAuthentication(String documentTitle, ComboPooledDataSource cpds, String username,
212 String password) throws SQLException {
213 cpds.setUser(username);
214 cpds.setPassword(password);
216 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
217 cpds.setAcquireRetryAttempts(1);
218 Connection conn = null;
220 // FIXME find a better way to check authentication
221 // it's possible that the connection could be failing for another reason
222 conn = cpds.getConnection();
224 } catch (SQLException e) {
225 Log.info("Username and password not correct for document: " + documentTitle);
229 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
235 * This is called when the servlet is stopped or restarted.
237 * @see javax.servlet.GenericServlet#destroy()
240 public void destroy() {
241 Glom.libglom_deinit();
243 for (String documenTitle : documents.keySet()) {
244 ConfiguredDocument configuredDoc = documents.get(documenTitle);
246 DataSources.destroy(configuredDoc.getCpds());
247 } catch (SQLException e) {
248 Log.error("Error cleaning up the ComboPooledDataSource for " + documenTitle, e);
254 public GlomDocument getGlomDocument(String documentTitle) {
256 Document document = documents.get(documentTitle).getDocument();
257 GlomDocument glomDocument = new GlomDocument();
259 // get arrays of table names and titles, and find the default table index
260 StringVector tablesVec = document.get_table_names();
262 int numTables = safeLongToInt(tablesVec.size());
263 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
265 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
266 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
267 boolean foundDefaultTable = false;
268 int visibleIndex = 0;
269 for (int i = 0; i < numTables; i++) {
270 String tableName = tablesVec.get(i);
271 if (!document.get_table_is_hidden(tableName)) {
272 tableNames.add(tableName);
273 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
274 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
275 glomDocument.setDefaultTableIndex(visibleIndex);
276 foundDefaultTable = true;
278 tableTitles.add(document.get_table_title(tableName));
283 // set everything we need
284 glomDocument.setTableNames(tableNames);
285 glomDocument.setTableTitles(tableTitles);
293 * @see org.glom.web.client.OnlineGlomService#getDefaultLayoutListTable(java.lang.String)
296 public LayoutListTable getDefaultLayoutListTable(String documentTitle) {
297 GlomDocument glomDocument = getGlomDocument(documentTitle);
298 String tableName = glomDocument.getTableNames().get(glomDocument.getDefaultTableIndex());
299 LayoutListTable layoutListTable = getLayoutListTable(documentTitle, tableName);
300 layoutListTable.setTableName(tableName);
301 return layoutListTable;
304 public LayoutListTable getLayoutListTable(String documentTitle, String table) {
305 ConfiguredDocument configuredDoc = documents.get(documentTitle);
306 Document document = configuredDoc.getDocument();
307 LayoutListTable tableInfo = new LayoutListTable();
309 // access the layout list
310 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", table);
311 ColumnInfo[] columns = null;
312 LayoutFieldVector layoutFields = new LayoutFieldVector();
313 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
314 if (listViewLayoutGroupSize > 0) {
315 // a layout list is defined, we can use it to for the LayoutListTable
316 if (listViewLayoutGroupSize > 1)
317 Log.warn(documentTitle + " - " + table + ": The size of the list view layout group for table " + table
318 + " is greater than 1. Attempting to use the first item for the layout list view.");
319 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
321 // find the defined layout list fields
322 int numItems = safeLongToInt(layoutItemsVec.size());
323 columns = new ColumnInfo[numItems];
324 for (int i = 0; i < numItems; i++) {
325 // TODO add support for other LayoutItems (Text, Image, Button)
326 LayoutItem item = layoutItemsVec.get(i);
327 LayoutItem_Field layoutItemField = LayoutItem_Field.cast_dynamic(item);
328 if (layoutItemField != null) {
329 layoutFields.add(layoutItemField);
330 columns[i] = new ColumnInfo(
331 layoutItemField.get_title_or_name(),
332 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
333 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
337 // no layout list is defined, use the table fields as the layout list
338 FieldVector fieldsVec = document.get_table_fields(table);
340 // find the fields to display in the layout list
341 int numItems = safeLongToInt(fieldsVec.size());
342 columns = new ColumnInfo[numItems];
343 for (int i = 0; i < numItems; i++) {
344 Field field = fieldsVec.get(i);
345 LayoutItem_Field layoutItemField = new LayoutItem_Field();
346 layoutItemField.set_full_field_details(field);
347 layoutFields.add(layoutItemField);
348 columns[i] = new ColumnInfo(layoutItemField.get_title_or_name(),
349 getColumnInfoHorizontalAlignment(layoutItemField.get_formatting_used_horizontal_alignment()),
350 getColumnInfoGlomFieldType(layoutItemField.get_glom_type()));
354 tableInfo.setColumns(columns);
356 // Get the number of rows a query with the table name and layout fields would return. This is needed for the
358 if (!configuredDoc.isAuthenticated())
360 Connection conn = null;
364 // Setup and execute the count query. Special care needs to be take to ensure that the results will be based
365 // on a cursor so that large amounts of memory are not consumed when the query retrieve a large amount of
366 // data. Here's the relevant PostgreSQL documentation:
367 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
368 ComboPooledDataSource cpds = configuredDoc.getCpds();
369 conn = cpds.getConnection();
370 conn.setAutoCommit(false);
371 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
372 String query = Glom.build_sql_select_count_simple(table, layoutFields);
373 // TODO Test execution time of this query with when the number of rows in the table is large (say >
374 // 1,000,000). Test memory usage at the same time (see the todo item in getTableData()).
375 rs = st.executeQuery(query);
377 // get the number of rows in the query
379 tableInfo.setNumRows(rs.getInt(1));
381 } catch (SQLException e) {
382 Log.error(documentTitle + " - " + table
383 + ": Error calculating number of rows in the query. Setting number of rows to 0.", e);
384 tableInfo.setNumRows(0);
386 // cleanup everything that has been used
394 } catch (Exception e) {
395 Log.error(documentTitle + " - " + table
396 + ": Error closing database resources. Subsequent database queries may not work.", e);
403 public ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length) {
404 return getTableData(documentTitle, tableName, start, length, false, 0, false);
407 public ArrayList<GlomField[]> getSortedTableData(String documentTitle, String tableName, int start, int length,
408 int sortColumnIndex, boolean isAscending) {
409 return getTableData(documentTitle, tableName, start, length, true, sortColumnIndex, isAscending);
412 private ArrayList<GlomField[]> getTableData(String documentTitle, String tableName, int start, int length,
413 boolean useSortClause, int sortColumnIndex, boolean isAscending) {
415 ConfiguredDocument configuredDoc = documents.get(documentTitle);
416 Document document = configuredDoc.getDocument();
418 // access the layout list using the defined layout list or the table fields if there's no layout list
419 LayoutGroupVector layoutListVec = document.get_data_layout_groups("list", tableName);
420 LayoutFieldVector layoutFields = new LayoutFieldVector();
421 SortClause sortClause = new SortClause();
422 int listViewLayoutGroupSize = safeLongToInt(layoutListVec.size());
423 if (layoutListVec.size() > 0) {
424 // a layout list is defined, we can use it to for the LayoutListTable
425 if (listViewLayoutGroupSize > 1)
426 Log.warn(documentTitle + ": The size of the list view layout group for table " + tableName
427 + " is greater than 1. Attempting to use the first item for the layout list view.");
428 LayoutItemVector layoutItemsVec = layoutListVec.get(0).get_items();
430 // find the defined layout list fields
431 int numItems = safeLongToInt(layoutItemsVec.size());
432 for (int i = 0; i < numItems; i++) {
433 // TODO add support for other LayoutItems (Text, Image, Button)
434 LayoutItem item = layoutItemsVec.get(i);
435 LayoutItem_Field layoutItemfield = LayoutItem_Field.cast_dynamic(item);
436 if (layoutItemfield != null) {
437 // use this field in the layout
438 layoutFields.add(layoutItemfield);
440 // create a sort clause if it's a primary key and we're not asked to sort a specific column
441 if (!useSortClause) {
442 Field details = layoutItemfield.get_full_field_details();
443 if (details != null && details.get_primary_key()) {
444 sortClause.addLast(new SortFieldPair(layoutItemfield, true)); // ascending
450 // no layout list is defined, use the table fields as the layout list
451 FieldVector fieldsVec = document.get_table_fields(tableName);
453 // find the fields to display in the layout list
454 int numItems = safeLongToInt(fieldsVec.size());
455 for (int i = 0; i < numItems; i++) {
456 Field field = fieldsVec.get(i);
457 LayoutItem_Field layoutItemField = new LayoutItem_Field();
458 layoutItemField.set_full_field_details(field);
459 layoutFields.add(layoutItemField);
461 // create a sort clause if it's a primary key and we're not asked to sort a specific column
462 if (!useSortClause) {
463 if (field.get_primary_key()) {
464 sortClause.addLast(new SortFieldPair(layoutItemField, true)); // ascending
470 // create a sort clause for the column we've been asked to sort
472 LayoutItem item = layoutFields.get(sortColumnIndex);
473 LayoutItem_Field field = LayoutItem_Field.cast_dynamic(item);
475 sortClause.addLast(new SortFieldPair(field, isAscending));
477 Log.error(documentTitle + " - " + tableName + ": Error getting LayoutItem_Field for column index "
478 + sortColumnIndex + ". Cannot create a sort clause for this column.");
483 ArrayList<GlomField[]> rowsList = new ArrayList<GlomField[]>();
484 if (!configuredDoc.isAuthenticated())
486 Connection conn = null;
490 // Setup and execute the query. Special care needs to be take to ensure that the results will be based on a
491 // cursor so that large amounts of memory are not consumed when the query retrieve a large amount of data.
492 // Here's the relevant PostgreSQL documentation:
493 // http://jdbc.postgresql.org/documentation/83/query.html#query-with-cursor
494 ComboPooledDataSource cpds = configuredDoc.getCpds();
495 conn = cpds.getConnection();
496 conn.setAutoCommit(false);
497 st = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
498 st.setFetchSize(length);
499 String query = Glom.build_sql_select_simple(tableName, layoutFields, sortClause) + " OFFSET " + start;
500 // TODO Test memory usage before and after we execute the query that would result in a large ResultSet.
501 // We need to ensure that the JDBC driver is in fact returning a cursor based result set that has a low
502 // memory footprint. Check the difference between this value before and after the query:
503 // Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()
504 // Test the execution time at the same time (see the todo item in getLayoutListTable()).
505 rs = st.executeQuery(query);
507 // get the data we've been asked for
509 while (rs.next() && rowCount <= length) {
510 int layoutFieldsSize = safeLongToInt(layoutFields.size());
511 GlomField[] rowArray = new GlomField[layoutFieldsSize];
512 for (int i = 0; i < layoutFieldsSize; i++) {
513 // make a new GlomField to set the text and colours
514 rowArray[i] = new GlomField();
516 // get foreground and background colours
517 LayoutItem_Field field = layoutFields.get(i);
518 FieldFormatting formatting = field.get_formatting_used();
519 String fgcolour = formatting.get_text_format_color_foreground();
520 if (!fgcolour.isEmpty())
521 rowArray[i].setFGColour(convertGdkColorToHtmlColour(fgcolour));
522 String bgcolour = formatting.get_text_format_color_background();
523 if (!bgcolour.isEmpty())
524 rowArray[i].setBGColour(convertGdkColorToHtmlColour(bgcolour));
526 // Convert the field value to a string based on the glom type. We're doing the formatting on the
527 // server side for now but it might be useful to move this to the client side.
528 switch (field.get_glom_type()) {
530 String text = rs.getString(i + 1);
531 rowArray[i].setText(text != null ? text : "");
534 rowArray[i].setBoolean(rs.getBoolean(i + 1));
537 // Take care of the numeric formatting before converting the number to a string.
538 NumericFormat numFormatGlom = formatting.getM_numeric_format();
539 // There's no isCurrency() method in the glom NumericFormat class so we're assuming that the
540 // number should be formatted as a currency if the currency code string is not empty.
541 String currencyCode = numFormatGlom.getM_currency_symbol();
542 NumberFormat numFormatJava = null;
543 boolean useGlomCurrencyCode = false;
544 if (currencyCode.length() == 3) {
545 // Try to format the currency using the Java Locales system.
547 Currency currency = Currency.getInstance(currencyCode);
548 Log.info(documentTitle
551 + ": A valid ISO 4217 currency code is being used. Overriding the numeric formatting with information from the locale.");
552 int digits = currency.getDefaultFractionDigits();
553 numFormatJava = NumberFormat.getCurrencyInstance(locale);
554 numFormatJava.setCurrency(currency);
555 numFormatJava.setMinimumFractionDigits(digits);
556 numFormatJava.setMaximumFractionDigits(digits);
557 } catch (IllegalArgumentException e) {
558 Log.warn(documentTitle
563 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
564 // The currency code is not this is not an ISO 4217 currency code.
565 // We're going to manually set the currency code and use the glom numeric formatting.
566 useGlomCurrencyCode = true;
567 numFormatJava = getJavaNumberFormat(numFormatGlom);
569 } else if (currencyCode.length() > 0) {
570 Log.warn(documentTitle + " - " + tableName + ": " + currencyCode
571 + " is not a valid ISO 4217 code. Manually setting currency code with this value.");
572 // The length of the currency code is > 0 and != 3; this is not an ISO 4217 currency code.
573 // We're going to manually set the currency code and use the glom numeric formatting.
574 useGlomCurrencyCode = true;
575 numFormatJava = getJavaNumberFormat(numFormatGlom);
577 // The length of the currency code is 0; the number is not a currency.
578 numFormatJava = getJavaNumberFormat(numFormatGlom);
581 // TODO: Do I need to do something with NumericFormat.get_default_precision() from libglom?
583 double number = rs.getDouble(i + 1);
585 if (formatting.getM_numeric_format().getM_alt_foreground_color_for_negatives())
586 // overrides the set foreground colour
587 rowArray[i].setFGColour(convertGdkColorToHtmlColour(NumericFormat
588 .get_alternative_color_for_negatives()));
591 // Finally convert the number to text using the glom currency string if required.
592 if (useGlomCurrencyCode) {
593 rowArray[i].setText(currencyCode + " " + numFormatJava.format(number));
595 rowArray[i].setText(numFormatJava.format(number));
599 Date date = rs.getDate(i + 1);
601 DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
602 rowArray[i].setText(dateFormat.format(date));
604 rowArray[i].setText("");
608 Time time = rs.getTime(i + 1);
610 DateFormat timeFormat = DateFormat.getTimeInstance(DateFormat.MEDIUM, locale);
611 rowArray[i].setText(timeFormat.format(time));
613 rowArray[i].setText("");
617 byte[] image = rs.getBytes(i + 1);
619 // TODO implement field TYPE_IMAGE
620 rowArray[i].setText("Image (FIXME)");
622 rowArray[i].setText("");
627 Log.warn(documentTitle + " - " + tableName
628 + ": Invalid LayoutItem Field type. Using empty string for value.");
629 rowArray[i].setText("");
634 // add the row of GlomFields to the ArrayList we're going to return and update the row count
635 rowsList.add(rowArray);
638 } catch (SQLException e) {
639 Log.error(documentTitle + " - " + tableName + ": Error executing database query.", e);
640 // TODO: somehow notify user of problem
642 // cleanup everything that has been used
650 } catch (Exception e) {
651 Log.error(documentTitle + " - " + tableName
652 + ": Error closing database resources. Subsequent database queries may not work.", e);
658 public ArrayList<String> getDocumentTitles() {
659 ArrayList<String> documentTitles = new ArrayList<String>();
660 for (String title : documents.keySet()) {
661 documentTitles.add(title);
663 return documentTitles;
667 * This method safely converts longs from libglom into ints. This method was taken from stackoverflow:
669 * http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
671 private int safeLongToInt(long value) {
672 if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {
673 throw new IllegalArgumentException(value + " cannot be cast to int without changing its value.");
678 private NumberFormat getJavaNumberFormat(NumericFormat numFormatGlom) {
679 NumberFormat numFormatJava = NumberFormat.getInstance(locale);
680 if (numFormatGlom.getM_decimal_places_restricted()) {
681 int digits = safeLongToInt(numFormatGlom.getM_decimal_places());
682 numFormatJava.setMinimumFractionDigits(digits);
683 numFormatJava.setMaximumFractionDigits(digits);
685 numFormatJava.setGroupingUsed(numFormatGlom.getM_use_thousands_separator());
686 return numFormatJava;
690 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
691 * significant 8-bits in each channel.
693 private String convertGdkColorToHtmlColour(String gdkColor) {
694 if (gdkColor.length() == 13)
695 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
696 else if (gdkColor.length() == 7) {
697 // This shouldn't happen but let's deal with it if it does.
698 Log.warn("convertGdkColorToHtmlColour(): Expected a 13 character string but received a 7 character string. Returning received string.");
701 Log.error("convertGdkColorToHtmlColour(): Did not receive a 13 or 7 character string. Returning black HTML colour code.");
707 * This method converts a FieldFormatting.HorizontalAlignment to the equivalent ColumnInfo.HorizontalAlignment. The
708 * need for this comes from the fact that the GWT HorizontalAlignment classes can't be used with RPC and there's no
709 * easy way to use the java-libglom FieldFormatting.HorizontalAlignment enum with RPC. An enum identical to
710 * FieldFormatting.HorizontalAlignment is included in the ColumnInfo class.
712 private ColumnInfo.HorizontalAlignment getColumnInfoHorizontalAlignment(
713 FieldFormatting.HorizontalAlignment alignment) {
715 case HORIZONTAL_ALIGNMENT_AUTO:
716 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
717 case HORIZONTAL_ALIGNMENT_LEFT:
718 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
719 case HORIZONTAL_ALIGNMENT_RIGHT:
720 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
722 Log.error("getColumnInfoGlomFieldType(): Recieved an alignment that I don't know about: "
723 + FieldFormatting.HorizontalAlignment.class.getName() + "." + alignment.toString() + ". Returning "
724 + ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT.toString() + ".");
725 return ColumnInfo.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
730 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
731 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
732 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
735 private ColumnInfo.GlomFieldType getColumnInfoGlomFieldType(Field.glom_field_type type) {
738 return ColumnInfo.GlomFieldType.TYPE_BOOLEAN;
740 return ColumnInfo.GlomFieldType.TYPE_DATE;
742 return ColumnInfo.GlomFieldType.TYPE_IMAGE;
744 return ColumnInfo.GlomFieldType.TYPE_NUMERIC;
746 return ColumnInfo.GlomFieldType.TYPE_TEXT;
748 return ColumnInfo.GlomFieldType.TYPE_TIME;
750 Log.info("getColumnInfoGlomFieldType(): Returning TYPE_INVALID.");
751 return ColumnInfo.GlomFieldType.TYPE_INVALID;
753 Log.error("getColumnInfoGlomFieldType(): Recieved a type that I don't know about: "
754 + Field.glom_field_type.class.getName() + "." + type.toString() + ". Returning "
755 + ColumnInfo.GlomFieldType.TYPE_INVALID.toString() + ".");
756 return ColumnInfo.GlomFieldType.TYPE_INVALID;
763 * @see org.glom.web.client.OnlineGlomService#isAuthenticated(java.lang.String)
765 public boolean isAuthenticated(String documentTitle) {
766 return documents.get(documentTitle).isAuthenticated();
772 * @see org.glom.web.client.OnlineGlomService#checkAuthentication(java.lang.String, java.lang.String,
775 public boolean checkAuthentication(String documentTitle, String username, String password) {
776 ConfiguredDocument configuredDoc = documents.get(documentTitle);
777 boolean authenticated;
779 authenticated = checkAuthentication(documentTitle, configuredDoc.getCpds(), username, password);
780 } catch (SQLException e) {
784 configuredDoc.setAuthenticated(authenticated);
785 return authenticated;