2 * Copyright (C) 2012 Openismus GmbH
4 * This file is part of GWT-Glom.
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.
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
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/>.
20 package org.glom.web.server.libglom;
23 import java.io.IOException;
24 import java.text.DateFormat;
25 import java.text.NumberFormat;
26 import java.text.ParseException;
27 import java.util.ArrayList;
28 import java.util.Date;
29 import java.util.HashMap;
30 import java.util.Hashtable;
31 import java.util.List;
32 import java.util.Locale;
34 import java.util.Map.Entry;
36 import javax.xml.parsers.DocumentBuilder;
37 import javax.xml.parsers.DocumentBuilderFactory;
38 import javax.xml.parsers.ParserConfigurationException;
39 import javax.xml.transform.OutputKeys;
40 import javax.xml.transform.Transformer;
41 import javax.xml.transform.TransformerConfigurationException;
42 import javax.xml.transform.TransformerException;
43 import javax.xml.transform.TransformerFactory;
44 import javax.xml.transform.dom.DOMSource;
45 import javax.xml.transform.stream.StreamResult;
47 import org.apache.commons.lang3.StringUtils;
48 import org.glom.web.shared.DataItem;
49 import org.glom.web.shared.libglom.CustomTitle;
50 import org.glom.web.shared.libglom.Field;
51 import org.glom.web.shared.libglom.Field.GlomFieldType;
52 import org.glom.web.shared.libglom.NumericFormat;
53 import org.glom.web.shared.libglom.Relationship;
54 import org.glom.web.shared.libglom.Report;
55 import org.glom.web.shared.libglom.Translatable;
56 import org.glom.web.shared.libglom.layout.Formatting;
57 import org.glom.web.shared.libglom.layout.LayoutGroup;
58 import org.glom.web.shared.libglom.layout.LayoutItem;
59 import org.glom.web.shared.libglom.layout.LayoutItemField;
60 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
61 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
62 import org.glom.web.shared.libglom.layout.LayoutItemPortal.NavigationType;
63 import org.glom.web.shared.libglom.layout.TableToViewDetails;
64 import org.glom.web.shared.libglom.layout.UsesRelationship;
65 import org.glom.web.shared.libglom.layout.UsesRelationshipImpl;
66 import org.glom.web.shared.libglom.layout.reportparts.LayoutItemGroupBy;
67 import org.jfree.util.Log;
68 import org.w3c.dom.Element;
69 import org.w3c.dom.Node;
70 import org.w3c.dom.NodeList;
71 import org.xml.sax.SAXException;
73 import com.google.common.io.Files;
76 * @author Murray Cumming <murrayc@openismus.com>
79 public class Document {
81 @SuppressWarnings("serial")
82 private static class TableInfo extends Translatable {
83 private boolean isDefault;
84 private boolean isHidden;
86 private final Hashtable<String, Field> fieldsMap = new Hashtable<String, Field>();
87 private final Hashtable<String, Relationship> relationshipsMap = new Hashtable<String, Relationship>();
88 private final Hashtable<String, Report> reportsMap = new Hashtable<String, Report>();
90 private List<LayoutGroup> layoutGroupsList = new ArrayList<LayoutGroup>();
91 private List<LayoutGroup> layoutGroupsDetails = new ArrayList<LayoutGroup>();
93 // A list of maps (field name to value).
94 private List<Map<String, DataItem>> exampleRows = null;
97 private String fileURI = "";
98 private org.w3c.dom.Document xmlDocument = null;
100 private final Translatable databaseTitle = new Translatable();
101 private String translationOriginalLocale = "";
102 private final List<String> translationAvailableLocales = new ArrayList<String>();
103 private boolean isExample = false;
104 private HostingMode hostingMode = HostingMode.HOSTING_MODE_POSTGRES_CENTRAL;
105 private String connectionServer = "";
106 private String connectionDatabase = "";
107 private int connectionPort = 0;
108 private final Hashtable<String, TableInfo> tablesMap = new Hashtable<String, TableInfo>();
110 private static final String NODE_ROOT = "glom_document";
111 private static final String ATTRIBUTE_IS_EXAMPLE = "is_example";
112 private static final String ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE = "translation_original_locale";
113 private static final String NODE_CONNECTION = "connection";
114 private static final String ATTRIBUTE_CONNECTION_HOSTING_MODE = "hosting_mode";
115 private static final String ATTRIBUTE_CONNECTION_SERVER = "server";
116 private static final String ATTRIBUTE_CONNECTION_DATABASE = "database";
117 private static final String ATTRIBUTE_CONNECTION_PORT = "port";
118 private static final String NODE_TABLE = "table";
119 private static final String ATTRIBUTE_NAME = "name";
120 private static final String ATTRIBUTE_TITLE = "title";
121 private static final String DEPRECATED_ATTRIBUTE_DATABASE_TITLE = "database_title";
122 private static final String ATTRIBUTE_DEFAULT = "default";
123 private static final String ATTRIBUTE_HIDDEN = "hidden";
124 private static final String NODE_TRANSLATIONS_SET = "trans_set";
125 private static final String NODE_TRANSLATIONS = "trans";
126 private static final String ATTRIBUTE_TRANSLATION_LOCALE = "loc";
127 private static final String ATTRIBUTE_TRANSLATION_TITLE = "val";
128 private static final String NODE_REPORTS = "reports";
129 private static final String NODE_REPORT = "report";
130 private static final String NODE_FIELDS = "fields";
131 private static final String NODE_FIELD = "field";
132 private static final String NODE_EXAMPLE_ROWS = "example_rows";
133 private static final String NODE_EXAMPLE_ROW = "example_row";
134 private static final String NODE_VALUE = "value";
135 private static final String ATTRIBUTE_COLUMN = "column";
136 private static final String ATTRIBUTE_PRIMARY_KEY = "primary_key";
137 private static final String ATTRIBUTE_FIELD_TYPE = "type";
138 private static final String NODE_FORMATTING = "formatting";
139 // private static final String ATTRIBUTE_TEXT_FORMAT_MULTILINE = "format_text_multiline";
140 private static final String ATTRIBUTE_USE_THOUSANDS_SEPARATOR = "format_thousands_separator";
141 private static final String ATTRIBUTE_DECIMAL_PLACES = "format_decimal_places";
142 private static final String NODE_RELATIONSHIPS = "relationships";
143 private static final String NODE_RELATIONSHIP = "relationship";
144 private static final String ATTRIBUTE_RELATIONSHIP_FROM_FIELD = "key";
145 private static final String ATTRIBUTE_RELATIONSHIP_TO_TABLE = "other_table";
146 private static final String ATTRIBUTE_RELATIONSHIP_TO_FIELD = "other_key";
147 private static final String NODE_DATA_LAYOUTS = "data_layouts";
148 private static final String NODE_DATA_LAYOUT = "data_layout";
149 private static final String NODE_DATA_LAYOUT_GROUPS = "data_layout_groups";
150 private static final String NODE_DATA_LAYOUT_GROUP = "data_layout_group";
151 private static final String ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT = "columns_count";
152 private static final String NODE_DATA_LAYOUT_NOTEBOOK = "data_layout_notebook";
153 private static final String NODE_DATA_LAYOUT_PORTAL = "data_layout_portal";
154 private static final String NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP = "portal_navigation_relationship";
155 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE = "navigation_type";
156 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC = "automatic";
157 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC = "specific";
158 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE = "none";
159 private static final String ATTRIBUTE_RELATIONSHIP_NAME = "relationship";
160 private static final String ATTRIBUTE_RELATED_RELATIONSHIP_NAME = "related_relationship";
161 private static final String NODE_DATA_LAYOUT_ITEM = "data_layout_item";
162 private static final String NODE_CUSTOM_TITLE = "title_custom";
163 private static final String ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM = "use_custom";
164 private static final String NODE_DATA_LAYOUT_ITEM_GROUPBY = "data_layout_item_groupby";
165 private static final String NODE_GROUPBY = "groupby";
166 private static final String NODE_SECONDARY_FIELDS = "secondary_fields";
167 private static final String ATTRIBUTE_USE_DEFAULT_FORMATTING = "use_default_formatting";
168 private static final String LAYOUT_NAME_DETAILS = "details";
169 private static final String LAYOUT_NAME_LIST = "list";
170 private static final String QUOTE_FOR_FILE_FORMAT = "\"";
172 public void setFileURI(final String fileURI) {
173 this.fileURI = fileURI;
176 public String getFileURI() {
180 // TODO: Make sure these have the correct values.
181 public enum LoadFailureCodes {
182 LOAD_FAILURE_CODE_NONE, LOAD_FAILURE_CODE_NOT_FOUND, LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW
185 public boolean load() {
186 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
187 DocumentBuilder documentBuilder = null;
189 documentBuilder = dbf.newDocumentBuilder();
190 } catch (final ParserConfigurationException e) {
191 // TODO Auto-generated catch block
197 xmlDocument = documentBuilder.parse(fileURI);
198 } catch (final SAXException e) {
199 // TODO Auto-generated catch block
202 } catch (final IOException e) {
203 // TODO Auto-generated catch block
208 final Element rootNode = xmlDocument.getDocumentElement();
209 if (rootNode.getNodeName() != NODE_ROOT) {
210 Log.error("Unexpected XML root node name found: " + rootNode.getNodeName());
214 //Get the database title, falling back to the deprecated XML format for it:
215 //TODO: load() show complain (via an enum result) if the document format version is less than 7.
216 final String databaseTitleStr = rootNode.getAttribute(ATTRIBUTE_TITLE);
217 final String deprecatedDatabaseTitleStr = rootNode.getAttribute(DEPRECATED_ATTRIBUTE_DATABASE_TITLE);
218 if(!StringUtils.isEmpty(databaseTitleStr)) {
219 databaseTitle.setTitleOriginal(databaseTitleStr);
221 databaseTitle.setTitleOriginal(deprecatedDatabaseTitleStr);
223 loadTitle(rootNode, databaseTitle);
225 translationOriginalLocale = rootNode.getAttribute(ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE);
226 translationAvailableLocales.add(translationOriginalLocale); // Just a cache.
228 isExample = getAttributeAsBoolean(rootNode, ATTRIBUTE_IS_EXAMPLE);
230 final Element nodeConnection = getElementByName(rootNode, NODE_CONNECTION);
231 if (nodeConnection != null) {
232 final String strHostingMode = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_HOSTING_MODE);
233 if (strHostingMode == "postgres_central") {
234 hostingMode = HostingMode.HOSTING_MODE_POSTGRES_CENTRAL;
235 } else if (strHostingMode == "sqlite") {
236 hostingMode = HostingMode.HOSTING_MODE_SQLITE;
238 hostingMode = HostingMode.HOSTING_MODE_POSTGRES_SELF;
241 connectionServer = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_SERVER);
242 connectionDatabase = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_DATABASE);
243 connectionPort = (int) getAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT);
246 // We first load the fields, relationships, etc,
248 final List<Node> listTableNodes = getChildrenByTagName(rootNode, NODE_TABLE);
249 for (final Node node : listTableNodes) {
250 if (!(node instanceof Element)) {
254 final Element element = (Element) node;
255 final TableInfo info = loadTableNodeBasic(element);
256 tablesMap.put(info.getName(), info);
259 // We then load the layouts for all tables, because they
260 // need the fields and relationships for all tables:
261 for (final Node node : listTableNodes) {
262 if (!(node instanceof Element)) {
266 final Element element = (Element) node;
267 final String tableName = element.getAttribute(ATTRIBUTE_NAME);
269 // We first load the fields, relationships, etc:
270 final TableInfo info = getTableInfo(tableName);
275 // We then load the layouts afterwards, because they
276 // need the fields and relationships:
277 loadTableLayouts(element, info);
279 tablesMap.put(info.getName(), info);
285 private Element getElementByName(final Element parentElement, final String tagName) {
286 final List<Node> listNodes = getChildrenByTagName(parentElement, tagName);
287 if (listNodes == null) {
291 if (listNodes.size() == 0) {
295 return (Element) listNodes.get(0);
298 private boolean getAttributeAsBoolean(final Element node, final String attributeName) {
299 final String str = node.getAttribute(attributeName);
304 return str.equals("true");
307 private void setAttributeAsBoolean(final Element node, final String attributeName, final boolean value) {
308 final String str = value ? "true" : "false";
309 node.setAttribute(attributeName, str);
312 private double getAttributeAsDecimal(final Element node, final String attributeName) {
313 final String str = node.getAttribute(attributeName);
314 if (StringUtils.isEmpty(str)) {
320 value = Double.valueOf(str);
321 } catch (final NumberFormatException e) {
322 // e.printStackTrace();
328 private String getStringForDecimal(final double value) {
329 final NumberFormat format = NumberFormat.getInstance(Locale.US);
330 format.setGroupingUsed(false); // TODO: Does this change it system-wide?
331 return format.format(value);
334 private void setAttributeAsDecimal(final Element node, final String attributeName, final double value) {
335 node.setAttribute(attributeName, getStringForDecimal(value));
339 * Load a title and its translations.
342 * The XML Element that may contain a title attribute and a trans_set of translations of the title.
345 private void loadTitle(final Element node, final Translatable title) {
346 title.setName(node.getAttribute(ATTRIBUTE_NAME));
348 title.setTitleOriginal(node.getAttribute(ATTRIBUTE_TITLE));
350 final Element nodeSet = getElementByName(node, NODE_TRANSLATIONS_SET);
351 if (nodeSet == null) {
355 final List<Node> listNodes = getChildrenByTagName(nodeSet, NODE_TRANSLATIONS);
356 if (listNodes == null) {
360 for (final Node transNode : listNodes) {
361 if (!(transNode instanceof Element)) {
365 final Element element = (Element) transNode;
367 final String locale = element.getAttribute(ATTRIBUTE_TRANSLATION_LOCALE);
368 final String translatedTitle = element.getAttribute(ATTRIBUTE_TRANSLATION_TITLE);
369 if (!StringUtils.isEmpty(locale) && !StringUtils.isEmpty(translatedTitle)) {
370 title.setTitle(translatedTitle, locale);
372 // Remember any new translation locales in our cached list:
373 if (!translationAvailableLocales.contains(locale)) {
374 translationAvailableLocales.add(locale);
380 private void saveTitle(final org.w3c.dom.Document doc, final Element node, final Translatable title) {
381 node.setAttribute(ATTRIBUTE_NAME, title.getName());
383 node.setAttribute(ATTRIBUTE_TITLE, title.getTitleOriginal());
385 final Element nodeSet = createElement(doc, node, NODE_TRANSLATIONS_SET);
387 for (final Entry<String, String> entry : title.getTranslationsMap().entrySet()) {
388 final Element element = createElement(doc, nodeSet, NODE_TRANSLATIONS);
390 element.setAttribute(ATTRIBUTE_TRANSLATION_LOCALE, entry.getKey());
391 element.setAttribute(ATTRIBUTE_TRANSLATION_TITLE, entry.getValue());
399 private TableInfo loadTableNodeBasic(final Element tableNode) {
400 final TableInfo info = new TableInfo();
401 loadTitle(tableNode, info);
402 final String tableName = info.getName();
404 info.isDefault = getAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT);
405 info.isHidden = getAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN);
407 // These should be loaded before the fields, because the fields use them.
408 final Element relationshipsNode = getElementByName(tableNode, NODE_RELATIONSHIPS);
409 if (relationshipsNode != null) {
410 final List<Node> listNodes = getChildrenByTagName(relationshipsNode, NODE_RELATIONSHIP);
411 for (final Node node : listNodes) {
412 if (!(node instanceof Element)) {
416 final Element element = (Element) node;
417 final Relationship relationship = new Relationship();
418 loadTitle(element, relationship);
419 relationship.setFromTable(tableName);
420 relationship.setFromField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD));
421 relationship.setToTable(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE));
422 relationship.setToField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD));
424 info.relationshipsMap.put(relationship.getName(), relationship);
428 final Element fieldsNode = getElementByName(tableNode, NODE_FIELDS);
429 if (fieldsNode != null) {
430 final List<Node> listNodes = getChildrenByTagName(fieldsNode, NODE_FIELD);
431 for (final Node node : listNodes) {
432 if (!(node instanceof Element)) {
436 final Element element = (Element) node;
437 final Field field = new Field();
438 loadField(element, field);
440 info.fieldsMap.put(field.getName(), field);
444 final Element exampleRowsNode = getElementByName(tableNode, NODE_EXAMPLE_ROWS);
445 if (exampleRowsNode != null) {
447 final List<Map<String, DataItem>> exampleRows = new ArrayList<Map<String, DataItem>>();
448 final List<Node> listNodes = getChildrenByTagName(exampleRowsNode, NODE_EXAMPLE_ROW);
449 for (final Node node : listNodes) {
450 if (!(node instanceof Element)) {
454 final Element element = (Element) node;
455 final Map<String, DataItem> row = new HashMap<String, DataItem>();
457 final List<Node> listNodesValues = getChildrenByTagName(element, NODE_VALUE);
458 for (final Node nodeValue : listNodesValues) {
459 if (!(nodeValue instanceof Element)) {
463 final Element elementValue = (Element) nodeValue;
464 final String fieldName = elementValue.getAttribute(ATTRIBUTE_COLUMN);
465 if (StringUtils.isEmpty(fieldName)) {
469 DataItem value = null;
470 final Field field = info.fieldsMap.get(fieldName);
472 value = getNodeTextChildAsValue(elementValue, field.getGlomType());
474 row.put(fieldName, value);
477 exampleRows.add(row);
480 info.exampleRows = exampleRows;
487 * @param elementValue
491 private DataItem getNodeTextChildAsValue(final Element element, final GlomFieldType type) {
492 final DataItem result = new DataItem();
494 final String str = element.getTextContent();
496 // Unescape "" to ", because to_file_format() escaped ", as specified by the CSV RFC:
497 String unescaped = "";
498 if (type == GlomFieldType.TYPE_IMAGE) {
499 unescaped = str; // binary data does not have quote characters so we do not bother to escape or unescape it.
501 unescaped = str.replace(QUOTE_FOR_FILE_FORMAT + QUOTE_FOR_FILE_FORMAT, QUOTE_FOR_FILE_FORMAT);
506 final boolean value = (unescaped == "true");
507 result.setBoolean(value);
511 final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ROOT);
514 value = dateFormat.parse(unescaped);
515 } catch (final ParseException e) {
516 // e.printStackTrace();
518 result.setDate(value);
522 final byte[] value = null; // TODO.
523 result.setImage(value);
529 value = Double.valueOf(unescaped);
530 } catch (final NumberFormatException e) {
531 // e.printStackTrace();
534 result.setNumber(value);
538 result.setText(unescaped);
544 Log.error("getNodeTextChildAsValue(): unexpected or invalid field type.");
551 private void setNodeTextChildAsValue(final Element element, final DataItem value, final GlomFieldType type) {
556 str = value.getBoolean() ? "true" : "false";
560 // TODO: This is not really the format used by the Glom document:
561 final DateFormat dateFormat = DateFormat.getDateInstance(DateFormat.SHORT, Locale.ROOT);
562 str = dateFormat.format(value.getDate());
570 str = getStringForDecimal(value.getNumber());
574 str = value.getText();
580 Log.error("setNodeTextChildAsValue(): unexpected or invalid field type.");
584 final String escaped = str.replace(QUOTE_FOR_FILE_FORMAT, QUOTE_FOR_FILE_FORMAT + QUOTE_FOR_FILE_FORMAT);
585 element.setTextContent(escaped);
588 private void saveTableNodeBasic(final org.w3c.dom.Document doc, final Element tableNode, final TableInfo info) {
589 saveTitle(doc, tableNode, info);
591 setAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT, info.isDefault);
592 setAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN, info.isHidden);
594 final Element relationshipsNode = createElement(doc, tableNode, NODE_RELATIONSHIPS);
595 for (final Relationship relationship : info.relationshipsMap.values()) {
596 final Element element = createElement(doc, relationshipsNode, NODE_RELATIONSHIP);
597 saveTitle(doc, element, relationship);
599 element.setAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD, relationship.getFromField());
600 element.setAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE, relationship.getToTable());
601 element.setAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD, relationship.getToField());
604 final Element fieldsNode = createElement(doc, tableNode, NODE_FIELDS);
605 for (final Field field : info.fieldsMap.values()) {
606 final Element element = createElement(doc, fieldsNode, NODE_FIELD);
607 saveField(doc, element, field);
610 final Element exampleRowsNode = createElement(doc, tableNode, NODE_EXAMPLE_ROWS);
612 for (final Map<String, DataItem> row : info.exampleRows) {
613 final Element node = createElement(doc, exampleRowsNode, NODE_EXAMPLE_ROW);
615 // TODO: This assumes that fieldsMap.values() will have the same sequence as the values,
617 for (final Field field : info.fieldsMap.values()) {
618 if (i < row.size()) {
622 final String fieldName = field.getName();
623 if (StringUtils.isEmpty(fieldName)) {
627 final DataItem dataItem = row.get(fieldName);
628 if (dataItem == null) {
632 final Element elementValue = createElement(doc, node, NODE_VALUE);
633 elementValue.setAttribute(ATTRIBUTE_COLUMN, fieldName);
634 setNodeTextChildAsValue(elementValue, dataItem, field.getGlomType());
644 private void saveField(final org.w3c.dom.Document doc, final Element element, final Field field) {
645 saveTitle(doc, element, field);
647 String fieldTypeStr = "";
649 switch (field.getGlomType()) {
651 fieldTypeStr = "Boolean";
654 fieldTypeStr = "Date";
657 fieldTypeStr = "Image";
660 fieldTypeStr = "Number";
663 fieldTypeStr = "Text";
666 fieldTypeStr = "Time";
671 element.setAttribute(ATTRIBUTE_FIELD_TYPE, fieldTypeStr);
673 setAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY, field.getPrimaryKey());
675 final Element elementFormatting = createElement(doc, element, NODE_FORMATTING);
676 saveFormatting(elementFormatting, field.getFormatting());
680 * @param elementFormatting
683 private void saveFormatting(final Element element, final Formatting formatting) {
684 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
686 final NumericFormat numericFormatting = formatting.getNumericFormat();
687 if (numericFormatting != null) {
688 setAttributeAsBoolean(element, ATTRIBUTE_USE_THOUSANDS_SEPARATOR,
689 numericFormatting.getUseThousandsSeparator());
690 setAttributeAsDecimal(element, ATTRIBUTE_DECIMAL_PLACES, numericFormatting.getDecimalPlaces());
698 private void loadTableLayouts(final Element tableNode, final TableInfo info) {
699 final String tableName = info.getName();
701 final Element layoutsNode = getElementByName(tableNode, NODE_DATA_LAYOUTS);
702 if (layoutsNode != null) {
703 final List<Node> listNodes = getChildrenByTagName(layoutsNode, NODE_DATA_LAYOUT);
704 for (final Node node : listNodes) {
705 if (!(node instanceof Element)) {
709 final Element element = (Element) node;
710 final String name = element.getAttribute(ATTRIBUTE_NAME);
711 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
712 if (name.equals(LAYOUT_NAME_DETAILS)) {
713 info.layoutGroupsDetails = listLayoutGroups;
714 } else if (name.equals(LAYOUT_NAME_LIST)) {
715 info.layoutGroupsList = listLayoutGroups;
717 Log.error("loadTableNode(): unexpected layout name: " + name);
722 final Element reportsNode = getElementByName(tableNode, NODE_REPORTS);
723 if (reportsNode != null) {
724 final List<Node> listNodes = getChildrenByTagName(reportsNode, NODE_REPORT);
725 for (final Node node : listNodes) {
726 if (!(node instanceof Element)) {
730 final Element element = (Element) node;
731 final Report report = new Report();
732 loadReport(element, report, tableName);
734 info.reportsMap.put(report.getName(), report);
743 private List<LayoutGroup> loadLayoutNode(final Element node, final String tableName) {
748 final List<LayoutGroup> result = new ArrayList<LayoutGroup>();
750 final List<Node> listNodes = getChildrenByTagName(node, NODE_DATA_LAYOUT_GROUPS);
751 for (final Node nodeGroups : listNodes) {
752 if (!(nodeGroups instanceof Element)) {
756 final Element elementGroups = (Element) nodeGroups;
758 final NodeList list = elementGroups.getChildNodes();
759 final int num = list.getLength();
760 for (int i = 0; i < num; i++) {
761 final Node nodeLayoutGroup = list.item(i);
762 if (nodeLayoutGroup == null) {
766 if (!(nodeLayoutGroup instanceof Element)) {
770 final Element element = (Element) nodeLayoutGroup;
771 final String tagName = element.getTagName();
772 if (tagName == NODE_DATA_LAYOUT_GROUP) {
773 final LayoutGroup group = new LayoutGroup();
774 loadDataLayoutGroup(element, group, tableName);
776 } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
777 final LayoutItemNotebook group = new LayoutItemNotebook();
778 loadDataLayoutGroup(element, group, tableName);
780 } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
781 final LayoutItemPortal portal = new LayoutItemPortal();
782 loadDataLayoutPortal(element, portal, tableName);
796 private void loadUsesRelationship(final Element element, final String tableName, final UsesRelationship item) {
797 if (element == null) {
805 final String relationshipName = element.getAttribute(ATTRIBUTE_RELATIONSHIP_NAME);
806 Relationship relationship = null;
807 if (!StringUtils.isEmpty(relationshipName)) {
808 // std::cout << " debug in : tableName=" << tableName << ", relationshipName=" << relationship_name <<
810 relationship = getRelationship(tableName, relationshipName);
811 item.setRelationship(relationship);
813 if (relationship == null) {
814 Log.error("relationship not found: " + relationshipName + ", in table: " + tableName);
818 // TODO: Unit test loading of doubly-related fields.
819 final String relatedRelationshipName = element.getAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME);
820 if (!StringUtils.isEmpty(relatedRelationshipName) && (relationship != null)) {
821 final Relationship relatedRelationship = getRelationship(relationship.getToTable(), relatedRelationshipName);
822 item.setRelatedRelationship(relatedRelationship);
824 if (relatedRelationship == null) {
825 Log.error("related relationship not found in table=" + relationship.getToTable() + ", name="
826 + relatedRelationshipName);
832 * getElementsByTagName() is recursive, but we do not want that.
838 private List<Node> getChildrenByTagName(final Element parentNode, final String tagName) {
839 final List<Node> result = new ArrayList<Node>();
841 final NodeList list = parentNode.getElementsByTagName(tagName);
842 final int num = list.getLength();
843 for (int i = 0; i < num; i++) {
844 final Node node = list.item(i);
849 final Node itemParentNode = node.getParentNode();
850 if (itemParentNode.equals(parentNode)) {
862 private void loadDataLayoutGroup(final Element nodeGroup, final LayoutGroup group, final String tableName) {
863 loadTitle(nodeGroup, group);
865 // Read the column count:
866 int columnCount = (int) getAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT);
867 if (columnCount < 1) {
868 columnCount = 1; // 0 is a useless default.
870 group.setColumnCount(columnCount);
872 // Get the child items:
873 final NodeList listNodes = nodeGroup.getChildNodes();
874 final int num = listNodes.getLength();
875 for (int i = 0; i < num; i++) {
876 final Node node = listNodes.item(i);
877 if (!(node instanceof Element)) {
881 final Element element = (Element) node;
882 final String tagName = element.getTagName();
883 if (tagName == NODE_DATA_LAYOUT_GROUP) {
884 final LayoutGroup childGroup = new LayoutGroup();
885 loadDataLayoutGroup(element, childGroup, tableName);
886 group.addItem(childGroup);
887 } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
888 final LayoutItemNotebook childGroup = new LayoutItemNotebook();
889 loadDataLayoutGroup(element, childGroup, tableName);
890 group.addItem(childGroup);
891 } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
892 final LayoutItemPortal childGroup = new LayoutItemPortal();
893 loadDataLayoutPortal(element, childGroup, tableName);
894 group.addItem(childGroup);
895 } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM) {
896 final LayoutItemField item = new LayoutItemField();
897 loadDataLayoutItemField(element, item, tableName);
899 } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM_GROUPBY) {
900 final LayoutItemGroupBy item = new LayoutItemGroupBy();
901 loadDataLayoutItemGroupBy(element, item, tableName);
912 private void loadDataLayoutItemGroupBy(final Element element, final LayoutItemGroupBy item, final String tableName) {
913 loadDataLayoutGroup(element, item, tableName);
915 final Element elementGroupBy = getElementByName(element, NODE_GROUPBY);
916 if (elementGroupBy == null) {
920 final LayoutItemField fieldGroupBy = new LayoutItemField();
921 loadDataLayoutItemField(elementGroupBy, fieldGroupBy, tableName);
922 item.setFieldGroupBy(fieldGroupBy);
924 final Element elementSecondaryFields = getElementByName(element, NODE_SECONDARY_FIELDS);
925 if (elementSecondaryFields == null) {
929 final Element elementLayoutGroup = getElementByName(elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
930 if (elementLayoutGroup != null) {
931 final LayoutGroup secondaryLayoutGroup = new LayoutGroup();
932 loadDataLayoutGroup(elementLayoutGroup, secondaryLayoutGroup, tableName);
933 item.setSecondaryFields(secondaryLayoutGroup);
941 private void loadDataLayoutItemField(final Element element, final LayoutItemField item, final String tableName) {
942 item.setName(element.getAttribute(ATTRIBUTE_NAME));
943 loadUsesRelationship(element, tableName, item);
945 final Element elementCustomTitle = getElementByName(element, NODE_CUSTOM_TITLE);
946 if (elementCustomTitle != null) {
947 final CustomTitle customTitle = item.getCustomTitle();
948 customTitle.setUseCustomTitle(getAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM));
949 loadTitle(elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
952 // Get the actual field:
953 final String fieldName = item.getName();
954 final String inTableName = item.getTableUsed(tableName);
955 final Field field = getField(inTableName, fieldName);
956 item.setFullFieldDetails(field);
958 item.setUseDefaultFormatting(getAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING));
960 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
961 if (elementFormatting != null) {
962 loadFormatting(elementFormatting, item.getFormatting());
970 private void loadDataLayoutPortal(final Element element, final LayoutItemPortal portal, final String tableName) {
971 loadUsesRelationship(element, tableName, portal);
972 final String relatedTableName = portal.getTableUsed(tableName);
973 loadDataLayoutGroup(element, portal, relatedTableName);
975 final Element elementNavigation = getElementByName(element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
976 if (elementNavigation != null) {
977 final String navigationTypeAsString = elementNavigation.getAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE);
978 if (StringUtils.isEmpty(navigationTypeAsString)
979 || navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC) {
980 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC);
981 } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE) {
982 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_NONE);
983 } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC) {
984 // Read the specified relationship name:
985 final UsesRelationship relationshipNavigationSpecific = new UsesRelationshipImpl();
986 loadUsesRelationship(elementNavigation, relatedTableName, relationshipNavigationSpecific);
987 portal.setNavigationRelationshipSpecific(relationshipNavigationSpecific);
997 private void loadField(final Element element, final Field field) {
998 loadTitle(element, field);
1000 Field.GlomFieldType fieldType = Field.GlomFieldType.TYPE_INVALID;
1001 final String fieldTypeStr = element.getAttribute(ATTRIBUTE_FIELD_TYPE);
1002 if (!StringUtils.isEmpty(fieldTypeStr)) {
1003 if (fieldTypeStr.equals("Boolean")) {
1004 fieldType = Field.GlomFieldType.TYPE_BOOLEAN;
1005 } else if (fieldTypeStr.equals("Date")) {
1006 fieldType = Field.GlomFieldType.TYPE_DATE;
1007 } else if (fieldTypeStr.equals("Image")) {
1008 fieldType = Field.GlomFieldType.TYPE_IMAGE;
1009 } else if (fieldTypeStr.equals("Number")) {
1010 fieldType = Field.GlomFieldType.TYPE_NUMERIC;
1011 } else if (fieldTypeStr.equals("Text")) {
1012 fieldType = Field.GlomFieldType.TYPE_TEXT;
1013 } else if (fieldTypeStr.equals("Time")) {
1014 fieldType = Field.GlomFieldType.TYPE_TIME;
1018 field.setGlomFieldType(fieldType);
1020 field.setPrimaryKey(getAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY));
1022 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
1023 if (elementFormatting != null) {
1024 loadFormatting(elementFormatting, field.getFormatting());
1029 * @param elementFormatting
1032 private void loadFormatting(final Element elementFormatting, final Formatting formatting) {
1033 if (elementFormatting == null) {
1037 if (formatting == null) {
1041 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
1043 final NumericFormat numericFormatting = formatting.getNumericFormat();
1044 if (numericFormatting != null) {
1045 numericFormatting.setUseThousandsSeparator(getAttributeAsBoolean(elementFormatting,
1046 ATTRIBUTE_USE_THOUSANDS_SEPARATOR));
1048 .setDecimalPlaces((int) getAttributeAsDecimal(elementFormatting, ATTRIBUTE_DECIMAL_PLACES));
1057 private void loadReport(final Element element, final Report report, final String tableName) {
1058 report.setName(element.getAttribute(ATTRIBUTE_NAME));
1059 loadTitle(element, report);
1061 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
1063 // A report can actually only have one LayoutGroup,
1064 // though it uses the same XML structure as List and Details layouts,
1065 // which (wrongly) suggests that it can have more than one group.
1066 LayoutGroup layoutGroup = null;
1067 if (!listLayoutGroups.isEmpty()) {
1068 layoutGroup = listLayoutGroups.get(0);
1071 report.setLayoutGroup(layoutGroup);
1074 private TableInfo getTableInfo(final String tableName) {
1075 return tablesMap.get(tableName);
1078 public enum HostingMode {
1079 HOSTING_MODE_POSTGRES_CENTRAL, HOSTING_MODE_POSTGRES_SELF, HOSTING_MODE_SQLITE
1082 public String getDatabaseTitle(final String locale) {
1083 return databaseTitle.getTitle(locale);
1086 public String getDatabaseTitleOriginal() {
1087 return databaseTitle.getTitleOriginal();
1090 public List<String> getTranslationAvailableLocales() {
1091 return translationAvailableLocales;
1094 public Document.HostingMode getHostingMode() {
1099 * @param hostingMode
1101 public void setHostingMode(final HostingMode hostingMode) {
1102 this.hostingMode = hostingMode;
1105 public String getConnectionServer() {
1106 return connectionServer;
1109 public int getConnectionPort() {
1110 return connectionPort;
1113 public void setConnectionPort(final int port) {
1114 connectionPort = port;
1117 public String getConnectionDatabase() {
1118 return connectionDatabase;
1121 public List<String> getTableNames() {
1122 // TODO: Return a Set?
1123 return new ArrayList<String>(tablesMap.keySet());
1126 public boolean getTableIsHidden(final String tableName) {
1127 final TableInfo info = getTableInfo(tableName);
1132 return info.isHidden;
1135 public String getTableTitle(final String tableName, final String locale) {
1136 final TableInfo info = getTableInfo(tableName);
1141 return info.getTitle(locale);
1144 public List<Map<String, DataItem>> getExampleRows(final String tableName) {
1145 final TableInfo info = getTableInfo(tableName);
1150 return info.exampleRows;
1153 public String getDefaultTable() {
1154 for (final TableInfo info : tablesMap.values()) {
1155 if (info.isDefault) {
1156 return info.getName();
1163 public boolean getTableIsKnown(final String tableName) {
1164 final TableInfo info = getTableInfo(tableName);
1172 public List<Field> getTableFields(final String tableName) {
1173 final TableInfo info = getTableInfo(tableName);
1178 return new ArrayList<Field>(info.fieldsMap.values());
1181 public Field getField(final String tableName, final String strFieldName) {
1182 final TableInfo info = getTableInfo(tableName);
1187 return info.fieldsMap.get(strFieldName);
1190 public List<LayoutGroup> getDataLayoutGroups(final String layoutName, final String parentTableName) {
1191 final TableInfo info = getTableInfo(parentTableName);
1193 return new ArrayList<LayoutGroup>();
1196 if (layoutName == LAYOUT_NAME_DETAILS) {
1197 return info.layoutGroupsDetails;
1198 } else if (layoutName == LAYOUT_NAME_LIST) {
1199 return info.layoutGroupsList;
1201 return new ArrayList<LayoutGroup>();
1205 public List<String> getReportNames(final String tableName) {
1206 final TableInfo info = getTableInfo(tableName);
1208 return new ArrayList<String>();
1211 return new ArrayList<String>(info.reportsMap.keySet());
1214 public Report getReport(final String tableName, final String reportName) {
1215 final TableInfo info = getTableInfo(tableName);
1220 return info.reportsMap.get(reportName);
1228 public Relationship getFieldUsedInRelationshipToOne(final String tableName, final LayoutItemField layoutField) {
1230 if (layoutField == null) {
1231 Log.error("layoutField was null");
1235 Relationship result = null;
1237 final String tableUsed = layoutField.getTableUsed(tableName);
1238 final TableInfo info = getTableInfo(tableUsed);
1240 // This table is special. We would not create a relationship to it using a field:
1241 // if(tableUsed == GLOM_STANDARD_TABLE_PREFS_TABLE_NAME)
1244 Log.error("table not found: " + tableUsed);
1248 // Look at each relationship:
1249 final String fieldName = layoutField.getName();
1250 for (final Relationship relationship : info.relationshipsMap.values()) {
1251 if (relationship != null) {
1252 // If the relationship uses the field
1253 if (StringUtils.equals(relationship.getFromField(), fieldName)) {
1254 // if the to_table is not hidden:
1255 if (!getTableIsHidden(relationship.getToTable())) {
1256 // TODO_Performance: The use of this convenience method means we get the full relationship
1257 // information again:
1258 if (getRelationshipIsToOne(tableName, relationship.getName())) {
1259 result = relationship;
1271 * @param relationshipName
1274 private boolean getRelationshipIsToOne(final String tableName, final String relationshipName) {
1275 final Relationship relationship = getRelationship(tableName, relationshipName);
1276 if (relationship != null) {
1277 final Field fieldTo = getField(relationship.getToTable(), relationship.getToField());
1278 if (fieldTo != null) {
1279 return (fieldTo.getPrimaryKey() || fieldTo.getUniqueKey());
1288 * @param relationshipName
1291 private Relationship getRelationship(final String tableName, final String relationshipName) {
1292 final TableInfo info = getTableInfo(tableName);
1294 Log.error("table not found: " + tableName);
1298 return info.relationshipsMap.get(relationshipName);
1304 * @param relationship
1308 public TableToViewDetails getPortalSuitableTableToViewDetails(final LayoutItemPortal portal) {
1309 UsesRelationship navigationRelationship = null;
1311 // Check whether a relationship was specified:
1312 if (portal.getNavigationType() == NavigationType.NAVIGATION_AUTOMATIC) {
1313 navigationRelationship = getPortalNavigationRelationshipAutomatic(portal);
1315 navigationRelationship = portal.getNavigationRelationshipSpecific();
1318 // Get the navigation table name from the chosen relationship:
1319 final String directlyRelatedTableName = portal.getTableUsed("" /* not relevant */);
1321 // The navigation_table_name (and therefore, the table_name output parameter,
1322 // as well) stays empty if the navrel type was set to none.
1323 String navigationTableName = null;
1324 if (navigationRelationship != null) {
1325 navigationTableName = navigationRelationship.getTableUsed(directlyRelatedTableName);
1326 } else if (portal.getNavigationType() != NavigationType.NAVIGATION_NONE) {
1327 // An empty result from get_portal_navigation_relationship_automatic() or
1328 // get_navigation_relationship_specific() means we should use the directly related table:
1329 navigationTableName = directlyRelatedTableName;
1332 if (StringUtils.isEmpty(navigationTableName)) {
1337 Log.error("document is null.");
1341 if (getTableIsHidden(navigationTableName)) {
1342 Log.error("navigation_table_name indicates a hidden table: " + navigationTableName);
1346 final TableToViewDetails result = new TableToViewDetails();
1347 result.tableName = navigationTableName;
1348 result.usesRelationship = navigationRelationship;
1357 private UsesRelationship getPortalNavigationRelationshipAutomatic(final LayoutItemPortal portal) {
1362 // If the related table is not hidden then we can just navigate to that:
1363 final String direct_related_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1364 if (!getTableIsHidden(direct_related_table_name)) {
1365 // Non-hidden tables can just be shown directly. Navigate to it:
1368 // If the related table is hidden,
1369 // then find a suitable related non-hidden table by finding the first layout field that mentions one:
1370 final LayoutItemField field = getPortalFieldIsFromNonHiddenRelatedRecord(portal);
1371 if (field != null) {
1372 return field; // Returns the UsesRelationship base part. (A relationship belonging to the portal's
1375 // Instead, find a key field that's used in a relationship,
1376 // and pretend that we are showing the to field as a related field:
1377 final Relationship fieldIndentifies = getPortalFieldIdentifiesNonHiddenRelatedRecord(portal);
1378 if (fieldIndentifies != null) {
1379 final UsesRelationship result = new UsesRelationshipImpl();
1380 result.setRelationship(fieldIndentifies);
1386 // There was no suitable related table to show:
1395 private LayoutItemField getPortalFieldIsFromNonHiddenRelatedRecord(final LayoutItemPortal portal) {
1396 // Find the first field that is from a non-hidden related table.
1402 final LayoutItemField result = null;
1404 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1406 final List<LayoutItem> items = portal.getItems();
1407 for (final LayoutItem item : items) {
1408 if (item instanceof LayoutItemField) {
1409 final LayoutItemField field = (LayoutItemField) item;
1410 if (field.getHasRelationshipName()) {
1411 final String table_name = field.getTableUsed(parent_table_name);
1412 if (!(getTableIsHidden(table_name))) {
1423 * @param used_in_relationship
1428 private Relationship getPortalFieldIdentifiesNonHiddenRelatedRecord(final LayoutItemPortal portal) {
1429 // Find the first field that is from a non-hidden related table.
1432 Log.error("document is null");
1436 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1438 final List<LayoutItem> items = portal.getItems();
1439 for (final LayoutItem item : items) {
1440 if (item instanceof LayoutItemField) {
1441 final LayoutItemField field = (LayoutItemField) item;
1442 if (field.getHasRelationshipName()) {
1443 final Relationship relationship = getFieldUsedInRelationshipToOne(parent_table_name, field);
1444 if (relationship != null) {
1445 final String table_name = relationship.getToTable();
1446 if (!StringUtils.isEmpty(table_name)) {
1447 if (!(getTableIsHidden(table_name))) {
1448 return relationship;
1462 * @return The destination table name for navigation.
1464 public String getLayoutItemFieldShouldHaveNavigation(final String tableName, final LayoutItemField layoutItem) {
1465 if (StringUtils.isEmpty(tableName)) {
1469 if (layoutItem == null) {
1473 // Check whether the field controls a relationship,
1474 // meaning it identifies a record in another table.
1475 final Relationship fieldUsedInRelationshipToOne = getFieldUsedInRelationshipToOne(tableName, layoutItem);
1476 if (fieldUsedInRelationshipToOne != null) {
1477 return fieldUsedInRelationshipToOne.getToTable();
1480 // Check whether the field identifies a record in another table
1481 // just because it is a primary key in that table:
1482 final Field fieldInfo = layoutItem.getFullFieldDetails();
1483 final boolean fieldIsRelatedPrimaryKey = layoutItem.getHasRelationshipName() && (fieldInfo != null)
1484 && fieldInfo.getPrimaryKey();
1485 if (fieldIsRelatedPrimaryKey) {
1486 return layoutItem.getRelationship().getToTable();
1495 public void setIsExampleFile(final boolean isExample) {
1496 this.isExample = isExample;
1501 public boolean getIsExampleFile() {
1508 public boolean save() {
1509 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
1510 DocumentBuilder documentBuilder = null;
1512 documentBuilder = dbf.newDocumentBuilder();
1513 } catch (final ParserConfigurationException e) {
1514 // TODO Auto-generated catch block
1515 e.printStackTrace();
1519 final org.w3c.dom.Document doc = documentBuilder.newDocument();
1520 final Element rootNode = doc.createElement(NODE_ROOT);
1521 doc.appendChild(rootNode);
1523 rootNode.setAttribute(ATTRIBUTE_TITLE, databaseTitle.getTitleOriginal());
1524 rootNode.setAttribute(ATTRIBUTE_TRANSLATION_ORIGINAL_LOCALE, translationOriginalLocale);
1525 setAttributeAsBoolean(rootNode, ATTRIBUTE_IS_EXAMPLE, isExample);
1527 String strHostingMode = "";
1528 if (hostingMode == HostingMode.HOSTING_MODE_POSTGRES_CENTRAL) {
1529 strHostingMode = "postgres_central";
1530 } else if (hostingMode == HostingMode.HOSTING_MODE_SQLITE) {
1531 strHostingMode = "sqlite";
1533 strHostingMode = "postgres_self";
1535 final Element nodeConnection = createElement(doc, rootNode, NODE_CONNECTION);
1536 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_HOSTING_MODE, strHostingMode);
1537 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_SERVER, connectionServer);
1538 nodeConnection.setAttribute(ATTRIBUTE_CONNECTION_DATABASE, connectionDatabase);
1539 setAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT, connectionPort);
1542 for (final TableInfo table : tablesMap.values()) {
1543 final Element nodeTable = createElement(doc, rootNode, NODE_TABLE);
1544 saveTableNodeBasic(doc, nodeTable, table);
1545 saveTableLayouts(doc, nodeTable, table);
1548 final TransformerFactory transformerFactory = TransformerFactory.newInstance();
1549 Transformer transformer;
1551 transformer = transformerFactory.newTransformer();
1552 } catch (final TransformerConfigurationException e) {
1553 // TODO Auto-generated catch block
1554 e.printStackTrace();
1558 // TODO: This probably distorts text nodes,
1559 // so careful when we load/save them. For instance, scripts.
1560 transformer.setOutputProperty(OutputKeys.INDENT, "yes");
1561 transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
1563 // Make sure that the parent directory exists:
1564 final File file = new File(fileURI);
1566 Files.createParentDirs(file);
1567 } catch (final IOException e) {
1568 e.printStackTrace();
1572 final DOMSource source = new DOMSource(doc);
1573 final StreamResult result = new StreamResult(file);
1575 // Output to console for testing
1576 // StreamResult result = new StreamResult(System.out);
1579 transformer.transform(source, result);
1580 } catch (final TransformerException e) {
1581 // TODO Auto-generated catch block
1582 e.printStackTrace();
1594 private void saveTableLayouts(final org.w3c.dom.Document doc, final Element tableNode, final TableInfo table) {
1596 final Element layoutsNode = createElement(doc, tableNode, NODE_DATA_LAYOUTS);
1598 final Element nodeLayoutDetails = createElement(doc, layoutsNode, NODE_DATA_LAYOUT);
1599 nodeLayoutDetails.setAttribute(ATTRIBUTE_NAME, LAYOUT_NAME_DETAILS);
1600 saveLayoutNode(doc, nodeLayoutDetails, table.layoutGroupsDetails);
1602 final Element nodeLayoutList = createElement(doc, layoutsNode, NODE_DATA_LAYOUT);
1603 nodeLayoutList.setAttribute(ATTRIBUTE_NAME, LAYOUT_NAME_LIST);
1604 saveLayoutNode(doc, nodeLayoutList, table.layoutGroupsList);
1606 final Element reportsNode = createElement(doc, tableNode, NODE_REPORTS);
1607 for (final Report report : table.reportsMap.values()) {
1608 final Element element = createElement(doc, reportsNode, NODE_REPORT);
1609 saveReport(doc, element, report);
1619 private void saveReport(final org.w3c.dom.Document doc, final Element element, final Report report) {
1620 // TODO Auto-generated method stub
1624 private void saveLayoutNode(final org.w3c.dom.Document doc, final Element element,
1625 final List<LayoutGroup> layoutGroups) {
1626 final Element elementGroups = createElement(doc, element, NODE_DATA_LAYOUT_GROUPS);
1628 for (final LayoutGroup layoutGroup : layoutGroups) {
1629 if (layoutGroup instanceof LayoutItemNotebook) {
1630 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_NOTEBOOK);
1631 saveDataLayoutGroup(doc, elementGroup, layoutGroup);
1632 } else if (layoutGroup instanceof LayoutItemPortal) {
1633 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_PORTAL);
1634 saveDataLayoutPortal(doc, elementGroup, (LayoutItemPortal) layoutGroup);
1636 final Element elementGroup = createElement(doc, elementGroups, NODE_DATA_LAYOUT_GROUP);
1637 saveDataLayoutGroup(doc, elementGroup, layoutGroup);
1645 * @param elementGroup
1646 * @param layoutGroup
1648 private void saveDataLayoutPortal(final org.w3c.dom.Document doc, final Element element,
1649 final LayoutItemPortal portal) {
1650 saveUsesRelationship(element, portal);
1651 saveDataLayoutGroup(doc, element, portal);
1653 final Element elementNavigation = createElement(doc, element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
1654 String navigationTypeAsString = "";
1655 switch (portal.getNavigationType()) {
1656 case NAVIGATION_AUTOMATIC:
1657 navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC;
1659 case NAVIGATION_NONE:
1660 navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE;
1662 case NAVIGATION_SPECIFIC:
1663 navigationTypeAsString = ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC;
1668 elementNavigation.setAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE, navigationTypeAsString);
1670 if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC) {
1671 // Write the specified relationship name:
1672 saveUsesRelationship(elementNavigation, portal.getNavigationRelationshipSpecific());
1678 * @param elementGroup
1679 * @param layoutGroup
1681 private void saveDataLayoutGroup(final org.w3c.dom.Document doc, final Element nodeGroup, final LayoutGroup group) {
1682 saveTitle(doc, nodeGroup, group);
1684 // Write the column count:
1685 setAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT, group.getColumnCount());
1687 // Write the child items:
1688 for (final LayoutItem layoutItem : group.getItems()) {
1689 if (layoutItem instanceof LayoutItemPortal) {
1690 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_PORTAL);
1691 saveDataLayoutPortal(doc, element, (LayoutItemPortal) layoutItem);
1692 } else if (layoutItem instanceof LayoutItemNotebook) {
1693 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_NOTEBOOK);
1694 saveDataLayoutGroup(doc, element, (LayoutItemNotebook) layoutItem);
1695 } else if (layoutItem instanceof LayoutGroup) {
1696 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_GROUP);
1697 saveDataLayoutGroup(doc, element, (LayoutGroup) layoutItem);
1698 } else if (layoutItem instanceof LayoutItemField) {
1699 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_ITEM);
1700 saveDataLayoutItemField(doc, element, (LayoutItemField) layoutItem);
1701 } else if (layoutItem instanceof LayoutItemGroupBy) {
1702 final Element element = createElement(doc, nodeGroup, NODE_DATA_LAYOUT_ITEM_GROUPBY);
1703 saveDataLayoutItemGroupBy(doc, element, (LayoutItemGroupBy) layoutItem);
1713 private void saveDataLayoutItemGroupBy(final org.w3c.dom.Document doc, final Element element,
1714 final LayoutItemGroupBy item) {
1715 saveDataLayoutGroup(doc, element, item);
1717 final Element elementGroupBy = createElement(doc, element, NODE_GROUPBY);
1718 saveDataLayoutItemField(doc, elementGroupBy, item.getFieldGroupBy());
1720 final Element elementSecondaryFields = createElement(doc, element, NODE_SECONDARY_FIELDS);
1721 final Element elementLayoutGroup = createElement(doc, elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
1722 saveDataLayoutGroup(doc, elementLayoutGroup, item.getSecondaryFields());
1730 private void saveDataLayoutItemField(final org.w3c.dom.Document doc, final Element element,
1731 final LayoutItemField item) {
1732 element.setAttribute(ATTRIBUTE_NAME, item.getName());
1733 saveUsesRelationship(element, item);
1735 final CustomTitle customTitle = item.getCustomTitle();
1736 if (customTitle != null) {
1737 final Element elementCustomTitle = createElement(doc, element, NODE_CUSTOM_TITLE);
1738 setAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM,
1739 customTitle.getUseCustomTitle());
1740 saveTitle(doc, elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
1743 setAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING, item.getUseDefaultFormatting());
1745 final Element elementFormatting = createElement(doc, element, NODE_FORMATTING);
1746 saveFormatting(elementFormatting, item.getFormatting());
1753 private void saveUsesRelationship(final Element element, final UsesRelationship item) {
1754 final Relationship relationship = item.getRelationship();
1755 if (relationship != null) {
1756 element.setAttribute(ATTRIBUTE_RELATIONSHIP_NAME, relationship.getName());
1759 final Relationship relatedRelationship = item.getRelatedRelationship();
1760 if (relatedRelationship != null) {
1761 element.setAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME, relatedRelationship.getName());
1767 * @param nodeConnection
1770 private Element createElement(final org.w3c.dom.Document doc, final Element parentNode, final String name) {
1771 final Element node = doc.createElement(name);
1772 parentNode.appendChild(node);
1776 public String getSelfHostedDirectoryPath() {
1777 final String uriFile = getFileURI();
1778 if (!StringUtils.isEmpty(uriFile)) {
1779 final File file = new File(uriFile);
1780 final File parent = file.getParentFile();
1781 if (parent == null) {
1786 File dataDir = null;
1787 switch (hostingMode) {
1788 case HOSTING_MODE_POSTGRES_SELF:
1789 dataDir = new File(parent, "glom_postgres_data");
1791 case HOSTING_MODE_POSTGRES_CENTRAL:
1794 case HOSTING_MODE_SQLITE:
1802 if (dataDir != null) {
1803 return dataDir.getPath();
1807 // TODO: std::cerr << G_STRFUNC << ": returning empty string." << std::endl;
1813 public void setConnectionDatabase(final String databaseName) {
1814 connectionDatabase = databaseName;