From d778a980d57acb673e17d2c218d1f835410a6eca Mon Sep 17 00:00:00 2001 From: Murray Cumming Date: Fri, 30 Nov 2012 21:41:45 +0100 Subject: [PATCH] Allow primary keys to be any type, and test it. * src/main/java/org/glom/web/shared/TypedDataItem.java: Add overrides for the other set*() methods, to set the type. Added getValue() which returns a generic Object. * src/main/java/org/glom/web/server/SqlUtils.java: buildSimpleWhereExpression(): Use TypedDataItem.getValue() instead of getNumber(). * src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java: Added this test, which is much like SelfHostExampleTest, but which uses this test file, take from Glom: * src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom: which has all the primary keys changes to text instead of numbers. However, parts of the UI code still assume that primary keys are numbers. --- ChangeLog | 18 + .../org/glom/web/client/place/DetailsPlace.java | 2 +- src/main/java/org/glom/web/server/SqlUtils.java | 3 +- .../java/org/glom/web/shared/TypedDataItem.java | 54 + .../glom/web/client/place/DetailsPlaceTest.java | 2 +- .../SelfHostExampleNonNumericPrimaryKeysTest.java | 63 + ...st_example_music_collection_text_pk_fields.glom | 1298 ++++++++++++++++++++ 7 files changed, 1437 insertions(+), 3 deletions(-) create mode 100644 src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java create mode 100644 src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom diff --git a/ChangeLog b/ChangeLog index 0c5a89f..c5de0c0 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,21 @@ +2012-11-30 Murray Cumming + + Allow primary keys to be any type, and test it. + + * src/main/java/org/glom/web/shared/TypedDataItem.java: + Add overrides for the other set*() methods, to set the type. + Added getValue() which returns a generic Object. + * src/main/java/org/glom/web/server/SqlUtils.java: + buildSimpleWhereExpression(): Use TypedDataItem.getValue() instead of + getNumber(). + * src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java: + Added this test, which is much like SelfHostExampleTest, but which uses this + test file, take from Glom: + * src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom: + which has all the primary keys changes to text instead of numbers. + + However, parts of the UI code still assume that primary keys are numbers. + 2012-11-28 Murray Cumming SelfHostExampleTest: Move some checks into a utility function. diff --git a/src/main/java/org/glom/web/client/place/DetailsPlace.java b/src/main/java/org/glom/web/client/place/DetailsPlace.java index 1c2258d..fea8ddc 100644 --- a/src/main/java/org/glom/web/client/place/DetailsPlace.java +++ b/src/main/java/org/glom/web/client/place/DetailsPlace.java @@ -63,7 +63,7 @@ public class DetailsPlace extends HasTablePlace { case TYPE_NUMERIC: // non-locale specific number-to-string conversion: // http://docs.oracle.com/javase/6/docs/api/java/lang/Double.html#toString%28double%29 - primaryKeyValueString = Double.toString(primaryKeyValue.getNumber()); + primaryKeyValueString = Double.toString(primaryKeyValue.getNumber()); //TODO: Handle other types. // Remove the trailing point and zero on integers. This just makes URL string look nicer. if (primaryKeyValueString.endsWith(".0")) { primaryKeyValueString = primaryKeyValueString.substring(0, primaryKeyValueString.length() - 2); diff --git a/src/main/java/org/glom/web/server/SqlUtils.java b/src/main/java/org/glom/web/server/SqlUtils.java index 59a0f05..8ca1669 100644 --- a/src/main/java/org/glom/web/server/SqlUtils.java +++ b/src/main/java/org/glom/web/server/SqlUtils.java @@ -85,7 +85,7 @@ public class SqlUtils { } final org.jooq.Field field = createField(tableName, fieldName); - result = field.equal(primaryKeyValue.getNumber()); // TODO: Handle other types too. + result = field.equal(primaryKeyValue.getValue()); return result; } @@ -418,6 +418,7 @@ public class SqlUtils { */ public static void fillDataItemFromResultSet(final DataItem dataItem, final LayoutItemField field, final int rsIndex, final ResultSet rs, final String documentID, final String tableName, final TypedDataItem primaryKeyValue) throws SQLException { + switch (field.getGlomType()) { case TYPE_TEXT: final String text = rs.getString(rsIndex); diff --git a/src/main/java/org/glom/web/shared/TypedDataItem.java b/src/main/java/org/glom/web/shared/TypedDataItem.java index da1eeaf..ebecc39 100644 --- a/src/main/java/org/glom/web/shared/TypedDataItem.java +++ b/src/main/java/org/glom/web/shared/TypedDataItem.java @@ -19,6 +19,8 @@ package org.glom.web.shared; +import java.util.Date; + import org.glom.web.shared.libglom.Field.GlomFieldType; /** @@ -37,6 +39,33 @@ public class TypedDataItem extends DataItem { return empty; } + /** Get the value. + * + * This is a generic alternative to getNumber(), getText(), etc. + * @return + */ + public Object getValue() { + switch(type) { + case TYPE_NUMERIC: + return getNumber(); + case TYPE_TEXT: + return getText(); + case TYPE_DATE: + return getDate(); + //TODO: case TYPE_TIME: + // return getTime(); + case TYPE_BOOLEAN: + return getBoolean(); + case TYPE_IMAGE: + return getImageData(); + case TYPE_INVALID: + return "value-with-invalid-type"; + default: + return "value-with-unknown-type"; + } + } + + //TODO: Why is this override necessary? /* * (non-Javadoc) * @@ -72,6 +101,31 @@ public class TypedDataItem extends DataItem { this.type = GlomFieldType.TYPE_TEXT; super.setText(text); } + + /* + * (non-Javadoc) + * + * @see org.glom.web.shared.DataItem#setNumber(double) + */ + @Override + public void setDate(final Date date) { + this.empty = false; + this.type = GlomFieldType.TYPE_DATE; + super.setDate(date); + } + + public void setImageData(final byte[] imageData) { + this.empty = false; + this.type = GlomFieldType.TYPE_IMAGE; + super.setImageData(imageData); + } + + public void setImageDataUrl(final String image) { + this.empty = false; + this.type = GlomFieldType.TYPE_IMAGE; + super.setImageDataUrl(image); + } + /* * (non-Javadoc) diff --git a/src/test/java/org/glom/web/client/place/DetailsPlaceTest.java b/src/test/java/org/glom/web/client/place/DetailsPlaceTest.java index 621b1f5..e9447fc 100644 --- a/src/test/java/org/glom/web/client/place/DetailsPlaceTest.java +++ b/src/test/java/org/glom/web/client/place/DetailsPlaceTest.java @@ -62,7 +62,7 @@ public class DetailsPlaceTest { assertNotNull(place.getPrimaryKeyValue()); assertTrue(place.getPrimaryKeyValue().isEmpty()); assertEquals(null, place.getPrimaryKeyValue().getUnknown()); - assertEquals(0.0, place.getPrimaryKeyValue().getNumber(), 0.0); + assertEquals(0.0, place.getPrimaryKeyValue().getNumber(), 0.0); //TODO: Handle other types. assertEquals(null, place.getPrimaryKeyValue().getText()); } diff --git a/src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java b/src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java new file mode 100644 index 0000000..3e5d95d --- /dev/null +++ b/src/test/java/org/glom/web/server/SelfHostExampleNonNumericPrimaryKeysTest.java @@ -0,0 +1,63 @@ +/* + * 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 java.net.URL; +import java.sql.SQLException; + +import org.glom.web.server.libglom.Document; +import org.glom.web.server.libglom.Document.HostingMode; +import org.junit.AfterClass; +import org.junit.Test; + +/** + * @author Murray Cumming + * + */ +public class SelfHostExampleNonNumericPrimaryKeysTest { + + private static SelfHoster selfHoster = null; + + @Test + public void test() throws SQLException { + final URL url = SelfHostExampleNonNumericPrimaryKeysTest.class.getResource("test_example_music_collection_text_pk_fields.glom"); + assertTrue(url != null); + final String strUri = url.toString(); + + final Document document = new Document(); + document.setFileURI(strUri); + assertTrue(document.load()); + + selfHoster = new SelfHoster(document); + final boolean hosted = selfHoster.createAndSelfHostFromExample(HostingMode.HOSTING_MODE_POSTGRES_SELF); + assertTrue(hosted); + + SelfHostTestUtils.testExampleMusiccollectionData(selfHoster, document); + } + + @AfterClass + public static void tearDown() { + if (selfHoster != null) { + selfHoster.cleanup(); + } + } +} diff --git a/src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom b/src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom new file mode 100644 index 0000000..0fdf8f6 --- /dev/null +++ b/src/test/resources/org/glom/web/server/test_example_music_collection_text_pk_fields.glom @@ -0,0 +1,1298 @@ + + + + + + + + + + + + + + + + + + + borntorun + + Born To Run + bruce + 0.000000 + 1975 + + + trueblue + + True Blue + mad + warnerbros + 1987 + + + thewildtheinnocent + + The Wild, the Innocent, & the E-Street Shuffle + bruce + 0.000000 + 1973 + + + signothetimes + + Sign 'O' The Times + prince + 1.000000 + 1987 + + + superfly + + Superfly + curtis + 2.000000 + 1972 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + bruce + + + Bruce Springsteen + + + mad + + + Madonna + + + prince + + + Prince + + + curtis + + + Curtis Mayfield + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + sony + + Sony + + + warnerbros + + Warner Bros + + + rhino + + Rhino + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + 4thofjuly + + borntorun + 4th of July, Asbury Park (Sandy) + + + backstreets + + borntorun + Backstreets + + + ballad + + borntorun + Ballad of Dorothy Parker + + + borntorun + + borntorun + Born To Run + + + estreetshuffle + + borntorun + E Street Shuffle + + + kittysback + + thewildtheinnocent + Kitty's Back + + + hotthing + + signothetimes + Hot Thing + + + foreverinmylife + + signothetimes + Forever In My Life + + + housequake + + signothetimes + Housequake + + + icouldnever + + signothetimes + I Could Never Take The Place Of Your Man + + + ifiwasyourgirlfriend + + signothetimes + If I Was Your Girlfriend + + + incident + + signothetimes + Incident on 57th Street + + + it + + signothetimes + It + + + jungleland + + thewildtheinnocent + Jungleland + + + meeting + + thewildtheinnocent + Meeting Across The River + + + newyork + + thewildtheinnocent + New York City Serenade + + + night + + thewildtheinnocent + Night + + + playinthtesunshine + + signothetimes + Play In The Sunshine + + + rosalita + + thewildtheinnocent + Rosalita (Come out Tonight) + + + shestheone + + thewildtheinnocent + She's The One + + + signothetimes + + signothetimes + Sign 'O' The Times + + + slowlove + + signothetimes + Slow Love + + + starfishand + + signothetimes + Starfish and Coffee + + + strangerelationship + + signothetimes + Strange Relationship + + + tenthavenue + + borntorun + Tenth Avenue Freeze Out + + + thunderroad + + borntorun + Thunder Road + + + ugotthelook + + signothetimes + U Got The Look + + + wildbilly + + thewildtheinnocent + Wild Billy's Circus Story + + + junkiechase + + thewildtheinnocent + Junkie Chase (Instrumental) + + + laisla + + trueblue + La Isla Bonita + + + littlechildrunningwild + + trueblue + Little Child Runnin' Wild + + + livetotell + + trueblue + Live To Tell + + + lovemakes + + thewildtheinnocent + Love Makes The World Go Round + + + nothingonme + + superfly + No Thing On Me (Cocaine Song) + + + openyourheart + + trueblue + Open Your Heart + + + papadontpreach + + trueblue + Papa Don't Preach + + + pusherman + + superfly + Pusherman + + + superfly + + superfly + Superfly + + + thecross + + signothetimes + The Cross + + + think + + signothetimes + Think (Instrumental) + + + trueblue + + trueblue + True Blue + + + wherestheparty + + trueblue + Where's The Party + + + whiteheart + + trueblue + White Heart + + + adore + + borntorun + Adore + + + eddieyoushouldknowbetter + + borntorun + Eddie You Should Know Better + + + freddiesdead + + signothetimes + Freddie's Dead + + + givemeyourlove + + signothetimes + Give Me Your Love (Love Song) + + + itsgonna + + signothetimes + It's Gonna Be A Beautiful Night + + + jimmyjimmy + + signothetimes + Jimmy Jimmy + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + +
-- 2.1.4