Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
Clear All
new posts

    Java enum support

    If I have a java enum defined as a property of a Java Object, what are the best practices to work with this within a datasource.

    The get is easy, I can just define a property within the datasource and then define a valueXPath as:

    Code:
    valueXPath="myEnumPropertyName/ordinal"
    Assuming that I have a getOrdinal method.

    and then for the displayField

    Code:
    valueXPath="myEnumPropertyName/description"
    How does the setter work however on the add/update. If I have the edit type as a select box with the valueField as the ordinal and a display field as the description? The DSRequest.getDataSource.setProperties() method is going to attempt to call myEnumPropertyName.setOrdinal(value), but that's not going to work.

    So my question is, what's the best practice in Smart Client to use Java enum types with datasources?

    #2
    We've recently introduced full JS <--> Java support for Java enums - it will be present in the next 7.0 release, whether that's 7.0 final or an interim beta. The new enum support allows you to treat the enum in Javascript in one of three ways:

    1 As a string, with the same value as the constant, so Planet.MERCURY serializes into JS as the string "MERCURY", and can be provided back to the Java side as "MERCURY" or "mercury" for correct assignment to the enum.

    2 As an integer, mapped to/from the enum's ordinal value.

    3 As a Javascript object, derived by treating the enum somewhat as if it were a bean. So, any Javabean-style properties your enum has (such as the ordinal you describe, although obviously that won't be required going forward) will be serialized out to JS, along with the constant and ordinal values themselves. So, in your example, you would end up with a JS object like this:

    Code:
    {
        _constant: "MERCURY",
        _ordinal: 0,
        ordinal: 0,
        someOtherProperty: "SomeOtherValue"
    }
    Coming back to the Java side, we would expect to be passed an object containing a "_constant" or "_ordinal" property (evaluated in that order if you pass both).

    All of this is configured with flags on the DataSource - you can also override the "_constant" and "_ordinal" field names to something else if you prefer. Finally, whatever the config, we try to do the right thing with inconsistent data, if we can - so even if you configured a DataSource for "bean"-type translations, we would assign a simple string if that's what was passed and it matched one of the enum's constant values.
    Last edited by Isomorphic; 20 Nov 2008, 13:14.

    Comment


      #3
      Just a further note - with any version of SmartClient that doesn't have this support and where you want Java beans auto-populated from DSRequest data, the best approach is to provide a parallel getter/setter of type String that converts to Enum.

      The follow code is how you would go from a String to an Enum:

      Code:
      enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }
      
      Suit suit = Enum.valueOf(Suit.class, "CLUBS");

      Comment

      Working...
      X