How to access datatable row from a converter. All is code p
supernovasoftware.com Jul 1, 2006 3:52 PMI am trying to validate a list of entry by using the EntityManager to query the database to check limits. I can also see myself using this for a list of
tracking numbers like on UPS as well. For each I would like to hit the DB and see if it meets my criteria and give a meaninful message to the user next to each field. I have this working and the EntityManager is accessable from my Converter, but I need additional information besides this value to do my checks in this case. It is present in the DataModel that this field is coming from, but I am unsure of the best way to get accesss to this once inside the converter. Any advice would be greatly appreciated.
As a minimum for this case I need to know the pk of the item in question so I can check its balance in the DB.
I have posted both the Java and Facelets code below with some comments.
Java Code
@Stateless
@Name("genericItemNumPieces")
@Interceptors(SeamInterceptor.class)
public class GenericItemNumPiecesConverterBean implements GenericItemNumPiecesConverterInterface
{
@Logger private Log log;
@PersistenceContext
EntityManager em;
public Converter getConverter() {
return new GenericItemNumPiecesConverter(em, log);
}
public static class GenericItemNumPiecesConverter implements Converter {
private EntityManager em;
private Log log;
public GenericItemNumPiecesConverter(EntityManager em, Log log) {
this.log=log;
this.em=em;
}
public String getAsString(FacesContext facesContext, UIComponent component, Object obj) {
if (obj == null) return null;
Long numPieces = (Long) obj;
String val = Long.toString(numPieces);
return val;
}
public Object getAsObject(FacesContext facesContext, UIComponent component, String str)
throws ConverterException {
// This is just to test the log and entity manager.
// But I need to access the row of the datatable this component is in
// so that I can verify the quantity.
List list = em.createQuery("from Size s").getResultList();
log.info("There are " + list.size() + " sizes");
// Check if the value entered is parsable as a Long. If not it throws ConverterException
Long numPieces = parseLong(str);
// Check if quantiy is positive.
if (numPieces < 0) throwConverterException();
// What I would like to do here is verify against the DB to see if the quantity is <= the number
// of pieces available and throw new ConverterException(new FacesMessage("The number of pieces must be <= X"))
// How can I access the row from the datatable that this component is located in so that I can
// check the quantity for the proper line item?
return numPieces;
}
}
private static Long parseLong(String str) {
Long numPieces = null;
try {
numPieces = Long.valueOf(str).longValue();
} catch (NumberFormatException e) {
throwConverterException();
}
return numPieces;
}
private static void throwConverterException() {
throw new ConverterException(new FacesMessage("The number of pieces must be >= 0."));
}
}
Facelets code
<t:dataTable id="#{dataTableId}"
var="couplingL"
rowIndexVar="rowIndex"
rows="20" preserveSort="true"
value="#{couplingList}">
<t:column styleClass="paddlr alignr">
<f:facet name="header">Qty Rec</f:facet>
<h:inputText id="recList_#{rowIndex}"
value="#{couplingL.numPiecesInput}"
converter="#{genericItemNumPieces.converter}" />
<h:messages for="recList_#{rowIndex}" />
</t:column>
</t:dataTable>