From bdd10449d5479b51df59e8359828738858eff7bc Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Thu, 15 Nov 2012 22:46:49 +0100 Subject: [PATCH] Document: Add getLayoutItemByPath(). * src/main/java/org/glom/web/server/Utils.java: Add buildLayoutPath() and parseLayoutPath(). * src/test/java/org/glom/web/server/LayoutPathTest.java: Test that these utility methods work. * src/main/java/org/glom/web/server/libglom/Document.java: Add Add getLayoutItemByPath(). * src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java: Test that it works. This will be useful in subsequent commits. --- ChangeLog | 69 +++++++++++++++++ src/main/java/org/glom/web/server/Utils.java | 54 ++++++++++++++ .../java/org/glom/web/server/libglom/Document.java | 70 ++++++++++++++++++ .../java/org/glom/web/server/LayoutPathTest.java | 45 +++++++++++ .../web/server/libglom/DocumentLayoutPathTest.java | 86 ++++++++++++++++++++++ 5 files changed, 324 insertions(+) create mode 100644 src/test/java/org/glom/web/server/LayoutPathTest.java create mode 100644 src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java diff --git a/ChangeLog b/ChangeLog index 6e3d3fb..8956403 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,74 @@ 2012-11-15 Murray Cumming + Document: Add getLayoutItemByPath(). + + * src/main/java/org/glom/web/server/Utils.java: + Add buildLayoutPath() and parseLayoutPath(). + * src/test/java/org/glom/web/server/LayoutPathTest.java: Test that these + utility methods work. + * src/main/java/org/glom/web/server/libglom/Document.java: + Add Add getLayoutItemByPath(). + * src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java: Test + that it works. + + This will be useful in subsequent commits. + +2012-11-15 Murray Cumming + + Need a short description (OOPS!). + Need the bug URL (OOPS!). + + Reviewed by NOBODY (OOPS!). + + Additional information of the change such as approach, rationale. Please add per-function descriptions below (OOPS!). + + * .classpath: + * .settings/com.google.gdt.eclipse.core.prefs: + * .settings/com.google.gwt.eclipse.core.prefs: + * src/main/java/org/glom/web/client/activity/DetailsActivity.java: + (DetailsActivity.createLayout): + (DetailsActivity.setData): + * src/main/java/org/glom/web/client/ui/details/DetailsCell.java: + (DetailsCell): + (DetailsCell.DetailsCell): + (DetailsCell.setupWidgets): + (DetailsCell.setData): + (DetailsCell.getLayoutItem): + * src/main/java/org/glom/web/client/ui/details/Group.java: + (Group.createChildWidget): + * src/main/java/org/glom/web/server/ConfiguredDocument.java: + (ConfiguredDocument.getValidListViewLayoutGroup): + (ConfiguredDocument.getDetailsLayoutGroup): + (ConfiguredDocument): + (ConfiguredDocument.updateLayoutItemImages): + * src/main/java/org/glom/web/server/ConfiguredDocumentSet.java: + (ConfiguredDocumentSet.getDocumentIdForFilename): + (ConfiguredDocumentSet): + (ConfiguredDocumentSet.addDocument): + (ConfiguredDocumentSet.readConfiguration): + * src/main/java/org/glom/web/server/OnlineGlomImages.java: + (OnlineGlomImages.OnlineGlomImages): + (OnlineGlomImages.doGet): + (OnlineGlomImages): + (OnlineGlomImages.getImageFromDocument): + (OnlineGlomImages.getImageFromDatabase): + * src/main/java/org/glom/web/server/Utils.java: + (Utils): + (Utils.buildImageDataUrl): + (Utils.buildImageDataUrlStart): + (Utils.buildLayoutPath): + (Utils.parseLayoutPath): + * src/main/java/org/glom/web/server/database/DBAccess.java: + (DBAccess.convertResultSetToDTO): + * src/main/java/org/glom/web/server/libglom/Document.java: + (Document): + (Document.Path): + (Document.Document): + * src/test/java/org/glom/web/server/libglom/DocumentTest.java: + (DocumentTest.TestThread.run): + +2012-11-15 Murray Cumming + Document: Use a better base64 decoder to read static image data. * src/test/java/org/glom/web/server/libglom/DocumentTest.java: diff --git a/src/main/java/org/glom/web/server/Utils.java b/src/main/java/org/glom/web/server/Utils.java index a483264..3a38989 100644 --- a/src/main/java/org/glom/web/server/Utils.java +++ b/src/main/java/org/glom/web/server/Utils.java @@ -26,6 +26,8 @@ import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import org.apache.commons.lang3.StringUtils; + /** * */ @@ -76,5 +78,57 @@ public class Utils { } } } + + /** Build a :-separated string to represent the path as a string. + * @param path + * @return + */ + public static String buildLayoutPath(int[] path) { + if((path == null) || (path.length == 0)) { + return null; + } + + String result = new String(); + for(int i:path) { + if(!result.isEmpty()) { + result += ":"; + } + + final String strIndex = Integer.toString(i); + result += strIndex; + } + + return result; + } + + /** Get an array of int indices from the :-separated string. + * See buildLayoutPath(). + * + * @param attrLayoutPath + * @return The array of indices of the layout items. + */ + public static int[] parseLayoutPath(final String attrLayoutPath) { + if(StringUtils.isEmpty(attrLayoutPath)) { + return null; + } + + final String[] strIndices = attrLayoutPath.split(":"); + final int[] indices = new int[strIndices.length]; + for (int i = 0; i < strIndices.length; ++i) { + final String str = strIndices[i]; + + try + { + indices[i] = Integer.parseInt(str); + } + catch (final NumberFormatException nfe) + { + //TODO: Log the error. + return null; + } + } + + return indices; + } } diff --git a/src/main/java/org/glom/web/server/libglom/Document.java b/src/main/java/org/glom/web/server/libglom/Document.java index b3fbba2..08442a3 100644 --- a/src/main/java/org/glom/web/server/libglom/Document.java +++ b/src/main/java/org/glom/web/server/libglom/Document.java @@ -1911,4 +1911,74 @@ public class Document { } return primaryKey; } + + /** + * @param resp + * @param attrDocumentID + * @param tableName + * @param layoutName + * @param layoutPath + * @return + * @throws IOException + */ + public LayoutItem getLayoutItemByPath( + final String tableName, final String layoutName, final String layoutPath) throws IOException { + final List listLayoutGroups = getDataLayoutGroups(layoutName, tableName); + if(listLayoutGroups == null) { + Log.error("The layout with the specified name was not found. tableName=" + tableName + ", layoutName=" + layoutName); + return null; + } + + if(listLayoutGroups.isEmpty()) { + Log.error("The layout was empty. attrTableName=" + tableName + ", layoutName=" + layoutName); + return null; + } + + final int[] indices = Utils.parseLayoutPath(layoutPath); + if((indices == null) || (indices.length == 0)) { + Log.error("The layout path was empty or could not be parsed. layoutPath=" + layoutPath); + return null; + } + + LayoutItem item = null; + int depth = 0; + for(int index:indices) { + if(index < 0) { + Log.error("An index in the layout path was negative, at depth=" + depth + ", layoutPath=" + layoutPath); + return null; + } + + //Get the nth item of either the top-level list or the current item: + if(depth == 0) { + if(index < listLayoutGroups.size()) { + item = listLayoutGroups.get(index); + } else { + Log.error("An index in the layout path is larger than the number of child items, at depth=" + depth + ", layoutPath=" + layoutPath); + return null; + } + } else { + if(item instanceof LayoutGroup) { + final LayoutGroup group = (LayoutGroup)item; + final List items = group.getItems(); + if(index < items.size()) { + item = items.get(index); + } else { + Log.error("An index in the layout path is larger than the number of child items, at depth=" + depth + ", layoutPath=" + layoutPath); + return null; + } + } else { + Log.error("An intermediate item in the layout path is not a layout group, at depth=" + depth + ", layoutPath=" + layoutPath); + return null; + } + } + + depth++; + } + + if(item == null) { + Log.error("The item specifed by the layout path could not be found. layoutPath=" + layoutPath); + return null; + } + return item; + } } diff --git a/src/test/java/org/glom/web/server/LayoutPathTest.java b/src/test/java/org/glom/web/server/LayoutPathTest.java new file mode 100644 index 0000000..45d9be9 --- /dev/null +++ b/src/test/java/org/glom/web/server/LayoutPathTest.java @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2012 Openismus GmbH + * + * This file is part of GWT-Glom. + * + * GWT-Glom is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with GWT-Glom. If not, see . + */ + +package org.glom.web.server; + +import static org.junit.Assert.*; + +import org.junit.Test; + +/** + * @author Murray Cumming + * + */ +public class LayoutPathTest { + + @Test + public void test() { + final String layoutPathExpected = "1:2:3:4"; + final int[] indicesExpected = {1, 2, 3, 4}; + + final int[] indices = Utils.parseLayoutPath(layoutPathExpected); + assertArrayEquals(indicesExpected, indices); + + final String layoutPath = Utils.buildLayoutPath(indicesExpected); + assertEquals(layoutPathExpected, layoutPath); + } + + +} diff --git a/src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java b/src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java new file mode 100644 index 0000000..ce1eabf --- /dev/null +++ b/src/test/java/org/glom/web/server/libglom/DocumentLayoutPathTest.java @@ -0,0 +1,86 @@ +/* + * Copyright (C) 2009, 2010, 2011 Openismus GmbH + * + * This file is part of gwt-glom + * + * gwt-glom is free software: you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as published by the + * Free Software Foundation, either version 3 of the License, or (at your + * option) any later version. + * + * gwt-glom is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with gwt-glom. If not, see . + */ + +package org.glom.web.server.libglom; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import java.io.IOException; +import java.net.URL; + +import org.apache.commons.lang3.StringUtils; +import org.glom.web.shared.libglom.layout.LayoutItem; +import org.glom.web.shared.libglom.layout.LayoutItemField; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; + +/** + * Simple test to ensure that the generated bindings are working. + */ +public class DocumentLayoutPathTest { + + private static Document document; + + @BeforeClass + static public void setUp() { + final URL url = DocumentLayoutPathTest.class.getResource("example_music_collection.glom"); + assertTrue(url != null); + final String documentUri= url.toString(); + assertTrue(!StringUtils.isEmpty(documentUri)); + + document = new Document(); + document.setFileURI(documentUri); + final boolean retval = document.load(); + assertTrue(retval); + } + + @AfterClass + static public void tearDown() { + } + + @Test + public void testNormal() throws IOException { + // Just an initial sanity check: + assertThat(document.getDatabaseTitleOriginal(), is("Music Collection")); + + final String layoutPath = "1:2"; + final LayoutItem item = document.getLayoutItemByPath("artists", Document.LAYOUT_NAME_DETAILS, layoutPath); + assertNotNull(item); + assertTrue(item instanceof LayoutItemField); + + assertEquals(item.getName(), "comments"); + } + + @Test + public void testOutOfBounds() throws IOException { + final String layoutPath = "1:200"; //Check that it does not crash. + final LayoutItem item = document.getLayoutItemByPath("artists", Document.LAYOUT_NAME_DETAILS, layoutPath); + assertNull(item); + } + + @Test + public void testOutOfBoundsNegative() throws IOException { + final String layoutPath = "-1:-50"; //Check that it does not crash. + final LayoutItem item = document.getLayoutItemByPath("artists", Document.LAYOUT_NAME_DETAILS, layoutPath); + assertNull(item); + } + +} -- 2.1.4