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;
26 import java.util.Locale;
28 import org.glom.libglom.Document;
29 import org.glom.libglom.Field;
30 import org.glom.libglom.FieldFormatting;
31 import org.glom.libglom.FieldVector;
32 import org.glom.libglom.Glom;
33 import org.glom.libglom.LayoutGroupVector;
34 import org.glom.libglom.LayoutItemVector;
35 import org.glom.libglom.LayoutItem_CalendarPortal;
36 import org.glom.libglom.LayoutItem_Field;
37 import org.glom.libglom.LayoutItem_Notebook;
38 import org.glom.libglom.LayoutItem_Portal;
39 import org.glom.libglom.NumericFormat;
40 import org.glom.libglom.Relationship;
41 import org.glom.libglom.Report;
42 import org.glom.libglom.StringVector;
43 import org.glom.web.server.database.DetailsDBAccess;
44 import org.glom.web.server.database.ListViewDBAccess;
45 import org.glom.web.server.database.RelatedListDBAccess;
46 import org.glom.web.server.database.RelatedListNavigation;
47 import org.glom.web.shared.DataItem;
48 import org.glom.web.shared.DocumentInfo;
49 import org.glom.web.shared.GlomNumericFormat;
50 import org.glom.web.shared.NavigationRecord;
51 import org.glom.web.shared.Reports;
52 import org.glom.web.shared.TypedDataItem;
53 import org.glom.web.shared.layout.Formatting;
54 import org.glom.web.shared.layout.LayoutGroup;
55 import org.glom.web.shared.layout.LayoutItem;
56 import org.glom.web.shared.layout.LayoutItemField;
57 import org.glom.web.shared.layout.LayoutItemNotebook;
58 import org.glom.web.shared.layout.LayoutItemPortal;
60 import com.mchange.v2.c3p0.ComboPooledDataSource;
63 * A class to hold configuration information for a given Glom document. This class retrieves layout information from
64 * libglom and data from the underlying PostgreSQL database.
66 final class ConfiguredDocument {
68 private Document document;
69 private ComboPooledDataSource cpds;
70 private boolean authenticated = false;
71 private String documentID = "";
72 private String defaultLocaleID = "";
74 @SuppressWarnings("unused")
75 private ConfiguredDocument() {
76 // disable default constructor
79 ConfiguredDocument(final Document document) throws PropertyVetoException {
81 // load the jdbc driver
82 cpds = new ComboPooledDataSource();
84 // We don't support sqlite or self-hosting yet.
85 if (document.get_hosting_mode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
86 Log.fatal("Error configuring the database connection." + " Only central PostgreSQL hosting is supported.");
87 // FIXME: Throw exception?
91 cpds.setDriverClass("org.postgresql.Driver");
92 } catch (final PropertyVetoException e) {
93 Log.fatal("Error loading the PostgreSQL JDBC driver."
94 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
98 // setup the JDBC driver for the current glom document
99 cpds.setJdbcUrl("jdbc:postgresql://" + document.get_connection_server() + ":" + document.get_connection_port()
100 + "/" + document.get_connection_database());
102 this.document = document;
106 * Sets the username and password for the database associated with the Glom document.
108 * @return true if the username and password works, false otherwise
110 boolean setUsernameAndPassword(final String username, final String password) throws SQLException {
111 cpds.setUser(username);
112 cpds.setPassword(password);
114 final int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
115 cpds.setAcquireRetryAttempts(1);
116 Connection conn = null;
118 // FIXME find a better way to check authentication
119 // it's possible that the connection could be failing for another reason
120 conn = cpds.getConnection();
121 authenticated = true;
122 } catch (final SQLException e) {
123 Log.info(Utils.getFileName(document.get_file_uri()), e.getMessage());
124 Log.info(Utils.getFileName(document.get_file_uri()),
125 "Connection Failed. Maybe the username or password is not correct.");
126 authenticated = false;
130 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
132 return authenticated;
135 Document getDocument() {
139 ComboPooledDataSource getCpds() {
143 boolean isAuthenticated() {
144 return authenticated;
147 String getDocumentID() {
151 void setDocumentID(final String documentID) {
152 this.documentID = documentID;
155 String getDefaultLocaleID() {
156 return defaultLocaleID;
159 void setDefaultLocaleID(final String localeID) {
160 this.defaultLocaleID = localeID;
166 DocumentInfo getDocumentInfo(final String localeID) {
167 final DocumentInfo documentInfo = new DocumentInfo();
169 // get arrays of table names and titles, and find the default table index
170 final StringVector tablesVec = document.get_table_names();
172 final int numTables = Utils.safeLongToInt(tablesVec.size());
173 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
175 final ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
176 final ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
177 boolean foundDefaultTable = false;
178 int visibleIndex = 0;
179 for (int i = 0; i < numTables; i++) {
180 final String tableName = tablesVec.get(i);
181 if (!document.get_table_is_hidden(tableName)) {
182 tableNames.add(tableName);
183 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
184 if (!foundDefaultTable && tableName.equals(document.get_default_table())) {
185 documentInfo.setDefaultTableIndex(visibleIndex);
186 foundDefaultTable = true;
188 tableTitles.add(document.get_table_title(tableName, localeID));
193 // set everything we need
194 documentInfo.setTableNames(tableNames);
195 documentInfo.setTableTitles(tableTitles);
196 documentInfo.setTitle(document.get_database_title());
198 // Fetch arrays of locale IDs and titles:
199 final StringVector localesVec = document.get_translation_available_locales();
200 final int numLocales = Utils.safeLongToInt(localesVec.size());
201 final ArrayList<String> localeIDs = new ArrayList<String>(numLocales);
202 final ArrayList<String> localeTitles = new ArrayList<String>(numLocales);
203 for (int i = 0; i < numLocales; i++) {
204 final String this_localeID = localesVec.get(i);
205 localeIDs.add(this_localeID);
207 // Use java.util.Locale to get a title for the locale:
208 final String[] locale_parts = this_localeID.split("_");
209 String locale_lang = this_localeID;
210 if (locale_parts.length > 0)
211 locale_lang = locale_parts[0];
212 String locale_country = "";
213 if (locale_parts.length > 1)
214 locale_country = locale_parts[1];
216 final Locale locale = new Locale(locale_lang, locale_country);
217 final String title = locale.getDisplayName(locale);
218 localeTitles.add(title);
220 documentInfo.setLocaleIDs(localeIDs);
221 documentInfo.setLocaleTitles(localeTitles);
227 * Gets the layout group for the list view using the defined layout list in the document or the table fields if
228 * there's no defined layout group for the list view.
230 private org.glom.libglom.LayoutGroup getValidListViewLayoutGroup(final String tableName) {
232 final LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("list", tableName);
234 final int listViewLayoutGroupSize = Utils.safeLongToInt(layoutGroupVec.size());
235 org.glom.libglom.LayoutGroup libglomLayoutGroup = null;
236 if (listViewLayoutGroupSize > 0) {
237 // a list layout group is defined; we can use the first group as the list
238 if (listViewLayoutGroupSize > 1)
239 Log.warn(documentID, tableName, "The size of the list layout group is greater than 1. "
240 + "Attempting to use the first item for the layout list view.");
242 libglomLayoutGroup = layoutGroupVec.get(0);
244 // a list layout group is *not* defined; we are going make a libglom layout group from the list of fields
245 Log.info(documentID, tableName,
246 "A list layout is not defined for this table. Displaying a list layout based on the field list.");
248 final FieldVector fieldsVec = document.get_table_fields(tableName);
249 libglomLayoutGroup = new org.glom.libglom.LayoutGroup();
250 for (int i = 0; i < fieldsVec.size(); i++) {
251 final Field field = fieldsVec.get(i);
252 final LayoutItem_Field layoutItemField = new LayoutItem_Field();
253 layoutItemField.set_full_field_details(field);
254 libglomLayoutGroup.add_item(layoutItemField);
258 return libglomLayoutGroup;
261 ArrayList<DataItem[]> getListViewData(String tableName, final String quickFind, final int start, final int length,
262 final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
263 // Validate the table name.
264 tableName = getTableNameToUse(tableName);
266 // Get the libglom LayoutGroup that represents the list view.
267 final org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
269 // Create a database access object for the list view.
270 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
274 return listViewDBAccess.getData(quickFind, start, length, useSortClause, sortColumnIndex, isAscending);
277 DataItem[] getDetailsData(String tableName, final TypedDataItem primaryKeyValue) {
278 // Validate the table name.
279 tableName = getTableNameToUse(tableName);
281 final DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
283 return detailsDBAccess.getData(primaryKeyValue);
286 ArrayList<DataItem[]> getRelatedListData(String tableName, final String relationshipName,
287 final TypedDataItem foreignKeyValue, final int start, final int length, final boolean useSortClause,
288 final int sortColumnIndex, final boolean isAscending) {
289 // Validate the table name.
290 tableName = getTableNameToUse(tableName);
292 // Create a database access object for the related list
293 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
297 return relatedListDBAccess.getData(start, length, foreignKeyValue, useSortClause, sortColumnIndex, isAscending);
300 ArrayList<LayoutGroup> getDetailsLayoutGroup(String tableName, final String localeID) {
301 // Validate the table name.
302 tableName = getTableNameToUse(tableName);
304 // Get the details layout group information for each LayoutGroup in the LayoutGroupVector
305 final LayoutGroupVector layoutGroupVec = document.get_data_layout_groups("details", tableName);
306 final ArrayList<LayoutGroup> layoutGroups = new ArrayList<LayoutGroup>();
307 for (int i = 0; i < layoutGroupVec.size(); i++) {
308 final org.glom.libglom.LayoutGroup libglomLayoutGroup = layoutGroupVec.get(i);
310 // satisfy the precondition of getDetailsLayoutGroup(String, org.glom.libglom.LayoutGroup)
311 if (libglomLayoutGroup == null)
314 layoutGroups.add(getDetailsLayoutGroup(tableName, libglomLayoutGroup, localeID));
321 * Gets the expected row count for a related list.
323 int getRelatedListRowCount(String tableName, final String relationshipName, final TypedDataItem foreignKeyValue) {
324 // Validate the table name.
325 tableName = getTableNameToUse(tableName);
327 // Create a database access object for the related list
328 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
331 // Return the row count
332 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
335 NavigationRecord getSuitableRecordToViewDetails(String tableName, final String relationshipName,
336 final TypedDataItem primaryKeyValue) {
337 // Validate the table name.
338 tableName = getTableNameToUse(tableName);
340 final RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds,
341 tableName, relationshipName);
343 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
346 LayoutGroup getListViewLayoutGroup(String tableName, final String localeID) {
347 // Validate the table name.
348 tableName = getTableNameToUse(tableName);
350 final org.glom.libglom.LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName);
352 final LayoutGroup layoutGroup = new LayoutGroup(); // the object that will be returned
353 int primaryKeyIndex = -1;
355 // look at each child item
356 final LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
357 final int numItems = Utils.safeLongToInt(layoutItemsVec.size());
358 for (int i = 0; i < numItems; i++) {
359 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
361 // TODO add support for other LayoutItems (Text, Image, Button etc.)
362 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
363 if (libglomLayoutItemField != null) {
364 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, localeID, false));
365 final Field field = libglomLayoutItemField.get_full_field_details();
366 if (field.get_primary_key())
369 Log.info(documentID, tableName,
370 "Ignoring unknown list LayoutItem of type " + libglomLayoutItem.get_part_type_name() + ".");
375 // set the expected result size for list view tables
376 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
378 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
380 // Set the primary key index for the table
381 if (primaryKeyIndex < 0) {
382 // Add a LayoutItemField for the primary key to the end of the item list in the LayoutGroup because it
383 // doesn't already contain a primary key.
384 Field primaryKey = null;
385 final FieldVector fieldsVec = document.get_table_fields(tableName);
386 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
387 final Field field = fieldsVec.get(i);
388 if (field.get_primary_key()) {
393 if (primaryKey != null) {
394 final LayoutItem_Field libglomLayoutItemField = new LayoutItem_Field();
395 libglomLayoutItemField.set_full_field_details(primaryKey);
396 layoutGroup.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, localeID, false));
397 layoutGroup.setPrimaryKeyIndex(layoutGroup.getItems().size() - 1);
398 layoutGroup.setHiddenPrimaryKey(true);
400 Log.error(document.get_database_title(), tableName,
401 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
405 layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
408 layoutGroup.setTableName(tableName);
414 * Gets a recursively defined Details LayoutGroup DTO for the specified libglom LayoutGroup object. This is used for
415 * getting layout information for the details view.
417 * @param documentID Glom document identifier
419 * @param tableName table name in the specified Glom document
421 * @param libglomLayoutGroup libglom LayoutGroup to convert
423 * @precondition libglomLayoutGroup must not be null
425 * @return {@link LayoutGroup} object that represents the layout for the specified {@link
426 * org.glom.libglom.LayoutGroup}
428 private LayoutGroup getDetailsLayoutGroup(final String tableName,
429 final org.glom.libglom.LayoutGroup libglomLayoutGroup, final String localeID) {
430 final LayoutGroup layoutGroup = new LayoutGroup();
431 layoutGroup.setColumnCount(Utils.safeLongToInt(libglomLayoutGroup.get_columns_count()));
432 final String layoutGroupTitle = libglomLayoutGroup.get_title(localeID);
433 if (layoutGroupTitle.isEmpty())
434 layoutGroup.setName(libglomLayoutGroup.get_name());
436 layoutGroup.setTitle(layoutGroupTitle);
438 // look at each child item
439 final LayoutItemVector layoutItemsVec = libglomLayoutGroup.get_items();
440 for (int i = 0; i < layoutItemsVec.size(); i++) {
441 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
443 // just a safety check
444 if (libglomLayoutItem == null)
447 org.glom.web.shared.layout.LayoutItem layoutItem = null;
448 final org.glom.libglom.LayoutGroup group = org.glom.libglom.LayoutGroup.cast_dynamic(libglomLayoutItem);
450 // libglomLayoutItem is a LayoutGroup
451 final LayoutItem_Portal libglomLayoutItemPortal = LayoutItem_Portal.cast_dynamic(group);
452 if (libglomLayoutItemPortal != null) {
453 // group is a LayoutItem_Portal
454 final LayoutItemPortal layoutItemPortal = createLayoutItemPortalDTO(tableName,
455 libglomLayoutItemPortal, localeID);
456 if (layoutItemPortal == null)
458 layoutItem = layoutItemPortal;
461 // libglomLayoutItem is a LayoutGroup
462 final LayoutItem_Notebook libglomLayoutItemNotebook = LayoutItem_Notebook.cast_dynamic(group);
463 if (libglomLayoutItemNotebook != null) {
464 // group is a LayoutItem_Notebook
465 final LayoutGroup tempLayoutGroup = getDetailsLayoutGroup(tableName, libglomLayoutItemNotebook,
467 final LayoutItemNotebook layoutItemNotebook = new LayoutItemNotebook();
468 for (final LayoutItem item : tempLayoutGroup.getItems()) {
469 layoutItemNotebook.addItem(item);
471 layoutItemNotebook.setName(tableName);
472 layoutItem = layoutItemNotebook;
474 // group is *not* a LayoutItem_Portal or a LayoutItem_Notebook
475 // recurse into child groups
476 layoutItem = getDetailsLayoutGroup(tableName, group, localeID);
480 // libglomLayoutItem is *not* a LayoutGroup
481 // create LayoutItem DTOs based on the the libglom type
482 // TODO add support for other LayoutItems (Text, Image, Button etc.)
483 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
484 if (libglomLayoutItemField != null) {
486 final LayoutItemField layoutItemField = convertToGWTGlomLayoutItemField(libglomLayoutItemField,
489 // Set the full field details with updated field details from the document.
490 libglomLayoutItemField.set_full_field_details(document.get_field(
491 libglomLayoutItemField.get_table_used(tableName), libglomLayoutItemField.get_name()));
493 // Determine if the field should have a navigation button and set this in the DTO.
494 final Relationship fieldUsedInRelationshipToOne = new Relationship();
495 final boolean addNavigation = Glom.layout_field_should_have_navigation(tableName,
496 libglomLayoutItemField, document, fieldUsedInRelationshipToOne);
497 layoutItemField.setAddNavigation(addNavigation);
499 // Set the the name of the table to navigate to if navigation should be enabled.
501 // It's not possible to directly check if fieldUsedInRelationshipToOne is
502 // null because of the way that the glom_sharedptr macro works. This workaround accomplishes the
504 String tableNameUsed;
506 final Relationship temp = new Relationship();
507 temp.equals(fieldUsedInRelationshipToOne); // this will throw an NPE if
508 // fieldUsedInRelationshipToOne is null
509 // fieldUsedInRelationshipToOne is *not* null
510 tableNameUsed = fieldUsedInRelationshipToOne.get_to_table();
512 } catch (final NullPointerException e) {
513 // fieldUsedInRelationshipToOne is null
514 tableNameUsed = libglomLayoutItemField.get_table_used(tableName);
517 // Set the navigation table name only if it's not different than the current table name.
518 if (!tableName.equals(tableNameUsed)) {
519 layoutItemField.setNavigationTableName(tableNameUsed);
523 layoutItem = layoutItemField;
526 Log.info(documentID, tableName,
527 "Ignoring unknown details LayoutItem of type " + libglomLayoutItem.get_part_type_name()
533 layoutGroup.addItem(layoutItem);
539 private LayoutItemPortal createLayoutItemPortalDTO(final String tableName,
540 final org.glom.libglom.LayoutItem_Portal libglomLayoutItemPortal, final String localeID) {
542 // Ignore LayoutItem_CalendarPortals for now:
543 // https://bugzilla.gnome.org/show_bug.cgi?id=664273
544 final LayoutItem_CalendarPortal liblglomLayoutItemCalendarPortal = LayoutItem_CalendarPortal
545 .cast_dynamic(libglomLayoutItemPortal);
546 if (liblglomLayoutItemCalendarPortal != null)
549 final LayoutItemPortal layoutItemPortal = new LayoutItemPortal();
550 final Relationship relationship = libglomLayoutItemPortal.get_relationship();
551 if (relationship != null) {
552 layoutItemPortal.setNavigationType(convertToGWTGlomNavigationType(libglomLayoutItemPortal
553 .get_navigation_type()));
555 layoutItemPortal.setTitle(libglomLayoutItemPortal.get_title_used("", localeID)); // parent title not
557 layoutItemPortal.setName(libglomLayoutItemPortal.get_relationship_name_used());
558 layoutItemPortal.setTableName(relationship.get_from_table());
559 layoutItemPortal.setFromField(relationship.get_from_field());
561 // convert the portal layout items into LayoutItemField DTOs
562 final LayoutItemVector layoutItemsVec = libglomLayoutItemPortal.get_items();
563 long numItems = layoutItemsVec.size();
564 for (int i = 0; i < numItems; i++) {
565 final org.glom.libglom.LayoutItem libglomLayoutItem = layoutItemsVec.get(i);
567 // TODO add support for other LayoutItems (Text, Image, Button etc.)
568 final LayoutItem_Field libglomLayoutItemField = LayoutItem_Field.cast_dynamic(libglomLayoutItem);
569 if (libglomLayoutItemField != null) {
570 // TODO EDITING If the relationship does not allow editing, then mark all these fields as
571 // non-editable. Check relationship.get_allow_edit() to see if it's editable.
572 layoutItemPortal.addItem(convertToGWTGlomLayoutItemField(libglomLayoutItemField, localeID, false));
574 Log.info(documentID, tableName, "Ignoring unknown related list LayoutItem of type "
575 + libglomLayoutItem.get_part_type_name() + ".");
580 // get the primary key for the related list table
581 final LayoutItem_Field layoutItemField = new LayoutItem_Field();
582 final String toTableName = relationship.get_to_table();
583 if (!toTableName.isEmpty()) {
585 // get the LayoutItem_Feild with details from its Field in the document
586 final FieldVector fields = document.get_table_fields(toTableName);
587 numItems = fields.size(); // reuse loop variable from above
588 for (int i = 0; i < numItems; i++) {
589 final Field field = fields.get(i);
590 // check the names to see if they're the same
591 if (field.get_primary_key()) {
592 layoutItemField.set_full_field_details(field);
593 layoutItemPortal.addItem(convertToGWTGlomLayoutItemField(layoutItemField, localeID, false));
594 layoutItemPortal.setPrimaryKeyIndex(layoutItemPortal.getItems().size() - 1);
595 layoutItemPortal.setHiddenPrimaryKey(true); // always hidden in portals
601 // Set whether or not the related list will need to show the navigation buttons.
602 // This was ported from Glom: Box_Data_Portal::get_has_suitable_record_to_view_details()
603 final StringBuffer navigationTableName = new StringBuffer();
604 final LayoutItem_Field navigationRelationship = new LayoutItem_Field(); // Ignored.
605 libglomLayoutItemPortal.get_suitable_table_to_view_details(navigationTableName, navigationRelationship,
607 layoutItemPortal.setAddNavigation(!(navigationTableName.toString().isEmpty()));
610 // Note: An empty LayoutItemPortal is returned if relationship is null.
611 return layoutItemPortal;
614 private GlomNumericFormat convertNumbericFormat(final NumericFormat libglomNumericFormat) {
615 final GlomNumericFormat gnf = new GlomNumericFormat();
616 gnf.setUseAltForegroundColourForNegatives(libglomNumericFormat.get_alt_foreground_color_for_negatives());
617 gnf.setCurrencyCode(libglomNumericFormat.get_currency_symbol());
618 gnf.setDecimalPlaces(Utils.safeLongToInt(libglomNumericFormat.get_decimal_places()));
619 gnf.setDecimalPlacesRestricted(libglomNumericFormat.get_decimal_places_restricted());
620 gnf.setUseThousandsSeparator(libglomNumericFormat.get_use_thousands_separator());
624 private Formatting convertFormatting(final FieldFormatting libglomFormatting) {
625 final Formatting formatting = new Formatting();
628 final String foregroundColour = libglomFormatting.get_text_format_color_foreground();
629 if (foregroundColour != null && !foregroundColour.isEmpty())
630 formatting.setTextFormatColourForeground(convertGdkColorToHtmlColour(foregroundColour));
631 final String backgroundColour = libglomFormatting.get_text_format_color_background();
632 if (backgroundColour != null && !backgroundColour.isEmpty())
633 formatting.setTextFormatColourBackground(convertGdkColorToHtmlColour(backgroundColour));
636 if (libglomFormatting.get_text_format_multiline()) {
637 formatting.setTextFormatMultilineHeightLines(Utils.safeLongToInt(libglomFormatting
638 .get_text_format_multiline_height_lines()));
644 private LayoutItemField convertToGWTGlomLayoutItemField(final LayoutItem_Field libglomLayoutItemField,
645 final String localeID, final boolean forDetailsView) {
646 final LayoutItemField layoutItemField = new LayoutItemField();
649 layoutItemField.setType(convertToGWTGlomFieldType(libglomLayoutItemField.get_glom_type()));
651 // set title and name
652 layoutItemField.setTitle(libglomLayoutItemField.get_title_or_name(localeID));
653 layoutItemField.setName(libglomLayoutItemField.get_name());
655 // convert formatting
656 final FieldFormatting glomFormatting = libglomLayoutItemField.get_formatting_used();
657 final Formatting formatting = convertFormatting(glomFormatting);
659 // set horizontal alignment
660 final org.glom.libglom.FieldFormatting.HorizontalAlignment libglomHorizontalAlignment = libglomLayoutItemField
661 .get_formatting_used_horizontal_alignment(forDetailsView); // only returns LEFT or RIGHT
662 Formatting.HorizontalAlignment horizontalAlignment;
663 if (libglomHorizontalAlignment == org.glom.libglom.FieldFormatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT) {
664 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_LEFT;
666 horizontalAlignment = Formatting.HorizontalAlignment.HORIZONTAL_ALIGNMENT_RIGHT;
668 formatting.setHorizontalAlignment(horizontalAlignment);
670 // create a GlomNumericFormat DTO for numeric values
671 if (libglomLayoutItemField.get_glom_type() == org.glom.libglom.Field.glom_field_type.TYPE_NUMERIC) {
672 formatting.setGlomNumericFormat(convertNumbericFormat(glomFormatting.get_numeric_format()));
674 layoutItemField.setFormatting(formatting);
676 return layoutItemField;
680 * This method converts a Field.glom_field_type to the equivalent ColumnInfo.FieldType. The need for this comes from
681 * the fact that the GWT FieldType classes can't be used with RPC and there's no easy way to use the java-libglom
682 * Field.glom_field_type enum with RPC. An enum identical to FieldFormatting.glom_field_type is included in the
685 private LayoutItemField.GlomFieldType convertToGWTGlomFieldType(final Field.glom_field_type type) {
688 return LayoutItemField.GlomFieldType.TYPE_BOOLEAN;
690 return LayoutItemField.GlomFieldType.TYPE_DATE;
692 return LayoutItemField.GlomFieldType.TYPE_IMAGE;
694 return LayoutItemField.GlomFieldType.TYPE_NUMERIC;
696 return LayoutItemField.GlomFieldType.TYPE_TEXT;
698 return LayoutItemField.GlomFieldType.TYPE_TIME;
700 Log.info("Returning TYPE_INVALID.");
701 return LayoutItemField.GlomFieldType.TYPE_INVALID;
703 Log.error("Recieved a type that I don't know about: " + Field.glom_field_type.class.getName() + "."
704 + type.toString() + ". Returning " + LayoutItemField.GlomFieldType.TYPE_INVALID.toString() + ".");
705 return LayoutItemField.GlomFieldType.TYPE_INVALID;
710 * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
711 * significant 8-bits in each channel.
713 private String convertGdkColorToHtmlColour(final String gdkColor) {
714 if (gdkColor.length() == 13)
715 return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
716 else if (gdkColor.length() == 7) {
717 // This shouldn't happen but let's deal with it if it does.
719 "Expected a 13 character string but received a 7 character string. Returning received string.");
722 Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
728 * This method converts a LayoutItem_Portal.navigation_type from java-libglom to the equivalent
729 * LayoutItemPortal.NavigationType from Online Glom. This conversion is required because the LayoutItem_Portal class
730 * from java-libglom can't be used with GWT-RPC. An enum identical to LayoutItem_Portal.navigation_type from
731 * java-libglom is included in the LayoutItemPortal data transfer object.
733 private LayoutItemPortal.NavigationType convertToGWTGlomNavigationType(
734 final LayoutItem_Portal.navigation_type navigationType) {
735 switch (navigationType) {
736 case NAVIGATION_NONE:
737 return LayoutItemPortal.NavigationType.NAVIGATION_NONE;
738 case NAVIGATION_AUTOMATIC:
739 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
740 case NAVIGATION_SPECIFIC:
741 return LayoutItemPortal.NavigationType.NAVIGATION_SPECIFIC;
743 Log.error("Recieved an unknown NavigationType: " + LayoutItem_Portal.navigation_type.class.getName() + "."
744 + navigationType.toString() + ". Returning " + LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC
746 return LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC;
751 * Gets the table name to use when accessing the database and the document. This method guards against SQL injection
752 * attacks by returning the default table if the requested table is not in the database or if the table name has not
756 * The table name to validate.
757 * @return The table name to use.
759 private String getTableNameToUse(final String tableName) {
760 if (tableName == null || tableName.isEmpty() || !document.get_table_is_known(tableName)) {
761 return document.get_default_table();
771 public Reports getReports(final String tableName, final String localeID) {
772 final Reports result = new Reports();
774 final StringVector names = document.get_report_names(tableName);
776 final int count = Utils.safeLongToInt(names.size());
777 for (int i = 0; i < count; i++) {
778 final String name = names.get(i);
779 final Report report = document.get_report(tableName, name);
783 final String title = report.get_title(localeID);
784 result.addReport(name, title);