1

I have a grails app using persistence annotated POJOs for domain model. Grails generates controllers and views from them as expected, but one class is a puzzle for me.

I need to represent a collection of strings ( at the moment an ArrayList of strings ) in that is grails-view 'friendly' and will render as a drop-down.

The data in ArrayList is 'fairly' constant so I thought enum could be used for it, but I'm just not sure.

The class in question:

/**
 *  available categories:
 *      Airplane
 *      Rotorcraft
 *      Glider
 *      Lighter than air
 *      Powered lift
 *      Powered parachute
 *      Weight-shift-control
 */
@Entity
public class AircraftCategory {

    public AircraftCategory(){

    this.aircraftCategories.add("Airplane");
    this.aircraftCategories.add("Rotorcraft");
    this.aircraftCategories.add("Glider");
    this.aircraftCategories.add("Lighter Than Air");
    this.aircraftCategories.add("Powered Lift");
    this.aircraftCategories.add("Powered Parachute");
    this.aircraftCategories.add("Weight Shift Control");   
    }

    long id;
    private long version;   
    private ArrayList <String> aircraftCategories = new ArrayList<String>();

    public ArrayList <String> getAircraftCategories() {
        return aircraftCategories;
    }

    public void setAircraftCategories(ArrayList <String> aircraftCategories) {
        this.aircraftCategories = aircraftCategories;

    }

    @Id
    @GeneratedValue
    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }    

    public long getVersion() {
        return version;
    }

    public void setVersion(long version) {
        this.version = version;
    } 
}
vector
  • 7,334
  • 8
  • 52
  • 80

1 Answers1

1

The easiest thing to do is to push this into the DB as a "proper" domain object. It's a bit silly to have an object that is essentially just a name field, but it will render as you want it to in the scaffolded views (assuming you have the association to your actual object).

The other advantage is that you have an easy extension point in your application later in case you need to add more data to AircraftCategory, like abbreviation for example.

For another possible solution, this question is very similar.

Community
  • 1
  • 1
cdeszaq
  • 30,869
  • 25
  • 117
  • 173
  • ... good points indeed and I'm considering both, but the challenge remains ;-) – vector Jul 09 '12 at 17:56
  • I would lobby heavily for making the `AircraftCategory` an actual domain class, since it doesn't hard-code in the categories, but makes them data instead. You can Bootstrap the categories in if you need to. – cdeszaq Jul 09 '12 at 18:00
  • ... yeah, my sentiments exactly. – vector Jul 09 '12 at 18:29