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