@FacesConverter(value = "com.liferay.faces.showcase.converter.CountryConverter")
public class CountryConverter implements Converter {
private static volatile Map<Long, Country> countryMap;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
Object countryObject = null;
if ((value != null) && !"".equals(value)) {
Long countryId = Long.parseLong(value);
countryObject = getCountryMap(facesContext).get(countryId);
}
return countryObject;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
String strValue = "";
if (value != null) {
Country country = (Country) value;
strValue = Long.toString(country.getCountryId());
}
return strValue;
}
protected Map<Long, Country> getCountryMap(FacesContext facesContext) {
Map<Long, Country> countryMap = CountryConverter.countryMap;
if (countryMap == null) {
synchronized (CountryConverter.class) {
countryMap = CountryConverter.countryMap;
if (countryMap == null) {
ELResolver elResolver = facesContext.getApplication().getELResolver();
ELContext elContext = facesContext.getELContext();
CountryService countryService = (CountryService) elResolver.getValue(elContext, null,
"countryService");
countryMap = CountryConverter.countryMap = countryService.getCountryMap();
}
}
}
return countryMap;
}
}