Friday, February 5, 2010

simple jsf converter

package org.tspakb.egitim.web.converter;

import java.util.HashMap;
import java.util.Map;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import org.tspakb.egitim.entity.EducationType;

/**
*
* @author telman
*/
public class EducationTypeConverter implements Converter {

Map mapEducationTypes = new HashMap();

public Object getAsObject(FacesContext context, UIComponent component, String value) {

if (mapEducationTypes == null || value == null) {
return null;
}
return mapEducationTypes.get(Long.valueOf(value));
}

public String getAsString(FacesContext context, UIComponent component, Object value) {
Long result = null;
if (value instanceof EducationType &&(result = ((EducationType) value).getId()) != null) {
mapEducationTypes.put(result, (EducationType) value);
return result.toString();
}
return null;
}
}