LayoutItemField: Fix loading of custom titles.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / server / libglom / Document.java
1 /*
2  * Copyright (C) 2012 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.libglom;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Hashtable;
25 import java.util.List;
26
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
30
31 import org.apache.commons.lang3.StringUtils;
32 import org.glom.web.shared.libglom.CustomTitle;
33 import org.glom.web.shared.libglom.Field;
34 import org.glom.web.shared.libglom.NumericFormat;
35 import org.glom.web.shared.libglom.Relationship;
36 import org.glom.web.shared.libglom.Report;
37 import org.glom.web.shared.libglom.Translatable;
38 import org.glom.web.shared.libglom.layout.Formatting;
39 import org.glom.web.shared.libglom.layout.LayoutGroup;
40 import org.glom.web.shared.libglom.layout.LayoutItem;
41 import org.glom.web.shared.libglom.layout.LayoutItemField;
42 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
43 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
44 import org.glom.web.shared.libglom.layout.LayoutItemPortal.NavigationType;
45 import org.glom.web.shared.libglom.layout.UsesRelationship;
46 import org.glom.web.shared.libglom.layout.UsesRelationshipImpl;
47 import org.glom.web.shared.libglom.layout.reportparts.LayoutItemGroupBy;
48 import org.jfree.util.Log;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.Node;
51 import org.w3c.dom.NodeList;
52 import org.xml.sax.SAXException;
53
54 /**
55  * @author Murray Cumming <murrayc@openismus.com>
56  * 
57  */
58 public class Document {
59
60         @SuppressWarnings("serial")
61         private static class TableInfo extends Translatable {
62                 public boolean isDefault;
63                 public boolean isHidden;
64
65                 private final Hashtable<String, Field> fieldsMap = new Hashtable<String, Field>();
66                 private final Hashtable<String, Relationship> relationshipsMap = new Hashtable<String, Relationship>();
67                 private final Hashtable<String, Report> reportsMap = new Hashtable<String, Report>();
68
69                 private List<LayoutGroup> layoutGroupsList = new ArrayList<LayoutGroup>();
70                 private List<LayoutGroup> layoutGroupsDetails = new ArrayList<LayoutGroup>();
71         }
72
73         private String fileURI = "";
74         private org.w3c.dom.Document xmlDocument = null;
75
76         private Translatable databaseTitle = new Translatable();
77         private List<String> translationAvailableLocales = new ArrayList<String>(); // TODO
78         private String connectionServer = "";
79         private String connectionDatabase = "";
80         private int connectionPort = 0;
81         private final Hashtable<String, TableInfo> tablesMap = new Hashtable<String, TableInfo>();
82
83         private static final String NODE_CONNECTION = "connection";
84         private static final String ATTRIBUTE_CONNECTION_SERVER = "server";
85         private static final String ATTRIBUTE_CONNECTION_DATABASE = "database";
86         private static final String ATTRIBUTE_CONNECTION_PORT = "port";
87         private static final String NODE_TABLE = "table";
88         private static final String ATTRIBUTE_NAME = "name";
89         private static final String ATTRIBUTE_TITLE = "title";
90         private static final String ATTRIBUTE_DEFAULT = "default";
91         private static final String ATTRIBUTE_HIDDEN = "hidden";
92         private static final String NODE_TRANSLATIONS_SET = "trans_set";
93         private static final String NODE_TRANSLATIONS = "trans";
94         private static final String ATTRIBUTE_TRANSLATION_LOCALE = "loc";
95         private static final String ATTRIBUTE_TRANSLATION_TITLE = "val";
96         private static final String NODE_REPORTS = "reports";
97         private static final String NODE_REPORT = "report";
98         private static final String NODE_FIELDS = "fields";
99         private static final String NODE_FIELD = "field";
100         private static final String ATTRIBUTE_PRIMARY_KEY = "primary_key";
101         private static final String ATTRIBUTE_FIELD_TYPE = "type";
102         private static final String NODE_FORMATTING = "formatting";
103         // private static final String ATTRIBUTE_TEXT_FORMAT_MULTILINE = "format_text_multiline";
104         private static final String ATTRIBUTE_USE_THOUSANDS_SEPARATOR = "format_thousands_separator";
105         private static final String ATTRIBUTE_DECIMAL_PLACES = "format_decimal_places";
106         private static final String NODE_RELATIONSHIPS = "relationships";
107         private static final String NODE_RELATIONSHIP = "relationship";
108         private static final String ATTRIBUTE_RELATIONSHIP_FROM_FIELD = "key";
109         private static final String ATTRIBUTE_RELATIONSHIP_TO_TABLE = "other_table";
110         private static final String ATTRIBUTE_RELATIONSHIP_TO_FIELD = "other_key";
111         private static final String NODE_DATA_LAYOUTS = "data_layouts";
112         private static final String NODE_DATA_LAYOUT = "data_layout";
113         private static final String NODE_DATA_LAYOUT_GROUPS = "data_layout_groups";
114         private static final String NODE_DATA_LAYOUT_GROUP = "data_layout_group";
115         private static final String ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT = "columns_count";
116         private static final String NODE_DATA_LAYOUT_NOTEBOOK = "data_layout_notebook";
117         private static final String NODE_DATA_LAYOUT_PORTAL = "data_layout_portal";
118         private static final String NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP = "portal_navigation_relationship";
119         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE = "navigation_type";
120         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC = "automatic";
121         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC = "specific";
122         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE = "none";
123         private static final String ATTRIBUTE_RELATIONSHIP_NAME = "relationship";
124         private static final String ATTRIBUTE_RELATED_RELATIONSHIP_NAME = "related_relationship";
125         private static final String NODE_DATA_LAYOUT_ITEM = "data_layout_item";
126         private static final String NODE_CUSTOM_TITLE = "title_custom";
127         private static final String ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM = "use_custom";
128         private static final String NODE_DATA_LAYOUT_ITEM_GROUPBY = "data_layout_item_groupby";
129         private static final String NODE_GROUPBY = "groupby";
130         private static final String NODE_SECONDARY_FIELDS = "secondary_fields";
131         private static final String ATTRIBUTE_USE_DEFAULT_FORMATTING = "use_default_formatting";
132         private static final String LAYOUT_NAME_DETAILS = "details";
133         private static final String LAYOUT_NAME_LIST = "list";
134
135         public void setFileURI(final String fileURI) {
136                 this.fileURI = fileURI;
137         }
138
139         public String getFileURI() {
140                 return fileURI;
141         }
142
143         // TODO: Make sure these have the correct values.
144         public enum LoadFailureCodes {
145                 LOAD_FAILURE_CODE_NONE, LOAD_FAILURE_CODE_NOT_FOUND, LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW
146         };
147
148         public boolean load() {
149                 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
150                 DocumentBuilder documentBuilder;
151                 try {
152                         documentBuilder = dbf.newDocumentBuilder();
153                 } catch (final ParserConfigurationException e) {
154                         // TODO Auto-generated catch block
155                         e.printStackTrace();
156                         return false;
157                 }
158
159                 try {
160                         xmlDocument = documentBuilder.parse(fileURI);
161                 } catch (final SAXException e) {
162                         // TODO Auto-generated catch block
163                         e.printStackTrace();
164                         return false;
165                 } catch (final IOException e) {
166                         // TODO Auto-generated catch block
167                         e.printStackTrace();
168                         return false;
169                 }
170
171                 final Element rootNode = xmlDocument.getDocumentElement();
172                 if (rootNode.getNodeName() != "glom_document") {
173                         Log.error("Unexpected XML root node name found: " + rootNode.getNodeName());
174                         return false;
175                 }
176
177                 databaseTitle.setTitleOriginal(rootNode.getAttribute(ATTRIBUTE_TITLE));
178
179                 // We first load the fields, relationships, etc,
180                 // for all tables:
181                 final List<Node> listTableNodes = getChildrenByTagName(rootNode, NODE_TABLE);
182                 for (final Node node : listTableNodes) {
183                         if (!(node instanceof Element))
184                                 continue;
185
186                         final Element element = (Element) node;
187                         final TableInfo info = loadTableNodeBasic(element);
188                         tablesMap.put(info.getName(), info);
189                 }
190
191                 // We then load the layouts for all tables, because they
192                 // need the fields and relationships for all tables:
193                 for (final Node node : listTableNodes) {
194                         if (!(node instanceof Element))
195                                 continue;
196
197                         final Element element = (Element) node;
198                         final String tableName = element.getAttribute(ATTRIBUTE_NAME);
199
200                         // We first load the fields, relationships, etc:
201                         final TableInfo info = getTableInfo(tableName);
202                         if (info == null) {
203                                 continue;
204                         }
205
206                         // We then load the layouts afterwards, because they
207                         // need the fields and relationships:
208                         loadTableLayouts(element, info);
209
210                         tablesMap.put(info.getName(), info);
211                 }
212
213                 final Element nodeConnection = getElementByName(rootNode, NODE_CONNECTION);
214                 if (nodeConnection != null) {
215                         connectionServer = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_SERVER);
216                         connectionDatabase = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_DATABASE);
217                         connectionPort = getAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT);
218                 }
219
220                 return true;
221         };
222
223         private Element getElementByName(final Element parentElement, final String tagName) {
224                 final List<Node> listNodes = getChildrenByTagName(parentElement, tagName);
225                 if (listNodes == null)
226                         return null;
227
228                 if (listNodes.size() == 0)
229                         return null;
230
231                 return (Element) listNodes.get(0);
232         }
233
234         private boolean getAttributeAsBoolean(final Element node, final String attributeName) {
235                 final String str = node.getAttribute(attributeName);
236                 if (str == null)
237                         return false;
238
239                 return str.equals("true");
240         }
241
242         /**
243          * @param elementFormatting
244          * @param aTTRIBUTE_DECIMAL_PLACES2
245          * @return
246          */
247         private int getAttributeAsDecimal(final Element node, final String attributeName) {
248                 final String str = node.getAttribute(attributeName);
249                 if (StringUtils.isEmpty(str))
250                         return 0;
251
252                 return Integer.valueOf(str);
253         }
254
255         /**
256          * Load a title and its translations.
257          * 
258          * @param node
259          *            The XML Element that may contain a title attribute and a trans_set of translations of the title.
260          * @param title
261          */
262         private void loadTitle(final Element node, final Translatable title) {
263                 title.setName(node.getAttribute(ATTRIBUTE_NAME));
264
265                 title.setTitleOriginal(node.getAttribute(ATTRIBUTE_TITLE));
266
267                 final Element nodeSet = getElementByName(node, NODE_TRANSLATIONS_SET);
268                 if (nodeSet == null) {
269                         return;
270                 }
271
272                 final List<Node> listNodes = getChildrenByTagName(nodeSet, NODE_TRANSLATIONS);
273                 if (listNodes == null)
274                         return;
275
276                 for (final Node transNode : listNodes) {
277                         if (!(transNode instanceof Element)) {
278                                 continue;
279                         }
280
281                         final Element element = (Element) transNode;
282
283                         final String locale = element.getAttribute(ATTRIBUTE_TRANSLATION_LOCALE);
284                         final String translatedTitle = element.getAttribute(ATTRIBUTE_TRANSLATION_TITLE);
285                         if (!StringUtils.isEmpty(locale) && !StringUtils.isEmpty(translatedTitle)) {
286                                 title.setTitle(translatedTitle, locale);
287                         }
288                 }
289         }
290
291         /**
292          * @param tableNode
293          * @return
294          */
295         private TableInfo loadTableNodeBasic(final Element tableNode) {
296                 final TableInfo info = new TableInfo();
297                 loadTitle(tableNode, info);
298                 final String tableName = info.getName();
299
300                 info.isDefault = getAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT);
301                 info.isHidden = getAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN);
302
303                 // These should be loaded before the fields, because the fields use them.
304                 final Element relationshipsNode = getElementByName(tableNode, NODE_RELATIONSHIPS);
305                 if (relationshipsNode != null) {
306                         final List<Node> listNodes = getChildrenByTagName(relationshipsNode, NODE_RELATIONSHIP);
307                         for (final Node node : listNodes) {
308                                 if (!(node instanceof Element)) {
309                                         continue;
310                                 }
311
312                                 final Element element = (Element) node;
313                                 final Relationship relationship = new Relationship();
314                                 loadTitle(element, relationship);
315                                 relationship.setFromTable(tableName);
316                                 relationship.setFromField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD));
317                                 relationship.setToTable(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE));
318                                 relationship.setToField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD));
319
320                                 info.relationshipsMap.put(relationship.getName(), relationship);
321                         }
322                 }
323
324                 final Element fieldsNode = getElementByName(tableNode, NODE_FIELDS);
325                 if (fieldsNode != null) {
326                         final List<Node> listNodes = getChildrenByTagName(fieldsNode, NODE_FIELD);
327                         for (final Node node : listNodes) {
328                                 if (!(node instanceof Element)) {
329                                         continue;
330                                 }
331
332                                 final Element element = (Element) node;
333                                 final Field field = new Field();
334                                 loadField(element, field);
335
336                                 info.fieldsMap.put(field.getName(), field);
337                         }
338                 }
339
340                 return info;
341         }
342
343         /**
344          * @param tableNode
345          * @param info
346          */
347         private void loadTableLayouts(final Element tableNode, final TableInfo info) {
348                 final String tableName = info.getName();
349
350                 final Element layoutsNode = getElementByName(tableNode, NODE_DATA_LAYOUTS);
351                 if (layoutsNode != null) {
352                         final List<Node> listNodes = getChildrenByTagName(layoutsNode, NODE_DATA_LAYOUT);
353                         for (final Node node : listNodes) {
354                                 if (!(node instanceof Element)) {
355                                         continue;
356                                 }
357
358                                 final Element element = (Element) node;
359                                 final String name = element.getAttribute("name");
360                                 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
361                                 if (name.equals(LAYOUT_NAME_DETAILS)) {
362                                         info.layoutGroupsDetails = listLayoutGroups;
363                                 } else if (name.equals(LAYOUT_NAME_LIST)) {
364                                         info.layoutGroupsList = listLayoutGroups;
365                                 } else {
366                                         Log.error("loadTableNode(): unexpected layout name: " + name);
367                                 }
368                         }
369                 }
370
371                 final Element reportsNode = getElementByName(tableNode, NODE_REPORTS);
372                 if (reportsNode != null) {
373                         final List<Node> listNodes = getChildrenByTagName(reportsNode, NODE_REPORT);
374                         for (final Node node : listNodes) {
375                                 if (!(node instanceof Element)) {
376                                         continue;
377                                 }
378
379                                 final Element element = (Element) node;
380                                 final Report report = new Report();
381                                 loadReport(element, report, tableName);
382
383                                 info.reportsMap.put(report.getName(), report);
384                         }
385                 }
386         }
387
388         /**
389          * @param node
390          * @return
391          */
392         private List<LayoutGroup> loadLayoutNode(final Element node, final String tableName) {
393                 if (node == null) {
394                         return null;
395                 }
396
397                 final List<LayoutGroup> result = new ArrayList<LayoutGroup>();
398
399                 final List<Node> listNodes = getChildrenByTagName(node, NODE_DATA_LAYOUT_GROUPS);
400                 for (final Node nodeGroups : listNodes) {
401                         if (!(nodeGroups instanceof Element)) {
402                                 continue;
403                         }
404
405                         final Element elementGroups = (Element) nodeGroups;
406
407                         final NodeList list = elementGroups.getChildNodes();
408                         final int num = list.getLength();
409                         for (int i = 0; i < num; i++) {
410                                 final Node nodeLayoutGroup = list.item(i);
411                                 if (nodeLayoutGroup == null) {
412                                         continue;
413                                 }
414
415                                 if (!(nodeLayoutGroup instanceof Element)) {
416                                         continue;
417                                 }
418
419                                 final Element element = (Element) nodeLayoutGroup;
420                                 final String tagName = element.getTagName();
421                                 if (tagName == NODE_DATA_LAYOUT_GROUP) {
422                                         final LayoutGroup group = new LayoutGroup();
423                                         loadDataLayoutGroup(element, group, tableName);
424                                         result.add(group);
425                                 } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
426                                         final LayoutItemNotebook group = new LayoutItemNotebook();
427                                         loadDataLayoutGroup(element, group, tableName);
428                                         result.add(group);
429                                 } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
430                                         final LayoutItemPortal portal = new LayoutItemPortal();
431                                         loadDataLayoutPortal(element, portal, tableName);
432                                         result.add(portal);
433                                 }
434                         }
435                 }
436
437                 return result;
438         }
439
440         /**
441          * @param element
442          * @param tableName
443          * @param portal
444          */
445         private void loadUsesRelationship(final Element element, final String tableName, final UsesRelationship item) {
446                 if (element == null) {
447                         return;
448                 }
449
450                 if (item == null) {
451                         return;
452                 }
453
454                 final String relationshipName = element.getAttribute(ATTRIBUTE_RELATIONSHIP_NAME);
455                 Relationship relationship = null;
456                 if (!StringUtils.isEmpty(relationshipName)) {
457                         // std::cout << "  debug in : tableName=" << tableName << ", relationshipName=" << relationship_name <<
458                         // std::endl;
459                         relationship = getRelationship(tableName, relationshipName);
460                         item.setRelationship(relationship);
461
462                         if (relationship == null) {
463                                 Log.error("relationship not found: " + relationshipName + ", in table: " + tableName);
464                         }
465                 }
466
467                 final String relatedRelationshipName = element.getAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME);
468                 if (!StringUtils.isEmpty(relatedRelationshipName) && (relationship != null)) {
469                         final Relationship relatedRelationship = getRelationship(relationship.getToTable(), relatedRelationshipName);
470                         if (relatedRelationship == null) {
471                                 Log.error("related relationship not found in table=" + relationship.getToTable() + ",  name="
472                                                 + relatedRelationshipName);
473
474                                 item.setRelatedRelationship(relatedRelationship);
475                         }
476                 }
477         }
478
479         /**
480          * getElementsByTagName() is recursive, but we do not want that.
481          * 
482          * @param node
483          * @param
484          * @return
485          */
486         private List<Node> getChildrenByTagName(final Element parentNode, final String tagName) {
487                 final List<Node> result = new ArrayList<Node>();
488
489                 final NodeList list = parentNode.getElementsByTagName(tagName);
490                 final int num = list.getLength();
491                 for (int i = 0; i < num; i++) {
492                         final Node node = list.item(i);
493                         if (node == null) {
494                                 continue;
495                         }
496
497                         final Node itemParentNode = node.getParentNode();
498                         if (itemParentNode.equals(parentNode)) {
499                                 result.add(node);
500                         }
501                 }
502
503                 return result;
504         }
505
506         /**
507          * @param element
508          * @param group
509          */
510         private void loadDataLayoutGroup(final Element nodeGroup, final LayoutGroup group, final String tableName) {
511                 loadTitle(nodeGroup, group);
512
513                 // Read the column count:
514                 int columnCount = getAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT);
515                 if (columnCount < 1) {
516                         columnCount = 1; // 0 is a useless default.
517                 }
518                 group.setColumnCount(columnCount);
519
520                 // Get the child items:
521                 final NodeList listNodes = nodeGroup.getChildNodes();
522                 final int num = listNodes.getLength();
523                 for (int i = 0; i < num; i++) {
524                         final Node node = listNodes.item(i);
525                         if (!(node instanceof Element))
526                                 continue;
527
528                         final Element element = (Element) node;
529                         final String tagName = element.getTagName();
530                         if (tagName == NODE_DATA_LAYOUT_GROUP) {
531                                 final LayoutGroup childGroup = new LayoutGroup();
532                                 loadDataLayoutGroup(element, childGroup, tableName);
533                                 group.addItem(childGroup);
534                         } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
535                                 final LayoutItemNotebook childGroup = new LayoutItemNotebook();
536                                 loadDataLayoutGroup(element, childGroup, tableName);
537                                 group.addItem(childGroup);
538                         } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
539                                 final LayoutItemPortal childGroup = new LayoutItemPortal();
540                                 loadDataLayoutPortal(element, childGroup, tableName);
541                                 group.addItem(childGroup);
542                         } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM) {
543                                 final LayoutItemField item = new LayoutItemField();
544                                 loadDataLayoutItemField(element, item, tableName);
545                                 group.addItem(item);
546                         } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM_GROUPBY) {
547                                 final LayoutItemGroupBy item = new LayoutItemGroupBy();
548                                 loadDataLayoutItemGroupBy(element, item, tableName);
549                                 group.addItem(item);
550                         }
551                 }
552         }
553
554         /**
555          * @param element
556          * @param item
557          * @param tableName
558          */
559         private void loadDataLayoutItemGroupBy(final Element element, final LayoutItemGroupBy item, final String tableName) {
560                 loadDataLayoutGroup(element, item, tableName);
561
562                 final Element elementGroupBy = getElementByName(element, NODE_GROUPBY);
563                 if (elementGroupBy == null) {
564                         return;
565                 }
566
567                 final LayoutItemField fieldGroupBy = new LayoutItemField();
568                 loadDataLayoutItemField(elementGroupBy, fieldGroupBy, tableName);
569                 item.setFieldGroupBy(fieldGroupBy);
570
571                 final Element elementSecondaryFields = getElementByName(element, NODE_SECONDARY_FIELDS);
572                 if (elementSecondaryFields == null) {
573                         return;
574                 }
575
576                 final Element elementLayoutGroup = getElementByName(elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
577                 if (elementLayoutGroup != null) {
578                         final LayoutGroup secondaryLayoutGroup = new LayoutGroup();
579                         loadDataLayoutGroup(elementLayoutGroup, secondaryLayoutGroup, tableName);
580                         item.setSecondaryFields(secondaryLayoutGroup);
581                 }
582         }
583
584         /**
585          * @param element
586          * @param item
587          */
588         private void loadDataLayoutItemField(final Element element, final LayoutItemField item, final String tableName) {
589                 item.setName(element.getAttribute(ATTRIBUTE_NAME));
590                 loadUsesRelationship(element, tableName, item);
591
592                 final Element elementCustomTitle = getElementByName(element, NODE_CUSTOM_TITLE);
593                 if (elementCustomTitle != null) {
594                         final CustomTitle customTitle = item.getCustomTitle();
595                         customTitle.setUseCustomTitle(getAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM));
596                         loadTitle(elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
597                 }
598
599                 // Get the actual field:
600                 final String fieldName = item.getName();
601                 final String inTableName = item.getTableUsed(tableName);
602                 final Field field = getField(inTableName, fieldName);
603                 item.setFullFieldDetails(field);
604
605                 item.setUseDefaultFormatting(getAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING));
606
607                 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
608                 if (elementFormatting != null) {
609                         loadFormatting(elementFormatting, item.getFormatting());
610                 }
611         }
612
613         /**
614          * @param element
615          * @param childGroup
616          */
617         private void loadDataLayoutPortal(final Element element, final LayoutItemPortal portal, final String tableName) {
618                 loadUsesRelationship(element, tableName, portal);
619                 final String relatedTableName = portal.getTableUsed(tableName);
620                 loadDataLayoutGroup(element, portal, relatedTableName);
621
622                 final Element elementNavigation = getElementByName(element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
623                 if (elementNavigation != null) {
624                         final String navigationTypeAsString = elementNavigation.getAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE);
625                         if (StringUtils.isEmpty(navigationTypeAsString)
626                                         || navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC) {
627                                 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC);
628                         } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE) {
629                                 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_NONE);
630                         } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC) {
631                                 // Read the specified relationship name:
632                                 final UsesRelationship relationshipNavigationSpecific = new UsesRelationshipImpl();
633                                 loadUsesRelationship(elementNavigation, relatedTableName, relationshipNavigationSpecific);
634                                 portal.setNavigationRelationshipSpecific(relationshipNavigationSpecific);
635                         }
636                 }
637
638         }
639
640         /**
641          * @param element
642          * @param field
643          */
644         private void loadField(final Element element, final Field field) {
645                 loadTitle(element, field);
646
647                 Field.GlomFieldType fieldType = Field.GlomFieldType.TYPE_INVALID;
648                 final String fieldTypeStr = element.getAttribute(ATTRIBUTE_FIELD_TYPE);
649                 if (!StringUtils.isEmpty(fieldTypeStr)) {
650                         if (fieldTypeStr.equals("Boolean")) {
651                                 fieldType = Field.GlomFieldType.TYPE_BOOLEAN;
652                         } else if (fieldTypeStr.equals("Date")) {
653                                 fieldType = Field.GlomFieldType.TYPE_DATE;
654                         } else if (fieldTypeStr.equals("Image")) {
655                                 fieldType = Field.GlomFieldType.TYPE_IMAGE;
656                         } else if (fieldTypeStr.equals("Number")) {
657                                 fieldType = Field.GlomFieldType.TYPE_NUMERIC;
658                         } else if (fieldTypeStr.equals("Text")) {
659                                 fieldType = Field.GlomFieldType.TYPE_TEXT;
660                         } else if (fieldTypeStr.equals("Time")) {
661                                 fieldType = Field.GlomFieldType.TYPE_TIME;
662                         }
663                 }
664
665                 field.setGlomFieldType(fieldType);
666
667                 field.setPrimaryKey(getAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY));
668                 loadTitle(element, field);
669
670                 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
671                 if (elementFormatting != null) {
672                         loadFormatting(elementFormatting, field.getFormatting());
673                 }
674         }
675
676         /**
677          * @param elementFormatting
678          * @param formatting
679          */
680         private void loadFormatting(final Element elementFormatting, final Formatting formatting) {
681                 if (elementFormatting == null)
682                         return;
683
684                 if (formatting == null)
685                         return;
686
687                 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
688
689                 final NumericFormat numericFormatting = formatting.getNumericFormat();
690                 if (numericFormatting != null) {
691                         numericFormatting.setUseThousandsSeparator(getAttributeAsBoolean(elementFormatting,
692                                         ATTRIBUTE_USE_THOUSANDS_SEPARATOR));
693                         numericFormatting.setDecimalPlaces(getAttributeAsDecimal(elementFormatting, ATTRIBUTE_DECIMAL_PLACES));
694                 }
695
696         }
697
698         /**
699          * @param element
700          * @param reportNode
701          */
702         private void loadReport(final Element element, final Report report, final String tableName) {
703                 report.setName(element.getAttribute(ATTRIBUTE_NAME));
704                 loadTitle(element, report);
705
706                 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
707
708                 // A report can actually only have one LayoutGroup,
709                 // though it uses the same XML structure as List and Details layouts,
710                 // which (wrongly) suggests that it can have more than one group.
711                 LayoutGroup layoutGroup = null;
712                 if (!listLayoutGroups.isEmpty()) {
713                         layoutGroup = listLayoutGroups.get(0);
714                 }
715
716                 report.setLayoutGroup(layoutGroup);
717         }
718
719         private TableInfo getTableInfo(final String tableName) {
720                 return tablesMap.get(tableName);
721         }
722
723         public enum HostingMode {
724                 HOSTING_MODE_POSTGRES_CENTRAL, HOSTING_MODE_POSTGRES_SELF, HOSTING_MODE_SQLITE
725         };
726
727         public String getDatabaseTitle(final String locale) {
728                 return databaseTitle.getTitle(locale);
729         }
730
731         public String getDatabaseTitleOriginal() {
732                 return databaseTitle.getTitleOriginal();
733         }
734
735         public List<String> getTranslationAvailableLocales() {
736                 return translationAvailableLocales;
737         }
738
739         public Document.HostingMode getHostingMode() {
740                 return HostingMode.HOSTING_MODE_POSTGRES_CENTRAL; // TODO
741         }
742
743         public String getConnectionServer() {
744                 return connectionServer;
745         }
746
747         public long getConnectionPort() {
748                 return connectionPort;
749         }
750
751         public String getConnectionDatabase() {
752                 return connectionDatabase;
753         }
754
755         public List<String> getTableNames() {
756                 // TODO: Return a Set?
757                 return new ArrayList<String>(tablesMap.keySet());
758         }
759
760         public boolean getTableIsHidden(final String tableName) {
761                 final TableInfo info = getTableInfo(tableName);
762                 if (info == null) {
763                         return false;
764                 }
765
766                 return info.isHidden;
767         }
768
769         public String getTableTitle(final String tableName, final String locale) {
770                 final TableInfo info = getTableInfo(tableName);
771                 if (info == null) {
772                         return "";
773                 }
774
775                 return info.getTitle(locale);
776         }
777
778         public String getDefaultTable() {
779                 for (final TableInfo info : tablesMap.values()) {
780                         if (info.isDefault) {
781                                 return info.getName();
782                         }
783                 }
784
785                 return "";
786         }
787
788         public boolean getTableIsKnown(final String tableName) {
789                 final TableInfo info = getTableInfo(tableName);
790                 if (info == null) {
791                         return false;
792                 }
793
794                 return true;
795         }
796
797         public List<Field> getTableFields(final String tableName) {
798                 final TableInfo info = getTableInfo(tableName);
799                 if (info == null)
800                         return null;
801
802                 return new ArrayList<Field>(info.fieldsMap.values());
803         }
804
805         public Field getField(final String tableName, final String strFieldName) {
806                 final TableInfo info = getTableInfo(tableName);
807                 if (info == null)
808                         return null;
809
810                 return info.fieldsMap.get(strFieldName);
811         }
812
813         public List<LayoutGroup> getDataLayoutGroups(final String layoutName, final String parentTableName) {
814                 final TableInfo info = getTableInfo(parentTableName);
815                 if (info == null)
816                         return new ArrayList<LayoutGroup>();
817
818                 if (layoutName == LAYOUT_NAME_DETAILS) {
819                         return info.layoutGroupsDetails;
820                 } else if (layoutName == LAYOUT_NAME_LIST) {
821                         return info.layoutGroupsList;
822                 } else {
823                         return new ArrayList<LayoutGroup>();
824                 }
825         }
826
827         public List<String> getReportNames(final String tableName) {
828                 final TableInfo info = getTableInfo(tableName);
829                 if (info == null)
830                         return new ArrayList<String>();
831
832                 return new ArrayList<String>(info.reportsMap.keySet());
833         }
834
835         public Report getReport(final String tableName, final String reportName) {
836                 final TableInfo info = getTableInfo(tableName);
837                 if (info == null)
838                         return null;
839
840                 return info.reportsMap.get(reportName);
841         }
842
843         /**
844          * @param tableName
845          * @param field
846          * @return
847          */
848         public Relationship getFieldUsedInRelationshipToOne(final String tableName, final LayoutItemField layoutField) {
849
850                 if (layoutField == null) {
851                         Log.error("layoutField was null");
852                         return null;
853                 }
854
855                 Relationship result = null;
856
857                 final String tableUsed = layoutField.getTableUsed(tableName);
858                 final TableInfo info = getTableInfo(tableUsed);
859                 if (info == null) {
860                         // This table is special. We would not create a relationship to it using a field:
861                         // if(tableUsed == GLOM_STANDARD_TABLE_PREFS_TABLE_NAME)
862                         // return result;
863
864                         Log.error("table not found: " + tableUsed);
865                         return null;
866                 }
867
868                 // Look at each relationship:
869                 final String fieldName = layoutField.getName();
870                 for (final Relationship relationship : info.relationshipsMap.values()) {
871                         if (relationship != null) {
872                                 // If the relationship uses the field
873                                 if (relationship.getFromField() == fieldName) {
874                                         // if the to_table is not hidden:
875                                         if (!getTableIsHidden(relationship.getToTable())) {
876                                                 // TODO_Performance: The use of this convenience method means we get the full relationship
877                                                 // information again:
878                                                 if (getRelationshipIsToOne(tableName, relationship.getName())) {
879                                                         result = relationship;
880                                                 }
881                                         }
882                                 }
883                         }
884                 }
885
886                 return result;
887         }
888
889         /**
890          * @param tableName
891          * @param relationshipName
892          * @return
893          */
894         private boolean getRelationshipIsToOne(final String tableName, final String relationshipName) {
895                 final Relationship relationship = getRelationship(tableName, relationshipName);
896                 if (relationship != null) {
897                         final Field fieldTo = getField(relationship.getToTable(), relationship.getToField());
898                         if (fieldTo != null) {
899                                 return (fieldTo.getPrimaryKey() || fieldTo.getUniqueKey());
900                         }
901                 }
902
903                 return false;
904         }
905
906         /**
907          * @param tableName
908          * @param relationshipName
909          * @return
910          */
911         private Relationship getRelationship(final String tableName, final String relationshipName) {
912                 final TableInfo info = getTableInfo(tableName);
913                 if (info == null) {
914                         Log.error("table not found: " + tableName);
915                         return null;
916                 }
917
918                 return info.relationshipsMap.get(relationshipName);
919         }
920
921         public class TableToViewDetails {
922                 public String tableName;
923                 public UsesRelationship usesRelationship;
924         }
925
926         /**
927          * @param tableName
928          *            Output parameter
929          * @param relationship
930          * @param portal
931          *            TODO
932          */
933         public TableToViewDetails getPortalSuitableTableToViewDetails(LayoutItemPortal portal) {
934                 UsesRelationship navigationRelationship = null;
935
936                 // Check whether a relationship was specified:
937                 if (portal.getNavigationType() == NavigationType.NAVIGATION_AUTOMATIC) {
938                         navigationRelationship = getPortalNavigationRelationshipAutomatic(portal);
939                 } else {
940                         navigationRelationship = portal.getNavigationRelationshipSpecific();
941                 }
942
943                 // Get the navigation table name from the chosen relationship:
944                 String directlyRelatedTableName = portal.getTableUsed("" /* not relevant */);
945
946                 // The navigation_table_name (and therefore, the table_name output parameter,
947                 // as well) stays empty if the navrel type was set to none.
948                 String navigationTableName = null;
949                 if (navigationRelationship != null) {
950                         navigationTableName = navigationRelationship.getTableUsed(directlyRelatedTableName);
951                 } else if (portal.getNavigationType() != NavigationType.NAVIGATION_NONE) {
952                         // An empty result from get_portal_navigation_relationship_automatic() or
953                         // get_navigation_relationship_specific() means we should use the directly related table:
954                         navigationTableName = directlyRelatedTableName;
955                 }
956
957                 if (StringUtils.isEmpty(navigationTableName)) {
958                         return null;
959                 }
960
961                 if (this == null) {
962                         Log.error("document is null.");
963                         return null;
964                 }
965
966                 if (getTableIsHidden(navigationTableName)) {
967                         Log.error("navigation_table_name indicates a hidden table: " + navigationTableName);
968                         return null;
969                 }
970
971                 TableToViewDetails result = new TableToViewDetails();
972                 result.tableName = navigationTableName;
973                 result.usesRelationship = navigationRelationship;
974                 return result;
975         }
976
977         /**
978          * @param portal
979          *            TODO
980          * @return
981          */
982         private UsesRelationship getPortalNavigationRelationshipAutomatic(LayoutItemPortal portal) {
983                 if (this == null) {
984                         return null;
985                 }
986
987                 // If the related table is not hidden then we can just navigate to that:
988                 final String direct_related_table_name = portal.getTableUsed("" /* parent table - not relevant */);
989                 if (!getTableIsHidden(direct_related_table_name)) {
990                         // Non-hidden tables can just be shown directly. Navigate to it:
991                         return null;
992                 } else {
993                         // If the related table is hidden,
994                         // then find a suitable related non-hidden table by finding the first layout field that mentions one:
995                         final LayoutItemField field = getPortalFieldIsFromNonHiddenRelatedRecord(portal);
996                         if (field != null) {
997                                 return field; // Returns the UsesRelationship base part. (A relationship belonging to the portal's
998                                                                 // related table.)
999                         } else {
1000                                 // Instead, find a key field that's used in a relationship,
1001                                 // and pretend that we are showing the to field as a related field:
1002                                 final Relationship fieldIndentifies = getPortalFieldIdentifiesNonHiddenRelatedRecord(portal);
1003                                 if (fieldIndentifies != null) {
1004                                         UsesRelationship result = new UsesRelationshipImpl();
1005                                         result.setRelationship(fieldIndentifies);
1006                                         return result;
1007                                 }
1008                         }
1009                 }
1010
1011                 // There was no suitable related table to show:
1012                 return null;
1013         }
1014
1015         /**
1016          * @param portal
1017          *            TODO
1018          * @return
1019          */
1020         private LayoutItemField getPortalFieldIsFromNonHiddenRelatedRecord(LayoutItemPortal portal) {
1021                 // Find the first field that is from a non-hidden related table.
1022
1023                 if (this == null) {
1024                         return null;
1025                 }
1026
1027                 LayoutItemField result = null;
1028
1029                 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1030
1031                 final List<LayoutItem> items = portal.getItems();
1032                 for (LayoutItem item : items) {
1033                         if (item instanceof LayoutItemField) {
1034                                 LayoutItemField field = (LayoutItemField) item;
1035                                 if (field.getHasRelationshipName()) {
1036                                         final String table_name = field.getTableUsed(parent_table_name);
1037                                         if (!(getTableIsHidden(table_name)))
1038                                                 return field;
1039                                 }
1040                         }
1041                 }
1042
1043                 return result;
1044         }
1045
1046         /**
1047          * @param used_in_relationship
1048          * @param portal
1049          *            TODO
1050          * @return
1051          */
1052         private Relationship getPortalFieldIdentifiesNonHiddenRelatedRecord(LayoutItemPortal portal) {
1053                 // Find the first field that is from a non-hidden related table.
1054
1055                 if (this == null) {
1056                         Log.error("document is null");
1057                         return null;
1058                 }
1059
1060                 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1061
1062                 List<LayoutItem> items = portal.getItems();
1063                 for (LayoutItem item : items) {
1064                         if (item instanceof LayoutItemField) {
1065                                 LayoutItemField field = (LayoutItemField) item;
1066                                 if (field.getHasRelationshipName()) {
1067                                         final Relationship relationship = getFieldUsedInRelationshipToOne(parent_table_name, field);
1068                                         if (relationship != null) {
1069                                                 final String table_name = relationship.getToTable();
1070                                                 if (!StringUtils.isEmpty(table_name)) {
1071                                                         if (!(getTableIsHidden(table_name))) {
1072                                                                 return relationship;
1073                                                         }
1074                                                 }
1075                                         }
1076                                 }
1077                         }
1078                 }
1079
1080                 return null;
1081         }
1082 }