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;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Hashtable;
25 import java.util.List;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
29 import javax.xml.parsers.ParserConfigurationException;
31 import org.apache.commons.lang3.StringUtils;
32 import org.glom.web.shared.libglom.CustomTitle;
33 import org.glom.web.shared.libglom.Field;
34 import org.glom.web.shared.libglom.NumericFormat;
35 import org.glom.web.shared.libglom.Relationship;
36 import org.glom.web.shared.libglom.Report;
37 import org.glom.web.shared.libglom.Translatable;
38 import org.glom.web.shared.libglom.layout.Formatting;
39 import org.glom.web.shared.libglom.layout.LayoutGroup;
40 import org.glom.web.shared.libglom.layout.LayoutItem;
41 import org.glom.web.shared.libglom.layout.LayoutItemField;
42 import org.glom.web.shared.libglom.layout.LayoutItemNotebook;
43 import org.glom.web.shared.libglom.layout.LayoutItemPortal;
44 import org.glom.web.shared.libglom.layout.LayoutItemPortal.NavigationType;
45 import org.glom.web.shared.libglom.layout.UsesRelationship;
46 import org.glom.web.shared.libglom.layout.UsesRelationshipImpl;
47 import org.glom.web.shared.libglom.layout.reportparts.LayoutItemGroupBy;
48 import org.jfree.util.Log;
49 import org.w3c.dom.Element;
50 import org.w3c.dom.Node;
51 import org.w3c.dom.NodeList;
52 import org.xml.sax.SAXException;
55 * @author Murray Cumming <murrayc@openismus.com>
58 public class Document {
60 @SuppressWarnings("serial")
61 private static class TableInfo extends Translatable {
62 public boolean isDefault;
63 public boolean isHidden;
65 private final Hashtable<String, Field> fieldsMap = new Hashtable<String, Field>();
66 private final Hashtable<String, Relationship> relationshipsMap = new Hashtable<String, Relationship>();
67 private final Hashtable<String, Report> reportsMap = new Hashtable<String, Report>();
69 private List<LayoutGroup> layoutGroupsList = new ArrayList<LayoutGroup>();
70 private List<LayoutGroup> layoutGroupsDetails = new ArrayList<LayoutGroup>();
73 private String fileURI = "";
74 private org.w3c.dom.Document xmlDocument = null;
76 private Translatable databaseTitle = new Translatable();
77 private List<String> translationAvailableLocales = new ArrayList<String>(); // TODO
78 private String connectionServer = "";
79 private String connectionDatabase = "";
80 private int connectionPort = 0;
81 private final Hashtable<String, TableInfo> tablesMap = new Hashtable<String, TableInfo>();
83 private static final String NODE_CONNECTION = "connection";
84 private static final String ATTRIBUTE_CONNECTION_SERVER = "server";
85 private static final String ATTRIBUTE_CONNECTION_DATABASE = "database";
86 private static final String ATTRIBUTE_CONNECTION_PORT = "port";
87 private static final String NODE_TABLE = "table";
88 private static final String ATTRIBUTE_NAME = "name";
89 private static final String ATTRIBUTE_TITLE = "title";
90 private static final String ATTRIBUTE_DEFAULT = "default";
91 private static final String ATTRIBUTE_HIDDEN = "hidden";
92 private static final String NODE_TRANSLATIONS_SET = "trans_set";
93 private static final String NODE_TRANSLATIONS = "trans";
94 private static final String ATTRIBUTE_TRANSLATION_LOCALE = "loc";
95 private static final String ATTRIBUTE_TRANSLATION_TITLE = "val";
96 private static final String NODE_REPORTS = "reports";
97 private static final String NODE_REPORT = "report";
98 private static final String NODE_FIELDS = "fields";
99 private static final String NODE_FIELD = "field";
100 private static final String ATTRIBUTE_PRIMARY_KEY = "primary_key";
101 private static final String ATTRIBUTE_FIELD_TYPE = "type";
102 private static final String NODE_FORMATTING = "formatting";
103 // private static final String ATTRIBUTE_TEXT_FORMAT_MULTILINE = "format_text_multiline";
104 private static final String ATTRIBUTE_USE_THOUSANDS_SEPARATOR = "format_thousands_separator";
105 private static final String ATTRIBUTE_DECIMAL_PLACES = "format_decimal_places";
106 private static final String NODE_RELATIONSHIPS = "relationships";
107 private static final String NODE_RELATIONSHIP = "relationship";
108 private static final String ATTRIBUTE_RELATIONSHIP_FROM_FIELD = "key";
109 private static final String ATTRIBUTE_RELATIONSHIP_TO_TABLE = "other_table";
110 private static final String ATTRIBUTE_RELATIONSHIP_TO_FIELD = "other_key";
111 private static final String NODE_DATA_LAYOUTS = "data_layouts";
112 private static final String NODE_DATA_LAYOUT = "data_layout";
113 private static final String NODE_DATA_LAYOUT_GROUPS = "data_layout_groups";
114 private static final String NODE_DATA_LAYOUT_GROUP = "data_layout_group";
115 private static final String ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT = "columns_count";
116 private static final String NODE_DATA_LAYOUT_NOTEBOOK = "data_layout_notebook";
117 private static final String NODE_DATA_LAYOUT_PORTAL = "data_layout_portal";
118 private static final String NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP = "portal_navigation_relationship";
119 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE = "navigation_type";
120 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC = "automatic";
121 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC = "specific";
122 private static final String ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE = "none";
123 private static final String ATTRIBUTE_RELATIONSHIP_NAME = "relationship";
124 private static final String ATTRIBUTE_RELATED_RELATIONSHIP_NAME = "related_relationship";
125 private static final String NODE_DATA_LAYOUT_ITEM = "data_layout_item";
126 private static final String NODE_CUSTOM_TITLE = "title_custom";
127 private static final String ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM = "use_custom";
128 private static final String NODE_DATA_LAYOUT_ITEM_GROUPBY = "data_layout_item_groupby";
129 private static final String NODE_GROUPBY = "groupby";
130 private static final String NODE_SECONDARY_FIELDS = "secondary_fields";
131 private static final String ATTRIBUTE_USE_DEFAULT_FORMATTING = "use_default_formatting";
132 private static final String LAYOUT_NAME_DETAILS = "details";
133 private static final String LAYOUT_NAME_LIST = "list";
135 public void setFileURI(final String fileURI) {
136 this.fileURI = fileURI;
139 public String getFileURI() {
143 // TODO: Make sure these have the correct values.
144 public enum LoadFailureCodes {
145 LOAD_FAILURE_CODE_NONE, LOAD_FAILURE_CODE_NOT_FOUND, LOAD_FAILURE_CODE_FILE_VERSION_TOO_NEW
148 public boolean load() {
149 final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
150 DocumentBuilder documentBuilder;
152 documentBuilder = dbf.newDocumentBuilder();
153 } catch (final ParserConfigurationException e) {
154 // TODO Auto-generated catch block
160 xmlDocument = documentBuilder.parse(fileURI);
161 } catch (final SAXException e) {
162 // TODO Auto-generated catch block
165 } catch (final IOException e) {
166 // TODO Auto-generated catch block
171 final Element rootNode = xmlDocument.getDocumentElement();
172 if (rootNode.getNodeName() != "glom_document") {
173 Log.error("Unexpected XML root node name found: " + rootNode.getNodeName());
177 databaseTitle.setTitleOriginal(rootNode.getAttribute(ATTRIBUTE_TITLE));
179 // We first load the fields, relationships, etc,
181 final List<Node> listTableNodes = getChildrenByTagName(rootNode, NODE_TABLE);
182 for (final Node node : listTableNodes) {
183 if (!(node instanceof Element))
186 final Element element = (Element) node;
187 final TableInfo info = loadTableNodeBasic(element);
188 tablesMap.put(info.getName(), info);
191 // We then load the layouts for all tables, because they
192 // need the fields and relationships for all tables:
193 for (final Node node : listTableNodes) {
194 if (!(node instanceof Element))
197 final Element element = (Element) node;
198 final String tableName = element.getAttribute(ATTRIBUTE_NAME);
200 // We first load the fields, relationships, etc:
201 final TableInfo info = getTableInfo(tableName);
206 // We then load the layouts afterwards, because they
207 // need the fields and relationships:
208 loadTableLayouts(element, info);
210 tablesMap.put(info.getName(), info);
213 final Element nodeConnection = getElementByName(rootNode, NODE_CONNECTION);
214 if (nodeConnection != null) {
215 connectionServer = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_SERVER);
216 connectionDatabase = nodeConnection.getAttribute(ATTRIBUTE_CONNECTION_DATABASE);
217 connectionPort = getAttributeAsDecimal(nodeConnection, ATTRIBUTE_CONNECTION_PORT);
223 private Element getElementByName(final Element parentElement, final String tagName) {
224 final List<Node> listNodes = getChildrenByTagName(parentElement, tagName);
225 if (listNodes == null)
228 if (listNodes.size() == 0)
231 return (Element) listNodes.get(0);
234 private boolean getAttributeAsBoolean(final Element node, final String attributeName) {
235 final String str = node.getAttribute(attributeName);
239 return str.equals("true");
243 * @param elementFormatting
244 * @param aTTRIBUTE_DECIMAL_PLACES2
247 private int getAttributeAsDecimal(final Element node, final String attributeName) {
248 final String str = node.getAttribute(attributeName);
249 if (StringUtils.isEmpty(str))
252 return Integer.valueOf(str);
256 * Load a title and its translations.
259 * The XML Element that may contain a title attribute and a trans_set of translations of the title.
262 private void loadTitle(final Element node, final Translatable title) {
263 title.setName(node.getAttribute(ATTRIBUTE_NAME));
265 title.setTitleOriginal(node.getAttribute(ATTRIBUTE_TITLE));
267 final Element nodeSet = getElementByName(node, NODE_TRANSLATIONS_SET);
268 if (nodeSet == null) {
272 final List<Node> listNodes = getChildrenByTagName(nodeSet, NODE_TRANSLATIONS);
273 if (listNodes == null)
276 for (final Node transNode : listNodes) {
277 if (!(transNode instanceof Element)) {
281 final Element element = (Element) transNode;
283 final String locale = element.getAttribute(ATTRIBUTE_TRANSLATION_LOCALE);
284 final String translatedTitle = element.getAttribute(ATTRIBUTE_TRANSLATION_TITLE);
285 if (!StringUtils.isEmpty(locale) && !StringUtils.isEmpty(translatedTitle)) {
286 title.setTitle(translatedTitle, locale);
295 private TableInfo loadTableNodeBasic(final Element tableNode) {
296 final TableInfo info = new TableInfo();
297 loadTitle(tableNode, info);
298 final String tableName = info.getName();
300 info.isDefault = getAttributeAsBoolean(tableNode, ATTRIBUTE_DEFAULT);
301 info.isHidden = getAttributeAsBoolean(tableNode, ATTRIBUTE_HIDDEN);
303 // These should be loaded before the fields, because the fields use them.
304 final Element relationshipsNode = getElementByName(tableNode, NODE_RELATIONSHIPS);
305 if (relationshipsNode != null) {
306 final List<Node> listNodes = getChildrenByTagName(relationshipsNode, NODE_RELATIONSHIP);
307 for (final Node node : listNodes) {
308 if (!(node instanceof Element)) {
312 final Element element = (Element) node;
313 final Relationship relationship = new Relationship();
314 loadTitle(element, relationship);
315 relationship.setFromTable(tableName);
316 relationship.setFromField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_FROM_FIELD));
317 relationship.setToTable(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_TABLE));
318 relationship.setToField(element.getAttribute(ATTRIBUTE_RELATIONSHIP_TO_FIELD));
320 info.relationshipsMap.put(relationship.getName(), relationship);
324 final Element fieldsNode = getElementByName(tableNode, NODE_FIELDS);
325 if (fieldsNode != null) {
326 final List<Node> listNodes = getChildrenByTagName(fieldsNode, NODE_FIELD);
327 for (final Node node : listNodes) {
328 if (!(node instanceof Element)) {
332 final Element element = (Element) node;
333 final Field field = new Field();
334 loadField(element, field);
336 info.fieldsMap.put(field.getName(), field);
347 private void loadTableLayouts(final Element tableNode, final TableInfo info) {
348 final String tableName = info.getName();
350 final Element layoutsNode = getElementByName(tableNode, NODE_DATA_LAYOUTS);
351 if (layoutsNode != null) {
352 final List<Node> listNodes = getChildrenByTagName(layoutsNode, NODE_DATA_LAYOUT);
353 for (final Node node : listNodes) {
354 if (!(node instanceof Element)) {
358 final Element element = (Element) node;
359 final String name = element.getAttribute("name");
360 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
361 if (name.equals(LAYOUT_NAME_DETAILS)) {
362 info.layoutGroupsDetails = listLayoutGroups;
363 } else if (name.equals(LAYOUT_NAME_LIST)) {
364 info.layoutGroupsList = listLayoutGroups;
366 Log.error("loadTableNode(): unexpected layout name: " + name);
371 final Element reportsNode = getElementByName(tableNode, NODE_REPORTS);
372 if (reportsNode != null) {
373 final List<Node> listNodes = getChildrenByTagName(reportsNode, NODE_REPORT);
374 for (final Node node : listNodes) {
375 if (!(node instanceof Element)) {
379 final Element element = (Element) node;
380 final Report report = new Report();
381 loadReport(element, report, tableName);
383 info.reportsMap.put(report.getName(), report);
392 private List<LayoutGroup> loadLayoutNode(final Element node, final String tableName) {
397 final List<LayoutGroup> result = new ArrayList<LayoutGroup>();
399 final List<Node> listNodes = getChildrenByTagName(node, NODE_DATA_LAYOUT_GROUPS);
400 for (final Node nodeGroups : listNodes) {
401 if (!(nodeGroups instanceof Element)) {
405 final Element elementGroups = (Element) nodeGroups;
407 final NodeList list = elementGroups.getChildNodes();
408 final int num = list.getLength();
409 for (int i = 0; i < num; i++) {
410 final Node nodeLayoutGroup = list.item(i);
411 if (nodeLayoutGroup == null) {
415 if (!(nodeLayoutGroup instanceof Element)) {
419 final Element element = (Element) nodeLayoutGroup;
420 final String tagName = element.getTagName();
421 if (tagName == NODE_DATA_LAYOUT_GROUP) {
422 final LayoutGroup group = new LayoutGroup();
423 loadDataLayoutGroup(element, group, tableName);
425 } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
426 final LayoutItemNotebook group = new LayoutItemNotebook();
427 loadDataLayoutGroup(element, group, tableName);
429 } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
430 final LayoutItemPortal portal = new LayoutItemPortal();
431 loadDataLayoutPortal(element, portal, tableName);
445 private void loadUsesRelationship(final Element element, final String tableName, final UsesRelationship item) {
446 if (element == null) {
454 final String relationshipName = element.getAttribute(ATTRIBUTE_RELATIONSHIP_NAME);
455 Relationship relationship = null;
456 if (!StringUtils.isEmpty(relationshipName)) {
457 // std::cout << " debug in : tableName=" << tableName << ", relationshipName=" << relationship_name <<
459 relationship = getRelationship(tableName, relationshipName);
460 item.setRelationship(relationship);
462 if (relationship == null) {
463 Log.error("relationship not found: " + relationshipName + ", in table: " + tableName);
467 final String relatedRelationshipName = element.getAttribute(ATTRIBUTE_RELATED_RELATIONSHIP_NAME);
468 if (!StringUtils.isEmpty(relatedRelationshipName) && (relationship != null)) {
469 final Relationship relatedRelationship = getRelationship(relationship.getToTable(), relatedRelationshipName);
470 if (relatedRelationship == null) {
471 Log.error("related relationship not found in table=" + relationship.getToTable() + ", name="
472 + relatedRelationshipName);
474 item.setRelatedRelationship(relatedRelationship);
480 * getElementsByTagName() is recursive, but we do not want that.
486 private List<Node> getChildrenByTagName(final Element parentNode, final String tagName) {
487 final List<Node> result = new ArrayList<Node>();
489 final NodeList list = parentNode.getElementsByTagName(tagName);
490 final int num = list.getLength();
491 for (int i = 0; i < num; i++) {
492 final Node node = list.item(i);
497 final Node itemParentNode = node.getParentNode();
498 if (itemParentNode.equals(parentNode)) {
510 private void loadDataLayoutGroup(final Element nodeGroup, final LayoutGroup group, final String tableName) {
511 loadTitle(nodeGroup, group);
513 // Read the column count:
514 int columnCount = getAttributeAsDecimal(nodeGroup, ATTRIBUTE_LAYOUT_GROUP_COLUMNS_COUNT);
515 if (columnCount < 1) {
516 columnCount = 1; // 0 is a useless default.
518 group.setColumnCount(columnCount);
520 // Get the child items:
521 final NodeList listNodes = nodeGroup.getChildNodes();
522 final int num = listNodes.getLength();
523 for (int i = 0; i < num; i++) {
524 final Node node = listNodes.item(i);
525 if (!(node instanceof Element))
528 final Element element = (Element) node;
529 final String tagName = element.getTagName();
530 if (tagName == NODE_DATA_LAYOUT_GROUP) {
531 final LayoutGroup childGroup = new LayoutGroup();
532 loadDataLayoutGroup(element, childGroup, tableName);
533 group.addItem(childGroup);
534 } else if (tagName == NODE_DATA_LAYOUT_NOTEBOOK) {
535 final LayoutItemNotebook childGroup = new LayoutItemNotebook();
536 loadDataLayoutGroup(element, childGroup, tableName);
537 group.addItem(childGroup);
538 } else if (tagName == NODE_DATA_LAYOUT_PORTAL) {
539 final LayoutItemPortal childGroup = new LayoutItemPortal();
540 loadDataLayoutPortal(element, childGroup, tableName);
541 group.addItem(childGroup);
542 } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM) {
543 final LayoutItemField item = new LayoutItemField();
544 loadDataLayoutItemField(element, item, tableName);
546 } else if (element.getTagName() == NODE_DATA_LAYOUT_ITEM_GROUPBY) {
547 final LayoutItemGroupBy item = new LayoutItemGroupBy();
548 loadDataLayoutItemGroupBy(element, item, tableName);
559 private void loadDataLayoutItemGroupBy(final Element element, final LayoutItemGroupBy item, final String tableName) {
560 loadDataLayoutGroup(element, item, tableName);
562 final Element elementGroupBy = getElementByName(element, NODE_GROUPBY);
563 if (elementGroupBy == null) {
567 final LayoutItemField fieldGroupBy = new LayoutItemField();
568 loadDataLayoutItemField(elementGroupBy, fieldGroupBy, tableName);
569 item.setFieldGroupBy(fieldGroupBy);
571 final Element elementSecondaryFields = getElementByName(element, NODE_SECONDARY_FIELDS);
572 if (elementSecondaryFields == null) {
576 final Element elementLayoutGroup = getElementByName(elementSecondaryFields, NODE_DATA_LAYOUT_GROUP);
577 if (elementLayoutGroup != null) {
578 final LayoutGroup secondaryLayoutGroup = new LayoutGroup();
579 loadDataLayoutGroup(elementLayoutGroup, secondaryLayoutGroup, tableName);
580 item.setSecondaryFields(secondaryLayoutGroup);
588 private void loadDataLayoutItemField(final Element element, final LayoutItemField item, final String tableName) {
589 item.setName(element.getAttribute(ATTRIBUTE_NAME));
590 loadUsesRelationship(element, tableName, item);
592 final Element elementCustomTitle = getElementByName(element, NODE_CUSTOM_TITLE);
593 if (elementCustomTitle != null) {
594 final CustomTitle customTitle = item.getCustomTitle();
595 customTitle.setUseCustomTitle(getAttributeAsBoolean(elementCustomTitle, ATTRIBUTE_CUSTOM_TITLE_USE_CUSTOM));
596 loadTitle(elementCustomTitle, customTitle); // LayoutItemField doesn't use its own title member.
599 // Get the actual field:
600 final String fieldName = item.getName();
601 final String inTableName = item.getTableUsed(tableName);
602 final Field field = getField(inTableName, fieldName);
603 item.setFullFieldDetails(field);
605 item.setUseDefaultFormatting(getAttributeAsBoolean(element, ATTRIBUTE_USE_DEFAULT_FORMATTING));
607 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
608 if (elementFormatting != null) {
609 loadFormatting(elementFormatting, item.getFormatting());
617 private void loadDataLayoutPortal(final Element element, final LayoutItemPortal portal, final String tableName) {
618 loadUsesRelationship(element, tableName, portal);
619 final String relatedTableName = portal.getTableUsed(tableName);
620 loadDataLayoutGroup(element, portal, relatedTableName);
622 final Element elementNavigation = getElementByName(element, NODE_DATA_LAYOUT_PORTAL_NAVIGATIONRELATIONSHIP);
623 if (elementNavigation != null) {
624 final String navigationTypeAsString = elementNavigation.getAttribute(ATTRIBUTE_PORTAL_NAVIGATION_TYPE);
625 if (StringUtils.isEmpty(navigationTypeAsString)
626 || navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_AUTOMATIC) {
627 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_AUTOMATIC);
628 } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_NONE) {
629 portal.setNavigationType(LayoutItemPortal.NavigationType.NAVIGATION_NONE);
630 } else if (navigationTypeAsString == ATTRIBUTE_PORTAL_NAVIGATION_TYPE_SPECIFIC) {
631 // Read the specified relationship name:
632 final UsesRelationship relationshipNavigationSpecific = new UsesRelationshipImpl();
633 loadUsesRelationship(elementNavigation, relatedTableName, relationshipNavigationSpecific);
634 portal.setNavigationRelationshipSpecific(relationshipNavigationSpecific);
644 private void loadField(final Element element, final Field field) {
645 loadTitle(element, field);
647 Field.GlomFieldType fieldType = Field.GlomFieldType.TYPE_INVALID;
648 final String fieldTypeStr = element.getAttribute(ATTRIBUTE_FIELD_TYPE);
649 if (!StringUtils.isEmpty(fieldTypeStr)) {
650 if (fieldTypeStr.equals("Boolean")) {
651 fieldType = Field.GlomFieldType.TYPE_BOOLEAN;
652 } else if (fieldTypeStr.equals("Date")) {
653 fieldType = Field.GlomFieldType.TYPE_DATE;
654 } else if (fieldTypeStr.equals("Image")) {
655 fieldType = Field.GlomFieldType.TYPE_IMAGE;
656 } else if (fieldTypeStr.equals("Number")) {
657 fieldType = Field.GlomFieldType.TYPE_NUMERIC;
658 } else if (fieldTypeStr.equals("Text")) {
659 fieldType = Field.GlomFieldType.TYPE_TEXT;
660 } else if (fieldTypeStr.equals("Time")) {
661 fieldType = Field.GlomFieldType.TYPE_TIME;
665 field.setGlomFieldType(fieldType);
667 field.setPrimaryKey(getAttributeAsBoolean(element, ATTRIBUTE_PRIMARY_KEY));
668 loadTitle(element, field);
670 final Element elementFormatting = getElementByName(element, NODE_FORMATTING);
671 if (elementFormatting != null) {
672 loadFormatting(elementFormatting, field.getFormatting());
677 * @param elementFormatting
680 private void loadFormatting(final Element elementFormatting, final Formatting formatting) {
681 if (elementFormatting == null)
684 if (formatting == null)
687 // formatting.setTextFormatMultiline(getAttributeAsBoolean(elementFormatting, ATTRIBUTE_TEXT_FORMAT_MULTILINE));
689 final NumericFormat numericFormatting = formatting.getNumericFormat();
690 if (numericFormatting != null) {
691 numericFormatting.setUseThousandsSeparator(getAttributeAsBoolean(elementFormatting,
692 ATTRIBUTE_USE_THOUSANDS_SEPARATOR));
693 numericFormatting.setDecimalPlaces(getAttributeAsDecimal(elementFormatting, ATTRIBUTE_DECIMAL_PLACES));
702 private void loadReport(final Element element, final Report report, final String tableName) {
703 report.setName(element.getAttribute(ATTRIBUTE_NAME));
704 loadTitle(element, report);
706 final List<LayoutGroup> listLayoutGroups = loadLayoutNode(element, tableName);
708 // A report can actually only have one LayoutGroup,
709 // though it uses the same XML structure as List and Details layouts,
710 // which (wrongly) suggests that it can have more than one group.
711 LayoutGroup layoutGroup = null;
712 if (!listLayoutGroups.isEmpty()) {
713 layoutGroup = listLayoutGroups.get(0);
716 report.setLayoutGroup(layoutGroup);
719 private TableInfo getTableInfo(final String tableName) {
720 return tablesMap.get(tableName);
723 public enum HostingMode {
724 HOSTING_MODE_POSTGRES_CENTRAL, HOSTING_MODE_POSTGRES_SELF, HOSTING_MODE_SQLITE
727 public String getDatabaseTitle(final String locale) {
728 return databaseTitle.getTitle(locale);
731 public String getDatabaseTitleOriginal() {
732 return databaseTitle.getTitleOriginal();
735 public List<String> getTranslationAvailableLocales() {
736 return translationAvailableLocales;
739 public Document.HostingMode getHostingMode() {
740 return HostingMode.HOSTING_MODE_POSTGRES_CENTRAL; // TODO
743 public String getConnectionServer() {
744 return connectionServer;
747 public long getConnectionPort() {
748 return connectionPort;
751 public String getConnectionDatabase() {
752 return connectionDatabase;
755 public List<String> getTableNames() {
756 // TODO: Return a Set?
757 return new ArrayList<String>(tablesMap.keySet());
760 public boolean getTableIsHidden(final String tableName) {
761 final TableInfo info = getTableInfo(tableName);
766 return info.isHidden;
769 public String getTableTitle(final String tableName, final String locale) {
770 final TableInfo info = getTableInfo(tableName);
775 return info.getTitle(locale);
778 public String getDefaultTable() {
779 for (final TableInfo info : tablesMap.values()) {
780 if (info.isDefault) {
781 return info.getName();
788 public boolean getTableIsKnown(final String tableName) {
789 final TableInfo info = getTableInfo(tableName);
797 public List<Field> getTableFields(final String tableName) {
798 final TableInfo info = getTableInfo(tableName);
802 return new ArrayList<Field>(info.fieldsMap.values());
805 public Field getField(final String tableName, final String strFieldName) {
806 final TableInfo info = getTableInfo(tableName);
810 return info.fieldsMap.get(strFieldName);
813 public List<LayoutGroup> getDataLayoutGroups(final String layoutName, final String parentTableName) {
814 final TableInfo info = getTableInfo(parentTableName);
816 return new ArrayList<LayoutGroup>();
818 if (layoutName == LAYOUT_NAME_DETAILS) {
819 return info.layoutGroupsDetails;
820 } else if (layoutName == LAYOUT_NAME_LIST) {
821 return info.layoutGroupsList;
823 return new ArrayList<LayoutGroup>();
827 public List<String> getReportNames(final String tableName) {
828 final TableInfo info = getTableInfo(tableName);
830 return new ArrayList<String>();
832 return new ArrayList<String>(info.reportsMap.keySet());
835 public Report getReport(final String tableName, final String reportName) {
836 final TableInfo info = getTableInfo(tableName);
840 return info.reportsMap.get(reportName);
848 public Relationship getFieldUsedInRelationshipToOne(final String tableName, final LayoutItemField layoutField) {
850 if (layoutField == null) {
851 Log.error("layoutField was null");
855 Relationship result = null;
857 final String tableUsed = layoutField.getTableUsed(tableName);
858 final TableInfo info = getTableInfo(tableUsed);
860 // This table is special. We would not create a relationship to it using a field:
861 // if(tableUsed == GLOM_STANDARD_TABLE_PREFS_TABLE_NAME)
864 Log.error("table not found: " + tableUsed);
868 // Look at each relationship:
869 final String fieldName = layoutField.getName();
870 for (final Relationship relationship : info.relationshipsMap.values()) {
871 if (relationship != null) {
872 // If the relationship uses the field
873 if (relationship.getFromField() == fieldName) {
874 // if the to_table is not hidden:
875 if (!getTableIsHidden(relationship.getToTable())) {
876 // TODO_Performance: The use of this convenience method means we get the full relationship
877 // information again:
878 if (getRelationshipIsToOne(tableName, relationship.getName())) {
879 result = relationship;
891 * @param relationshipName
894 private boolean getRelationshipIsToOne(final String tableName, final String relationshipName) {
895 final Relationship relationship = getRelationship(tableName, relationshipName);
896 if (relationship != null) {
897 final Field fieldTo = getField(relationship.getToTable(), relationship.getToField());
898 if (fieldTo != null) {
899 return (fieldTo.getPrimaryKey() || fieldTo.getUniqueKey());
908 * @param relationshipName
911 private Relationship getRelationship(final String tableName, final String relationshipName) {
912 final TableInfo info = getTableInfo(tableName);
914 Log.error("table not found: " + tableName);
918 return info.relationshipsMap.get(relationshipName);
921 public class TableToViewDetails {
922 public String tableName;
923 public UsesRelationship usesRelationship;
929 * @param relationship
933 public TableToViewDetails getPortalSuitableTableToViewDetails(LayoutItemPortal portal) {
934 UsesRelationship navigationRelationship = null;
936 // Check whether a relationship was specified:
937 if (portal.getNavigationType() == NavigationType.NAVIGATION_AUTOMATIC) {
938 navigationRelationship = getPortalNavigationRelationshipAutomatic(portal);
940 navigationRelationship = portal.getNavigationRelationshipSpecific();
943 // Get the navigation table name from the chosen relationship:
944 String directlyRelatedTableName = portal.getTableUsed("" /* not relevant */);
946 // The navigation_table_name (and therefore, the table_name output parameter,
947 // as well) stays empty if the navrel type was set to none.
948 String navigationTableName = null;
949 if (navigationRelationship != null) {
950 navigationTableName = navigationRelationship.getTableUsed(directlyRelatedTableName);
951 } else if (portal.getNavigationType() != NavigationType.NAVIGATION_NONE) {
952 // An empty result from get_portal_navigation_relationship_automatic() or
953 // get_navigation_relationship_specific() means we should use the directly related table:
954 navigationTableName = directlyRelatedTableName;
957 if (StringUtils.isEmpty(navigationTableName)) {
962 Log.error("document is null.");
966 if (getTableIsHidden(navigationTableName)) {
967 Log.error("navigation_table_name indicates a hidden table: " + navigationTableName);
971 TableToViewDetails result = new TableToViewDetails();
972 result.tableName = navigationTableName;
973 result.usesRelationship = navigationRelationship;
982 private UsesRelationship getPortalNavigationRelationshipAutomatic(LayoutItemPortal portal) {
987 // If the related table is not hidden then we can just navigate to that:
988 final String direct_related_table_name = portal.getTableUsed("" /* parent table - not relevant */);
989 if (!getTableIsHidden(direct_related_table_name)) {
990 // Non-hidden tables can just be shown directly. Navigate to it:
993 // If the related table is hidden,
994 // then find a suitable related non-hidden table by finding the first layout field that mentions one:
995 final LayoutItemField field = getPortalFieldIsFromNonHiddenRelatedRecord(portal);
997 return field; // Returns the UsesRelationship base part. (A relationship belonging to the portal's
1000 // Instead, find a key field that's used in a relationship,
1001 // and pretend that we are showing the to field as a related field:
1002 final Relationship fieldIndentifies = getPortalFieldIdentifiesNonHiddenRelatedRecord(portal);
1003 if (fieldIndentifies != null) {
1004 UsesRelationship result = new UsesRelationshipImpl();
1005 result.setRelationship(fieldIndentifies);
1011 // There was no suitable related table to show:
1020 private LayoutItemField getPortalFieldIsFromNonHiddenRelatedRecord(LayoutItemPortal portal) {
1021 // Find the first field that is from a non-hidden related table.
1027 LayoutItemField result = null;
1029 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1031 final List<LayoutItem> items = portal.getItems();
1032 for (LayoutItem item : items) {
1033 if (item instanceof LayoutItemField) {
1034 LayoutItemField field = (LayoutItemField) item;
1035 if (field.getHasRelationshipName()) {
1036 final String table_name = field.getTableUsed(parent_table_name);
1037 if (!(getTableIsHidden(table_name)))
1047 * @param used_in_relationship
1052 private Relationship getPortalFieldIdentifiesNonHiddenRelatedRecord(LayoutItemPortal portal) {
1053 // Find the first field that is from a non-hidden related table.
1056 Log.error("document is null");
1060 final String parent_table_name = portal.getTableUsed("" /* parent table - not relevant */);
1062 List<LayoutItem> items = portal.getItems();
1063 for (LayoutItem item : items) {
1064 if (item instanceof LayoutItemField) {
1065 LayoutItemField field = (LayoutItemField) item;
1066 if (field.getHasRelationshipName()) {
1067 final Relationship relationship = getFieldUsedInRelationshipToOne(parent_table_name, field);
1068 if (relationship != null) {
1069 final String table_name = relationship.getToTable();
1070 if (!StringUtils.isEmpty(table_name)) {
1071 if (!(getTableIsHidden(table_name))) {
1072 return relationship;