Binding a Map to a DropDownChoice in Wicket App
February 5, 2010
One day i had to change a page (WebPage) in a way that it would allow me to persist a simple attribute of an entity. First i tried to use an Enum, however i’m using JPA persistence with Hibernate and was a little bit concerned about how Hibernate would deal with this Enum in searches or some other more advanced stuff. So i decided to create a simple Integer attribute and set a constant on it.
This is a sample of code using a Map to exhibit a list of options (DropDownChoice) in a Wicket application to persist a constant (Integer) in an entity.
Code of ControlSerum entity where the attribute status will be persisted:
@Entity
public class ControlSerum {
private Integer status;
public final static int STATUS_PREP = 0;
public final static int STATUS_ACTIVE = 1;
public final static int STATUS_INACTIVE = 2;
private final static Map<Integer, String> statusMap = new HashMap<Integer, String>();
static{
statusMap.put(STATUS_PREP, "Em preparo");
statusMap.put(STATUS_ACTIVE, "Ativo");
statusMap.put(STATUS_INACTIVE, "Inativo");
}
@Column(nullable=false)
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
@Transient
public static Map<Integer, String> getStatusMap(){
return new HashMap<Integer, String>(statusMap);
}
}
At ControlSerumEditPage page class, i implemented a IChoiceRenderer to renderize correctly the options of select tag using the Map key as the value of option tag and the Map value as the name for exibition of option tag.
ControlSerumEditPage class constructor snippet where a renderizer for DropDownChoice is instantiated and added to Form:
IChoiceRenderer choicesRenderer = new IChoiceRenderer() {
@Override
public String getDisplayValue(Integer object) {
return ControlSerum.getStatusMap().get(object);
}
@Override
public String getIdValue(Integer object, int index) {
return object.toString();
}
};
IModel optionsModel = new Model(){
@Override
public Serializable getObject() {
return new ArrayList(ControlSerum.getStatusMap().keySet());
}
};
IModel statusModel = new PropertyModel<Integer>(controlSerum, "status");
DropDownChoice<Integer> status = new DropDownChoice<Integer>("status", statusModel, optionsModel, choicesRenderer);
form.add(status);
To download the full code of this application (still in development):
http://github.com/ziglee/biomedcalib
Please post your doubts or comments and i’ll promptly answer you!




February 7, 2010 at 11:08
umm… I am not
February 8, 2010 at 18:20
OP: I could be slow (lord knows I have been told lol) but that made absolutely no sense what so ever…
February 13, 2010 at 20:45
Can I help you? Tell me what you didnt understand, please.
February 15, 2010 at 11:54
I think I get it… and I am definitely slow…
So Cassio, is the idea that instead of using a raw enum (which can’t be mapped with Hibernate since enums aren’t classes), you’re creating a class that wraps your enum so it can be mapped by Hibernate, correct?
February 15, 2010 at 21:37
The Integer is being directly persisted in ControlSerum class. Hibernate does persist Enums, but to keep it simple, I didn’t use it. The objective of this code sample is just to show how to use a DropDownChoice to persist a Integer directly into an Entity.
Please, be confortable to ask anything! =D
November 2, 2011 at 10:07
Hi, Cassio
Thank you very much for that great example, especially for carefully naming of variables.
statusModel and optionsModel names just save my day and give me more than I could dig out of plenty of tutorials on DropDownChoice for understanding and implementing DDC for my own needs.