SelfHosting test: Also try using a relationship.
[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.ByteArrayInputStream;
23 import java.io.File;
24 import java.io.IOException;
25 //import java.io.InputStream;
26 //import java.net.URLConnection;
27 import java.text.DateFormat;
28 import java.text.NumberFormat;
29 import java.text.ParseException;
30 import java.util.ArrayList;
31 import java.util.Date;
32 import java.util.HashMap;
33 import java.util.Hashtable;
34 import java.util.List;
35 import java.util.Locale;
36 import java.util.Map;
37 import java.util.Map.Entry;
38
39 import javax.xml.parsers.DocumentBuilder;
40 import javax.xml.parsers.DocumentBuilderFactory;
41 import javax.xml.parsers.ParserConfigurationException;
42 import javax.xml.transform.OutputKeys;
43 import javax.xml.transform.Transformer;
44 import javax.xml.transform.TransformerConfigurationException;
45 import javax.xml.transform.TransformerException;
46 import javax.xml.transform.TransformerFactory;
47 import javax.xml.transform.dom.DOMSource;
48 import javax.xml.transform.stream.StreamResult;
49
50 import org.apache.commons.codec.binary.Base64;
51 import org.apache.commons.lang3.StringUtils;
52 import org.glom.web.server.Log;
53 import org.glom.web.server.Utils;
54 import org.glom.web.shared.DataItem;
55 import org.glom.web.shared.libglom.CustomTitle;
56 import org.glom.web.shared.libglom.Field;
57 import org.glom.web.shared.libglom.Field.GlomFieldType;
58 import org.glom.web.shared.libglom.NumericFormat;
59 import org.glom.web.shared.libglom.Relationship;
60 import org.glom.web.shared.libglom.Report;
61 import org.glom.web.shared.libglom.Translatable;
62 import org.glom.web.shared.libglom.layout.Formatting;
63 import org.glom.web.shared.libglom.layout.LayoutGroup;
64 import org.glom.web.shared.libglom.layout.LayoutItem;
65 import org.glom.web.shared.libglom.layout.LayoutItemField;
66 import org.glom.web.shared.libglom.layout.LayoutItemImage;
67 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
68 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
69 import org.glom.web.shared.libglom.layout.LayoutItemPortal.NavigationType;
70 import org.glom.web.shared.libglom.layout.LayoutItemText;
71 import org.glom.web.shared.libglom.layout.StaticText;
72 import org.glom.web.shared.libglom.layout.TableToViewDetails;
73 import org.glom.web.shared.libglom.layout.UsesRelationship;
74 import org.glom.web.shared.libglom.layout.UsesRelationshipImpl;
75 import org.glom.web.shared.libglom.layout.reportparts.LayoutItemGroupBy;
76 import org.w3c.dom.Element;
77 import org.w3c.dom.Node;
78 import org.w3c.dom.NodeList;
79 import org.xml.sax.SAXException;
80
81 import com.google.common.io.Files;
82
83 /**
84  * @author Murray Cumming <murrayc@openismus.com>
85  * 
86  */
87 public class Document {
88
89         @SuppressWarnings("serial")
90         private static class TableInfo extends Translatable {
91                 private boolean isDefault;
92                 private boolean isHidden;
93
94                 private final Hashtable<String, Field> fieldsMap = new Hashtable<String, Field>();
95                 private final Hashtable<String, Relationship> relationshipsMap = new Hashtable<String, Relationship>();
96                 private final Hashtable<String, Report> reportsMap = new Hashtable<String, Report>();
97
98                 private List<LayoutGroup> layoutGroupsList = new ArrayList<LayoutGroup>();
99                 private List<LayoutGroup> layoutGroupsDetails = new ArrayList<LayoutGroup>();
100
101                 // A list of maps (field name to value).
102                 private List<Map<String, DataItem>> exampleRows = null;
103         }
104         
105         /** This is passed between methods to keep track of the hierarchy of layout items,
106          * so we can later use it to specify the path to a layout item.
107          */
108         private static class Path {
109                 public String tableName = null;
110                 public String layoutName = null;
111                 public int[] indices = new int[1];
112         }
113
114         private String fileURI = "";
115         private org.w3c.dom.Document xmlDocument = null;
116
117         private final Translatable databaseTitle = new Translatable();
118         private String translationOriginalLocale = "";
119         private final List<String> translationAvailableLocales = new ArrayList<String>();
120         private boolean isExample = false;
121         private HostingMode hostingMode = HostingMode.HOSTING_MODE_POSTGRES_CENTRAL;
122         private String connectionServer = "";
123         private String connectionDatabase = "";
124         private int connectionPort = 0;
125         private final Hashtable<String, TableInfo> tablesMap = new Hashtable<String, TableInfo>();
126         private String documentID = null; //Only for use in the Path, for use in image DataItems.
127
128         private static final String NODE_ROOT = "glom_document";
129         private static final String ATTRIBUTE_IS_EXAMPLE = "is_example";
130         private static final String ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE = "translation_original_locale";
131         private static final String NODE_CONNECTION = "connection";
132         private static final String ATTRIBUTE_CONNECTION_HOSTING_MODE = "hosting_mode";
133         private static final String ATTRIBUTE_CONNECTION_SERVER = "server";
134         private static final String ATTRIBUTE_CONNECTION_DATABASE = "database";
135         private static final String ATTRIBUTE_CONNECTION_PORT = "port";
136         private static final String NODE_TABLE = "table";
137         private static final String ATTRIBUTE_NAME = "name";
138         private static final String ATTRIBUTE_TITLE = "title";
139         private static final String DEPRECATED_ATTRIBUTE_DATABASE_TITLE = "database_title";
140         private static final String ATTRIBUTE_DEFAULT = "default";
141         private static final String ATTRIBUTE_HIDDEN = "hidden";
142         private static final String NODE_TRANSLATIONS_SET = "trans_set";
143         private static final String NODE_TRANSLATIONS = "trans";
144         private static final String ATTRIBUTE_TRANSLATION_LOCALE = "loc";
145         private static final String ATTRIBUTE_TRANSLATION_TITLE = "val";
146         private static final String NODE_REPORTS = "reports";
147         private static final String NODE_REPORT = "report";
148         private static final String NODE_FIELDS = "fields";
149         private static final String NODE_FIELD = "field";
150         private static final String NODE_EXAMPLE_ROWS = "example_rows";
151         private static final String NODE_EXAMPLE_ROW = "example_row";
152         private static final String NODE_VALUE = "value";
153         private static final String ATTRIBUTE_COLUMN = "column";
154         private static final String ATTRIBUTE_PRIMARY_KEY = "primary_key";
155         private static final String ATTRIBUTE_FIELD_TYPE = "type";
156         private static final String NODE_FORMATTING = "formatting";
157         // private static final String ATTRIBUTE_TEXT_FORMAT_MULTILINE = "format_text_multiline";
158         private static final String ATTRIBUTE_USE_THOUSANDS_SEPARATOR = "format_thousands_separator";
159         private static final String ATTRIBUTE_DECIMAL_PLACES = "format_decimal_places";
160         private static final String NODE_RELATIONSHIPS = "relationships";
161         private static final String NODE_RELATIONSHIP = "relationship";
162         private static final String ATTRIBUTE_RELATIONSHIP_FROM_FIELD = "key";
163         private static final String ATTRIBUTE_RELATIONSHIP_TO_TABLE = "other_table";
164         private static final String ATTRIBUTE_RELATIONSHIP_TO_FIELD = "other_key";
165         private static final String NODE_DATA_LAYOUTS = "data_layouts";
166         private static final String NODE_DATA_LAYOUT = "data_layout";
167         private static final String NODE_DATA_LAYOUT_GROUPS = "data_layout_groups";
168         private static final String NODE_DATA_LAYOUT_GROUP = "data_layout_group";
169         private static final String ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT = "columns_count";
170         private static final String NODE_DATA_LAYOUT_NOTEBOOK = "data_layout_notebook";
171         private static final String NODE_DATA_LAYOUT_PORTAL = "data_layout_portal";
172         private static final String NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP = "portal_navigation_relationship";
173         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE = "navigation_type";
174         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC = "automatic";
175         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC = "specific";
176         private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE = "none";
177         private static final String ATTRIBUTE_RELATIONSHIP_NAME = "relationship";
178         private static final String ATTRIBUTE_RELATED_RELATIONSHIP_NAME = "related_relationship";
179         private static final String NODE_DATA_LAYOUT_ITEM = "data_layout_item";
180         private static final String NODE_CUSTOM_TITLE = "title_custom";
181         private static final String ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM = "use_custom";
182         private static final String NODE_DATA_LAYOUT_TEXTOBJECT = "data_layout_text";
183         private static final String NODE_DATA_LAYOUT_TEXTOBJECT_TEXT = "text";
184         private static final String NODE_DATA_LAYOUT_IMAGEOBJECT = "data_layout_image";
185         private static final String NODE_DATA_LAYOUT_ITEM_GROUPBY = "data_layout_item_groupby";
186         private static final String NODE_GROUPBY = "groupby";
187         private static final String NODE_SECONDARY_FIELDS = "secondary_fields";
188         private static final String ATTRIBUTE_USE_DEFAULT_FORMATTING = "use_default_formatting";
189         public static final String LAYOUT_NAME_DETAILS = "details";
190         public static final String LAYOUT_NAME_LIST = "list";
191         private static final String QUOTE_FOR_FILE_FORMAT = "\"";
192
193         /**
194          * Instantiate a Document with no documentID,
195          * meaning that its LayoutItemImage items will not be able to provide a URI to request their data.
196          * This constructor is useful for tests.
197          */
198         public Document() {
199         }
200         
201         /**
202          * Instantiate a Document.
203          *
204          * @param documentID Used by LayoutItemImage items to provide a URI to request their data.
205          */
206         public Document(final String documentID) {
207                 this.documentID = documentID;
208         }
209
210         public void setFileURI(final String fileURI) {
211                 this.fileURI = fileURI;
212         }
213
214         public String getFileURI() {
215                 return fileURI;
216         }
217
218         // TODO: Make sure these have the correct values.
219         public enum LoadFailureCodes {
220                 LOAD_FAILURE_CODE_NONE, LOAD_FAILURE_CODE_NOT_FOUND, LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW
221         };
222
223         public boolean load() {
224                 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
225                 DocumentBuilder documentBuilder = null;
226                 try {
227                         documentBuilder = dbf.newDocumentBuilder();
228                 } catch (final ParserConfigurationException e) {
229                         // TODO Auto-generated catch block
230                         e.printStackTrace();
231                         return false;
232                 }
233
234                 try {
235                         xmlDocument = documentBuilder.parse(fileURI);
236                 } catch (final SAXException e) {
237                         // TODO Auto-generated catch block
238                         e.printStackTrace();
239                         return false;
240                 } catch (final IOException e) {
241                         // TODO Auto-generated catch block
242                         e.printStackTrace();
243                         return false;
244                 }
245
246                 final Element rootNode = xmlDocument.getDocumentElement();
247                 if (rootNode.getNodeName() != NODE_ROOT) {
248                         Log.error("Unexpected XML root node name found: " + rootNode.getNodeName());
249                         return false;
250                 }
251
252                 //Get the database title, falling back to the deprecated XML format for it:
253                 //TODO: load() show complain (via an enum result) if the document format version is less than 7.
254                 final String databaseTitleStr = rootNode.getAttribute(ATTRIBUTE_TITLE);
255                 final String deprecatedDatabaseTitleStr = rootNode.getAttribute(DEPRECATED_ATTRIBUTE_DATABASE_TITLE);
256                 if(!StringUtils.isEmpty(databaseTitleStr)) {
257                         databaseTitle.setTitleOriginal(databaseTitleStr);
258                 } else {
259                         databaseTitle.setTitleOriginal(deprecatedDatabaseTitleStr);
260                 }
261                 loadTitle(rootNode, databaseTitle);
262
263                 translationOriginalLocale = rootNode.getAttribute(ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE);
264                 translationAvailableLocales.add(translationOriginalLocale); // Just a cache.
265
266                 isExample = getAttributeAsBoolean(rootNode, ATTRIBUTE_IS_EXAMPLE);
267
268                 final Element nodeConnection = getElementByName(rootNode, NODE_CONNECTION);
269                 if (nodeConnection != null) {
270                         final String strHostingMode = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_HOSTING_MODE);
271                         if (strHostingMode.equals("postgres_central")) {
272                                 hostingMode = HostingMode.HOSTING_MODE_POSTGRES_CENTRAL;
273                         } else if (strHostingMode.equals("sqlite")) {
274                                 hostingMode = HostingMode.HOSTING_MODE_SQLITE;
275                         } else {
276                                 hostingMode = HostingMode.HOSTING_MODE_POSTGRES_SELF;
277                         }
278
279                         connectionServer = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_SERVER);
280                         connectionDatabase = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_DATABASE);
281                         connectionPort = (int) getAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT);
282                 }
283
284                 // We first load the fields, relationships, etc,
285                 // for all tables:
286                 final List<Node> listTableNodes = getChildrenByTagName(rootNode, NODE_TABLE);
287                 for (final Node node : listTableNodes) {
288                         if (!(node instanceof Element)) {
289                                 continue;
290                         }
291
292                         final Element element = (Element) node;
293                         final TableInfo info = loadTableNodeBasic(element);
294                         tablesMap.put(info.getName(), info);
295                 }
296
297                 // We then load the layouts for all tables, because they
298                 // need the fields and relationships for all tables:
299                 for (final Node node : listTableNodes) {
300                         if (!(node instanceof Element)) {
301                                 continue;
302                         }
303
304                         final Element element = (Element) node;
305                         final String tableName = element.getAttribute(ATTRIBUTE_NAME);
306
307                         // We first load the fields, relationships, etc:
308                         final TableInfo info = getTableInfo(tableName);
309                         if (info == null) {
310                                 continue;
311                         }
312
313                         // We then load the layouts afterwards, because they
314                         // need the fields and relationships:
315                         loadTableLayouts(element, info);
316
317                         tablesMap.put(info.getName(), info);
318                 }
319
320                 return true;
321         };
322
323         private Element getElementByName(final Element parentElement, final String tagName) {
324                 final List<Node> listNodes = getChildrenByTagName(parentElement, tagName);
325                 if (listNodes == null) {
326                         return null;
327                 }
328
329                 if (listNodes.size() == 0) {
330                         return null;
331                 }
332
333                 return (Element) listNodes.get(0);
334         }
335
336         private boolean getAttributeAsBoolean(final Element node, final String attributeName) {
337                 final String str = node.getAttribute(attributeName);
338                 if (str == null) {
339                         return false;
340                 }
341
342                 return str.equals("true");
343         }
344
345         private void setAttributeAsBoolean(final Element node, final String attributeName, final boolean value) {
346                 final String str = value ? "true" : "false";
347                 node.setAttribute(attributeName, str);
348         }
349
350         private double getAttributeAsDecimal(final Element node, final String attributeName) {
351                 final String str = node.getAttribute(attributeName);
352                 if (StringUtils.isEmpty(str)) {
353                         return 0;
354                 }
355
356                 double value = 0;
357                 try {
358                         value = Double.valueOf(str);
359                 } catch (final NumberFormatException e) {
360                         // e.printStackTrace();
361                 }
362
363                 return value;
364         }
365
366         private String getStringForDecimal(final double value) {
367                 final NumberFormat format = NumberFormat.getInstance(Locale.US);
368                 format.setGroupingUsed(false); // TODO: Does this change it system-wide?
369                 return format.format(value);
370         }
371
372         private void setAttributeAsDecimal(final Element node, final String attributeName, final double value) {
373                 node.setAttribute(attributeName, getStringForDecimal(value));
374         }
375
376         /**
377          * Load a title and its translations.
378          * 
379          * @param node
380          *            The XML Element that may contain a title attribute and a trans_set of translations of the title.
381          * @param title
382          */
383         private void loadTitle(final Element node, final Translatable title) {
384                 title.setName(node.getAttribute(ATTRIBUTE_NAME));
385
386                 title.setTitleOriginal(node.getAttribute(ATTRIBUTE_TITLE));
387
388                 final Element nodeSet = getElementByName(node, NODE_TRANSLATIONS_SET);
389                 if (nodeSet == null) {
390                         return;
391                 }
392
393                 final List<Node> listNodes = getChildrenByTagName(nodeSet, NODE_TRANSLATIONS);
394                 if (listNodes == null) {
395                         return;
396                 }
397
398                 for (final Node transNode : listNodes) {
399                         if (!(transNode instanceof Element)) {
400                                 continue;
401                         }
402
403                         final Element element = (Element) transNode;
404
405                         final String locale = element.getAttribute(ATTRIBUTE_TRANSLATION_LOCALE);
406                         final String translatedTitle = element.getAttribute(ATTRIBUTE_TRANSLATION_TITLE);
407                         if (!StringUtils.isEmpty(locale) && !StringUtils.isEmpty(translatedTitle)) {
408                                 title.setTitle(translatedTitle, locale);
409
410                                 // Remember any new translation locales in our cached list:
411                                 if (!translationAvailableLocales.contains(locale)) {
412                                         translationAvailableLocales.add(locale);
413                                 }
414                         }
415                 }
416         }
417
418         private void saveTitle(final org.w3c.dom.Document doc, final Element node, final Translatable title) {
419                 node.setAttribute(ATTRIBUTE_NAME, title.getName());
420
421                 node.setAttribute(ATTRIBUTE_TITLE, title.getTitleOriginal());
422
423                 final Element nodeSet = createElement(doc, node, NODE_TRANSLATIONS_SET);
424
425                 for (final Entry<String, String> entry : title.getTranslationsMap().entrySet()) {
426                         final Element element = createElement(doc, nodeSet, NODE_TRANSLATIONS);
427
428                         element.setAttribute(ATTRIBUTE_TRANSLATION_LOCALE, entry.getKey());
429                         element.setAttribute(ATTRIBUTE_TRANSLATION_TITLE, entry.getValue());
430                 }
431         }
432
433         /**
434          * @param tableNode
435          * @return
436          */
437         private TableInfo loadTableNodeBasic(final Element tableNode) {
438                 final TableInfo info = new TableInfo();
439                 loadTitle(tableNode, info);
440                 final String tableName = info.getName();
441
442                 info.isDefault = getAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT);
443                 info.isHidden = getAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN);
444
445                 // These should be loaded before the fields, because the fields use them.
446                 final Element relationshipsNode = getElementByName(tableNode, NODE_RELATIONSHIPS);
447                 if (relationshipsNode != null) {
448                         final List<Node> listNodes = getChildrenByTagName(relationshipsNode, NODE_RELATIONSHIP);
449                         for (final Node node : listNodes) {
450                                 if (!(node instanceof Element)) {
451                                         continue;
452                                 }
453
454                                 final Element element = (Element) node;
455                                 final Relationship relationship = new Relationship();
456                                 loadTitle(element, relationship);
457                                 relationship.setFromTable(tableName);
458                                 relationship.setFromField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD));
459                                 relationship.setToTable(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE));
460                                 relationship.setToField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD));
461
462                                 info.relationshipsMap.put(relationship.getName(), relationship);
463                         }
464                 }
465
466                 final Element fieldsNode = getElementByName(tableNode, NODE_FIELDS);
467                 if (fieldsNode != null) {
468                         final List<Node> listNodes = getChildrenByTagName(fieldsNode, NODE_FIELD);
469                         for (final Node node : listNodes) {
470                                 if (!(node instanceof Element)) {
471                                         continue;
472                                 }
473
474                                 final Element element = (Element) node;
475                                 final Field field = new Field();
476                                 loadField(element, field);
477
478                                 info.fieldsMap.put(field.getName(), field);
479                         }
480                 }
481
482                 // We do not normally use this,
483                 // though we do use it during testing, in SelfHoster, to recreate the database data.
484                 final Element exampleRowsNode = getElementByName(tableNode, NODE_EXAMPLE_ROWS);
485                 if (exampleRowsNode != null) {
486
487                         final List<Map<String, DataItem>> exampleRows = new ArrayList<Map<String, DataItem>>();
488                         final List<Node> listNodes = getChildrenByTagName(exampleRowsNode, NODE_EXAMPLE_ROW);
489                         for (final Node node : listNodes) {
490                                 if (!(node instanceof Element)) {
491                                         continue;
492                                 }
493
494                                 final Element element = (Element) node;
495                                 final Map<String, DataItem> row = new HashMap<String, DataItem>();
496
497                                 final List<Node> listNodesValues = getChildrenByTagName(element, NODE_VALUE);
498                                 for (final Node nodeValue : listNodesValues) {
499                                         if (!(nodeValue instanceof Element)) {
500                                                 continue;
501                                         }
502
503                                         final Element elementValue = (Element) nodeValue;
504                                         final String fieldName = elementValue.getAttribute(ATTRIBUTE_COLUMN);
505                                         if (StringUtils.isEmpty(fieldName)) {
506                                                 continue;
507                                         }
508
509                                         DataItem value = null;
510                                         final Field field = info.fieldsMap.get(fieldName);
511                                         if (field != null) {
512                                                 value = getNodeTextChildAsValue(elementValue, field.getGlomType());
513                                         }
514                                         row.put(fieldName, value);
515                                 }
516
517                                 exampleRows.add(row);
518                         }
519
520                         info.exampleRows = exampleRows;
521                 }
522
523                 return info;
524         }
525
526         /**
527          * @param elementValue
528          * @param glomType
529          * @return
530          */
531         private DataItem getNodeTextChildAsValue(final Element element, final GlomFieldType type) {
532                 final DataItem result = new DataItem();
533
534                 final String str = element.getTextContent();
535
536                 // Unescape "" to ", because to_file_format() escaped ", as specified by the CSV RFC:
537                 String unescaped = "";
538                 if (type == GlomFieldType.TYPE_IMAGE) {
539                         unescaped = str; // binary data does not have quote characters so we do not bother to escape or unescape it.
540                 } else {
541                         unescaped = str.replace(QUOTE_FOR_FILE_FORMAT + QUOTE_FOR_FILE_FORMAT, QUOTE_FOR_FILE_FORMAT);
542                 }
543
544                 switch (type) {
545                 case TYPE_BOOLEAN: {
546                         final boolean value = (unescaped.equals("true"));
547                         result.setBoolean(value);
548                         break;
549                 }
550                 case TYPE_DATE: {
551                         final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ROOT);
552                         Date value = null;
553                         try {
554                                 value = dateFormat.parse(unescaped);
555                         } catch (final ParseException e) {
556                                 // e.printStackTrace();
557                         }
558                         result.setDate(value);
559                         break;
560                 }
561                 case TYPE_IMAGE: {
562                         //Glom (at least since 2.23/24) uses base64 for the images:
563                         
564                         //This is only used on the server-side,
565                         //either to create a database, during tests,
566                         //or to return the full data from our OnlineGlomImage service.
567                         //It is removed before being passed to the client-side.
568                         
569                         /* This does not seem to work with the text from g_base64_encode() that Glom uses,
570                          * maybe because of the newlines, which are apparently OK:
571                          * http://en.wikipedia.org/wiki/Base64#MIME
572                          * final byte[] bytes = com.google.gwt.user.server.Base64Utils.fromBase64(unescaped);
573                          */
574                         
575                         /* Use org.apache.commons.codec.binary.Base64: */
576                         final Base64 decoder = new Base64();
577                         byte[] bytes = (byte[]) decoder.decode(unescaped.getBytes());
578
579                         result.setImageData(bytes);
580
581                         break;
582                 }
583                 case TYPE_NUMERIC: {
584                         double value = 0;
585                         try {
586                                 value = Double.valueOf(unescaped);
587                         } catch (final NumberFormatException e) {
588                                 // e.printStackTrace();
589                         }
590
591                         result.setNumber(value);
592                         break;
593                 }
594                 case TYPE_TEXT:
595                         result.setText(unescaped);
596                         break;
597                 case TYPE_TIME:
598                         // TODO
599                         break;
600                 default:
601                         Log.error(documentID, "getNodeTextChildAsValue(): unexpected or invalid field type.");
602                         break;
603                 }
604
605                 return result;
606         }
607
608         private void setNodeTextChildAsValue(final Element element, final DataItem value, final GlomFieldType type) {
609                 String str = "";
610
611                 switch (type) {
612                 case TYPE_BOOLEAN: {
613                         str = value.getBoolean() ? "true" : "false";
614                         break;
615                 }
616                 case TYPE_DATE: {
617                         // TODO: This is not really the format used by the Glom document:
618                         final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ROOT);
619                         str = dateFormat.format(value.getDate());
620                         break;
621                 }
622                 case TYPE_IMAGE: {
623                         str = ""; // TODO
624                         break;
625                 }
626                 case TYPE_NUMERIC: {
627                         str = getStringForDecimal(value.getNumber());
628                         break;
629                 }
630                 case TYPE_TEXT:
631                         str = value.getText();
632                         break;
633                 case TYPE_TIME:
634                         str = ""; // TODO
635                         break;
636                 default:
637                         Log.error(documentID, "setNodeTextChildAsValue(): unexpected or invalid field type.");
638                         break;
639                 }
640
641                 final String escaped = str.replace(QUOTE_FOR_FILE_FORMAT, QUOTE_FOR_FILE_FORMAT + QUOTE_FOR_FILE_FORMAT);
642                 element.setTextContent(escaped);
643         }
644
645         private void saveTableNodeBasic(final org.w3c.dom.Document doc, final Element tableNode, final TableInfo info) {
646                 saveTitle(doc, tableNode, info);
647
648                 setAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT, info.isDefault);
649                 setAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN, info.isHidden);
650
651                 final Element relationshipsNode = createElement(doc, tableNode, NODE_RELATIONSHIPS);
652                 for (final Relationship relationship : info.relationshipsMap.values()) {
653                         final Element element = createElement(doc, relationshipsNode, NODE_RELATIONSHIP);
654                         saveTitle(doc, element, relationship);
655
656                         element.setAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD, relationship.getFromField());
657                         element.setAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE, relationship.getToTable());
658                         element.setAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD, relationship.getToField());
659                 }
660
661                 final Element fieldsNode = createElement(doc, tableNode, NODE_FIELDS);
662                 for (final Field field : info.fieldsMap.values()) {
663                         final Element element = createElement(doc, fieldsNode, NODE_FIELD);
664                         saveField(doc, element, field);
665                 }
666
667                 final Element exampleRowsNode = createElement(doc, tableNode, NODE_EXAMPLE_ROWS);
668
669                 for (final Map<String, DataItem> row : info.exampleRows) {
670                         final Element node = createElement(doc, exampleRowsNode, NODE_EXAMPLE_ROW);
671
672                         // TODO: This assumes that fieldsMap.values() will have the same sequence as the values,
673                         final int i = 0;
674                         for (final Field field : info.fieldsMap.values()) {
675                                 if (i < row.size()) {
676                                         break;
677                                 }
678
679                                 final String fieldName = field.getName();
680                                 if (StringUtils.isEmpty(fieldName)) {
681                                         continue;
682                                 }
683
684                                 final DataItem dataItem = row.get(fieldName);
685                                 if (dataItem == null) {
686                                         continue;
687                                 }
688
689                                 final Element elementValue = createElement(doc, node, NODE_VALUE);
690                                 elementValue.setAttribute(ATTRIBUTE_COLUMN, fieldName);
691                                 setNodeTextChildAsValue(elementValue, dataItem, field.getGlomType());
692                         }
693                 }
694         }
695
696         /**
697          * @param doc
698          * @param element
699          * @param field
700          */
701         private void saveField(final org.w3c.dom.Document doc, final Element element, final Field field) {
702                 saveTitle(doc, element, field);
703
704                 String fieldTypeStr = "";
705
706                 switch (field.getGlomType()) {
707                 case TYPE_BOOLEAN:
708                         fieldTypeStr = "Boolean";
709                         break;
710                 case TYPE_DATE:
711                         fieldTypeStr = "Date";
712                         break;
713                 case TYPE_IMAGE:
714                         fieldTypeStr = "Image";
715                         break;
716                 case TYPE_NUMERIC:
717                         fieldTypeStr = "Number";
718                         break;
719                 case TYPE_TEXT:
720                         fieldTypeStr = "Text";
721                         break;
722                 case TYPE_TIME:
723                         fieldTypeStr = "Time";
724                         break;
725                 default:
726                         break;
727                 }
728                 element.setAttribute(ATTRIBUTE_FIELD_TYPE, fieldTypeStr);
729
730                 setAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY, field.getPrimaryKey());
731
732                 final Element elementFormatting = createElement(doc, element, NODE_FORMATTING);
733                 saveFormatting(elementFormatting, field.getFormatting());
734         }
735
736         /**
737          * @param elementFormatting
738          * @param formatting
739          */
740         private void saveFormatting(final Element element, final Formatting formatting) {
741                 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
742
743                 final NumericFormat numericFormatting = formatting.getNumericFormat();
744                 if (numericFormatting != null) {
745                         setAttributeAsBoolean(element, ATTRIBUTE_USE_THOUSANDS_SEPARATOR,
746                                         numericFormatting.getUseThousandsSeparator());
747                         setAttributeAsDecimal(element, ATTRIBUTE_DECIMAL_PLACES, numericFormatting.getDecimalPlaces());
748                 }
749         }
750
751         /**
752          * @param tableNode
753          * @param info
754          */
755         private void loadTableLayouts(final Element tableNode, final TableInfo info) {
756                 final String tableName = info.getName();
757
758                 final Element layoutsNode = getElementByName(tableNode, NODE_DATA_LAYOUTS);
759                 if (layoutsNode != null) {
760                         final List<Node> listNodes = getChildrenByTagName(layoutsNode, NODE_DATA_LAYOUT);
761                         for (final Node node : listNodes) {
762                                 if (!(node instanceof Element)) {
763                                         continue;
764                                 }
765
766                                 final Element element = (Element) node;
767                                 final String name = element.getAttribute(ATTRIBUTE_NAME);
768                                 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName, name);
769                                 if (name.equals(LAYOUT_NAME_DETAILS)) {
770                                         info.layoutGroupsDetails = listLayoutGroups;
771                                 } else if (name.equals(LAYOUT_NAME_LIST)) {
772                                         info.layoutGroupsList = listLayoutGroups;
773                                 } else {
774                                         Log.error(documentID, "loadTableNode(): unexpected layout name: " + name);
775                                 }
776                         }
777                 }
778
779                 final Element reportsNode = getElementByName(tableNode, NODE_REPORTS);
780                 if (reportsNode != null) {
781                         final List<Node> listNodes = getChildrenByTagName(reportsNode, NODE_REPORT);
782                         for (final Node node : listNodes) {
783                                 if (!(node instanceof Element)) {
784                                         continue;
785                                 }
786
787                                 final Element element = (Element) node;
788                                 final Report report = new Report();
789                                 loadReport(element, report, tableName);
790
791                                 info.reportsMap.put(report.getName(), report);
792                         }
793                 }
794         }
795
796         /**
797          * @param node
798          * @return
799          */
800         private List<LayoutGroup> loadLayoutNode(final Element node, final String tableName, final String layoutName) {
801                 if (node == null) {
802                         return null;
803                 }
804
805                 final List<LayoutGroup> result = new ArrayList<LayoutGroup>();
806                 int groupIndex = 0;
807                 final List<Node> listNodes = getChildrenByTagName(node, NODE_DATA_LAYOUT_GROUPS);
808                 for (final Node nodeGroups : listNodes) {
809                         if (!(nodeGroups instanceof Element)) {
810                                 continue;
811                         }
812
813                         final Element elementGroups = (Element) nodeGroups;
814
815                         final NodeList list = elementGroups.getChildNodes();
816                         final int num = list.getLength();
817                         for (int i = 0; i < num; i++) {
818                                 final Node nodeLayoutGroup = list.item(i);
819                                 if (nodeLayoutGroup == null) {
820                                         continue;
821                                 }
822
823                                 if (!(nodeLayoutGroup instanceof Element)) {
824                                         continue;
825                                 }
826                                 
827                                 final Path path = new Path();
828                                 path.tableName = tableName;
829                                 path.layoutName = layoutName;
830                                 path.indices[0 /* depth */] = groupIndex;
831                                 ++groupIndex;
832
833                                 final Element element = (Element) nodeLayoutGroup;
834                                 final String tagName = element.getTagName();
835                                 if (tagName.equals(NODE_DATA_LAYOUT_GROUP)) {
836                                         final LayoutGroup group = new LayoutGroup();
837                                         loadDataLayoutGroup(element, group, tableName, path);
838                                         result.add(group);
839                                 } else if (tagName.equals(NODE_DATA_LAYOUT_NOTEBOOK)) {
840                                         final LayoutItemNotebook group = new LayoutItemNotebook();
841                                         loadDataLayoutGroup(element, group, tableName, path);
842                                         result.add(group);
843                                 } else if (tagName.equals(NODE_DATA_LAYOUT_PORTAL)) {
844                                         final LayoutItemPortal portal = new LayoutItemPortal();
845                                         loadDataLayoutPortal(element, portal, tableName, path);
846                                         result.add(portal);
847                                 }
848                         }
849                 }
850
851                 return result;
852         }
853
854         /**
855          * @param element
856          * @param tableName
857          * @param portal
858          */
859         private void loadUsesRelationship(final Element element, final String tableName, final UsesRelationship item) {
860                 if (element == null) {
861                         return;
862                 }
863
864                 if (item == null) {
865                         return;
866                 }
867
868                 final String relationshipName = element.getAttribute(ATTRIBUTE_RELATIONSHIP_NAME);
869                 Relationship relationship = null;
870                 if (!StringUtils.isEmpty(relationshipName)) {
871                         // std::cout << "  debug in : tableName=" << tableName << ", relationshipName=" << relationship_name <<
872                         // std::endl;
873                         relationship = getRelationship(tableName, relationshipName);
874                         item.setRelationship(relationship);
875
876                         if (relationship == null) {
877                                 Log.error("relationship not found: " + relationshipName + ", in table: " + tableName);
878                         }
879                 }
880
881                 // TODO: Unit test loading of doubly-related fields.
882                 final String relatedRelationshipName = element.getAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME);
883                 if (!StringUtils.isEmpty(relatedRelationshipName) && (relationship != null)) {
884                         final Relationship relatedRelationship = getRelationship(relationship.getToTable(), relatedRelationshipName);
885                         item.setRelatedRelationship(relatedRelationship);
886
887                         if (relatedRelationship == null) {
888                                 Log.error("related relationship not found in table=" + relationship.getToTable() + ",  name="
889                                                 + relatedRelationshipName);
890                         }
891                 }
892         }
893
894         /**
895          * getElementsByTagName() is recursive, but we do not want that.
896          * 
897          * @param node
898          * @param
899          * @return
900          */
901         private List<Node> getChildrenByTagName(final Element parentNode, final String tagName) {
902                 final List<Node> result = new ArrayList<Node>();
903
904                 final NodeList list = parentNode.getElementsByTagName(tagName);
905                 final int num = list.getLength();
906                 for (int i = 0; i < num; i++) {
907                         final Node node = list.item(i);
908                         if (node == null) {
909                                 continue;
910                         }
911
912                         final Node itemParentNode = node.getParentNode();
913                         if (itemParentNode.equals(parentNode)) {
914                                 result.add(node);
915                         }
916                 }
917
918                 return result;
919         }
920
921         /**
922          * @param element
923          * @param group
924          */
925         private void loadDataLayoutGroup(final Element nodeGroup, final LayoutGroup group, final String tableName, final Path path) {
926                 loadTitle(nodeGroup, group);
927
928                 // Read the column count:
929                 int columnCount = (int) getAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT);
930                 if (columnCount < 1) {
931                         columnCount = 1; // 0 is a useless default.
932                 }
933                 group.setColumnCount(columnCount);
934
935                 final int depth = path.indices.length;
936
937                 // Get the child items:
938                 final NodeList listNodes = nodeGroup.getChildNodes();
939                 final int num = listNodes.getLength();
940                 int pathIndex = 0;
941                 for (int i = 0; i < num; i++) {
942
943                         final Node node = listNodes.item(i);
944                         if (!(node instanceof Element)) {
945                                 continue;
946                         }
947
948                         final Element element = (Element) node;
949                         final String tagName = element.getTagName();
950                         
951                         //Do not increment pathIndex for an item
952                         //that we will not use:
953                         if(tagName.equals(NODE_TRANSLATIONS_SET)) {
954                                 continue;
955                         }
956
957                         // Create a path of indices for the child:
958                         final Path pathChild = new Path();
959                         pathChild.tableName = path.tableName;
960                         pathChild.layoutName = path.layoutName;
961                         pathChild.indices = new int[path.indices.length + 1];
962                         System.arraycopy( path.indices, 0, pathChild.indices, 0, path.indices.length );
963                         pathChild.indices[depth] = pathIndex;
964                         pathIndex++;
965
966                         if (tagName.equals(NODE_DATA_LAYOUT_GROUP)) {
967                                 final LayoutGroup childGroup = new LayoutGroup();
968                                 loadDataLayoutGroup(element, childGroup, tableName, pathChild);
969                                 group.addItem(childGroup);
970                         } else if (tagName.equals(NODE_DATA_LAYOUT_NOTEBOOK)) {
971                                 final LayoutItemNotebook childGroup = new LayoutItemNotebook();
972                                 loadDataLayoutGroup(element, childGroup, tableName, pathChild);
973                                 group.addItem(childGroup);
974                         } else if (tagName.equals(NODE_DATA_LAYOUT_PORTAL)) {
975                                 final LayoutItemPortal childGroup = new LayoutItemPortal();
976                                 loadDataLayoutPortal(element, childGroup, tableName, pathChild);
977                                 group.addItem(childGroup);
978                         } else if (tagName.equals(NODE_DATA_LAYOUT_ITEM)) {
979                                 final LayoutItemField item = new LayoutItemField();
980                                 loadDataLayoutItemField(element, item, tableName);
981                                 group.addItem(item);
982                         } else if (tagName.equals(NODE_DATA_LAYOUT_TEXTOBJECT)) {
983                                 final LayoutItemText item = new LayoutItemText();
984                                 loadDataLayoutItemText(element, item);
985                                 group.addItem(item);
986                         } else if (tagName.equals(NODE_DATA_LAYOUT_IMAGEOBJECT)) {
987                                 final LayoutItemImage item = new LayoutItemImage();
988                                 loadDataLayoutItemImage(element, item, pathChild);
989                                 group.addItem(item);
990                         } else if (tagName.equals(NODE_DATA_LAYOUT_ITEM_GROUPBY)) {
991                                 final LayoutItemGroupBy item = new LayoutItemGroupBy();
992                                 loadDataLayoutItemGroupBy(element, item, tableName, pathChild);
993                                 group.addItem(item);
994                         }
995                 }
996         }
997
998         /**
999          * @param element
1000          * @param item
1001          */
1002         private void loadDataLayoutItemImage(Element element, LayoutItemImage item, final Path path) {
1003                 loadTitle(element, item);
1004                 
1005                 final Element elementValue = getElementByName(element, NODE_VALUE);
1006                 if (elementValue == null) {
1007                         return;
1008                 }
1009
1010                 final DataItem image = getNodeTextChildAsValue(elementValue, Field.GlomFieldType.TYPE_IMAGE);
1011                 
1012                 //This lets the client-side request the full data from our OnlineGlomImage service.
1013                 final String layoutPath = Utils.buildImageDataUrl(documentID, path.tableName, path.layoutName, path.indices);
1014                 image.setImageDataUrl(layoutPath);
1015                 
1016                 item.setImage(image);
1017         }
1018
1019         /**
1020          * @param element
1021          * @param item
1022          */
1023         private void loadDataLayoutItemText(Element element, LayoutItemText item) {
1024                 loadTitle(element, item);
1025                 
1026                 final Element elementText = getElementByName(element, NODE_DATA_LAYOUT_TEXTOBJECT_TEXT);
1027                 if (elementText == null) {
1028                         return;
1029                 }
1030                 
1031                 final StaticText text = new StaticText();
1032                 loadTitle(elementText, text); //This node reuses the title structure to hold its text.
1033                 item.setText(text);
1034         }
1035
1036         /**
1037          * @param element
1038          * @param item
1039          * @param tableName
1040          */
1041         private void loadDataLayoutItemGroupBy(final Element element, final LayoutItemGroupBy item, final String tableName, final Path path) {
1042                 loadDataLayoutGroup(element, item, tableName, path);
1043
1044                 final Element elementGroupBy = getElementByName(element, NODE_GROUPBY);
1045                 if (elementGroupBy == null) {
1046                         return;
1047                 }
1048
1049                 final LayoutItemField fieldGroupBy = new LayoutItemField();
1050                 loadDataLayoutItemField(elementGroupBy, fieldGroupBy, tableName);
1051                 item.setFieldGroupBy(fieldGroupBy);
1052
1053                 final Element elementSecondaryFields = getElementByName(element, NODE_SECONDARY_FIELDS);
1054                 if (elementSecondaryFields == null) {
1055                         return;
1056                 }
1057
1058                 final Element elementLayoutGroup = getElementByName(elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
1059                 if (elementLayoutGroup != null) {
1060                         final LayoutGroup secondaryLayoutGroup = new LayoutGroup();
1061                         loadDataLayoutGroup(elementLayoutGroup, secondaryLayoutGroup, tableName, path); //TODO: Add the main group items count to path first?
1062                         item.setSecondaryFields(secondaryLayoutGroup);
1063                 }
1064         }
1065
1066         /**
1067          * @param element
1068          * @param item
1069          */
1070         private void loadDataLayoutItemField(final Element element, final LayoutItemField item, final String tableName) {
1071                 item.setName(element.getAttribute(ATTRIBUTE_NAME));
1072                 loadUsesRelationship(element, tableName, item);
1073
1074                 final Element elementCustomTitle = getElementByName(element, NODE_CUSTOM_TITLE);
1075                 if (elementCustomTitle != null) {
1076                         final CustomTitle customTitle = item.getCustomTitle();
1077                         customTitle.setUseCustomTitle(getAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM));
1078                         loadTitle(elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
1079                 }
1080
1081                 // Get the actual field:
1082                 final String fieldName = item.getName();
1083                 final String inTableName = item.getTableUsed(tableName);
1084                 final Field field = getField(inTableName, fieldName);
1085                 item.setFullFieldDetails(field);
1086
1087                 item.setUseDefaultFormatting(getAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING));
1088
1089                 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
1090                 if (elementFormatting != null) {
1091                         loadFormatting(elementFormatting, item.getFormatting());
1092                 }
1093         }
1094
1095         /**
1096          * @param element
1097          * @param childGroup
1098          */
1099         private void loadDataLayoutPortal(final Element element, final LayoutItemPortal portal, final String tableName, final Path path) {
1100                 loadUsesRelationship(element, tableName, portal);
1101                 final String relatedTableName = portal.getTableUsed(tableName);
1102                 loadDataLayoutGroup(element, portal, relatedTableName, path);
1103
1104                 final Element elementNavigation = getElementByName(element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
1105                 if (elementNavigation != null) {
1106                         final String navigationTypeAsString = elementNavigation.getAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE);
1107                         if (StringUtils.isEmpty(navigationTypeAsString)
1108                                         || navigationTypeAsString.equals(ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC)) {
1109                                 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC);
1110                         } else if (navigationTypeAsString.equals(ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE)) {
1111                                 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_NONE);
1112                         } else if (navigationTypeAsString.equals(ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC)) {
1113                                 // Read the specified relationship name:
1114                                 final UsesRelationship relationshipNavigationSpecific = new UsesRelationshipImpl();
1115                                 loadUsesRelationship(elementNavigation, relatedTableName, relationshipNavigationSpecific);
1116                                 portal.setNavigationRelationshipSpecific(relationshipNavigationSpecific);
1117                         }
1118                 }
1119
1120         }
1121
1122         /**
1123          * @param element
1124          * @param field
1125          */
1126         private void loadField(final Element element, final Field field) {
1127                 loadTitle(element, field);
1128
1129                 Field.GlomFieldType fieldType = Field.GlomFieldType.TYPE_INVALID;
1130                 final String fieldTypeStr = element.getAttribute(ATTRIBUTE_FIELD_TYPE);
1131                 if (!StringUtils.isEmpty(fieldTypeStr)) {
1132                         if (fieldTypeStr.equals("Boolean")) {
1133                                 fieldType = Field.GlomFieldType.TYPE_BOOLEAN;
1134                         } else if (fieldTypeStr.equals("Date")) {
1135                                 fieldType = Field.GlomFieldType.TYPE_DATE;
1136                         } else if (fieldTypeStr.equals("Image")) {
1137                                 fieldType = Field.GlomFieldType.TYPE_IMAGE;
1138                         } else if (fieldTypeStr.equals("Number")) {
1139                                 fieldType = Field.GlomFieldType.TYPE_NUMERIC;
1140                         } else if (fieldTypeStr.equals("Text")) {
1141                                 fieldType = Field.GlomFieldType.TYPE_TEXT;
1142                         } else if (fieldTypeStr.equals("Time")) {
1143                                 fieldType = Field.GlomFieldType.TYPE_TIME;
1144                         }
1145                 }
1146
1147                 field.setGlomFieldType(fieldType);
1148
1149                 field.setPrimaryKey(getAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY));
1150
1151                 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
1152                 if (elementFormatting != null) {
1153                         loadFormatting(elementFormatting, field.getFormatting());
1154                 }
1155         }
1156
1157         /**
1158          * @param elementFormatting
1159          * @param formatting
1160          */
1161         private void loadFormatting(final Element elementFormatting, final Formatting formatting) {
1162                 if (elementFormatting == null) {
1163                         return;
1164                 }
1165
1166                 if (formatting == null) {
1167                         return;
1168                 }
1169
1170                 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
1171
1172                 final NumericFormat numericFormatting = formatting.getNumericFormat();
1173                 if (numericFormatting != null) {
1174                         numericFormatting.setUseThousandsSeparator(getAttributeAsBoolean(elementFormatting,
1175                                         ATTRIBUTE_USE_THOUSANDS_SEPARATOR));
1176                         numericFormatting
1177                                         .setDecimalPlaces((int) getAttributeAsDecimal(elementFormatting, ATTRIBUTE_DECIMAL_PLACES));
1178                 }
1179
1180         }
1181
1182         /**
1183          * @param element
1184          * @param reportNode
1185          */
1186         private void loadReport(final Element element, final Report report, final String tableName) {
1187                 report.setName(element.getAttribute(ATTRIBUTE_NAME));
1188                 loadTitle(element, report);
1189
1190                 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName, null /* not needed */);
1191
1192                 // A report can actually only have one LayoutGroup,
1193                 // though it uses the same XML structure as List and Details layouts,
1194                 // which (wrongly) suggests that it can have more than one group.
1195                 LayoutGroup layoutGroup = null;
1196                 if (!listLayoutGroups.isEmpty()) {
1197                         layoutGroup = listLayoutGroups.get(0);
1198                 }
1199
1200                 report.setLayoutGroup(layoutGroup);
1201         }
1202
1203         private TableInfo getTableInfo(final String tableName) {
1204                 return tablesMap.get(tableName);
1205         }
1206
1207         public enum HostingMode {
1208                 HOSTING_MODE_POSTGRES_CENTRAL, HOSTING_MODE_POSTGRES_SELF, HOSTING_MODE_SQLITE
1209         };
1210
1211         public String getDatabaseTitle(final String locale) {
1212                 return databaseTitle.getTitle(locale);
1213         }
1214
1215         public String getDatabaseTitleOriginal() {
1216                 return databaseTitle.getTitleOriginal();
1217         }
1218
1219         public List<String> getTranslationAvailableLocales() {
1220                 return translationAvailableLocales;
1221         }
1222
1223         public Document.HostingMode getHostingMode() {
1224                 return hostingMode;
1225         }
1226
1227         /**
1228          * @param hostingMode
1229          */
1230         public void setHostingMode(final HostingMode hostingMode) {
1231                 this.hostingMode = hostingMode;
1232         }
1233
1234         public String getConnectionServer() {
1235                 return connectionServer;
1236         }
1237
1238         public int getConnectionPort() {
1239                 return connectionPort;
1240         }
1241
1242         public void setConnectionPort(final int port) {
1243                 connectionPort = port;
1244         }
1245
1246         public String getConnectionDatabase() {
1247                 return connectionDatabase;
1248         }
1249
1250         public List<String> getTableNames() {
1251                 // TODO: Return a Set?
1252                 return new ArrayList<String>(tablesMap.keySet());
1253         }
1254
1255         public boolean getTableIsHidden(final String tableName) {
1256                 final TableInfo info = getTableInfo(tableName);
1257                 if (info == null) {
1258                         return false;
1259                 }
1260
1261                 return info.isHidden;
1262         }
1263
1264         public String getTableTitle(final String tableName, final String locale) {
1265                 final TableInfo info = getTableInfo(tableName);
1266                 if (info == null) {
1267                         return "";
1268                 }
1269
1270                 return info.getTitle(locale);
1271         }
1272
1273         public List<Map<String, DataItem>> getExampleRows(final String tableName) {
1274                 final TableInfo info = getTableInfo(tableName);
1275                 if (info == null) {
1276                         return null;
1277                 }
1278
1279                 return info.exampleRows;
1280         }
1281
1282         public String getDefaultTable() {
1283                 for (final TableInfo info : tablesMap.values()) {
1284                         if (info.isDefault) {
1285                                 return info.getName();
1286                         }
1287                 }
1288
1289                 return "";
1290         }
1291
1292         public boolean getTableIsKnown(final String tableName) {
1293                 final TableInfo info = getTableInfo(tableName);
1294                 if (info == null) {
1295                         return false;
1296                 }
1297
1298                 return true;
1299         }
1300
1301         public List<Field> getTableFields(final String tableName) {
1302                 final TableInfo info = getTableInfo(tableName);
1303                 if (info == null) {
1304                         return null;
1305                 }
1306
1307                 return new ArrayList<Field>(info.fieldsMap.values());
1308         }
1309
1310         public Field getField(final String tableName, final String strFieldName) {
1311                 final TableInfo info = getTableInfo(tableName);
1312                 if (info == null) {
1313                         return null;
1314                 }
1315
1316                 return info.fieldsMap.get(strFieldName);
1317         }
1318
1319         public List<LayoutGroup> getDataLayoutGroups(final String layoutName, final String parentTableName) {
1320                 final TableInfo info = getTableInfo(parentTableName);
1321                 if (info == null) {
1322                         return new ArrayList<LayoutGroup>();
1323                 }
1324
1325                 if (layoutName.equals(LAYOUT_NAME_DETAILS)) {
1326                         return info.layoutGroupsDetails;
1327                 } else if (layoutName.equals(LAYOUT_NAME_LIST)) {
1328                         return info.layoutGroupsList;
1329                 } else {
1330                         return new ArrayList<LayoutGroup>();
1331                 }
1332         }
1333
1334         public List<String> getReportNames(final String tableName) {
1335                 final TableInfo info = getTableInfo(tableName);
1336                 if (info == null) {
1337                         return new ArrayList<String>();
1338                 }
1339
1340                 return new ArrayList<String>(info.reportsMap.keySet());
1341         }
1342
1343         public Report getReport(final String tableName, final String reportName) {
1344                 final TableInfo info = getTableInfo(tableName);
1345                 if (info == null) {
1346                         return null;
1347                 }
1348
1349                 return info.reportsMap.get(reportName);
1350         }
1351
1352         /**
1353          * @param tableName
1354          * @param field
1355          * @return
1356          */
1357         public Relationship getFieldUsedInRelationshipToOne(final String tableName, final LayoutItemField layoutField) {
1358
1359                 if (layoutField == null) {
1360                         Log.error("layoutField was null");
1361                         return null;
1362                 }
1363
1364                 Relationship result = null;
1365
1366                 final String tableUsed = layoutField.getTableUsed(tableName);
1367                 final TableInfo info = getTableInfo(tableUsed);
1368                 if (info == null) {
1369                         // This table is special. We would not create a relationship to it using a field:
1370                         // if(tableUsed == GLOM_STANDARD_TABLE_PREFS_TABLE_NAME)
1371                         // return result;
1372
1373                         Log.error("table not found: " + tableUsed);
1374                         return null;
1375                 }
1376
1377                 // Look at each relationship:
1378                 final String fieldName = layoutField.getName();
1379                 for (final Relationship relationship : info.relationshipsMap.values()) {
1380                         if (relationship != null) {
1381                                 // If the relationship uses the field
1382                                 if (StringUtils.equals(relationship.getFromField(), fieldName)) {
1383                                         // if the to_table is not hidden:
1384                                         if (!getTableIsHidden(relationship.getToTable())) {
1385                                                 // TODO_Performance: The use of this convenience method means we get the full relationship
1386                                                 // information again:
1387                                                 if (getRelationshipIsToOne(tableName, relationship.getName())) {
1388                                                         result = relationship;
1389                                                 }
1390                                         }
1391                                 }
1392                         }
1393                 }
1394
1395                 return result;
1396         }
1397
1398         /**
1399          * @param tableName
1400          * @param relationshipName
1401          * @return
1402          */
1403         private boolean getRelationshipIsToOne(final String tableName, final String relationshipName) {
1404                 final Relationship relationship = getRelationship(tableName, relationshipName);
1405                 if (relationship != null) {
1406                         final Field fieldTo = getField(relationship.getToTable(), relationship.getToField());
1407                         if (fieldTo != null) {
1408                                 return (fieldTo.getPrimaryKey() || fieldTo.getUniqueKey());
1409                         }
1410                 }
1411
1412                 return false;
1413         }
1414
1415         /** Get the relationship by name for a table.
1416          * 
1417          * @param tableName
1418          * @param relationshipName
1419          * @return
1420          */
1421         public Relationship getRelationship(final String tableName, final String relationshipName) {
1422                 final TableInfo info = getTableInfo(tableName);
1423                 if (info == null) {
1424                         Log.error("table not found: " + tableName);
1425                         return null;
1426                 }
1427
1428                 return info.relationshipsMap.get(relationshipName);
1429         }
1430
1431         /**
1432          * @param tableName
1433          *            Output parameter
1434          * @param relationship
1435          * @param portal
1436          *            TODO
1437          */
1438         public TableToViewDetails getPortalSuitableTableToViewDetails(final LayoutItemPortal portal) {
1439                 UsesRelationship navigationRelationship = null;
1440
1441                 // Check whether a relationship was specified:
1442                 if (portal.getNavigationType() == NavigationType.NAVIGATION_AUTOMATIC) {
1443                         navigationRelationship = getPortalNavigationRelationshipAutomatic(portal);
1444                 } else {
1445                         navigationRelationship = portal.getNavigationRelationshipSpecific();
1446                 }
1447
1448                 // Get the navigation table name from the chosen relationship:
1449                 final String directlyRelatedTableName = portal.getTableUsed("" /* not relevant */);
1450
1451                 // The navigation_table_name (and therefore, the table_name output parameter,
1452                 // as well) stays empty if the navrel type was set to none.
1453                 String navigationTableName = null;
1454                 if (navigationRelationship != null) {
1455                         navigationTableName = navigationRelationship.getTableUsed(directlyRelatedTableName);
1456                 } else if (portal.getNavigationType() != NavigationType.NAVIGATION_NONE) {
1457                         // An empty result from get_portal_navigation_relationship_automatic() or
1458                         // get_navigation_relationship_specific() means we should use the directly related table:
1459                         navigationTableName = directlyRelatedTableName;
1460                 }
1461
1462                 if (StringUtils.isEmpty(navigationTableName)) {
1463                         return null;
1464                 }
1465
1466                 if (this == null) {
1467                         Log.error("document is null.");
1468                         return null;
1469                 }
1470
1471                 if (getTableIsHidden(navigationTableName)) {
1472                         Log.error("navigation_table_name indicates a hidden table: " + navigationTableName);
1473                         return null;
1474                 }
1475
1476                 final TableToViewDetails result = new TableToViewDetails();
1477                 result.tableName = navigationTableName;
1478                 result.usesRelationship = navigationRelationship;
1479                 return result;
1480         }
1481
1482         /**
1483          * @param portal
1484          *            TODO
1485          * @return
1486          */
1487         private UsesRelationship getPortalNavigationRelationshipAutomatic(final LayoutItemPortal portal) {
1488                 if (this == null) {
1489                         return null;
1490                 }
1491
1492                 // If the related table is not hidden then we can just navigate to that:
1493                 final String direct_related_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1494                 if (!getTableIsHidden(direct_related_table_name)) {
1495                         // Non-hidden tables can just be shown directly. Navigate to it:
1496                         return null;
1497                 } else {
1498                         // If the related table is hidden,
1499                         // then find a suitable related non-hidden table by finding the first layout field that mentions one:
1500                         final LayoutItemField field = getPortalFieldIsFromNonHiddenRelatedRecord(portal);
1501                         if (field != null) {
1502                                 return field; // Returns the UsesRelationship base part. (A relationship belonging to the portal's
1503                                                                 // related table.)
1504                         } else {
1505                                 // Instead, find a key field that's used in a relationship,
1506                                 // and pretend that we are showing the to field as a related field:
1507                                 final Relationship fieldIndentifies = getPortalFieldIdentifiesNonHiddenRelatedRecord(portal);
1508                                 if (fieldIndentifies != null) {
1509                                         final UsesRelationship result = new UsesRelationshipImpl();
1510                                         result.setRelationship(fieldIndentifies);
1511                                         return result;
1512                                 }
1513                         }
1514                 }
1515
1516                 // There was no suitable related table to show:
1517                 return null;
1518         }
1519
1520         /**
1521          * @param portal
1522          *            TODO
1523          * @return
1524          */
1525         private LayoutItemField getPortalFieldIsFromNonHiddenRelatedRecord(final LayoutItemPortal portal) {
1526                 // Find the first field that is from a non-hidden related table.
1527
1528                 if (this == null) {
1529                         return null;
1530                 }
1531
1532                 final LayoutItemField result = null;
1533
1534                 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1535
1536                 final List<LayoutItem> items = portal.getItems();
1537                 for (final LayoutItem item : items) {
1538                         if (item instanceof LayoutItemField) {
1539                                 final LayoutItemField field = (LayoutItemField) item;
1540                                 if (field.getHasRelationshipName()) {
1541                                         final String table_name = field.getTableUsed(parent_table_name);
1542                                         if (!(getTableIsHidden(table_name))) {
1543                                                 return field;
1544                                         }
1545                                 }
1546                         }
1547                 }
1548
1549                 return result;
1550         }
1551
1552         /**
1553          * @param used_in_relationship
1554          * @param portal
1555          *            TODO
1556          * @return
1557          */
1558         private Relationship getPortalFieldIdentifiesNonHiddenRelatedRecord(final LayoutItemPortal portal) {
1559                 // Find the first field that is from a non-hidden related table.
1560
1561                 if (this == null) {
1562                         Log.error("document is null");
1563                         return null;
1564                 }
1565
1566                 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1567
1568                 final List<LayoutItem> items = portal.getItems();
1569                 for (final LayoutItem item : items) {
1570                         if (item instanceof LayoutItemField) {
1571                                 final LayoutItemField field = (LayoutItemField) item;
1572                                 if (field.getHasRelationshipName()) {
1573                                         final Relationship relationship = getFieldUsedInRelationshipToOne(parent_table_name, field);
1574                                         if (relationship != null) {
1575                                                 final String table_name = relationship.getToTable();
1576                                                 if (!StringUtils.isEmpty(table_name)) {
1577                                                         if (!(getTableIsHidden(table_name))) {
1578                                                                 return relationship;
1579                                                         }
1580                                                 }
1581                                         }
1582                                 }
1583                         }
1584                 }
1585
1586                 return null;
1587         }
1588
1589         /**
1590          * @param tableName
1591          * @param layoutItem
1592          * @return The destination table name for navigation.
1593          */
1594         public String getLayoutItemFieldShouldHaveNavigation(final String tableName, final LayoutItemField layoutItem) {
1595                 if (StringUtils.isEmpty(tableName)) {
1596                         return null;
1597                 }
1598
1599                 if (layoutItem == null) {
1600                         return null;
1601                 }
1602
1603                 // Check whether the field controls a relationship,
1604                 // meaning it identifies a record in another table.
1605                 final Relationship fieldUsedInRelationshipToOne = getFieldUsedInRelationshipToOne(tableName, layoutItem);
1606                 if (fieldUsedInRelationshipToOne != null) {
1607                         return fieldUsedInRelationshipToOne.getToTable();
1608                 }
1609
1610                 // Check whether the field identifies a record in another table
1611                 // just because it is a primary key in that table:
1612                 final Field fieldInfo = layoutItem.getFullFieldDetails();
1613                 final boolean fieldIsRelatedPrimaryKey = layoutItem.getHasRelationshipName() && (fieldInfo != null)
1614                                 && fieldInfo.getPrimaryKey();
1615                 if (fieldIsRelatedPrimaryKey) {
1616                         return layoutItem.getRelationship().getToTable();
1617                 }
1618
1619                 return null;
1620         }
1621
1622         /**
1623          * @param isExample
1624          */
1625         public void setIsExampleFile(final boolean isExample) {
1626                 this.isExample = isExample;
1627         }
1628
1629         /**
1630          */
1631         public boolean getIsExampleFile() {
1632                 return isExample;
1633         }
1634
1635         /**
1636          * @return
1637          */
1638         public boolean save() {
1639                 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
1640                 DocumentBuilder documentBuilder = null;
1641                 try {
1642                         documentBuilder = dbf.newDocumentBuilder();
1643                 } catch (final ParserConfigurationException e) {
1644                         // TODO Auto-generated catch block
1645                         e.printStackTrace();
1646                         return false;
1647                 }
1648
1649                 final org.w3c.dom.Document doc = documentBuilder.newDocument();
1650                 final Element rootNode = doc.createElement(NODE_ROOT);
1651                 doc.appendChild(rootNode);
1652
1653                 rootNode.setAttribute(ATTRIBUTE_TITLE, databaseTitle.getTitleOriginal());
1654                 rootNode.setAttribute(ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE, translationOriginalLocale);
1655                 setAttributeAsBoolean(rootNode, ATTRIBUTE_IS_EXAMPLE, isExample);
1656
1657                 String strHostingMode = "";
1658                 if (hostingMode == HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
1659                         strHostingMode = "postgres_central";
1660                 } else if (hostingMode == HostingMode.HOSTING_MODE_SQLITE) {
1661                         strHostingMode = "sqlite";
1662                 } else {
1663                         strHostingMode = "postgres_self";
1664                 }
1665                 final Element nodeConnection = createElement(doc, rootNode, NODE_CONNECTION);
1666                 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_HOSTING_MODE, strHostingMode);
1667                 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_SERVER, connectionServer);
1668                 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_DATABASE, connectionDatabase);
1669                 setAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT, connectionPort);
1670
1671                 // for all tables:
1672                 for (final TableInfo table : tablesMap.values()) {
1673                         final Element nodeTable = createElement(doc, rootNode, NODE_TABLE);
1674                         saveTableNodeBasic(doc, nodeTable, table);
1675                         saveTableLayouts(doc, nodeTable, table);
1676                 }
1677
1678                 final TransformerFactory transformerFactory = TransformerFactory.newInstance();
1679                 Transformer transformer;
1680                 try {
1681                         transformer = transformerFactory.newTransformer();
1682                 } catch (final TransformerConfigurationException e) {
1683                         // TODO Auto-generated catch block
1684                         e.printStackTrace();
1685                         return false;
1686                 }
1687
1688                 // TODO: This probably distorts text nodes,
1689                 // so careful when we load/save them. For instance, scripts.
1690                 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1691                 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
1692
1693                 // Make sure that the parent directory exists:
1694                 final File file = new File(fileURI);
1695                 try {
1696                         Files.createParentDirs(file);
1697                 } catch (final IOException e) {
1698                         e.printStackTrace();
1699                         return false;
1700                 }
1701
1702                 final DOMSource source = new DOMSource(doc);
1703                 final StreamResult result = new StreamResult(file);
1704
1705                 // Output to console for testing
1706                 // StreamResult result = new StreamResult(System.out);
1707
1708                 try {
1709                         transformer.transform(source, result);
1710                 } catch (final TransformerException e) {
1711                         // TODO Auto-generated catch block
1712                         e.printStackTrace();
1713                         return false;
1714                 }
1715
1716                 return true;
1717         }
1718
1719         /**
1720          * @param doc
1721          * @param nodeTable
1722          * @param table
1723          */
1724         private void saveTableLayouts(final org.w3c.dom.Document doc, final Element tableNode, final TableInfo table) {
1725
1726                 final Element layoutsNode = createElement(doc, tableNode, NODE_DATA_LAYOUTS);
1727
1728                 final Element nodeLayoutDetails = createElement(doc, layoutsNode, NODE_DATA_LAYOUT);
1729                 nodeLayoutDetails.setAttribute(ATTRIBUTE_NAME, LAYOUT_NAME_DETAILS);
1730                 saveLayoutNode(doc, nodeLayoutDetails, table.layoutGroupsDetails);
1731
1732                 final Element nodeLayoutList = createElement(doc, layoutsNode, NODE_DATA_LAYOUT);
1733                 nodeLayoutList.setAttribute(ATTRIBUTE_NAME, LAYOUT_NAME_LIST);
1734                 saveLayoutNode(doc, nodeLayoutList, table.layoutGroupsList);
1735
1736                 final Element reportsNode = createElement(doc, tableNode, NODE_REPORTS);
1737                 for (final Report report : table.reportsMap.values()) {
1738                         final Element element = createElement(doc, reportsNode, NODE_REPORT);
1739                         saveReport(doc, element, report);
1740                 }
1741
1742         }
1743
1744         /**
1745          * @param doc
1746          * @param element
1747          * @param report
1748          */
1749         private void saveReport(final org.w3c.dom.Document doc, final Element element, final Report report) {
1750                 // TODO Auto-generated method stub
1751
1752         }
1753
1754         private void saveLayoutNode(final org.w3c.dom.Document doc, final Element element,
1755                         final List<LayoutGroup> layoutGroups) {
1756                 final Element elementGroups = createElement(doc, element, NODE_DATA_LAYOUT_GROUPS);
1757
1758                 for (final LayoutGroup layoutGroup : layoutGroups) {
1759                         if (layoutGroup instanceof LayoutItemNotebook) {
1760                                 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_NOTEBOOK);
1761                                 saveDataLayoutGroup(doc, elementGroup, layoutGroup);
1762                         } else if (layoutGroup instanceof LayoutItemPortal) {
1763                                 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_PORTAL);
1764                                 saveDataLayoutPortal(doc, elementGroup, (LayoutItemPortal) layoutGroup);
1765                         } else {
1766                                 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_GROUP);
1767                                 saveDataLayoutGroup(doc, elementGroup, layoutGroup);
1768                         }
1769                 }
1770
1771         }
1772
1773         /**
1774          * @param doc
1775          * @param elementGroup
1776          * @param layoutGroup
1777          */
1778         private void saveDataLayoutPortal(final org.w3c.dom.Document doc, final Element element,
1779                         final LayoutItemPortal portal) {
1780                 saveUsesRelationship(element, portal);
1781                 saveDataLayoutGroup(doc, element, portal);
1782
1783                 final Element elementNavigation = createElement(doc, element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
1784                 String navigationTypeAsString = "";
1785                 switch (portal.getNavigationType()) {
1786                 case NAVIGATION_AUTOMATIC:
1787                         navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC;
1788                         break;
1789                 case NAVIGATION_NONE:
1790                         navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE;
1791                         break;
1792                 case NAVIGATION_SPECIFIC:
1793                         navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC;
1794                         break;
1795                 default:
1796                         break;
1797                 }
1798                 elementNavigation.setAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE, navigationTypeAsString);
1799
1800                 if (navigationTypeAsString.equals(ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC)) {
1801                         // Write the specified relationship name:
1802                         saveUsesRelationship(elementNavigation, portal.getNavigationRelationshipSpecific());
1803                 }
1804         }
1805
1806         /**
1807          * @param doc
1808          * @param elementGroup
1809          * @param layoutGroup
1810          */
1811         private void saveDataLayoutGroup(final org.w3c.dom.Document doc, final Element nodeGroup, final LayoutGroup group) {
1812                 saveTitle(doc, nodeGroup, group);
1813
1814                 // Write the column count:
1815                 setAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT, group.getColumnCount());
1816
1817                 // Write the child items:
1818                 for (final LayoutItem layoutItem : group.getItems()) {
1819                         if (layoutItem instanceof LayoutItemPortal) {
1820                                 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_PORTAL);
1821                                 saveDataLayoutPortal(doc, element, (LayoutItemPortal) layoutItem);
1822                         } else if (layoutItem instanceof LayoutItemNotebook) {
1823                                 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_NOTEBOOK);
1824                                 saveDataLayoutGroup(doc, element, (LayoutItemNotebook) layoutItem);
1825                         } else if (layoutItem instanceof LayoutGroup) {
1826                                 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_GROUP);
1827                                 saveDataLayoutGroup(doc, element, (LayoutGroup) layoutItem);
1828                         } else if (layoutItem instanceof LayoutItemField) {
1829                                 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_ITEM);
1830                                 saveDataLayoutItemField(doc, element, (LayoutItemField) layoutItem);
1831                         } else if (layoutItem instanceof LayoutItemGroupBy) {
1832                                 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_ITEM_GROUPBY);
1833                                 saveDataLayoutItemGroupBy(doc, element, (LayoutItemGroupBy) layoutItem);
1834                         }
1835                 }
1836         }
1837
1838         /**
1839          * @param doc
1840          * @param element
1841          * @param layoutItem
1842          */
1843         private void saveDataLayoutItemGroupBy(final org.w3c.dom.Document doc, final Element element,
1844                         final LayoutItemGroupBy item) {
1845                 saveDataLayoutGroup(doc, element, item);
1846
1847                 final Element elementGroupBy = createElement(doc, element, NODE_GROUPBY);
1848                 saveDataLayoutItemField(doc, elementGroupBy, item.getFieldGroupBy());
1849
1850                 final Element elementSecondaryFields = createElement(doc, element, NODE_SECONDARY_FIELDS);
1851                 final Element elementLayoutGroup = createElement(doc, elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
1852                 saveDataLayoutGroup(doc, elementLayoutGroup, item.getSecondaryFields());
1853         }
1854
1855         /**
1856          * @param doc
1857          * @param element
1858          * @param layoutItem
1859          */
1860         private void saveDataLayoutItemField(final org.w3c.dom.Document doc, final Element element,
1861                         final LayoutItemField item) {
1862                 element.setAttribute(ATTRIBUTE_NAME, item.getName());
1863                 saveUsesRelationship(element, item);
1864
1865                 final CustomTitle customTitle = item.getCustomTitle();
1866                 if (customTitle != null) {
1867                         final Element elementCustomTitle = createElement(doc, element, NODE_CUSTOM_TITLE);
1868                         setAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM,
1869                                         customTitle.getUseCustomTitle());
1870                         saveTitle(doc, elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
1871                 }
1872
1873                 setAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING, item.getUseDefaultFormatting());
1874
1875                 final Element elementFormatting = createElement(doc, element, NODE_FORMATTING);
1876                 saveFormatting(elementFormatting, item.getFormatting());
1877         }
1878
1879         /**
1880          * @param element
1881          * @param item
1882          */
1883         private void saveUsesRelationship(final Element element, final UsesRelationship item) {
1884                 final Relationship relationship = item.getRelationship();
1885                 if (relationship != null) {
1886                         element.setAttribute(ATTRIBUTE_RELATIONSHIP_NAME, relationship.getName());
1887                 }
1888
1889                 final Relationship relatedRelationship = item.getRelatedRelationship();
1890                 if (relatedRelationship != null) {
1891                         element.setAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME, relatedRelationship.getName());
1892                 }
1893         }
1894
1895         /**
1896          * @param rootNode
1897          * @param nodeConnection
1898          * @return
1899          */
1900         private Element createElement(final org.w3c.dom.Document doc, final Element parentNode, final String name) {
1901                 final Element node = doc.createElement(name);
1902                 parentNode.appendChild(node);
1903                 return node;
1904         }
1905
1906         public String getSelfHostedDirectoryPath() {
1907                 final String uriFile = getFileURI();
1908                 if (!StringUtils.isEmpty(uriFile)) {
1909                         final File file = new File(uriFile);
1910                         final File parent = file.getParentFile();
1911                         if (parent == null) {
1912                                 // TODO: Warn.
1913                                 return "";
1914                         }
1915
1916                         File dataDir = null;
1917                         switch (hostingMode) {
1918                         case HOSTING_MODE_POSTGRES_SELF:
1919                                 dataDir = new File(parent, "glom_postgres_data");
1920                                 break;
1921                         case HOSTING_MODE_POSTGRES_CENTRAL:
1922                                 dataDir = parent;
1923                                 break;
1924                         case HOSTING_MODE_SQLITE:
1925                                 dataDir = parent;
1926                                 break;
1927                         default:
1928                                 // TODO: Warn.
1929                                 break;
1930                         }
1931
1932                         if (dataDir != null) {
1933                                 return dataDir.getPath();
1934                         }
1935                 }
1936
1937                 // TODO: std::cerr << G_STRFUNC << ": returning empty string." << std::endl;
1938                 return "";
1939         }
1940
1941         /**
1942          */
1943         public void setConnectionDatabase(final String databaseName) {
1944                 connectionDatabase = databaseName;
1945
1946         }
1947
1948         /**
1949          * Gets the primary key Field for the specified table name.
1950          * 
1951          * @param tableName
1952          *            name of table to search for the primary key field
1953          * @return primary key Field
1954          */
1955         public Field getTablePrimaryKeyField(final String tableName) {
1956                 Field primaryKey = null;
1957                 final List<Field> fieldsVec = getTableFields(tableName);
1958                 for (int i = 0; i < Utils.safeLongToInt(fieldsVec.size()); i++) {
1959                         final Field field = fieldsVec.get(i);
1960                         if (field.getPrimaryKey()) {
1961                                 primaryKey = field;
1962                                 break;
1963                         }
1964                 }
1965                 return primaryKey;
1966         }
1967
1968         /**
1969          * @param resp
1970          * @param attrDocumentID
1971          * @param tableName
1972          * @param layoutName
1973          * @param layoutPath
1974          * @return
1975          * @throws IOException
1976          */
1977         public LayoutItem getLayoutItemByPath(
1978                         final String tableName, final String layoutName, final String layoutPath) throws IOException {
1979                 final List<LayoutGroup> listLayoutGroups = getDataLayoutGroups(layoutName, tableName);
1980                 if(listLayoutGroups == null) {
1981                         Log.error("The layout with the specified name was not found. tableName=" + tableName + ", layoutName=" + layoutName);
1982                         return null;
1983                 }
1984
1985                 if(listLayoutGroups.isEmpty()) {
1986                         Log.error("The layout was empty. attrTableName=" + tableName + ", layoutName=" + layoutName);
1987                         return null;
1988                 }
1989                 
1990                 final int[] indices = Utils.parseLayoutPath(layoutPath);
1991                 if((indices == null) || (indices.length == 0)) {
1992                         Log.error("The layout path was empty or could not be parsed. layoutPath=" + layoutPath);
1993                         return null;
1994                 }
1995         
1996                 LayoutItem item = null;
1997                 int depth = 0;
1998                 for(int index:indices) {
1999                         if(index < 0) {
2000                                 Log.error("An index in the layout path was negative, at depth=" + depth + ", layoutPath=" + layoutPath);
2001                                 return null;
2002                         }
2003
2004                         //Get the nth item of either the top-level list or the current item:
2005                         if(depth == 0) {
2006                                 if(index < listLayoutGroups.size()) {
2007                                         item = listLayoutGroups.get(index);
2008                                 } else {
2009                                         Log.error("An index in the layout path is larger than the number of child items, at depth=" + depth + ", layoutPath=" + layoutPath);
2010                                         return null;
2011                                 }
2012                         } else {
2013                                 if(item instanceof LayoutGroup) {
2014                                         final LayoutGroup group = (LayoutGroup)item;
2015                                         final List<LayoutItem> items = group.getItems();
2016                                         if(index < items.size()) {
2017                                                 item = items.get(index);
2018                                         } else {
2019                                                 Log.error("An index in the layout path is larger than the number of child items, at depth=" + depth + ", layoutPath=" + layoutPath);
2020                                                 return null;
2021                                         }
2022                                 } else {
2023                                         Log.error("An intermediate item in the layout path is not a layout group, at depth=" + depth + ", layoutPath=" + layoutPath);
2024                                         return null;
2025                                 }
2026                         }
2027                         
2028                         depth++;
2029                 }
2030                 
2031                 if(item == null) {
2032                         Log.error("The item specifed by the layout path could not be found. layoutPath=" + layoutPath);
2033                         return null;
2034                 }
2035                 return item;
2036         }
2037 }