views:

275

answers:

2

I have an entity with Integer attributes that looks like this in proto code:

class MyEntity:
    String name

    @Choices({1, "BSD", 2, "Apache", 3, "GPL"}
    Integer frequency

    @ChoicesSegment({1000, 2000, 3000}, {"BSD", "Apache", "GPL"})
    Integer type

    String getFrequency()
           return getChoice("frequency", frequency)
    String getType()
           return getChoice("type", type)

maybe this solution is more feasible:

class MyEntity:
    String name

    final static private Something? frequencyChoices = {1000, 2000, 3000}, {"BSD", "Apache", "GPL"}
    Integer frequency

    final static private String[] typeChoices = new String[] {"BSD", "Apache", "GPL"}
    Integer type

    @Choices(MyEntity.frequencyChoices)
    String getFrequency()
           return frequency)

    @IntervalChoices(MyEntity.typeChoices)
    String getType()
           return type

get* accessors return strings according to this table.

value(type) HumanReadableString(type)
  1             BSD
  2             Apache
  3             GPL

min frequency         max frequency    HumanReadableString(frequency)
    0                     1000                rare
    1000                  2000              frequent
    2001                  3000                sexy

It should be possible to get all possible values that an attribute can take, example:

getChoices(MyEntity, "type") returns ("rare", "frequent", "sexy")

It should be possible to get the bound value from the string:

getValue(MyEntity, "frequency", "sexy") returns (2000,3000)

edit: purpose of all of this This methods should simplify the generation of forms and requests (of course this should not be view implementation bound)

edit: added how I would like to tell Java that some attributes are spécial so that it can generate get* accessors acordingly.

edit: added how to submit in the code the choices

edit: the only thing that I store in the db is integers, when I want to print them they should be converted somehow to their human readable string.

edit: FLOSS ? a brave new world.

thx for all your input I will digg all suggestions to find the best one, I still think that django/python ecosystem is far superior to java's one. Get Inspired.

A: 

You can have additional info in enums:

public enum License { 

    GPL("GPL"),

    APACHE("Apache License");

    public License(String displayName) {
        this.displayName=displayName;
    }

    String displayName;

 } 

Additional functions as required, but have a close look which functions are already provided by the Enum classes.

Daniel
A: 

You can do it without any hassle (but note, that the value in the DB will be the ordinal() value of the enums. So:

public enum License { GPL, APACHE, BSD }

FrequencyChoices could go into an @ElementCollection.

If you need human readable values, you may want to convert your Enum to an ordinary class, and persist it as a separate table, so you can add new licenses more easily to the list...

@Entity
public class License {
    @Id long id;
    String name;
}
pihentagy