DetailsActivity: Check for authentication here too.
[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.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;
30
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;
53
54 import com.mchange.v2.c3p0.ComboPooledDataSource;
55
56 /**
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.
59  */
60 final class ConfiguredDocument {
61
62         private Document document;
63         private ComboPooledDataSource cpds;
64         private boolean authenticated = false;
65         private String documentID = "";
66         private String defaultLocaleID = "";
67
68         private static class LayoutLocaleMap extends Hashtable<String, List<LayoutGroup>> {
69                 private static final long serialVersionUID = 6542501521673767267L;
70         };
71
72         private static class TableLayouts {
73                 public LayoutLocaleMap listLayouts;
74                 public LayoutLocaleMap detailsLayouts;
75         }
76
77         private static class TableLayoutsForLocale extends Hashtable<String, TableLayouts> {
78                 private static final long serialVersionUID = -1947929931925049013L;
79
80                 public LayoutGroup getListLayout(final String tableName, final String locale) {
81                         final List<LayoutGroup> groups = getLayout(tableName, locale, false);
82                         if (groups == null) {
83                                 return null;
84                         }
85
86                         if (groups.isEmpty()) {
87                                 return null;
88                         }
89
90                         return groups.get(0);
91                 }
92
93                 public List<LayoutGroup> getDetailsLayout(final String tableName, final String locale) {
94                         return getLayout(tableName, locale, true);
95                 }
96
97                 public void setListLayout(final String tableName, final String locale, final LayoutGroup layout) {
98                         final List<LayoutGroup> list = new ArrayList<LayoutGroup>();
99                         list.add(layout);
100                         setLayout(tableName, locale, list, false);
101                 }
102
103                 public void setDetailsLayout(final String tableName, final String locale, final List<LayoutGroup> layout) {
104                         setLayout(tableName, locale, layout, true);
105                 }
106
107                 private List<LayoutGroup> getLayout(final String tableName, final String locale, final boolean details) {
108                         final LayoutLocaleMap map = getMap(tableName, details);
109
110                         if (map == null) {
111                                 return null;
112                         }
113
114                         return map.get(locale);
115                 }
116
117                 private LayoutLocaleMap getMap(final String tableName, final boolean details) {
118                         final TableLayouts tableLayouts = get(tableName);
119                         if (tableLayouts == null) {
120                                 return null;
121                         }
122
123                         LayoutLocaleMap map = null;
124                         if (details) {
125                                 map = tableLayouts.detailsLayouts;
126                         } else {
127                                 map = tableLayouts.listLayouts;
128                         }
129
130                         return map;
131                 }
132
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);
138                         }
139
140                         LayoutLocaleMap map = null;
141                         if (details) {
142                                 if (tableLayouts.detailsLayouts == null) {
143                                         tableLayouts.detailsLayouts = new LayoutLocaleMap();
144                                 }
145
146                                 map = tableLayouts.detailsLayouts;
147                         } else {
148                                 if (tableLayouts.listLayouts == null) {
149                                         tableLayouts.listLayouts = new LayoutLocaleMap();
150                                 }
151
152                                 map = tableLayouts.listLayouts;
153                         }
154
155                         return map;
156                 }
157
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);
161                         if (map != null) {
162                                 map.put(locale, layout);
163                         }
164                 }
165         }
166
167         private final 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 = createAndSetupDataSource(document);
178
179                 this.document = document;
180         }
181
182         /**
183          * @param document
184          * @return
185          */
186         private static ComboPooledDataSource createAndSetupDataSource(final Document document) {
187                 final ComboPooledDataSource cpds = new ComboPooledDataSource();
188
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?
196                 }
197
198                 try {
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);
203                         return null;
204                 }
205
206                 // setup the JDBC driver for the current glom document
207                 String jdbcURL = "jdbc:postgresql://" + document.getConnectionServer() + ":" + document.getConnectionPort();
208
209                 String db = document.getConnectionDatabase();
210                 if (StringUtils.isEmpty(db)) {
211                         // Use the default PostgreSQL database, because ComboPooledDataSource.connect() fails otherwise.
212                         db = "template1";
213                 }
214                 jdbcURL += "/" + db; // TODO: Quote the database name?
215
216                 cpds.setJdbcUrl(jdbcURL);
217
218                 return cpds;
219         }
220
221         /**
222          * Sets the username and password for the database associated with the Glom document.
223          * 
224          * @return true if the username and password works, false otherwise
225          */
226         boolean setUsernameAndPassword(final String username, final String password) throws SQLException {
227                 cpds.setUser(username);
228                 cpds.setPassword(password);
229
230                 final int acquireRetryAttempts = cpds.getAcquireRetryAttempts();
231                 cpds.setAcquireRetryAttempts(1);
232                 Connection conn = null;
233                 try {
234                         // FIXME find a better way to check authentication
235                         // it's possible that the connection could be failing for another reason
236                         
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); 
240                         
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;
248                 } finally {
249                         if (conn != null) {
250                                 conn.close();
251                         }
252                         cpds.setAcquireRetryAttempts(acquireRetryAttempts);
253                 }
254                 return authenticated;
255         }
256
257         Document getDocument() {
258                 return document;
259         }
260
261         ComboPooledDataSource getCpds() {
262                 return cpds;
263         }
264
265         boolean isAuthenticated() {
266                 return authenticated;
267         }
268
269         String getDocumentID() {
270                 return documentID;
271         }
272
273         void setDocumentID(final String documentID) {
274                 this.documentID = documentID;
275         }
276
277         String getDefaultLocaleID() {
278                 return defaultLocaleID;
279         }
280
281         void setDefaultLocaleID(final String localeID) {
282                 this.defaultLocaleID = localeID;
283         }
284
285         /**
286          * @return
287          */
288         DocumentInfo getDocumentInfo(final String localeID) {
289                 final DocumentInfo documentInfo = new DocumentInfo();
290
291                 // get arrays of table names and titles, and find the default table index
292                 final List<String> tablesVec = document.getTableNames();
293
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
296                 // of the ArrayList
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);
305
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;
310                                 }
311                                 tableTitles.add(document.getTableTitle(tableName, localeID));
312                                 visibleIndex++;
313                         }
314                 }
315
316                 // set everything we need
317                 documentInfo.setTableNames(tableNames);
318                 documentInfo.setTableTitles(tableTitles);
319                 documentInfo.setTitle(document.getDatabaseTitle(localeID));
320
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);
329
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];
335                         }
336                         String locale_country = "";
337                         if (locale_parts.length > 1) {
338                                 locale_country = locale_parts[1];
339                         }
340
341                         final Locale locale = new Locale(locale_lang, locale_country);
342                         final String title = locale.getDisplayName(locale);
343                         localeTitles.add(title);
344                 }
345                 documentInfo.setLocaleIDs(localeIDs);
346                 documentInfo.setLocaleTitles(localeTitles);
347
348                 return documentInfo;
349         }
350
351         /*
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.
354          */
355         private LayoutGroup getValidListViewLayoutGroup(final String tableName, final String localeID) {
356
357                 // Try to return a cached version:
358                 final LayoutGroup result = mapTableLayouts.getListLayout(tableName, localeID);
359                 if (result != null) {
360                         updateLayoutGroupExpectedResultSize(result, tableName);
361                         return result;
362                 }
363
364                 final List<LayoutGroup> layoutGroupVec = document.getDataLayoutGroups(Document.LAYOUT_NAME_LIST, tableName);
365
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.");
374                         }
375
376                         libglomLayoutGroup = layoutGroupVec.get(0);
377                 } else {
378                         // A list layout group is *not* defined; we are going make a LayoutGroup from the list of fields.
379                         // This is unusual.
380                         Log.info(documentID, tableName,
381                                         "A list layout is not defined for this table. Displaying a list layout based on the field list.");
382
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);
390                         }
391                 }
392
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.
396
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);
403
404                         // Discard unwanted translations so that getTitle(void) returns what we want.
405                         updateTitlesForLocale(cloned, localeID);
406                         
407                         // Discard binary image data:
408                         updateLayoutItemImages(cloned);
409                 }
410
411                 // Store it in the cache for next time.
412                 mapTableLayouts.setListLayout(tableName, localeID, cloned);
413
414                 return cloned;
415         }
416
417         /**
418          * @param libglomLayoutGroup
419          */
420         private void updateTopLevelListLayoutGroup(final LayoutGroup layoutGroup, final String tableName,
421                         final String localeID) {
422                 final List<LayoutItem> layoutItemsVec = layoutGroup.getItems();
423
424                 int primaryKeyIndex = -1;
425
426                 final int numItems = Utils.safeLongToInt(layoutItemsVec.size());
427                 for (int i = 0; i < numItems; i++) {
428                         final LayoutItem layoutItem = layoutItemsVec.get(i);
429
430                         if (layoutItem instanceof LayoutItemField) {
431                                 final LayoutItemField layoutItemField = (LayoutItemField) layoutItem;
432                                 final Field field = layoutItemField.getFullFieldDetails();
433                                 if ((field != null) && field.getPrimaryKey()) {
434                                         primaryKeyIndex = i;
435                                 }
436                         }
437                 }
438
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()) {
448                                         primaryKey = field;
449                                         break;
450                                 }
451                         }
452
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);
460                         } else {
461                                 Log.error(document.getDatabaseTitleOriginal(), tableName,
462                                                 "A primary key was not found in the FieldVector for this table. Navigation buttons will not work.");
463                         }
464                 } else {
465                         layoutGroup.setPrimaryKeyIndex(primaryKeyIndex);
466                 }
467         }
468
469         private void updateLayoutGroupExpectedResultSize(final LayoutGroup layoutGroup, final String tableName) {
470                 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
471                                 layoutGroup);
472                 layoutGroup.setExpectedResultSize(listViewDBAccess.getExpectedResultSize());
473         }
474
475         /**
476          * 
477          * @param tableName
478          * @param quickFind
479          * @param start
480          * @param length
481          * @param useSortClause
482          * @param sortColumnIndex
483          *            The index of the column to sort by, or -1 for none.
484          * @param isAscending
485          * @return
486          */
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);
491
492                 // Get the LayoutGroup that represents the list view.
493                 // TODO: Performance: Avoid calling this again:
494                 final LayoutGroup libglomLayoutGroup = getValidListViewLayoutGroup(tableName, "" /* irrelevant locale */);
495
496                 // Create a database access object for the list view.
497                 final ListViewDBAccess listViewDBAccess = new ListViewDBAccess(document, documentID, cpds, tableName,
498                                 libglomLayoutGroup);
499
500                 // Return the data.
501                 return listViewDBAccess.getData(quickFind, start, length, sortColumnIndex, isAscending);
502         }
503
504         DataItem[] getDetailsData(String tableName, final TypedDataItem primaryKeyValue) {
505                 // Validate the table name.
506                 tableName = getTableNameToUse(tableName);
507                 
508                 setDataItemType(tableName, primaryKeyValue);
509                 
510                 final DetailsDBAccess detailsDBAccess = new DetailsDBAccess(document, documentID, cpds, tableName);
511
512                 return detailsDBAccess.getData(primaryKeyValue);
513         }
514
515         /** 
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:
518          * 
519          * @param tableName The name of the table for which this is the primary key value
520          * @param primaryKeyValue The TypedDataItem to be changes.
521          */
522         private void setDataItemType(final String tableName, final TypedDataItem primaryKeyValue) {
523
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.");
529                         }
530
531                         Utils.transformUnknownToActualType(primaryKeyValue, primaryKeyField.getGlomType());
532                 }
533         }
534
535         /**
536          * 
537          * @param tableName
538          * @param portal
539          * @param foreignKeyValue
540          * @param start
541          * @param length
542          * @param sortColumnIndex
543          *            The index of the column to sort by, or -1 for none.
544          * @param isAscending
545          * @return
546          */
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");
552                         return null;
553                 }
554
555                 // Validate the table name.
556                 tableName = getTableNameToUse(tableName);
557
558                 // Create a database access object for the related list
559                 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
560                                 portal);
561
562                 // Return the data
563                 return relatedListDBAccess.getData(start, length, foreignKeyValue, sortColumnIndex, isAscending);
564         }
565
566         List<LayoutGroup> getDetailsLayoutGroup(String tableName, final String localeID) {
567                 // Validate the table name.
568                 tableName = getTableNameToUse(tableName);
569
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.
574                         return result;
575                 }
576
577                 final List<LayoutGroup> listGroups = document.getDataLayoutGroups(Document.LAYOUT_NAME_DETAILS, tableName);
578
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.
582
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);
591                         }
592                 }
593
594                 updatePortalsExtras(listCloned, tableName);
595                 updateFieldsExtras(listCloned, tableName);
596
597                 // Discard unwanted translations so that getTitle(void) returns what we want.
598                 updateTitlesForLocale(listCloned, localeID);
599                 
600                 // Discard binary image data:
601                 updateLayoutItemImages(listCloned);
602
603                 // Store it in the cache for next time.
604                 mapTableLayouts.setDetailsLayout(tableName, localeID, listCloned);
605
606                 return listCloned;
607         }
608
609         /** Discard binary image data.
610          * @param listCloned
611          */
612         private void updateLayoutItemImages(final List<LayoutGroup> listGroups) {
613                 for (final LayoutGroup group : listGroups) {
614                         updateLayoutItemImages(group);
615                 }
616                 
617         }
618
619         /** Discard binary image data.
620          * @param group
621          */
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) {
626                                 // Recurse:
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().
634                         }
635                 }
636                 
637         }
638
639         /**
640          * @param result
641          * @param tableName
642          */
643         private void updatePortalsExtras(final List<LayoutGroup> listGroups, final String tableName) {
644                 for (final LayoutGroup group : listGroups) {
645                         updatePortalsExtras(group, tableName);
646                 }
647
648         }
649
650         /**
651          * @param result
652          * @param tableName
653          */
654         private void updateFieldsExtras(final List<LayoutGroup> listGroups, final String tableName) {
655                 for (final LayoutGroup group : listGroups) {
656                         updateFieldsExtras(group, tableName);
657                 }
658         }
659
660         /**
661          * @param result
662          * @param tableName
663          */
664         private void updateTitlesForLocale(final List<LayoutGroup> listGroups, final String localeID) {
665                 for (final LayoutGroup group : listGroups) {
666                         updateTitlesForLocale(group, localeID);
667                 }
668         }
669
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);
675
676                         //Do not add a primary key field if there is already one:
677                         if(portal.getPrimaryKeyIndex() != -1 )
678                                 return;
679
680                         final Relationship relationship = portal.getRelationship();
681                         if (relationship != null) {
682
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());
687
688                                 // get the primary key for the related list table
689                                 final String toTableName = relationship.getToTable();
690                                 if (!StringUtils.isEmpty(toTableName)) {
691
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
703                                                         break;
704                                                 }
705                                         }
706                                 }
707                         }
708
709                 }
710
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);
716                         }
717                 }
718
719         }
720
721         private void updateFieldsExtras(final LayoutGroup group, final String tableName) {
722
723                 final List<LayoutItem> childItems = group.getItems();
724                 for (final LayoutItem item : childItems) {
725                         if (item instanceof LayoutGroup) {
726                                 // Recurse:
727                                 final LayoutGroup childGroup = (LayoutGroup) item;
728                                 updateFieldsExtras(childGroup, tableName);
729                         } else if (item instanceof LayoutItemField) {
730                                 final LayoutItemField field = (LayoutItemField) item;
731
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);
737                                 }
738                         }
739                 }
740         }
741
742         private void updateTitlesForLocale(final LayoutGroup group, final String localeID) {
743
744                 updateItemTitlesForLocale(group, localeID);
745
746                 final List<LayoutItem> childItems = group.getItems();
747                 for (final LayoutItem item : childItems) {
748
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;
753
754                                 final Field field = layoutItemField.getFullFieldDetails();
755                                 if (field != null) {
756                                         field.makeTitleOriginal(localeID);
757                                 }
758
759                                 final CustomTitle customTitle = layoutItemField.getCustomTitle();
760                                 if (customTitle != null) {
761                                         customTitle.makeTitleOriginal(localeID);
762                                 }
763                         }
764
765                         updateItemTitlesForLocale(item, localeID);
766
767                         if (item instanceof LayoutGroup) {
768                                 // Recurse:
769                                 final LayoutGroup childGroup = (LayoutGroup) item;
770                                 updateTitlesForLocale(childGroup, localeID);
771                         }
772                 }
773         }
774
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();
779
780                         if (rel != null) {
781                                 rel.makeTitleOriginal(localeID);
782                         }
783
784                         final Relationship relatedRel = usesRelationship.getRelatedRelationship();
785                         if (relatedRel != null) {
786                                 relatedRel.makeTitleOriginal(localeID);
787                         }
788                 }
789
790                 if (item instanceof Translatable) {
791                         final Translatable translatable = item;
792                         translatable.makeTitleOriginal(localeID);
793                 }
794         }
795
796         /*
797          * Gets the expected row count for a related list.
798          */
799         int getRelatedListRowCount(String tableName, final LayoutItemPortal portal, final TypedDataItem foreignKeyValue) {
800                 if (portal == null) {
801                         Log.error("getRelatedListData(): portal is null");
802                         return 0;
803                 }
804
805                 // Validate the table name.
806                 tableName = getTableNameToUse(tableName);
807
808                 // Create a database access object for the related list
809                 final RelatedListDBAccess relatedListDBAccess = new RelatedListDBAccess(document, documentID, cpds, tableName,
810                                 portal);
811
812                 // Return the row count
813                 return relatedListDBAccess.getExpectedResultSize(foreignKeyValue);
814         }
815
816         NavigationRecord getSuitableRecordToViewDetails(String tableName, final LayoutItemPortal portal,
817                         final TypedDataItem primaryKeyValue) {
818                 // Validate the table name.
819                 tableName = getTableNameToUse(tableName);
820                 
821                 setDataItemType(tableName, primaryKeyValue);
822
823                 final RelatedListNavigation relatedListNavigation = new RelatedListNavigation(document, documentID, cpds,
824                                 tableName, portal);
825
826                 return relatedListNavigation.getNavigationRecord(primaryKeyValue);
827         }
828
829         LayoutGroup getListViewLayoutGroup(String tableName, final String localeID) {
830                 // Validate the table name.
831                 tableName = getTableNameToUse(tableName);
832                 return getValidListViewLayoutGroup(tableName, localeID);
833         }
834
835         /**
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
838          * been set.
839          * 
840          * @param tableName
841          *            The table name to validate.
842          * @return The table name to use.
843          */
844         private String getTableNameToUse(final String tableName) {
845                 if (StringUtils.isEmpty(tableName) || !document.getTableIsKnown(tableName)) {
846                         return document.getDefaultTable();
847                 }
848                 return tableName;
849         }
850
851         /**
852          * @param tableName
853          * @param localeID
854          * @return
855          */
856         public Reports getReports(final String tableName, final String localeID) {
857                 final Reports result = new Reports();
858
859                 final List<String> names = document.getReportNames(tableName);
860
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) {
866                                 continue;
867                         }
868
869                         final String title = report.getTitle(localeID);
870                         result.addReport(name, title);
871                 }
872
873                 return result;
874         }
875 }