Fix portal navigation via the Details button.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / ConfiguredDocument.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
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.
10  *
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
14  * for more details.
15  *
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/>.
18  */
19
20 package org.glom.web.server;
21
22 import java.beans.PropertyVetoException;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.IOException;
26 import java.io.ObjectInputStream;
27 import java.io.ObjectOutputStream;
28 import java.sql.Connection;
29 import java.sql.SQLException;
30 import java.util.ArrayList;
31 import java.util.Hashtable;
32 import java.util.List;
33 import java.util.Locale;
34
35 import org.apache.commons.lang3.StringUtils;
36 import org.glom.web.server.database.DetailsDBAccess;
37 import org.glom.web.server.database.ListViewDBAccess;
38 import org.glom.web.server.database.RelatedListDBAccess;
39 import org.glom.web.server.database.RelatedListNavigation;
40 import org.glom.web.server.libglom.Document;
41 import org.glom.web.shared.DataItem;
42 import org.glom.web.shared.DocumentInfo;
43 import org.glom.web.shared.NavigationRecord;
44 import org.glom.web.shared.Reports;
45 import org.glom.web.shared.TypedDataItem;
46 import org.glom.web.shared.libglom.Field;
47 import org.glom.web.shared.libglom.Relationship;
48 import org.glom.web.shared.libglom.Report;
49 import org.glom.web.shared.libglom.layout.LayoutGroup;
50 import org.glom.web.shared.libglom.layout.LayoutItem;
51 import org.glom.web.shared.libglom.layout.LayoutItemCalendarPortal;
52 import org.glom.web.shared.libglom.layout.LayoutItemField;
53 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
54
55 import com.mchange.v2.c3p0.ComboPooledDataSource;
56
57 /**
58  * A class to hold configuration information for a given Glom document. This class retrieves layout information from
59  * libglom and data from the underlying PostgreSQL database.
60  */
61 final class ConfiguredDocument {
62
63         private Document document;
64         private ComboPooledDataSource cpds;
65         private boolean authenticated = false;
66         private String documentID = "";
67         private String defaultLocaleID = "";
68
69         private static class LayoutLocaleMap extends Hashtable<String, List<LayoutGroup>> {
70                 private static final long serialVersionUID = 6542501521673767267L;
71         };
72
73         private static class TableLayouts {
74                 public LayoutLocaleMap listLayouts;
75                 public LayoutLocaleMap detailsLayouts;
76         }
77
78         private static class TableLayoutsForLocale extends Hashtable<String, TableLayouts> {
79                 private static final long serialVersionUID = -1947929931925049013L;
80
81                 public LayoutGroup getListLayout(final String tableName, final String locale) {
82                         final List<LayoutGroup> groups = getLayout(tableName, locale, false);
83                         if(groups == null) {
84                                 return null;
85                         }
86                         
87                         if(groups.isEmpty()) {
88                                 return null;
89                         }
90                         
91                         return groups.get(0);
92                 }
93                 
94                 public List<LayoutGroup> getDetailsLayout(final String tableName, final String locale) {
95                         return getLayout(tableName, locale, true);
96                 }
97                 
98                 public void setListLayout(final String tableName, final String locale, LayoutGroup layout) {
99                         List<LayoutGroup> list = new ArrayList<LayoutGroup>();
100                         list.add(layout);
101                         setLayout(tableName, locale, list, false);
102                 }
103
104                 public void setDetailsLayout(final String tableName, final String locale, final List<LayoutGroup> layout) {
105                         setLayout(tableName, locale, layout, true);
106                 }
107                 
108                 private List<LayoutGroup> getLayout(final String tableName, final String locale, boolean details) {
109                         LayoutLocaleMap map = getMap(tableName, details);
110                         
111                         if(map == null) {
112                                 return null;
113                         }
114                         
115                         return map.get(locale);
116                 }
117
118                 private LayoutLocaleMap getMap(final String tableName, boolean details) {
119                         final TableLayouts tableLayouts = get(tableName);
120                         if(tableLayouts == null) {
121                                 return null;
122                         }
123                         
124                         LayoutLocaleMap map = null;
125                         if(details) {
126                                 map = tableLayouts.detailsLayouts;
127                         } else {
128                                 map = tableLayouts.listLayouts;
129                         }
130
131                         return map;
132                 }
133                 
134                 private LayoutLocaleMap getMapWithAdd(final String tableName, boolean details) {
135                         TableLayouts tableLayouts = get(tableName);
136                         if(tableLayouts == null) {
137                                 tableLayouts = new TableLayouts();
138                                 put(tableName, tableLayouts);
139                         }
140
141                         LayoutLocaleMap map = null;
142                         if (details) {
143                                 if (tableLayouts.detailsLayouts == null) {
144                                         tableLayouts.detailsLayouts = new LayoutLocaleMap();
145                                 }
146
147                                 map = tableLayouts.detailsLayouts;
148                         } else {
149                                 if (tableLayouts.listLayouts == null) {
150                                         tableLayouts.listLayouts = new LayoutLocaleMap();
151                                 }
152
153                                 map = tableLayouts.listLayouts;
154                         }
155
156                         return map;
157                 }
158                 
159                 private void setLayout(final String tableName, final String locale, final List<LayoutGroup> layout, boolean details) {
160                         LayoutLocaleMap map = getMapWithAdd(tableName, details);
161                         if (map != null) {
162                                 map.put(locale, layout);
163                         }
164                 }
165         }
166         
167         private TableLayoutsForLocale mapTableLayouts = new TableLayoutsForLocale();
168
169         @SuppressWarnings("unused")
170         private ConfiguredDocument() {
171                 // disable default constructor
172         }
173
174         ConfiguredDocument(final Document document) throws PropertyVetoException {
175
176                 // load the jdbc driver
177                 cpds = new ComboPooledDataSource();
178
179                 // We don't support sqlite or self-hosting yet.
180                 if (document.getHostingMode() != Document.HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
181                         Log.fatal("Error configuring the database connection." + " Only central PostgreSQL hosting is supported.");
182                         // FIXME: Throw exception?
183                 }
184
185                 try {
186                         cpds.setDriverClass("org.postgresql.Driver");
187                 } catch (final PropertyVetoException e) {
188                         Log.fatal("Error loading the PostgreSQL JDBC driver."
189                                         + " Is the PostgreSQL JDBC jar available to the servlet?", e);
190                         throw e;
191                 }
192
193                 // setup the JDBC driver for the current glom document
194                 cpds.setJdbcUrl("jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort()
195                                 + "/" + document.getConnectionDatabase());
196
197                 this.document = document;
198         }
199
200         /**
201          * Sets the username and password for the database associated with the Glom document.
202          * 
203          * @return true if the username and password works, false otherwise
204          */
205         boolean setUsernameAndPassword(final String username, final String password) throws SQLException {
206                 cpds.setUser(username);
207                 cpds.setPassword(password);
208
209                 final int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
210                 cpds.setAcquireRetryAttempts(1);
211                 Connection conn = null;
212                 try {
213                         // FIXME find a better way to check authentication
214                         // it's possible that the connection could be failing for another reason
215                         conn = cpds.getConnection();
216                         authenticated = true;
217                 } catch (final SQLException e) {
218                         Log.info(Utils.getFileName(document.getFileURI()), e.getMessage());
219                         Log.info(Utils.getFileName(document.getFileURI()),
220                                         "Connection Failed. Maybe the username or password is not correct.");
221                         authenticated = false;
222                 } finally {
223                         if (conn != null)
224                                 conn.close();
225                         cpds.setAcquireRetryAttempts(acquireRetryAttempts);
226                 }
227                 return authenticated;
228         }
229
230         Document getDocument() {
231                 return document;
232         }
233
234         ComboPooledDataSource getCpds() {
235                 return cpds;
236         }
237
238         boolean isAuthenticated() {
239                 return authenticated;
240         }
241
242         String getDocumentID() {
243                 return documentID;
244         }
245
246         void setDocumentID(final String documentID) {
247                 this.documentID = documentID;
248         }
249
250         String getDefaultLocaleID() {
251                 return defaultLocaleID;
252         }
253
254         void setDefaultLocaleID(final String localeID) {
255                 this.defaultLocaleID = localeID;
256         }
257
258         /**
259          * @return
260          */
261         DocumentInfo getDocumentInfo(final String localeID) {
262                 final DocumentInfo documentInfo = new DocumentInfo();
263
264                 // get arrays of table names and titles, and find the default table index
265                 final List<String> tablesVec = document.getTableNames();
266
267                 final int numTables = Utils.safeLongToInt(tablesVec.size());
268                 // we don't know how many tables will be hidden so we'll use half of the number of tables for the default size
269                 // of the ArrayList
270                 final ArrayList<String> tableNames = new ArrayList<String>(numTables / 2);
271                 final ArrayList<String> tableTitles = new ArrayList<String>(numTables / 2);
272                 boolean foundDefaultTable = false;
273                 int visibleIndex = 0;
274                 for (int i = 0; i < numTables; i++) {
275                         final String tableName = tablesVec.get(i);
276                         if (!document.getTableIsHidden(tableName)) {
277                                 tableNames.add(tableName);
278                                 // JNI is "expensive", the comparison will only be called if we haven't already found the default table
279                                 if (!foundDefaultTable && tableName.equals(document.getDefaultTable())) {
280                                         documentInfo.setDefaultTableIndex(visibleIndex);
281                                         foundDefaultTable = true;
282                                 }
283                                 tableTitles.add(document.getTableTitle(tableName, localeID));
284                                 visibleIndex++;
285                         }
286                 }
287
288                 // set everything we need
289                 documentInfo.setTableNames(tableNames);
290                 documentInfo.setTableTitles(tableTitles);
291                 documentInfo.setTitle(document.getDatabaseTitle(localeID));
292
293                 // Fetch arrays of locale IDs and titles:
294                 final List<String> localesVec = document.getTranslationAvailableLocales();
295                 final int numLocales = Utils.safeLongToInt(localesVec.size());
296                 final ArrayList<String> localeIDs = new ArrayList<String>(numLocales);
297                 final ArrayList<String> localeTitles = new ArrayList<String>(numLocales);
298                 for (int i = 0; i < numLocales; i++) {
299                         final String this_localeID = localesVec.get(i);
300                         localeIDs.add(this_localeID);
301
302                         // Use java.util.Locale to get a title for the locale:
303                         final String[] locale_parts = this_localeID.split("_");
304                         String locale_lang = this_localeID;
305                         if (locale_parts.length > 0)
306                                 locale_lang = locale_parts[0];
307                         String locale_country = "";
308                         if (locale_parts.length > 1)
309                                 locale_country = locale_parts[1];
310
311                         final Locale locale = new Locale(locale_lang, locale_country);
312                         final String title = locale.getDisplayName(locale);
313                         localeTitles.add(title);
314                 }
315                 documentInfo.setLocaleIDs(localeIDs);
316                 documentInfo.setLocaleTitles(localeTitles);
317
318                 return documentInfo;
319         }
320
321         /*
322          * Gets the layout group for the list view using the defined layout list in the document or the table fields if
323          * there's no defined layout group for the list view.
324          */
325         private LayoutGroup getValidListViewLayoutGroup(final String tableName, final String localeID) {
326
327                 // Try to return a cached version:
328                 final LayoutGroup result = mapTableLayouts.getListLayout(tableName, localeID);
329                 if (result != null) {
330                         updateLayoutGroupExpectedResultSize(result, tableName);
331                         return result;
332                 }
333
334                 final List<LayoutGroup> layoutGroupVec = document.getDataLayoutGroups("list", tableName);
335
336                 final int listViewLayoutGroupSize = Utils.safeLongToInt(layoutGroupVec.size());
337                 LayoutGroup libglomLayoutGroup = null;
338                 if (listViewLayoutGroupSize > 0) {
339                         // A list layout group is defined.
340                         // We use the first group as the list.
341                         if (listViewLayoutGroupSize > 1)
342                                 Log.warn(documentID, tableName, "The size of the list layout group is greater than 1. "
343                                                 + "Attempting to use the first item for the layout list view.");
344
345                         libglomLayoutGroup = layoutGroupVec.get(0);
346                 } else {
347                         // A list layout group is *not* defined; we are going make a LayoutGroup from the list of fields.
348                         // This is unusual.
349                         Log.info(documentID, tableName,
350                                         "A list layout is not defined for this table. Displaying a list layout based on the field list.");
351
352                         final List<Field> fieldsVec = document.getTableFields(tableName);
353                         libglomLayoutGroup = new LayoutGroup();
354                         for (int i = 0; i < fieldsVec.size(); i++) {
355                                 final Field field = fieldsVec.get(i);
356                                 final LayoutItemField layoutItemField = new LayoutItemField();
357                                 layoutItemField.setFullFieldDetails(field);
358                                 libglomLayoutGroup.addItem(layoutItemField);
359                         }
360                 }
361
362                 // TODO: Clone the group and change the clone, to discard unwanted informatin (such as translations)
363                 //store some information that we do not want to calculate on the client side.
364                 
365                 //Note that we don't use clone() here, because that would need clone() implementations
366                 //in classes which are also used in the client code (though the clone() methods would
367                 //not be used) and that makes the GWT java->javascript compilation fail.
368                 final LayoutGroup cloned = (LayoutGroup) deepCopy(libglomLayoutGroup);
369                 if(cloned != null) {
370                         updateLayoutGroup(cloned, tableName, localeID);
371                 }
372                 
373                 //Store it in the cache for next time.
374                 mapTableLayouts.setListLayout(tableName, localeID, cloned);
375
376                 return cloned;
377         }
378         
379         static public Object deepCopy(Object oldObj)
380            {
381                 ObjectOutputStream oos = null;
382                 ObjectInputStream ois = null;
383
384                 try {
385                         ByteArrayOutputStream bos = 
386                                         new ByteArrayOutputStream(); 
387                         oos = new ObjectOutputStream(bos);
388                         // serialize and pass the object
389                         oos.writeObject(oldObj);   // C
390                         oos.flush();               // D
391                         ByteArrayInputStream bin = 
392                                         new ByteArrayInputStream(bos.toByteArray());
393                         ois = new ObjectInputStream(bin);
394                         // return the new object
395                         return ois.readObject();
396                 } catch(Exception e) {
397                         System.out.println("Exception in deepCopy:" + e);
398                         return null;
399                 } finally {
400                         try {
401                                 oos.close();
402                                 ois.close();
403                         } catch(IOException e) {
404                                 System.out.println("Exception in deepCopy during finally: " + e);
405                                 return null;
406                         }
407                 }
408         }
409
410         /**
411          * @param libglomLayoutGroup
412          */
413         private void updateLayoutGroup(final LayoutGroup layoutGroup, final String tableName, final String localeID) {
414                 final List<LayoutItem> layoutItemsVec = layoutGroup.getItems();
415
416                 int primaryKeyIndex = -1;
417
418                 final int numItems = Utils.safeLongToInt(layoutItemsVec.size());
419                 for (int i = 0; i < numItems; i++) {
420                         final LayoutItem layoutItem = layoutItemsVec.get(i);
421
422                         if (layoutItem instanceof LayoutItemField) {
423                                 LayoutItemField layoutItemField = (LayoutItemField) layoutItem;
424                                 final Field field = layoutItemField.getFullFieldDetails();
425                                 if ((field != null) && field.getPrimaryKey())
426                                         primaryKeyIndex = i;
427                         } else if (layoutItem instanceof LayoutGroup) {
428                                 LayoutGroup childGroup = (LayoutGroup) layoutItem;
429                                 updateLayoutGroup(childGroup, tableName, localeID);
430                         }
431                 }
432
433                 // Set the primary key index for the table
434                 if (primaryKeyIndex < 0) {
435                         // Add a LayoutItemField for the primary key to the end of the item list in the LayoutGroup because it
436                         // doesn't already contain a primary key.
437                         Field primaryKey = null;
438                         final List<Field> fieldsVec = document.getTableFields(tableName);
439                         for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
440                                 final Field field = fieldsVec.get(i);
441                                 if (field.getPrimaryKey()) {
442                                         primaryKey = field;
443                                         break;
444                                 }
445                         }
446
447                         if (primaryKey != null) {
448                                 final LayoutItemField layoutItemField = new LayoutItemField();
449                                 layoutItemField.setFullFieldDetails(primaryKey);
450                                 layoutGroup.addItem(layoutItemField); // TODO: Update the field to show just one locale?
451                                 layoutGroup.setPrimaryKeyIndex(layoutGroup.getItems().size() - 1);
452                                 layoutGroup.setHiddenPrimaryKey(true);
453                         } else {
454                                 Log.error(document.getDatabaseTitleOriginal(), tableName,
455                                                 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
456                         }
457                 } else {
458                         layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
459                 }
460         }
461
462         private void updateLayoutGroupExpectedResultSize(final LayoutGroup layoutGroup, final String tableName) {
463                 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
464                                 layoutGroup);
465                 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
466         }
467
468         ArrayList<DataItem[]> getListViewData(String tableName, final String quickFind, final int start, final int length,
469                         final boolean useSortClause, final int sortColumnIndex, final boolean isAscending) {
470                 // Validate the table name.
471                 tableName = getTableNameToUse(tableName);
472
473                 // Get the LayoutGroup that represents the list view.
474                 // TODO: Performance: Avoid calling this again:
475                 final LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName, "" /* irrelevant locale */);
476
477                 // Create a database access object for the list view.
478                 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
479                                 libglomLayoutGroup);
480
481                 // Return the data.
482                 return listViewDBAccess.getData(quickFind, start, length, useSortClause, sortColumnIndex, isAscending);
483         }
484
485         DataItem[] getDetailsData(String tableName, final TypedDataItem primaryKeyValue) {
486                 // Validate the table name.
487                 tableName = getTableNameToUse(tableName);
488
489                 final DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
490
491                 return detailsDBAccess.getData(primaryKeyValue);
492         }
493
494         ArrayList<DataItem[]> getRelatedListData(String tableName, final String relationshipName,
495                         final TypedDataItem foreignKeyValue, final int start, final int length, final boolean useSortClause,
496                         final int sortColumnIndex, final boolean isAscending) {
497                 if(StringUtils.isEmpty(relationshipName)) {
498                         return null;
499                 }
500
501                 // Validate the table name.
502                 tableName = getTableNameToUse(tableName);
503
504                 // Create a database access object for the related list
505                 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
506                                 relationshipName);
507
508                 // Return the data
509                 return relatedListDBAccess.getData(start, length, foreignKeyValue, useSortClause, sortColumnIndex, isAscending);
510         }
511
512         List<LayoutGroup> getDetailsLayoutGroup(String tableName, final String localeID) {
513                 // Validate the table name.
514                 tableName = getTableNameToUse(tableName);
515
516                 // Try to return a cached version:
517                 final List<LayoutGroup> result = mapTableLayouts.getDetailsLayout(tableName, localeID);
518                 if (result != null) {
519                         updatePortalsExpectedResultsSize(result, tableName);
520                         return result;
521                 }
522
523                 final List<LayoutGroup> listGroups = document.getDataLayoutGroups("details", tableName);
524
525                 // TODO: Clone the group and change the clone, to discard unwanted informatin (such as translations)
526                 // store some information that we do not want to calculate on the client side.
527
528                 // Note that we don't use clone() here, because that would need clone() implementations
529                 // in classes which are also used in the client code (though the clone() methods would
530                 // not be used) and that makes the GWT java->javascript compilation fail.
531                 final List<LayoutGroup> listCloned = new ArrayList<LayoutGroup>();
532                 for (LayoutGroup group : listGroups) {
533                         final LayoutGroup cloned = (LayoutGroup) deepCopy(group);
534                         if (cloned != null) {
535                                 updateLayoutGroup(cloned, tableName, localeID);
536                                 listCloned.add(cloned);
537                         }
538                 }
539
540                 // Store it in the cache for next time.
541                 mapTableLayouts.setDetailsLayout(tableName, localeID, listCloned);
542
543                 updatePortalsExpectedResultsSize(listCloned, tableName);
544
545                 return listCloned;
546         }
547
548         /**
549          * @param result
550          * @param tableName
551          */
552         private void updatePortalsExpectedResultsSize(List<LayoutGroup> listGroups, String tableName) {
553                 for (LayoutGroup group : listGroups) {
554                         updatePortalsExpectedResultsSize(group, tableName);
555                 }
556
557         }
558
559         private void updatePortalsExpectedResultsSize(LayoutGroup group, String tableName) {
560                 if (group instanceof LayoutItemPortal) {
561                         final LayoutItemPortal portal = (LayoutItemPortal) group;
562                         final String tableNameUsed = portal.getTableUsed(tableName);
563                         updateLayoutGroupExpectedResultSize(portal, tableNameUsed);
564                 }
565
566                 List<LayoutItem> childItems = group.getItems();
567                 for (LayoutItem item : childItems) {
568                         if (item instanceof LayoutGroup) {
569                                 final LayoutGroup childGroup = (LayoutGroup) item;
570                                 updatePortalsExpectedResultsSize(childGroup, tableName);
571                         }
572                 }
573
574         }
575
576         /*
577          * Gets the expected row count for a related list.
578          */
579         int getRelatedListRowCount(String tableName, final String relationshipName, final TypedDataItem foreignKeyValue) {
580                 // Validate the table name.
581                 tableName = getTableNameToUse(tableName);
582
583                 // Create a database access object for the related list
584                 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
585                                 relationshipName);
586
587                 // Return the row count
588                 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
589         }
590
591         NavigationRecord getSuitableRecordToViewDetails(String tableName, final String relationshipName,
592                         final TypedDataItem primaryKeyValue) {
593                 // Validate the table name.
594                 tableName = getTableNameToUse(tableName);
595
596                 final RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds,
597                                 tableName, relationshipName);
598
599                 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
600         }
601
602         LayoutGroup getListViewLayoutGroup(String tableName, final String localeID) {
603                 // Validate the table name.
604                 tableName = getTableNameToUse(tableName);
605                 return getValidListViewLayoutGroup(tableName, localeID);
606         }
607
608         /**
609          * Store some cache values in the LayoutItemPortal.
610          * 
611          * @param tableName
612          * @param layoutItemPortal
613          * @param localeID
614          * @return
615          */
616         private void updateLayoutItemPortalDTO(final String tableName, final LayoutItemPortal layoutItemPortal,
617                         final String localeID) {
618
619                 // Ignore LayoutItem_CalendarPortals for now:
620                 // https://bugzilla.gnome.org/show_bug.cgi?id=664273
621                 if (layoutItemPortal instanceof LayoutItemCalendarPortal) {
622                         return;
623                 }
624
625                 final Relationship relationship = layoutItemPortal.getRelationship();
626                 if (relationship != null) {
627                         // layoutItemPortal.set_name(libglomLayoutItemPortal.get_relationship_name_used());
628                         // layoutItemPortal.setTableName(relationship.get_from_table());
629                         // layoutItemPortal.setFromField(relationship.get_from_field());
630
631                         // Set whether or not the related list will need to show the navigation buttons.
632                         // This was ported from Glom: Box_Data_Portal::get_has_suitable_record_to_view_details()
633                         final Document.TableToViewDetails viewDetails = document
634                                         .getPortalSuitableTableToViewDetails(layoutItemPortal);
635                         boolean addNavigation = false;
636                         if (viewDetails != null) {
637                                 addNavigation = !StringUtils.isEmpty(viewDetails.tableName);
638                         }
639                         layoutItemPortal.setAddNavigation(addNavigation);
640                 }
641         }
642
643         /*
644          * Converts a Gdk::Color (16-bits per channel) to an HTML colour (8-bits per channel) by discarding the least
645          * significant 8-bits in each channel.
646          */
647         private String convertGdkColorToHtmlColour(final String gdkColor) {
648                 if (gdkColor.length() == 13)
649                         return gdkColor.substring(0, 3) + gdkColor.substring(5, 7) + gdkColor.substring(9, 11);
650                 else if (gdkColor.length() == 7) {
651                         // This shouldn't happen but let's deal with it if it does.
652                         Log.warn(documentID,
653                                         "Expected a 13 character string but received a 7 character string. Returning received string.");
654                         return gdkColor;
655                 } else {
656                         Log.error("Did not receive a 13 or 7 character string. Returning black HTML colour code.");
657                         return "#000000";
658                 }
659         }
660
661         /**
662          * Gets the table name to use when accessing the database and the document. This method guards against SQL injection
663          * attacks by returning the default table if the requested table is not in the database or if the table name has not
664          * been set.
665          * 
666          * @param tableName
667          *            The table name to validate.
668          * @return The table name to use.
669          */
670         private String getTableNameToUse(final String tableName) {
671                 if (StringUtils.isEmpty(tableName) || !document.getTableIsKnown(tableName)) {
672                         return document.getDefaultTable();
673                 }
674                 return tableName;
675         }
676
677         /**
678          * @param tableName
679          * @param localeID
680          * @return
681          */
682         public Reports getReports(final String tableName, final String localeID) {
683                 final Reports result = new Reports();
684
685                 final List<String> names = document.getReportNames(tableName);
686
687                 final int count = Utils.safeLongToInt(names.size());
688                 for (int i = 0; i < count; i++) {
689                         final String name = names.get(i);
690                         final Report report = document.getReport(tableName, name);
691                         if (report == null)
692                                 continue;
693
694                         final String title = report.getTitle(localeID);
695                         result.addReport(name, title);
696                 }
697
698                 return result;
699         }
700 }