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;
}
}
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.