2 * Copyright (C) 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;
23 import java.sql.Connection;
24 import java.sql.SQLException;
25 import java.util.ArrayList;
27 import org.glom.libglom.Document;
28 import org.glom.libglom.Field;
29 import org.glom.libglom.FieldFormatting;
30 import org.glom.libglom.FieldVector;
31 import org.glom.libglom.Glom;
32 import org.glom.libglom.LayoutGroupVector;
33 import org.glom.libglom.LayoutItemVector;
34 import org.glom.libglom.LayoutItem_CalendarPortal;
35 import org.glom.libglom.LayoutItem_Field;
36 import org.glom.libglom.LayoutItem_Notebook;
37 import org.glom.libglom.LayoutItem_Portal;
38 import org.glom.libglom.NumericFormat;
39 import org.glom.libglom.Relationship;
40 import org.glom.libglom.StringVector;
41 import org.glom.web.server.database.DetailsDBAccess;
42 import org.glom.web.server.database.ListViewDBAccess;
43 import org.glom.web.server.database.RelatedListDBAccess;
44 import org.glom.web.server.database.RelatedListNavigation;
45 import org.glom.web.shared.DataItem;
46 import org.glom.web.shared.DocumentInfo;
47 import org.glom.web.shared.GlomNumericFormat;
48 import org.glom.web.shared.NavigationRecord;
49 import org.glom.web.shared.TypedDataItem;
50 import org.glom.web.shared.layout.Formatting;
51 import org.glom.web.shared.layout.LayoutGroup;
52 import org.glom.web.shared.layout.LayoutItem;
53 import org.glom.web.shared.layout.LayoutItemField;
54 import org.glom.web.shared.layout.LayoutItemNotebook;
55 import org.glom.web.shared.layout.LayoutItemPortal;
57 import com.mchange.v2.c3p0.ComboPooledDataSource;
60 * A class to hold configuration information for a given Glom document.
61 * This class retrieves layout information from libglom and data from
62 * the underlying PostgreSQL database.
64 final class ConfiguredDocument {
66 private Document document;
67 private ComboPooledDataSource cpds;
68 private boolean authenticated = false;
69 private String documentID;
71 @SuppressWarnings("unused")
72 private ConfiguredDocument() {
73 // disable default constructor
76 ConfiguredDocument(final Document document) throws PropertyVetoException {
78 // load the jdbc driver
79 cpds = new ComboPooledDataSource();
81 // We don't support sqlite or self-hosting yet.
82 if (document.get_hosting_mode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
83 Log.fatal("Error configuring the database connection." + " Only central PostgreSQL hosting is supported.");
84 // FIXME: Throw exception?
88 cpds.setDriverClass("org.postgresql.Driver");
89 } catch (final PropertyVetoException e) {
90 Log.fatal("Error loading the PostgreSQL JDBC driver."
91 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
95 // setup the JDBC driver for the current glom document
96 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + ":" + document.get_connection_port()
97 + "/" + document.get_connection_database());
99 this.document = document;
103 * Sets the username and password for the database associated with the Glom document.
105 * @return true if the username and password works, false otherwise
107 boolean setUsernameAndPassword(final String username, final String password) throws SQLException {
108 cpds.setUser(username);
109 cpds.setPassword(password);
111 final int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
112 cpds.setAcquireRetryAttempts(1);
113 Connection conn = null;
115 // FIXME find a better way to check authentication
116 // it's possible that the connection could be failing for another reason
117 conn = cpds.getConnection();
118 authenticated = true;
119 } catch (final SQLException e) {
120 Log.info(Utils.getFileName(document.get_file_uri()), e.getMessage());
121 Log.info(Utils.getFileName(document.get_file_uri()),
122 "Connection Failed. Maybe the username or password is not correct.");
123 authenticated = false;
127 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
129 return authenticated;
132 Document getDocument() {
136 ComboPooledDataSource getCpds() {
140 boolean isAuthenticated() {
141 return authenticated;
144 String getDocumentID() {
148 void setDocumentID(final String documentID) {
149 this.documentID = documentID;
155 DocumentInfo getDocumentInfo() {
156 final DocumentInfo documentInfo = new DocumentInfo();
158 // get arrays of table names and titles, and find the default table index
159 final StringVector tablesVec = document.get_table_names();
161 final int numTables = Utils.safeLongToInt(tablesVec.size());
162 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
164 final ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
165 final ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
166 boolean foundDefaultTable = false;
167 int visibleIndex = 0;
168 for (int i = 0; i < numTables; i++) {
169 final String tableName = tablesVec.get(i);
170 if (!document.get_table_is_hidden(tableName)) {
171 tableNames.add(tableName);
172 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
173 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
174 documentInfo.setDefaultTableIndex(visibleIndex);
175 foundDefaultTable = true;
177 tableTitles.add(document.get_table_title(tableName));
182 // set everything we need
183 documentInfo.setTableNames(tableNames);
184 documentInfo.setTableTitles(tableTitles);
185 documentInfo.setTitle(document.get_database_title());
191 * Gets the layout group for the list view using the defined layout list in the document or the table fields if
192 * there's no defined layout group for the list view.
194 private org.glom.libglom.LayoutGroup getValidListViewLayoutGroup(final String tableName) {
196 final LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("list", tableName);
198 final int listViewLayoutGroupSize = Utils.safeLongToInt(layoutGroupVec.size());
199 org.glom.libglom.LayoutGroup libglomLayoutGroup = null;
200 if (listViewLayoutGroupSize > 0) {
201 // a list layout group is defined; we can use the first group as the list
202 if (listViewLayoutGroupSize > 1)
203 Log.warn(documentID, tableName, "The size of the list layout group is greater than 1. "
204 + "Attempting to use the first item for the layout list view.");
206 libglomLayoutGroup = layoutGroupVec.get(0);
208 // a list layout group is *not* defined; we are going make a libglom layout group from the list of fields
209 Log.info(documentID, tableName,
210 "A list layout is not defined for this table. Displaying a list layout based on the field list.");
212 final FieldVector fieldsVec = document.get_table_fields(tableName);
213 libglomLayoutGroup = new org.glom.libglom.LayoutGroup();
214 for (int i = 0; i < fieldsVec.size(); i++) {
215 final Field field = fieldsVec.get(i);
216 final LayoutItem_Field layoutItemField = new LayoutItem_Field();
217 layoutItemField.set_full_field_details(field);
218 libglomLayoutGroup.add_item(layoutItemField);
222 return libglomLayoutGroup;
225 ArrayList<DataItem[]> getListViewData(String tableName, final String quickFind, final int start, final int length,
226 final boolean useSortClause,
227 final int sortColumnIndex, final boolean isAscending) {
228 // Validate the table name.
229 tableName = getTableNameToUse(tableName);
231 // Get the libglom LayoutGroup that represents the list view.
232 final org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
234 // Create a database access object for the list view.
235 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
239 return listViewDBAccess.getData(quickFind, start, length, useSortClause, sortColumnIndex, isAscending);
242 DataItem[] getDetailsData(String tableName, final TypedDataItem primaryKeyValue) {
243 // Validate the table name.
244 tableName = getTableNameToUse(tableName);
246 final DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
248 return detailsDBAccess.getData(primaryKeyValue);
251 ArrayList<DataItem[]> getRelatedListData(String tableName, final String relationshipName, final TypedDataItem foreignKeyValue,
252 final int start, final int length, final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
253 // Validate the table name.
254 tableName = getTableNameToUse(tableName);
256 // Create a database access object for the related list
257 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
261 return relatedListDBAccess.getData(start, length, foreignKeyValue, useSortClause, sortColumnIndex, isAscending);
264 ArrayList<LayoutGroup> getDetailsLayoutGroup(String tableName) {
265 // Validate the table name.
266 tableName = getTableNameToUse(tableName);
268 // Get the details layout group information for each LayoutGroup in the LayoutGroupVector
269 final LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
270 final ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
271 for (int i = 0; i < layoutGroupVec.size(); i++) {
272 final org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
274 // satisfy the precondition of getDetailsLayoutGroup(String, org.glom.libglom.LayoutGroup)
275 if (libglomLayoutGroup == null)
278 layoutGroups.add(getDetailsLayoutGroup(tableName, libglomLayoutGroup));
285 * Gets the expected row count for a related list.
287 int getRelatedListRowCount(String tableName, final String relationshipName, final TypedDataItem foreignKeyValue) {
288 // Validate the table name.
289 tableName = getTableNameToUse(tableName);
291 // Create a database access object for the related list
292 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
295 // Return the row count
296 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
299 NavigationRecord getSuitableRecordToViewDetails(String tableName, final String relationshipName,
300 final TypedDataItem primaryKeyValue) {
301 // Validate the table name.
302 tableName = getTableNameToUse(tableName);
304 final RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds, tableName,
307 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
310 LayoutGroup getListViewLayoutGroup(String tableName) {
311 // Validate the table name.
312 tableName = getTableNameToUse(tableName);
314 final org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
316 final LayoutGroup layoutGroup = new LayoutGroup(); // the object that will be returned
317 int primaryKeyIndex = -1;
319 // look at each child item
320 final LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
321 final int numItems = Utils.safeLongToInt(layoutItemsVec.size());
322 for (int i = 0; i < numItems; i++) {
323 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
325 // TODO add support for other LayoutItems (Text, Image, Button etc.)
326 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
327 if (libglomLayoutItemField != null) {
328 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, false));
329 final Field field = libglomLayoutItemField.get_full_field_details();
330 if (field.get_primary_key())
333 Log.info(documentID, tableName,
334 "Ignoring unknown list LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
339 // set the expected result size for list view tables
340 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
342 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
344 // Set the primary key index for the table
345 if (primaryKeyIndex < 0) {
346 // Add a LayoutItemField for the primary key to the end of the item list in the LayoutGroup because it
347 // doesn't already contain a primary key.
348 Field primaryKey = null;
349 final FieldVector fieldsVec = document.get_table_fields(tableName);
350 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
351 final Field field = fieldsVec.get(i);
352 if (field.get_primary_key()) {
357 if (primaryKey != null) {
358 final LayoutItem_Field libglomLayoutItemField = new LayoutItem_Field();
359 libglomLayoutItemField.set_full_field_details(primaryKey);
360 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, false));
361 layoutGroup.setPrimaryKeyIndex(layoutGroup.getItems().size() - 1);
362 layoutGroup.setHiddenPrimaryKey(true);
364 Log.error(document.get_database_title(), tableName,
365 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
369 layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
372 layoutGroup.setTableName(tableName);
378 * Gets a recursively defined Details LayoutGroup DTO for the specified libglom LayoutGroup object. This is used for
379 * getting layout information for the details view.
381 * @param documentID Glom document identifier
383 * @param tableName table name in the specified Glom document
385 * @param libglomLayoutGroup libglom LayoutGroup to convert
387 * @precondition libglomLayoutGroup must not be null
389 * @return {@link LayoutGroup} object that represents the layout for the specified {@link
390 * org.glom.libglom.LayoutGroup}
392 private LayoutGroup getDetailsLayoutGroup(final String tableName, final org.glom.libglom.LayoutGroup libglomLayoutGroup) {
393 final LayoutGroup layoutGroup = new LayoutGroup();
394 layoutGroup.setColumnCount(Utils.safeLongToInt(libglomLayoutGroup.get_columns_count()));
395 final String layoutGroupTitle = libglomLayoutGroup.get_title();
396 if (layoutGroupTitle.isEmpty())
397 layoutGroup.setName(libglomLayoutGroup.get_name());
399 layoutGroup.setTitle(layoutGroupTitle);
401 // look at each child item
402 final LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
403 for (int i = 0; i < layoutItemsVec.size(); i++) {
404 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
406 // just a safety check
407 if (libglomLayoutItem == null)
410 org.glom.web.shared.layout.LayoutItem layoutItem = null;
411 final org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
413 // libglomLayoutItem is a LayoutGroup
414 final LayoutItem_Portal libglomLayoutItemPortal = LayoutItem_Portal.cast_dynamic(group);
415 if (libglomLayoutItemPortal != null) {
416 // group is a LayoutItem_Portal
417 final LayoutItemPortal layoutItemPortal = createLayoutItemPortalDTO(tableName, libglomLayoutItemPortal);
418 if (layoutItemPortal == null)
420 layoutItem = layoutItemPortal;
423 // libglomLayoutItem is a LayoutGroup
424 final LayoutItem_Notebook libglomLayoutItemNotebook = LayoutItem_Notebook.cast_dynamic(group);
425 if (libglomLayoutItemNotebook != null) {
426 // group is a LayoutItem_Notebook
427 final LayoutGroup tempLayoutGroup = getDetailsLayoutGroup(tableName, libglomLayoutItemNotebook);
428 final LayoutItemNotebook layoutItemNotebook = new LayoutItemNotebook();
429 for (final LayoutItem item : tempLayoutGroup.getItems()) {
430 layoutItemNotebook.addItem(item);
432 layoutItemNotebook.setName(tableName);
433 layoutItem = layoutItemNotebook;
435 // group is *not* a LayoutItem_Portal or a LayoutItem_Notebook
436 // recurse into child groups
437 layoutItem = getDetailsLayoutGroup(tableName, group);
441 // libglomLayoutItem is *not* a LayoutGroup
442 // create LayoutItem DTOs based on the the libglom type
443 // TODO add support for other LayoutItems (Text, Image, Button etc.)
444 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
445 if (libglomLayoutItemField != null) {
447 final LayoutItemField layoutItemField = convertToGWTGlomLayoutItemField(libglomLayoutItemField, true);
449 // Set the full field details with updated field details from the document.
450 libglomLayoutItemField.set_full_field_details(document.get_field(
451 libglomLayoutItemField.get_table_used(tableName), libglomLayoutItemField.get_name()));
453 // Determine if the field should have a navigation button and set this in the DTO.
454 final Relationship fieldUsedInRelationshipToOne = new Relationship();
455 final boolean addNavigation = Glom.layout_field_should_have_navigation(tableName, libglomLayoutItemField,
456 document, fieldUsedInRelationshipToOne);
457 layoutItemField.setAddNavigation(addNavigation);
459 // Set the the name of the table to navigate to if navigation should be enabled.
461 // It's not possible to directly check if fieldUsedInRelationshipToOne is
462 // null because of the way that the glom_sharedptr macro works. This workaround accomplishes the
464 String tableNameUsed;
466 final Relationship temp = new Relationship();
467 temp.equals(fieldUsedInRelationshipToOne); // this will throw an NPE if
468 // fieldUsedInRelationshipToOne is null
469 // fieldUsedInRelationshipToOne is *not* null
470 tableNameUsed = fieldUsedInRelationshipToOne.get_to_table();
472 } catch (final NullPointerException e) {
473 // fieldUsedInRelationshipToOne is null
474 tableNameUsed = libglomLayoutItemField.get_table_used(tableName);
477 // Set the navigation table name only if it's not different than the current table name.
478 if (!tableName.equals(tableNameUsed)) {
479 layoutItemField.setNavigationTableName(tableNameUsed);
483 layoutItem = layoutItemField;
486 Log.info(documentID, tableName,
487 "Ignoring unknown details LayoutItem of type " + libglomLayoutItem.get_part_type_name()
493 layoutGroup.addItem(layoutItem);
499 private LayoutItemPortal createLayoutItemPortalDTO(final String tableName,
500 final org.glom.libglom.LayoutItem_Portal libglomLayoutItemPortal) {
502 // Ignore LayoutItem_CalendarPortals for now:
503 // https://bugzilla.gnome.org/show_bug.cgi?id=664273
504 final LayoutItem_CalendarPortal liblglomLayoutItemCalendarPortal = LayoutItem_CalendarPortal
505 .cast_dynamic(libglomLayoutItemPortal);
506 if (liblglomLayoutItemCalendarPortal != null)
509 final LayoutItemPortal layoutItemPortal = new LayoutItemPortal();
510 final Relationship relationship = libglomLayoutItemPortal.get_relationship();
511 if (relationship != null) {
512 layoutItemPortal.setNavigationType(convertToGWTGlomNavigationType(libglomLayoutItemPortal
513 .get_navigation_type()));
515 layoutItemPortal.setTitle(libglomLayoutItemPortal.get_title_used("")); // parent title not relevant
516 layoutItemPortal.setName(libglomLayoutItemPortal.get_relationship_name_used());
517 layoutItemPortal.setTableName(relationship.get_from_table());
518 layoutItemPortal.setFromField(relationship.get_from_field());
520 // convert the portal layout items into LayoutItemField DTOs
521 final LayoutItemVector layoutItemsVec = libglomLayoutItemPortal.get_items();
522 long numItems = layoutItemsVec.size();
523 for (int i = 0; i < numItems; i++) {
524 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
526 // TODO add support for other LayoutItems (Text, Image, Button etc.)
527 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
528 if (libglomLayoutItemField != null) {
529 // TODO EDITING If the relationship does not allow editing, then mark all these fields as
530 // non-editable. Check relationship.get_allow_edit() to see if it's editable.
531 layoutItemPortal.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, false));
533 Log.info(documentID, tableName, "Ignoring unknown related list LayoutItem of type "
534 + libglomLayoutItem.get_part_type_name() + ".");
539 // get the primary key for the related list table
540 final LayoutItem_Field layoutItemField = new LayoutItem_Field();
541 final String toTableName = relationship.get_to_table();
542 if (!toTableName.isEmpty()) {
544 // get the LayoutItem_Feild with details from its Field in the document
545 final FieldVector fields = document.get_table_fields(toTableName);
546 numItems = fields.size(); // reuse loop variable from above
547 for (int i = 0; i < numItems; i++) {
548 final Field field = fields.get(i);
549 // check the names to see if they're the same
550 if (field.get_primary_key()) {
551 layoutItemField.set_full_field_details(field);
552 layoutItemPortal.addItem(convertToGWTGlomLayoutItemField(layoutItemField, false));
553 layoutItemPortal.setPrimaryKeyIndex(layoutItemPortal.getItems().size() - 1);
554 layoutItemPortal.setHiddenPrimaryKey(true); // always hidden in portals
560 // Set whether or not the related list will need to show the navigation buttons.
561 // This was ported from Glom: Box_Data_Portal::get_has_suitable_record_to_view_details()
562 final StringBuffer navigationTableName = new StringBuffer();
563 final LayoutItem_Field navigationRelationship = new LayoutItem_Field(); // Ignored.
564 libglomLayoutItemPortal.get_suitable_table_to_view_details(navigationTableName, navigationRelationship,
566 layoutItemPortal.setAddNavigation(!(navigationTableName.toString().isEmpty()));
569 // Note: An empty LayoutItemPortal is returned if relationship is null.
570 return layoutItemPortal;
573 private GlomNumericFormat convertNumbericFormat(final NumericFormat libglomNumericFormat) {
574 final GlomNumericFormat gnf = new GlomNumericFormat();
575 gnf.setUseAltForegroundColourForNegatives(libglomNumericFormat.get_alt_foreground_color_for_negatives());
576 gnf.setCurrencyCode(libglomNumericFormat.get_currency_symbol());
577 gnf.setDecimalPlaces(Utils.safeLongToInt(libglomNumericFormat.get_decimal_places()));
578 gnf.setDecimalPlacesRestricted(libglomNumericFormat.get_decimal_places_restricted());
579 gnf.setUseThousandsSeparator(libglomNumericFormat.get_use_thousands_separator());
583 private Formatting convertFormatting(final FieldFormatting libglomFormatting) {
584 final Formatting formatting = new Formatting();
587 final String foregroundColour = libglomFormatting.get_text_format_color_foreground();
588 if (foregroundColour != null && !foregroundColour.isEmpty())
589 formatting.setTextFormatColourForeground(convertGdkColorToHtmlColour(foregroundColour));
590 final String backgroundColour = libglomFormatting.get_text_format_color_background();
591 if (backgroundColour != null && !backgroundColour.isEmpty())
592 formatting.setTextFormatColourBackground(convertGdkColorToHtmlColour(backgroundColour));
595 if (libglomFormatting.get_text_format_multiline()) {
596 formatting.setTextFormatMultilineHeightLines(Utils.safeLongToInt(libglomFormatting
597 .get_text_format_multiline_height_lines()));
603 private LayoutItemField convertToGWTGlomLayoutItemField(final LayoutItem_Field libglomLayoutItemField,
604 final boolean forDetailsView) {
605 final LayoutItemField layoutItemField = new LayoutItemField();
608 layoutItemField.setType(convertToGWTGlomFieldType(libglomLayoutItemField.get_glom_type()));
610 // set title and name
611 layoutItemField.setTitle(libglomLayoutItemField.get_title_or_name());
612 layoutItemField.setName(libglomLayoutItemField.get_name());
614 // convert formatting
615 final FieldFormatting glomFormatting = libglomLayoutItemField.get_formatting_used();
616 final Formatting formatting = convertFormatting(glomFormatting);
618 // set horizontal alignment
619 final org.glom.libglom.FieldFormatting.HorizontalAlignment libglomHorizontalAlignment = libglomLayoutItemField
620 .get_formatting_used_horizontal_alignment(forDetailsView); // only returns LEFT or RIGHT
621 Formatting.HorizontalAlignment horizontalAlignment;
622 if (libglomHorizontalAlignment == org.glom.libglom.FieldFormatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT) {
623 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
625 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
627 formatting.setHorizontalAlignment(horizontalAlignment);
629 // create a GlomNumericFormat DTO for numeric values
630 if (libglomLayoutItemField.get_glom_type() == org.glom.libglom.Field.glom_field_type.TYPE_NUMERIC) {
631 formatting.setGlomNumericFormat(convertNumbericFormat(glomFormatting.get_numeric_format()));
633 layoutItemField.setFormatting(formatting);
635 return layoutItemField;
639 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
640 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
641 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
644 private LayoutItemField.GlomFieldType convertToGWTGlomFieldType(final Field.glom_field_type type) {
647 return LayoutItemField.GlomFieldType.TYPE_BOOLEAN;
649 return LayoutItemField.GlomFieldType.TYPE_DATE;
651 return LayoutItemField.GlomFieldType.TYPE_IMAGE;
653 return LayoutItemField.GlomFieldType.TYPE_NUMERIC;
655 return LayoutItemField.GlomFieldType.TYPE_TEXT;
657 return LayoutItemField.GlomFieldType.TYPE_TIME;
659 Log.info("Returning TYPE_INVALID.");
660 return LayoutItemField.GlomFieldType.TYPE_INVALID;
662 Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
663 + type.toString() + ". Returning " + LayoutItemField.GlomFieldType.TYPE_INVALID.toString() + ".");
664 return LayoutItemField.GlomFieldType.TYPE_INVALID;
669 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
670 * significant 8-bits in each channel.
672 private String convertGdkColorToHtmlColour(final String gdkColor) {
673 if (gdkColor.length() == 13)
674 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
675 else if (gdkColor.length() == 7) {
676 // This shouldn't happen but let's deal with it if it does.
678 "Expected a 13 character string but received a 7 character string. Returning received string.");
681 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
687 * This method converts a LayoutItem_Portal.navigation_type from java-libglom to the equivalent
688 * LayoutItemPortal.NavigationType from Online Glom. This conversion is required because the LayoutItem_Portal class
689 * from java-libglom can't be used with GWT-RPC. An enum identical to LayoutItem_Portal.navigation_type from
690 * java-libglom is included in the LayoutItemPortal data transfer object.
692 private LayoutItemPortal.NavigationType convertToGWTGlomNavigationType(
693 final LayoutItem_Portal.navigation_type navigationType) {
694 switch (navigationType) {
695 case NAVIGATION_NONE:
696 return LayoutItemPortal.NavigationType.NAVIGATION_NONE;
697 case NAVIGATION_AUTOMATIC:
698 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
699 case NAVIGATION_SPECIFIC:
700 return LayoutItemPortal.NavigationType.NAVIGATION_SPECIFIC;
702 Log.error("Recieved an unknown NavigationType: " + LayoutItem_Portal.navigation_type.class.getName() + "."
703 + navigationType.toString() + ". Returning " + LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC
705 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
710 * Gets the table name to use when accessing the database and the document. This method guards against SQL injection
711 * attacks by returning the default table if the requested table is not in the database or if the table name has not
715 * The table name to validate.
716 * @return The table name to use.
718 private String getTableNameToUse(final String tableName) {
719 if (tableName == null || tableName.isEmpty() || !document.get_table_is_known(tableName)) {
720 return document.get_default_table();