package org.xulfaces.rubis.web.faces;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.el.ValueBinding;
import org.xulfaces.rubis.admin.dao.RegionDAO;
import org.xulfaces.rubis.admin.dao.RegionNotFoundException;
import org.xulfaces.rubis.model.Region;
public class RegionConverter implements Converter {
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if(value!=null){
int regionId = Integer.parseInt(value);
ValueBinding valueBinding = context.getApplication().createValueBinding("#{regionDAO}");
RegionDAO regionDAO = (RegionDAO) valueBinding.getValue(context);
try {
return regionDAO.loadRegion(new Integer(regionId));
} catch (RegionNotFoundException e) {
return null;
}
}
return null;
}
public String getAsString(FacesContext context, UIComponent component, Object value) {
if(value instanceof Region){
Region region = (Region) value;
return region.getId().toString();
}
return null;
}
}