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.DriverManager;
25 import java.sql.SQLException;
26 import java.util.ArrayList;
27 import java.util.Hashtable;
28 import java.util.List;
29 import java.util.Locale;
31 import org.apache.commons.lang3.StringUtils;
32 import org.glom.web.server.database.DetailsDBAccess;
33 import org.glom.web.server.database.ListViewDBAccess;
34 import org.glom.web.server.database.RelatedListDBAccess;
35 import org.glom.web.server.database.RelatedListNavigation;
36 import org.glom.web.server.libglom.Document;
37 import org.glom.web.shared.DataItem;
38 import org.glom.web.shared.DocumentInfo;
39 import org.glom.web.shared.NavigationRecord;
40 import org.glom.web.shared.Reports;
41 import org.glom.web.shared.TypedDataItem;
42 import org.glom.web.shared.libglom.CustomTitle;
43 import org.glom.web.shared.libglom.Field;
44 import org.glom.web.shared.libglom.Relationship;
45 import org.glom.web.shared.libglom.Report;
46 import org.glom.web.shared.libglom.Translatable;
47 import org.glom.web.shared.libglom.layout.LayoutGroup;
48 import org.glom.web.shared.libglom.layout.LayoutItem;
49 import org.glom.web.shared.libglom.layout.LayoutItemField;
50 import org.glom.web.shared.libglom.layout.LayoutItemImage;
51 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
52 import org.glom.web.shared.libglom.layout.UsesRelationship;
54 import com.mchange.v2.c3p0.ComboPooledDataSource;
57 * A class to hold configuration information for a given Glom document. This class retrieves layout information from
58 * libglom and data from the underlying PostgreSQL database.
60 final class ConfiguredDocument {
62 private Document document;
63 private ComboPooledDataSource cpds;
64 private boolean authenticated = false;
65 private String documentID = "";
66 private String defaultLocaleID = "";
68 private static class LayoutLocaleMap extends Hashtable<String, List<LayoutGroup>> {
69 private static final long serialVersionUID = 6542501521673767267L;
72 private static class TableLayouts {
73 public LayoutLocaleMap listLayouts;
74 public LayoutLocaleMap detailsLayouts;
77 private static class TableLayoutsForLocale extends Hashtable<String, TableLayouts> {
78 private static final long serialVersionUID = -1947929931925049013L;
80 public LayoutGroup getListLayout(final String tableName, final String locale) {
81 final List<LayoutGroup> groups = getLayout(tableName, locale, false);
86 if (groups.isEmpty()) {
93 public List<LayoutGroup> getDetailsLayout(final String tableName, final String locale) {
94 return getLayout(tableName, locale, true);
97 public void setListLayout(final String tableName, final String locale, final LayoutGroup layout) {
98 final List<LayoutGroup> list = new ArrayList<LayoutGroup>();
100 setLayout(tableName, locale, list, false);
103 public void setDetailsLayout(final String tableName, final String locale, final List<LayoutGroup> layout) {
104 setLayout(tableName, locale, layout, true);
107 private List<LayoutGroup> getLayout(final String tableName, final String locale, final boolean details) {
108 final LayoutLocaleMap map = getMap(tableName, details);
114 return map.get(locale);
117 private LayoutLocaleMap getMap(final String tableName, final boolean details) {
118 final TableLayouts tableLayouts = get(tableName);
119 if (tableLayouts == null) {
123 LayoutLocaleMap map = null;
125 map = tableLayouts.detailsLayouts;
127 map = tableLayouts.listLayouts;
133 private LayoutLocaleMap getMapWithAdd(final String tableName, final boolean details) {
134 TableLayouts tableLayouts = get(tableName);
135 if (tableLayouts == null) {
136 tableLayouts = new TableLayouts();
137 put(tableName, tableLayouts);
140 LayoutLocaleMap map = null;
142 if (tableLayouts.detailsLayouts == null) {
143 tableLayouts.detailsLayouts = new LayoutLocaleMap();
146 map = tableLayouts.detailsLayouts;
148 if (tableLayouts.listLayouts == null) {
149 tableLayouts.listLayouts = new LayoutLocaleMap();
152 map = tableLayouts.listLayouts;
158 private void setLayout(final String tableName, final String locale, final List<LayoutGroup> layout,
159 final boolean details) {
160 final LayoutLocaleMap map = getMapWithAdd(tableName, details);
162 map.put(locale, layout);
167 private final TableLayoutsForLocale mapTableLayouts = new TableLayoutsForLocale();
169 @SuppressWarnings("unused")
170 private ConfiguredDocument() {
171 // disable default constructor
174 ConfiguredDocument(final Document document) throws PropertyVetoException {
176 // load the jdbc driver
177 cpds = createAndSetupDataSource(document);
179 this.document = document;
186 private static ComboPooledDataSource createAndSetupDataSource(final Document document) {
187 final ComboPooledDataSource cpds = new ComboPooledDataSource();
189 // We don't support sqlite or self-hosting yet.
190 if ((document.getHostingMode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL)
191 && (document.getHostingMode() != Document.HostingMode.HOSTING_MODE_POSTGRES_SELF)) {
192 // TODO: We allow self-hosting here, for testing,
193 // but maybe the startup of self-hosting should happen here.
194 Log.fatal("Error configuring the database connection." + " Only PostgreSQL hosting is supported.");
195 // FIXME: Throw exception?
199 cpds.setDriverClass("org.postgresql.Driver");
200 } catch (final PropertyVetoException e) {
201 Log.fatal("Error loading the PostgreSQL JDBC driver."
202 + " Is the PostgreSQL JDBC jar available to the servlet?", e);
206 // setup the JDBC driver for the current glom document
207 String jdbcURL = "jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort();
209 String db = document.getConnectionDatabase();
210 if (StringUtils.isEmpty(db)) {
211 // Use the default PostgreSQL database, because ComboPooledDataSource.connect() fails otherwise.
214 jdbcURL += "/" + db; // TODO: Quote the database name?
216 cpds.setJdbcUrl(jdbcURL);
222 * Sets the username and password for the database associated with the Glom document.
224 * @return true if the username and password works, false otherwise
226 boolean setUsernameAndPassword(final String username, final String password) throws SQLException {
227 cpds.setUser(username);
228 cpds.setPassword(password);
230 final int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
231 cpds.setAcquireRetryAttempts(1);
232 Connection conn = null;
234 // FIXME find a better way to check authentication
235 // it's possible that the connection could be failing for another reason
237 //Change the timeout, because it otherwise takes ages to fail sometimes when the details are not setup.
238 //This is more than enough.
239 DriverManager.setLoginTimeout(5);
241 conn = cpds.getConnection();
242 authenticated = true;
243 } catch (final SQLException e) {
244 Log.info(Utils.getFileName(document.getFileURI()), e.getMessage());
245 Log.info(Utils.getFileName(document.getFileURI()),
246 "Connection Failed. Maybe the username or password is not correct.");
247 authenticated = false;
252 cpds.setAcquireRetryAttempts(acquireRetryAttempts);
254 return authenticated;
257 Document getDocument() {
261 ComboPooledDataSource getCpds() {
265 boolean isAuthenticated() {
266 return authenticated;
269 String getDocumentID() {
273 void setDocumentID(final String documentID) {
274 this.documentID = documentID;
277 String getDefaultLocaleID() {
278 return defaultLocaleID;
281 void setDefaultLocaleID(final String localeID) {
282 this.defaultLocaleID = localeID;
288 DocumentInfo getDocumentInfo(final String localeID) {
289 final DocumentInfo documentInfo = new DocumentInfo();
291 // get arrays of table names and titles, and find the default table index
292 final List<String> tablesVec = document.getTableNames();
294 final int numTables = Utils.safeLongToInt(tablesVec.size());
295 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
297 final ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
298 final ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
299 boolean foundDefaultTable = false;
300 int visibleIndex = 0;
301 for (int i = 0; i < numTables; i++) {
302 final String tableName = tablesVec.get(i);
303 if (!document.getTableIsHidden(tableName)) {
304 tableNames.add(tableName);
306 //The comparison will only be called if we haven't already found the default table
307 if (!foundDefaultTable && tableName.equals(document.getDefaultTable())) {
308 documentInfo.setDefaultTableIndex(visibleIndex);
309 foundDefaultTable = true;
311 tableTitles.add(document.getTableTitle(tableName, localeID));
316 // set everything we need
317 documentInfo.setTableNames(tableNames);
318 documentInfo.setTableTitles(tableTitles);
319 documentInfo.setTitle(document.getDatabaseTitle(localeID));
321 // Fetch arrays of locale IDs and titles:
322 final List<String> localesVec = document.getTranslationAvailableLocales();
323 final int numLocales = Utils.safeLongToInt(localesVec.size());
324 final ArrayList<String> localeIDs = new ArrayList<String>(numLocales);
325 final ArrayList<String> localeTitles = new ArrayList<String>(numLocales);
326 for (int i = 0; i < numLocales; i++) {
327 final String this_localeID = localesVec.get(i);
328 localeIDs.add(this_localeID);
330 // Use java.util.Locale to get a title for the locale:
331 final String[] locale_parts = this_localeID.split("_");
332 String locale_lang = this_localeID;
333 if (locale_parts.length > 0) {
334 locale_lang = locale_parts[0];
336 String locale_country = "";
337 if (locale_parts.length > 1) {
338 locale_country = locale_parts[1];
341 final Locale locale = new Locale(locale_lang, locale_country);
342 final String title = locale.getDisplayName(locale);
343 localeTitles.add(title);
345 documentInfo.setLocaleIDs(localeIDs);
346 documentInfo.setLocaleTitles(localeTitles);
352 * Gets the layout group for the list view using the defined layout list in the document or the table fields if
353 * there's no defined layout group for the list view.
355 private LayoutGroup getValidListViewLayoutGroup(final String tableName, final String localeID) {
357 // Try to return a cached version:
358 final LayoutGroup result = mapTableLayouts.getListLayout(tableName, localeID);
359 if (result != null) {
360 updateLayoutGroupExpectedResultSize(result, tableName);
364 final List<LayoutGroup> layoutGroupVec = document.getDataLayoutGroups(Document.LAYOUT_NAME_LIST, tableName);
366 final int listViewLayoutGroupSize = Utils.safeLongToInt(layoutGroupVec.size());
367 LayoutGroup libglomLayoutGroup = null;
368 if (listViewLayoutGroupSize > 0) {
369 // A list layout group is defined.
370 // We use the first group as the list.
371 if (listViewLayoutGroupSize > 1) {
372 Log.warn(documentID, tableName, "The size of the list layout group is greater than 1. "
373 + "Attempting to use the first item for the layout list view.");
376 libglomLayoutGroup = layoutGroupVec.get(0);
378 // A list layout group is *not* defined; we are going make a LayoutGroup from the list of fields.
380 Log.info(documentID, tableName,
381 "A list layout is not defined for this table. Displaying a list layout based on the field list.");
383 final List<Field> fieldsVec = document.getTableFields(tableName);
384 libglomLayoutGroup = new LayoutGroup();
385 for (int i = 0; i < fieldsVec.size(); i++) {
386 final Field field = fieldsVec.get(i);
387 final LayoutItemField layoutItemField = new LayoutItemField();
388 layoutItemField.setFullFieldDetails(field);
389 libglomLayoutGroup.addItem(layoutItemField);
393 // Clone the group and change the clone, to discard unwanted information
394 // (such as translations or binary image data) and to
395 // store some information that we do not want to calculate on the client side.
397 // Note that we don't use clone() here, because that would need clone() implementations
398 // in classes which are also used in the client code (though the clone() methods would
399 // not be used) and that makes the GWT java->javascript compilation fail.
400 final LayoutGroup cloned = (LayoutGroup) Utils.deepCopy(libglomLayoutGroup);
401 if (cloned != null) {
402 updateTopLevelListLayoutGroup(cloned, tableName, localeID);
404 // Discard unwanted translations so that getTitle(void) returns what we want.
405 updateTitlesForLocale(cloned, localeID);
407 // Discard binary image data:
408 updateLayoutItemImages(cloned);
411 // Store it in the cache for next time.
412 mapTableLayouts.setListLayout(tableName, localeID, cloned);
418 * @param libglomLayoutGroup
420 private void updateTopLevelListLayoutGroup(final LayoutGroup layoutGroup, final String tableName,
421 final String localeID) {
422 final List<LayoutItem> layoutItemsVec = layoutGroup.getItems();
424 int primaryKeyIndex = -1;
426 final int numItems = Utils.safeLongToInt(layoutItemsVec.size());
427 for (int i = 0; i < numItems; i++) {
428 final LayoutItem layoutItem = layoutItemsVec.get(i);
430 if (layoutItem instanceof LayoutItemField) {
431 final LayoutItemField layoutItemField = (LayoutItemField) layoutItem;
432 final Field field = layoutItemField.getFullFieldDetails();
433 if ((field != null) && field.getPrimaryKey()) {
439 // Set the primary key index for the table
440 if (primaryKeyIndex < 0) {
441 // Add a LayoutItemField for the primary key to the end of the item list in the LayoutGroup because it
442 // doesn't already contain a primary key.
443 Field primaryKey = null;
444 final List<Field> fieldsVec = document.getTableFields(tableName);
445 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
446 final Field field = fieldsVec.get(i);
447 if (field.getPrimaryKey()) {
453 if (primaryKey != null) {
454 final LayoutItemField layoutItemField = new LayoutItemField();
455 layoutItemField.setName(primaryKey.getName());
456 layoutItemField.setFullFieldDetails(primaryKey);
457 layoutGroup.addItem(layoutItemField);
458 layoutGroup.setPrimaryKeyIndex(layoutGroup.getItems().size() - 1);
459 layoutGroup.setHiddenPrimaryKey(true);
461 Log.error(document.getDatabaseTitleOriginal(), tableName,
462 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
465 layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
469 private void updateLayoutGroupExpectedResultSize(final LayoutGroup layoutGroup, final String tableName) {
470 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
472 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
481 * @param useSortClause
482 * @param sortColumnIndex
483 * The index of the column to sort by, or -1 for none.
487 ArrayList<DataItem[]> getListViewData(String tableName, final String quickFind, final int start, final int length,
488 final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
489 // Validate the table name.
490 tableName = getTableNameToUse(tableName);
492 // Get the LayoutGroup that represents the list view.
493 // TODO: Performance: Avoid calling this again:
494 final LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName, "" /* irrelevant locale */);
496 // Create a database access object for the list view.
497 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
501 return listViewDBAccess.getData(quickFind, start, length, sortColumnIndex, isAscending);
504 DataItem[] getDetailsData(String tableName, final TypedDataItem primaryKeyValue) {
505 // Validate the table name.
506 tableName = getTableNameToUse(tableName);
508 setDataItemType(tableName, primaryKeyValue);
510 final DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
512 return detailsDBAccess.getData(primaryKeyValue);
516 * This make sure that the primary key value knows its actual type,
517 * in case it was received via a URL parameter as a string representation:
519 * @param tableName The name of the table for which this is the primary key value
520 * @param primaryKeyValue The TypedDataItem to be changes.
522 private void setDataItemType(final String tableName, final TypedDataItem primaryKeyValue) {
524 if(primaryKeyValue.isUnknownType()) {
525 final Field primaryKeyField = document.getTablePrimaryKeyField(tableName);
526 if(primaryKeyField == null) {
527 Log.error(document.getDatabaseTitleOriginal(), tableName,
528 "Could not find the primary key.");
531 Utils.transformUnknownToActualType(primaryKeyValue, primaryKeyField.getGlomType());
539 * @param foreignKeyValue
542 * @param sortColumnIndex
543 * The index of the column to sort by, or -1 for none.
547 ArrayList<DataItem[]> getRelatedListData(String tableName, final LayoutItemPortal portal,
548 final TypedDataItem foreignKeyValue, final int start, final int length, final int sortColumnIndex,
549 final boolean isAscending) {
550 if (portal == null) {
551 Log.error("getRelatedListData(): portal is null");
555 // Validate the table name.
556 tableName = getTableNameToUse(tableName);
558 // Create a database access object for the related list
559 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
563 return relatedListDBAccess.getData(start, length, foreignKeyValue, sortColumnIndex, isAscending);
566 List<LayoutGroup> getDetailsLayoutGroup(String tableName, final String localeID) {
567 // Validate the table name.
568 tableName = getTableNameToUse(tableName);
570 // Try to return a cached version:
571 final List<LayoutGroup> result = mapTableLayouts.getDetailsLayout(tableName, localeID);
572 if (result != null) {
573 updatePortalsExtras(result, tableName); // Update expected results sizes.
577 final List<LayoutGroup> listGroups = document.getDataLayoutGroups(Document.LAYOUT_NAME_DETAILS, tableName);
579 // Clone the group and change the clone, to discard unwanted information
580 // (such as translations or binary image data)
581 // and to store some information that we do not want to calculate on the client side.
583 // Note that we don't use clone() here, because that would need clone() implementations
584 // in classes which are also used in the client code (though the clone() methods would
585 // not be used) and that makes the GWT java->javascript compilation fail.
586 final List<LayoutGroup> listCloned = new ArrayList<LayoutGroup>();
587 for (final LayoutGroup group : listGroups) {
588 final LayoutGroup cloned = (LayoutGroup) Utils.deepCopy(group);
589 if (cloned != null) {
590 listCloned.add(cloned);
594 updatePortalsExtras(listCloned, tableName);
595 updateFieldsExtras(listCloned, tableName);
597 // Discard unwanted translations so that getTitle(void) returns what we want.
598 updateTitlesForLocale(listCloned, localeID);
600 // Discard binary image data:
601 updateLayoutItemImages(listCloned);
603 // Store it in the cache for next time.
604 mapTableLayouts.setDetailsLayout(tableName, localeID, listCloned);
609 /** Discard binary image data.
612 private void updateLayoutItemImages(final List<LayoutGroup> listGroups) {
613 for (final LayoutGroup group : listGroups) {
614 updateLayoutItemImages(group);
619 /** Discard binary image data.
622 private void updateLayoutItemImages(final LayoutGroup group) {
623 final List<LayoutItem> childItems = group.getItems();
624 for (final LayoutItem item : childItems) {
625 if (item instanceof LayoutGroup) {
627 final LayoutGroup childGroup = (LayoutGroup) item;
628 updateLayoutItemImages(childGroup);
629 } else if (item instanceof LayoutItemImage) {
630 final LayoutItemImage imageItem = (LayoutItemImage) item;
631 final DataItem image = imageItem.getImage();
632 image.setImageData(null);
633 //The client can now request the full data via the path in DataItem.getImageDataUrl().
643 private void updatePortalsExtras(final List<LayoutGroup> listGroups, final String tableName) {
644 for (final LayoutGroup group : listGroups) {
645 updatePortalsExtras(group, tableName);
654 private void updateFieldsExtras(final List<LayoutGroup> listGroups, final String tableName) {
655 for (final LayoutGroup group : listGroups) {
656 updateFieldsExtras(group, tableName);
664 private void updateTitlesForLocale(final List<LayoutGroup> listGroups, final String localeID) {
665 for (final LayoutGroup group : listGroups) {
666 updateTitlesForLocale(group, localeID);
670 private void updatePortalsExtras(final LayoutGroup group, final String tableName) {
671 if (group instanceof LayoutItemPortal) {
672 final LayoutItemPortal portal = (LayoutItemPortal) group;
673 final String tableNameUsed = portal.getTableUsed(tableName);
674 updateLayoutGroupExpectedResultSize(portal, tableNameUsed);
676 //Do not add a primary key field if there is already one:
677 if(portal.getPrimaryKeyIndex() != -1 )
680 final Relationship relationship = portal.getRelationship();
681 if (relationship != null) {
683 // Cache the navigation information:
684 // layoutItemPortal.set_name(libglomLayoutItemPortal.get_relationship_name_used());
685 // layoutItemPortal.setTableName(relationship.get_from_table());
686 // layoutItemPortal.setFromField(relationship.get_from_field());
688 // get the primary key for the related list table
689 final String toTableName = relationship.getToTable();
690 if (!StringUtils.isEmpty(toTableName)) {
692 // get the LayoutItemField with details from its Field in the document
693 final List<Field> fields = document.getTableFields(toTableName); // TODO_Performance: Cache this.
694 for (final Field field : fields) {
695 // check the names to see if they're the same
696 if (field.getPrimaryKey()) {
697 final LayoutItemField layoutItemField = new LayoutItemField();
698 layoutItemField.setName(field.getName());
699 layoutItemField.setFullFieldDetails(field);
700 portal.addItem(layoutItemField);
701 portal.setPrimaryKeyIndex(portal.getItems().size() - 1);
702 portal.setHiddenPrimaryKey(true); // always hidden in portals
711 final List<LayoutItem> childItems = group.getItems();
712 for (final LayoutItem item : childItems) {
713 if (item instanceof LayoutGroup) {
714 final LayoutGroup childGroup = (LayoutGroup) item;
715 updatePortalsExtras(childGroup, tableName);
721 private void updateFieldsExtras(final LayoutGroup group, final String tableName) {
723 final List<LayoutItem> childItems = group.getItems();
724 for (final LayoutItem item : childItems) {
725 if (item instanceof LayoutGroup) {
727 final LayoutGroup childGroup = (LayoutGroup) item;
728 updateFieldsExtras(childGroup, tableName);
729 } else if (item instanceof LayoutItemField) {
730 final LayoutItemField field = (LayoutItemField) item;
732 // Set whether the field should have a navigation button,
733 // because it identifies a related record.
734 final String navigationTableName = document.getLayoutItemFieldShouldHaveNavigation(tableName, field);
735 if (navigationTableName != null) {
736 field.setNavigationTableName(navigationTableName);
742 private void updateTitlesForLocale(final LayoutGroup group, final String localeID) {
744 updateItemTitlesForLocale(group, localeID);
746 final List<LayoutItem> childItems = group.getItems();
747 for (final LayoutItem item : childItems) {
749 // Call makeTitleOriginal on all Translatable items and all special
750 // Translatable items that they use:
751 if (item instanceof LayoutItemField) {
752 final LayoutItemField layoutItemField = (LayoutItemField) item;
754 final Field field = layoutItemField.getFullFieldDetails();
756 field.makeTitleOriginal(localeID);
759 final CustomTitle customTitle = layoutItemField.getCustomTitle();
760 if (customTitle != null) {
761 customTitle.makeTitleOriginal(localeID);
765 updateItemTitlesForLocale(item, localeID);
767 if (item instanceof LayoutGroup) {
769 final LayoutGroup childGroup = (LayoutGroup) item;
770 updateTitlesForLocale(childGroup, localeID);
775 private void updateItemTitlesForLocale(final LayoutItem item, final String localeID) {
776 if (item instanceof UsesRelationship) {
777 final UsesRelationship usesRelationship = (UsesRelationship) item;
778 final Relationship rel = usesRelationship.getRelationship();
781 rel.makeTitleOriginal(localeID);
784 final Relationship relatedRel = usesRelationship.getRelatedRelationship();
785 if (relatedRel != null) {
786 relatedRel.makeTitleOriginal(localeID);
790 if (item instanceof Translatable) {
791 final Translatable translatable = item;
792 translatable.makeTitleOriginal(localeID);
797 * Gets the expected row count for a related list.
799 int getRelatedListRowCount(String tableName, final LayoutItemPortal portal, final TypedDataItem foreignKeyValue) {
800 if (portal == null) {
801 Log.error("getRelatedListData(): portal is null");
805 // Validate the table name.
806 tableName = getTableNameToUse(tableName);
808 // Create a database access object for the related list
809 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
812 // Return the row count
813 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
816 NavigationRecord getSuitableRecordToViewDetails(String tableName, final LayoutItemPortal portal,
817 final TypedDataItem primaryKeyValue) {
818 // Validate the table name.
819 tableName = getTableNameToUse(tableName);
821 setDataItemType(tableName, primaryKeyValue);
823 final RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds,
826 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
829 LayoutGroup getListViewLayoutGroup(String tableName, final String localeID) {
830 // Validate the table name.
831 tableName = getTableNameToUse(tableName);
832 return getValidListViewLayoutGroup(tableName, localeID);
836 * Gets the table name to use when accessing the database and the document. This method guards against SQL injection
837 * attacks by returning the default table if the requested table is not in the database or if the table name has not
841 * The table name to validate.
842 * @return The table name to use.
844 private String getTableNameToUse(final String tableName) {
845 if (StringUtils.isEmpty(tableName) || !document.getTableIsKnown(tableName)) {
846 return document.getDefaultTable();
856 public Reports getReports(final String tableName, final String localeID) {
857 final Reports result = new Reports();
859 final List<String> names = document.getReportNames(tableName);
861 final int count = Utils.safeLongToInt(names.size());
862 for (int i = 0; i < count; i++) {
863 final String name = names.get(i);
864 final Report report = document.getReport(tableName, name);
865 if (report == null) {
869 final String title = report.getTitle(localeID);
870 result.addReport(name, title);