Extended Enumerations

From: Michael Gentry (blacknex..mail.com)
Date: Mon Feb 25 2008 - 09:43:14 EST

  • Next message: Kevin Menard: "RE: Extended Enumerations"

    I did some work on extended enumerations (where the DB value can be
    defined) this weekend, but wanted to do more testing and get a little
    feedback before checking it in. I apologize in advance for the length
    of the e-mail, but hopefully it won't be too bad.

    First off, there is a new interface to implement in your Enum to flag
    it as a Cayenne extended enumeration. I initially had
    IntegerEnumeration and StringEnumeration, but then boiled it down to
    one. I'll leave it that way unless there are compelling reasons not
    to.

    public interface ExtendedEnumeration
    {
        // Return the value to be stored in the database for this enumeration. In
        // actuallity, this should be an Integer or a String.
        public Object databaseValue();
    }

    Here are examples of the enumerations I used for testing:

    // String example
    public enum State implements ExtendedEnumeration
    {
      ALABAMA("AL"), ALASKA("AK"), ARIZONA("AZ"), MARYLAND("MD"), VIRGINIA("VA");

      private String value;

      private State(String value)
      {
        this.value = value;
      }

      public String databaseValue()
      {
        return value;
      }
    }

    // Integer example
    public enum Color implements ExtendedEnumeration
    {
      RED(3), GREEN(6), BLUE(9);

      private Integer value;

      private Color(Integer value)
      {
        this.value = value;
      }

      public Integer databaseValue()
      {
        return value;
      }
    }

    // Integer example where stored value is different than the value you
    want to use at runtime
    public enum InterestTerm implements ExtendedEnumeration
    {
      YEARLY(1, 1), QUARTERLY(2, 4), MONTHLY(3, 12);

      private Integer dbValue;
      private int value;

      private InterestTerm(Integer dbValue, int value)
      {
        this.dbValue = dbValue;
        this.value = value;
      }

      public Integer databaseValue()
      {
        return dbValue;
      }

      public int value()
      {
        return value;
      }
    }

    My sample code using it:

        user = dataContext.newObject(User.class);
        user.setFavoriteColor(Color.RED);
        user.setInterestTerm(InterestTerm.MONTHLY);
        user.setLocation(State.ALABAMA);
        user.setName("Joe Alabama");

        user = dataContext.newObject(User.class);
        user.setFavoriteColor(Color.GREEN);
        user.setInterestTerm(InterestTerm.QUARTERLY);
        user.setLocation(State.ARIZONA);
        user.setName("Jose Arizona");

        user = dataContext.newObject(User.class);
        user.setFavoriteColor(Color.BLUE);
        user.setInterestTerm(InterestTerm.YEARLY);
        user.setLocation(State.VIRGINIA);
        user.setName("Josephine Virginia");

    There is currently no Cayenne Modeler support -- you just type the
    full class name for your enum for the ObjEntity column type. I'm not
    sure if it is required or desired to add support, either. If we do, I
    think we should add a value() method to the interface. This could be
    useful for localization of string values. For example, you have a
    pulldown list of values that is static with specified values in the
    DB, but want to present them to the user in their specific languages.
    Of course, if the developer needs to do more magic in the value()
    method than what CM would generate, then you get into a maintenance
    headache with the code generation.

    Feedback appreciated.

    Thanks!

    /dev/mrg



    This archive was generated by hypermail 2.0.0 : Mon Feb 25 2008 - 09:43:56 EST