Enum Internationalised
tony.herstell1 May 26, 2007 6:27 AMThis is probably obvious to all of you, but taken me days to get this working (and still not sure how!).
Inside an Entity I have an internationalised enum (as I am hoping to persist a value of its kind with the Entity):
@SuppressWarnings("serial")
@Entity // Defines this as an Entity that is a class mapped toDate the Datastore.
@Name("booking") // Name used within SEAM for an instance of this class.
public class Booking implements Serializable {
public enum OrganisationKind {
PRIVATE_BOOKING ("booking_private"),
PRIVATE_BOOKING_OPEN ("booking_private_open"),
NON_PROFIT_ORG ("booking_non_profit_org"),
COMMERCIAL ("booking_commercial_org"),
OPEN_EVENT ("booking_open_event"); // Only booked by Selwyn Equestrian Staff
private final String inlLabel;
OrganisationKind(String inlLabel) {
this.inlLabel = inlLabel;
}
public String getInlLabel() { return inlLabel; }
}
...
private OrganisationKind organisationKind;
...
I have a controller that requires this enum to be displayed as selectOneRadio... these routines handle the selection:
public Map getOrganisationKindList(); public Booking.OrganisationKind getSelectedOrganisationKind(); public void setSelectedOrganisationKind(Booking.OrganisationKind orgKind); not use I need this one as the Change fires ok public void selectedOrganisationKindChanged(ValueChangeEvent event);
The Impl:
...
/**
* The currently selected Booking Type
*/
private Booking.OrganisationKind selectedOrganisationKind;
...
public Map getOrganisationKindList() {
Map<String,Booking.OrganisationKind> choices = new HashMap<String,Booking.OrganisationKind>();
for (Booking.OrganisationKind type : Booking.OrganisationKind.values()) {
choices.put(messages.get(type.getInlLabel()), type);
}
return choices;
}
public Booking.OrganisationKind getSelectedOrganisationKind() {
return selectedOrganisationKind;
}
public void setSelectedOrganisationKind(Booking.OrganisationKind orgKind) {
this.selectedOrganisationKind = orgKind;
}
public void selectedOrganisationKindChanged(ValueChangeEvent event) {
if (event.getNewValue() != null) {
Booking.OrganisationKind theOrganisationKind = Booking.OrganisationKind.valueOf(event.getNewValue().toString());
selectedOrganisationKind = theOrganisationKind;
} else {
log.error("null new event passed in. Error!");
}
}
The screen shows all this using:
<ice:panelGrid columns="1" styleClass="center">
<ice:selectOneRadio
value="#{availabilityController.selectedOrganisationKind}"
valueChangeListener="#{availabilityController.selectedOrganisationKindChanged}"
partialSubmit="true"
layout="pageDirection"
converter="#{enumTypeConverter}">
<f:selectItems value="#{availabilityController.organisationKindList}"/>
</ice:selectOneRadio>
</ice:panelGrid>
Now... I have to provide a Converter (no idea if this is right.. and the screen seems to find this without outjecting it to the view at all!!):
@Intercept(InterceptionType.NEVER)
@SuppressWarnings("serial")
@Stateless // A new component is created or re-initialised and re-used each time it is called.
@Name("enumTypeConverter") // Name used within SEAM for an instance of this class.
// All stateless session beans belong to the Stateless Context Scope
public class EnumTypeConverterImpl implements EnumTypeConverter {
@SuppressWarnings("unchecked")
public Object getAsObject(FacesContext context, UIComponent comp, String value) throws ConverterException {
Class enumType = comp.getValueBinding("value").getType(context);
return Enum.valueOf(enumType, value);
}
public String getAsString(FacesContext context, UIComponent component, Object object) throws ConverterException {
if (object == null) {
return null;
}
Enum type = (Enum) object;
return type.toString();
}
}
Anyhow, it may help some poor sap in the future!
FINALLY GOT TO USE INTERNATIONALISED ENUMS!!!
From the screen:
O open booking (Selwyn Equestrian Centre staff only) O commercial booking (e.g. NZ Elite Horse Auctions Ltd.) O private booking but open to anyone (cost is per horse - max of 4 places) O non profit booking (Dressage Canterbury etc.) O private booking (cost is for Arena) <<---Selected Result (on screen and in controller) = PRIVATE_BOOKING
:)