@FacesConverter(value = "com.liferay.faces.showcase.converter.CountryGmapConverter")
public class CountryGmapConverter implements Converter {
private static volatile Map<Long, Country> countryMap;
@Override
public Object getAsObject(FacesContext facesContext, UIComponent uiComponent, String value) {
return null;
}
@Override
public String getAsString(FacesContext facesContext, UIComponent uiComponent, Object value) {
Country country = getCountryMap(facesContext).get(value);
String URL = "https://www.google.com/maps/place/";
try {
return URL + URLEncoder.encode(country.getCountryName(), "UTF-8");
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
return null;
}
}
protected Map<Long, Country> getCountryMap(FacesContext facesContext) {
Map<Long, Country> countryMap = CountryGmapConverter.countryMap;
if (countryMap == null) {
synchronized (CountryGmapConverter.class) {
countryMap = CountryGmapConverter.countryMap;
if (countryMap == null) {
ELResolver elResolver = facesContext.getApplication().getELResolver();
ELContext elContext = facesContext.getELContext();
CountryService countryService = (CountryService) elResolver.getValue(elContext, null,
"countryService");
countryMap = CountryGmapConverter.countryMap = countryService.getCountryMap();
}
}
}
return countryMap;
}
}