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. This class is used to retrieve layout
61 * information from libglom and data from the underlying PostgreSQL database.
63 * @author Ben Konrath <ben@bagu.org>
66 final class ConfiguredDocument {
68 private Document document;
69 private ComboPooledDataSource cpds;
70 private boolean authenticated = false;
71 private String documentID;
73 @SuppressWarnings("unused")
74 private ConfiguredDocument() {
75 // disable default constructor
78 ConfiguredDocument(Document document) throws PropertyVetoException {
80 // load the jdbc driver
81 cpds = new ComboPooledDataSource();
83 // We don't support sqlite or self-hosting yet.
84 if (document.get_hosting_mode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
85 Log.fatal("Error configuring the database connection." + " Only central PostgreSQL hosting is supported.");
86 // FIXME: Throw exception?
90 cpds.setDriverClass("org.postgresql.Driver");
91 } catch (PropertyVetoException e) {
92 Log.fatal("Error loading the PostgreSQL JDBC driver."
93 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
97 // setup the JDBC driver for the current glom document
98 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + ":" + document.get_connection_port()
99 + "/" + document.get_connection_database());
101 this.document = document;
105 * Sets the username and password for the database associated with the Glom document.
107 * @return true if the username and password works, false otherwise
109 boolean setUsernameAndPassword(String username, String password) throws SQLException {
110 cpds.setUser(username);
111 cpds.setPassword(password);
113 int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
114 cpds.setAcquireRetryAttempts(1);
115 Connection conn = null;
117 // FIXME find a better way to check authentication
118 // it's possible that the connection could be failing for another reason
119 conn = cpds.getConnection();
120 authenticated = true;
121 } catch (SQLException e) {
122 Log.info(Utils.getFileName(document.get_file_uri()), e.getMessage());
123 Log.info(Utils.getFileName(document.get_file_uri()),
124 "Connection Failed. Maybe the username or password is not correct.");
125 authenticated = false;
129 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
131 return authenticated;
134 Document getDocument() {
138 ComboPooledDataSource getCpds() {
142 boolean isAuthenticated() {
143 return authenticated;
146 String getDocumentID() {
150 void setDocumentID(String documentID) {
151 this.documentID = documentID;
157 DocumentInfo getDocumentInfo() {
158 DocumentInfo documentInfo = new DocumentInfo();
160 // get arrays of table names and titles, and find the default table index
161 StringVector tablesVec = document.get_table_names();
163 int numTables = Utils.safeLongToInt(tablesVec.size());
164 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
166 ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
167 ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
168 boolean foundDefaultTable = false;
169 int visibleIndex = 0;
170 for (int i = 0; i < numTables; i++) {
171 String tableName = tablesVec.get(i);
172 if (!document.get_table_is_hidden(tableName)) {
173 tableNames.add(tableName);
174 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
175 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
176 documentInfo.setDefaultTableIndex(visibleIndex);
177 foundDefaultTable = true;
179 tableTitles.add(document.get_table_title(tableName));
184 // set everything we need
185 documentInfo.setTableNames(tableNames);
186 documentInfo.setTableTitles(tableTitles);
187 documentInfo.setTitle(document.get_database_title());
193 * Gets the layout group for the list view using the defined layout list in the document or the table fields if
194 * there's no defined layout group for the list view.
196 private org.glom.libglom.LayoutGroup getValidListViewLayoutGroup(String tableName) {
198 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("list", tableName);
200 int listViewLayoutGroupSize = Utils.safeLongToInt(layoutGroupVec.size());
201 org.glom.libglom.LayoutGroup libglomLayoutGroup = null;
202 if (listViewLayoutGroupSize > 0) {
203 // a list layout group is defined; we can use the first group as the list
204 if (listViewLayoutGroupSize > 1)
205 Log.warn(documentID, tableName, "The size of the list layout group is greater than 1. "
206 + "Attempting to use the first item for the layout list view.");
208 libglomLayoutGroup = layoutGroupVec.get(0);
210 // a list layout group is *not* defined; we are going make a libglom layout group from the list of fields
211 Log.info(documentID, tableName,
212 "A list layout is not defined for this table. Displaying a list layout based on the field list.");
214 FieldVector fieldsVec = document.get_table_fields(tableName);
215 libglomLayoutGroup = new org.glom.libglom.LayoutGroup();
216 for (int i = 0; i < fieldsVec.size(); i++) {
217 Field field = fieldsVec.get(i);
218 LayoutItem_Field layoutItemField = new LayoutItem_Field();
219 layoutItemField.set_full_field_details(field);
220 libglomLayoutGroup.add_item(layoutItemField);
224 return libglomLayoutGroup;
227 ArrayList<DataItem[]> getListViewData(String tableName, int start, int length, boolean useSortClause,
228 int sortColumnIndex, boolean isAscending) {
229 // Validate the table name.
230 tableName = getTableNameToUse(tableName);
232 // Get the libglom LayoutGroup that represents the list view.
233 org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
235 // Create a database access object for the list view.
236 ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
240 return listViewDBAccess.getData(start, length, useSortClause, sortColumnIndex, isAscending);
243 DataItem[] getDetailsData(String tableName, TypedDataItem primaryKeyValue) {
244 // Validate the table name.
245 tableName = getTableNameToUse(tableName);
247 DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
249 return detailsDBAccess.getData(primaryKeyValue);
252 ArrayList<DataItem[]> getRelatedListData(String tableName, String relationshipName, TypedDataItem foreignKeyValue,
253 int start, int length, boolean useSortClause, int sortColumnIndex, boolean isAscending) {
254 // Validate the table name.
255 tableName = getTableNameToUse(tableName);
257 // Create a database access object for the related list
258 RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
262 return relatedListDBAccess.getData(start, length, foreignKeyValue, useSortClause, sortColumnIndex, isAscending);
265 ArrayList<LayoutGroup> getDetailsLayoutGroup(String tableName) {
266 // Validate the table name.
267 tableName = getTableNameToUse(tableName);
269 // Get the details layout group information for each LayoutGroup in the LayoutGroupVector
270 LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
271 ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
272 for (int i = 0; i < layoutGroupVec.size(); i++) {
273 org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
275 // satisfy the precondition of getDetailsLayoutGroup(String, org.glom.libglom.LayoutGroup)
276 if (libglomLayoutGroup == null)
279 layoutGroups.add(getDetailsLayoutGroup(tableName, libglomLayoutGroup));
285 LayoutGroup getListViewLayoutGroup(String tableName) {
286 // Validate the table name.
287 tableName = getTableNameToUse(tableName);
289 org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
291 return getListLayoutGroup(tableName, libglomLayoutGroup);
295 * Gets the expected row count for a related list.
297 int getRelatedListRowCount(String tableName, String relationshipName, TypedDataItem foreignKeyValue) {
298 // Validate the table name.
299 tableName = getTableNameToUse(tableName);
301 // Create a database access object for the related list
302 RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
305 // Return the row count
306 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
309 NavigationRecord getSuitableRecordToViewDetails(String tableName, String relationshipName,
310 TypedDataItem primaryKeyValue) {
311 // Validate the table name.
312 tableName = getTableNameToUse(tableName);
314 RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds, tableName,
317 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
321 * Gets a LayoutGroup DTO for the given table name and libglom LayoutGroup. This method can be used for the main
322 * list view table and for the related list table.
324 private LayoutGroup getListLayoutGroup(String tableName, org.glom.libglom.LayoutGroup libglomLayoutGroup) {
325 LayoutGroup layoutGroup = new LayoutGroup();
326 int primaryKeyIndex = -1;
328 // look at each child item
329 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
330 int numItems = Utils.safeLongToInt(layoutItemsVec.size());
331 for (int i = 0; i < numItems; i++) {
332 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
334 // TODO add support for other LayoutItems (Text, Image, Button etc.)
335 LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
336 if (libglomLayoutItemField != null) {
337 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, true));
338 Field field = libglomLayoutItemField.get_full_field_details();
339 if (field.get_primary_key())
342 Log.info(documentID, tableName,
343 "Ignoring unknown list LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
348 // set the expected result size for list view tables
349 LayoutItem_Portal libglomLayoutItemPortal = LayoutItem_Portal.cast_dynamic(libglomLayoutGroup);
350 if (libglomLayoutItemPortal == null) {
351 // libglomLayoutGroup is a list view
352 ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
354 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
357 // Set the primary key index for the table
358 if (primaryKeyIndex < 0) {
359 // Add a LayoutItemField for the primary key to the end of the item list in the LayoutGroup because it
360 // doesn't already contain a primary key.
361 Field primaryKey = null;
362 FieldVector fieldsVec = document.get_table_fields(tableName);
363 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
364 Field field = fieldsVec.get(i);
365 if (field.get_primary_key()) {
370 if (primaryKey != null) {
371 LayoutItem_Field libglomLayoutItemField = new LayoutItem_Field();
372 libglomLayoutItemField.set_full_field_details(primaryKey);
373 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, false));
374 layoutGroup.setPrimaryKeyIndex(layoutGroup.getItems().size() - 1);
375 layoutGroup.setHiddenPrimaryKey(true);
377 Log.error(document.get_database_title(), tableName,
378 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
382 layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
385 layoutGroup.setTableName(tableName);
391 * Gets a recursively defined Details LayoutGroup DTO for the specified libglom LayoutGroup object. This is used for
392 * getting layout information for the details view.
394 * @param documentID Glom document identifier
396 * @param tableName table name in the specified Glom document
398 * @param libglomLayoutGroup libglom LayoutGroup to convert
400 * @precondition libglomLayoutGroup must not be null
402 * @return {@link LayoutGroup} object that represents the layout for the specified {@link
403 * org.glom.libglom.LayoutGroup}
405 private LayoutGroup getDetailsLayoutGroup(String tableName, org.glom.libglom.LayoutGroup libglomLayoutGroup) {
406 LayoutGroup layoutGroup = new LayoutGroup();
407 layoutGroup.setColumnCount(Utils.safeLongToInt(libglomLayoutGroup.get_columns_count()));
408 String layoutGroupTitle = libglomLayoutGroup.get_title();
409 if (layoutGroupTitle.isEmpty())
410 layoutGroup.setName(libglomLayoutGroup.get_name());
412 layoutGroup.setTitle(layoutGroupTitle);
414 // look at each child item
415 LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
416 for (int i = 0; i < layoutItemsVec.size(); i++) {
417 org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
419 // just a safety check
420 if (libglomLayoutItem == null)
423 org.glom.web.shared.layout.LayoutItem layoutItem = null;
424 org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
426 // libglomLayoutItem is a LayoutGroup
427 LayoutItem_Portal libglomLayoutItemPortal = LayoutItem_Portal.cast_dynamic(group);
428 if (libglomLayoutItemPortal != null) {
429 // group is a LayoutItem_Portal
430 LayoutItemPortal layoutItemPortal = createLayoutItemPortalDTO(tableName, libglomLayoutItemPortal);
431 if (layoutItemPortal == null)
433 layoutItem = layoutItemPortal;
436 // libglomLayoutItem is a LayoutGroup
437 LayoutItem_Notebook libglomLayoutItemNotebook = LayoutItem_Notebook.cast_dynamic(group);
438 if (libglomLayoutItemNotebook != null) {
439 // group is a LayoutItem_Notebook
440 LayoutGroup tempLayoutGroup = getDetailsLayoutGroup(tableName, libglomLayoutItemNotebook);
441 LayoutItemNotebook layoutItemNotebook = new LayoutItemNotebook();
442 for (LayoutItem item : tempLayoutGroup.getItems()) {
443 layoutItemNotebook.addItem(item);
445 layoutItemNotebook.setName(tableName);
446 layoutItem = layoutItemNotebook;
448 // group is *not* a LayoutItem_Portal or a LayoutItem_Notebook
449 // recurse into child groups
450 layoutItem = getDetailsLayoutGroup(tableName, group);
454 // libglomLayoutItem is *not* a LayoutGroup
455 // create LayoutItem DTOs based on the the libglom type
456 // TODO add support for other LayoutItems (Text, Image, Button etc.)
457 LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
458 if (libglomLayoutItemField != null) {
460 LayoutItemField layoutItemField = convertToGWTGlomLayoutItemField(libglomLayoutItemField, true);
462 // Set the full field details with updated field details from the document.
463 libglomLayoutItemField.set_full_field_details(document.get_field(
464 libglomLayoutItemField.get_table_used(tableName), libglomLayoutItemField.get_name()));
466 // Determine if the field should have a navigation button and set this in the DTO.
467 Relationship fieldUsedInRelationshipToOne = new Relationship();
468 boolean addNavigation = Glom.layout_field_should_have_navigation(tableName, libglomLayoutItemField,
469 document, fieldUsedInRelationshipToOne);
470 layoutItemField.setAddNavigation(addNavigation);
472 // Set the the name of the table to navigate to if navigation should be enabled.
474 // It's not possible to directly check if fieldUsedInRelationshipToOne is
475 // null because of the way that the glom_sharedptr macro works. This workaround accomplishes the
477 String tableNameUsed;
479 Relationship temp = new Relationship();
480 temp.equals(fieldUsedInRelationshipToOne); // this will throw an NPE if
481 // fieldUsedInRelationshipToOne is null
482 // fieldUsedInRelationshipToOne is *not* null
483 tableNameUsed = fieldUsedInRelationshipToOne.get_to_table();
485 } catch (NullPointerException e) {
486 // fieldUsedInRelationshipToOne is null
487 tableNameUsed = libglomLayoutItemField.get_table_used(tableName);
490 // Set the navigation table name only if it's not different than the current table name.
491 if (!tableName.equals(tableNameUsed)) {
492 layoutItemField.setNavigationTableName(tableNameUsed);
496 layoutItem = layoutItemField;
499 Log.info(documentID, tableName,
500 "Ignoring unknown details LayoutItem of type " + libglomLayoutItem.get_part_type_name()
506 layoutGroup.addItem(layoutItem);
512 private LayoutItemPortal createLayoutItemPortalDTO(String tableName,
513 org.glom.libglom.LayoutItem_Portal libglomLayoutItemPortal) {
515 // Ignore LayoutItem_CalendarPortals for now:
516 // https://bugzilla.gnome.org/show_bug.cgi?id=664273
517 LayoutItem_CalendarPortal liblglomLayoutItemCalendarPortal = LayoutItem_CalendarPortal
518 .cast_dynamic(libglomLayoutItemPortal);
519 if (liblglomLayoutItemCalendarPortal != null)
522 LayoutItemPortal layoutItemPortal = new LayoutItemPortal();
523 Relationship relationship = libglomLayoutItemPortal.get_relationship();
524 if (relationship != null) {
525 layoutItemPortal.setNavigationType(convertToGWTGlomNavigationType(libglomLayoutItemPortal
526 .get_navigation_type()));
528 layoutItemPortal.setTitle(libglomLayoutItemPortal.get_title_used("")); // parent title not
530 LayoutGroup tempLayoutGroup = getListLayoutGroup(tableName, libglomLayoutItemPortal);
531 for (org.glom.web.shared.layout.LayoutItem item : tempLayoutGroup.getItems()) {
532 // TODO EDITING If the relationship does not allow editing, then mark all these fields as
533 // non-editable. Check relationship.get_allow_edit() to see if it's editable.
534 layoutItemPortal.addItem(item);
536 layoutItemPortal.setPrimaryKeyIndex(tempLayoutGroup.getPrimaryKeyIndex());
537 layoutItemPortal.setHiddenPrimaryKey(tempLayoutGroup.hasHiddenPrimaryKey());
538 layoutItemPortal.setName(libglomLayoutItemPortal.get_relationship_name_used());
539 layoutItemPortal.setTableName(relationship.get_from_table());
540 layoutItemPortal.setFromField(relationship.get_from_field());
542 // Set whether or not the related list will need to show the navigation buttons.
543 // This was ported from Glom: Box_Data_Portal::get_has_suitable_record_to_view_details()
544 StringBuffer navigationTableName = new StringBuffer();
545 LayoutItem_Field navigationRelationship = new LayoutItem_Field(); // Ignored.
546 libglomLayoutItemPortal.get_suitable_table_to_view_details(navigationTableName, navigationRelationship,
548 layoutItemPortal.setAddNavigation(!(navigationTableName.toString().isEmpty()));
551 // Note: An empty LayoutItemPortal is returned if relationship is null.
552 return layoutItemPortal;
555 private GlomNumericFormat convertNumbericFormat(NumericFormat libglomNumericFormat) {
556 GlomNumericFormat gnf = new GlomNumericFormat();
557 gnf.setUseAltForegroundColourForNegatives(libglomNumericFormat.get_alt_foreground_color_for_negatives());
558 gnf.setCurrencyCode(libglomNumericFormat.get_currency_symbol());
559 gnf.setDecimalPlaces(Utils.safeLongToInt(libglomNumericFormat.get_decimal_places()));
560 gnf.setDecimalPlacesRestricted(libglomNumericFormat.get_decimal_places_restricted());
561 gnf.setUseThousandsSeparator(libglomNumericFormat.get_use_thousands_separator());
565 private Formatting convertFormatting(FieldFormatting libglomFormatting) {
566 Formatting formatting = new Formatting();
568 // horizontal alignment
569 Formatting.HorizontalAlignment horizontalAlignment;
570 switch (libglomFormatting.get_horizontal_alignment()) {
571 case HORIZONTAL_ALIGNMENT_LEFT:
572 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
573 case HORIZONTAL_ALIGNMENT_RIGHT:
574 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
575 case HORIZONTAL_ALIGNMENT_AUTO:
577 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_AUTO;
579 formatting.setHorizontalAlignment(horizontalAlignment);
582 String foregroundColour = libglomFormatting.get_text_format_color_foreground();
583 if (foregroundColour != null && !foregroundColour.isEmpty())
584 formatting.setTextFormatColourForeground(convertGdkColorToHtmlColour(foregroundColour));
585 String backgroundColour = libglomFormatting.get_text_format_color_background();
586 if (backgroundColour != null && !backgroundColour.isEmpty())
587 formatting.setTextFormatColourBackground(convertGdkColorToHtmlColour(backgroundColour));
590 if (libglomFormatting.get_text_format_multiline()) {
591 formatting.setTextFormatMultilineHeightLines(Utils.safeLongToInt(libglomFormatting
592 .get_text_format_multiline_height_lines()));
598 private LayoutItemField convertToGWTGlomLayoutItemField(LayoutItem_Field libglomLayoutItemField,
599 boolean includeFormatting) {
600 LayoutItemField layoutItemField = new LayoutItemField();
603 layoutItemField.setType(convertToGWTGlomFieldType(libglomLayoutItemField.get_glom_type()));
605 // set title and name
606 layoutItemField.setTitle(libglomLayoutItemField.get_title_or_name());
607 layoutItemField.setName(libglomLayoutItemField.get_name());
609 if (includeFormatting) {
610 FieldFormatting glomFormatting = libglomLayoutItemField.get_formatting_used();
611 Formatting formatting = convertFormatting(glomFormatting);
613 // create a GlomNumericFormat DTO for numeric values
614 if (libglomLayoutItemField.get_glom_type() == org.glom.libglom.Field.glom_field_type.TYPE_NUMERIC) {
615 formatting.setGlomNumericFormat(convertNumbericFormat(glomFormatting.get_numeric_format()));
617 layoutItemField.setFormatting(formatting);
620 return layoutItemField;
624 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
625 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
626 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
629 private LayoutItemField.GlomFieldType convertToGWTGlomFieldType(Field.glom_field_type type) {
632 return LayoutItemField.GlomFieldType.TYPE_BOOLEAN;
634 return LayoutItemField.GlomFieldType.TYPE_DATE;
636 return LayoutItemField.GlomFieldType.TYPE_IMAGE;
638 return LayoutItemField.GlomFieldType.TYPE_NUMERIC;
640 return LayoutItemField.GlomFieldType.TYPE_TEXT;
642 return LayoutItemField.GlomFieldType.TYPE_TIME;
644 Log.info("Returning TYPE_INVALID.");
645 return LayoutItemField.GlomFieldType.TYPE_INVALID;
647 Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
648 + type.toString() + ". Returning " + LayoutItemField.GlomFieldType.TYPE_INVALID.toString() + ".");
649 return LayoutItemField.GlomFieldType.TYPE_INVALID;
654 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
655 * significant 8-bits in each channel.
657 private String convertGdkColorToHtmlColour(String gdkColor) {
658 if (gdkColor.length() == 13)
659 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
660 else if (gdkColor.length() == 7) {
661 // This shouldn't happen but let's deal with it if it does.
663 "Expected a 13 character string but received a 7 character string. Returning received string.");
666 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
672 * This method converts a LayoutItem_Portal.navigation_type from java-libglom to the equivalent
673 * LayoutItemPortal.NavigationType from Online Glom. This conversion is required because the LayoutItem_Portal class
674 * from java-libglom can't be used with GWT-RPC. An enum identical to LayoutItem_Portal.navigation_type from
675 * java-libglom is included in the LayoutItemPortal data transfer object.
677 private LayoutItemPortal.NavigationType convertToGWTGlomNavigationType(
678 LayoutItem_Portal.navigation_type navigationType) {
679 switch (navigationType) {
680 case NAVIGATION_NONE:
681 return LayoutItemPortal.NavigationType.NAVIGATION_NONE;
682 case NAVIGATION_AUTOMATIC:
683 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
684 case NAVIGATION_SPECIFIC:
685 return LayoutItemPortal.NavigationType.NAVIGATION_SPECIFIC;
687 Log.error("Recieved an unknown NavigationType: " + LayoutItem_Portal.navigation_type.class.getName() + "."
688 + navigationType.toString() + ". Returning " + LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC
690 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
695 * Gets the table name to use when accessing the database and the document. This method guards against SQL injection
696 * attacks by returning the default table if the requested table is not in the database or if the table name has not
700 * The table name to validate.
701 * @return The table name to use.
703 private String getTableNameToUse(String tableName) {
704 if (tableName == null || tableName.isEmpty() || !document.get_table_is_known(tableName)) {
705 return document.get_default_table();