/**
*
* @author ilya portnyagin iportnyagin@gmail.com
*/
package ru.portnyagin.helpdeskru.converter;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.ConverterException;
import javax.faces.convert.FacesConverter;
import ru.portnyagin.helpdeskru.model.UserHD;
import ru.portnyagin.helpdeskru.service.UserHDService;
import ru.portnyagin.helpdeskru.util.EJB;
@FacesConverter(forClass = UserHD.class)
public class UserHDConverter implements Converter {
private UserHDService userHDService = EJB.lookup(UserHDService.class);
@Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (value == null || value.length() == 0){
return null;
}
UserHD sotrudnik = userHDService.find(Long.valueOf(value));
if (sotrudnik == null) {
throw new ConverterException(new FacesMessage("Unknown UserHD ID: " + value));
}
return sotrudnik;
}
@Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (!(value instanceof UserHD) || ((UserHD) value).getId() == null) {
return null;
}
return ((UserHD) value).getId().toString();
}
}