I'm trying to make selectManyMenu works, but I have problems writing the converter for it correctly. This is the first time I need to write converter so things are not so clear to me. I have read on several places that I shouldn't do any calls to database in converter, but how could I fetch object for some ID in getAsObject method in other way than make call to DB?
@FacesConverter(value = "converterTest")
public class ConverterTest implements Converter {
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
Configuration cfg = new Configuration();
cfg.configure("hibernate.cfg.xml");
System.out.println("ID: " + value);
SessionFactory factory = cfg.buildSessionFactory();
try {
Session session = factory.openSession();
Equipment e = (Equipment) session.get(Equipment.class, Integer.valueOf(value));
System.out.println("EQUIPMENT ID : " + e.getId());
return e;
} catch(Exception ex) {
System.out.println("ERROR: " + ex.getMessage());
return null;
}
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object object) {
if(object != null) {
System.out.println("ID: " + String.valueOf(((Equipment) object).getId()));
return String.valueOf(((Equipment) object).getId());
}
else {
return null;
}
}
}
IMPORTANT:
When page loads, my selectManyMenu is filled with records but I don't see its names, just checkboxes and empty spaces instead of name of record (equipment). But, if I try to put some characters in search filter it works even though I see only checkboxes and not names. I added itemDescription="#{record.name}" and when I put mouse pointer on some record itemDescription is shown. So, obviously component has access to all records but its names is not showing up. You can see screen shot on: http://i57.tinypic.com/2wpm2ok.png ("Autoklav" is name of record that doesn't want to show up on a list, but it shows as a description). It isn't css problem, I checked :)
This is my selectManyMenu:
<p:selectManyMenu id="advanced" value="#{pickListView.recordSet}" converter="converterTest"
var="t" filter="true" filterMatchMode="contains" showCheckbox="true">
<f:selectItems value="#{pickListView.allEquipment}" var="record" itemLabel="#{record.name}" itemValue="#{record}" />
<p:column style="width:90%">
<h:outputText value="#{record.name}" />
</p:column>
</p:selectManyMenu>
Second problem is converting String to Object, I'm not sure what I have to do - to make a call to DB or not? At the moment I'm getting errors whatever I try, what is the correct way to get Object from String (id?) ?
I followed instructions on http://www.primefaces.org/showcase/ui/input/manyMenu.xhtml but not sure what I'm doing wrong and why I can't get at least record name shown up (getAsString is really simple, it should work)
Does anybody has some suggestion or already has converter for selectManyMenu? If you need some more information, I would be glad to give you.
Thanks!