package com.din.din.webapp.beans;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import com.din.din.model.dao.ProjectDAO;
import com.din.din.model.entities.Project;
import com.din.din.model.util.EntityCachingManager;
import com.din.din.webapp.beans.interfaces.LocaleWrapper;
import com.din.din.webapp.listeners.LoginPhaseListener;
import com.din.din.webapp.listeners.LoginPhaseListener.LoginListener;
@ManagedBean
@SessionScoped
public class LocaleBean implements Serializable, LoginListener {
private static final String BUNDLE_BASENAME = "com.din.din.webapp.l18n.messages";
private static final long serialVersionUID = -3022833920207494213L;
public static final String ACCESSOR = "#{localeBean}";
private static Set<LocaleWrapper> supportedLocales = null;
private Locale locale = null;
private transient ResourceBundle bundle = null;
@ManagedProperty(value=ProjectBean.ACCESSOR)
private ProjectBean projectBean = null;
@ManagedProperty("#{cacheBean.cache}")
private EntityCachingManager cache = null;
public LocaleBean() {
if(supportedLocales == null) {
loadSupportedLocales();
}
FacesContext fc = FacesContext.getCurrentInstance();
Locale requestLocale = fc.getExternalContext().getRequestLocale();
requestLocale = getClosestSupportedLocale(requestLocale);
if(requestLocale == null) {
requestLocale = fc.getApplication().getDefaultLocale();
}
setLocaleInner(requestLocale);
LoginPhaseListener.addListener(this);
}
/**
* This method finds the closest match, so that say, even if we don't support
* it_IT, we can still attempt to satisfy the user with locale it instead.
* @param requestLocale Locale to approximate
* @return Closest locale match in LocaleBean.supportedLocales, null if no reasonable approximate
* exists.
*/
private Locale getClosestSupportedLocale(Locale requestLocale) {
LocaleWrapper closestLocale = null;
if(requestLocale != null) {
for(LocaleWrapper locale : supportedLocales) {
if(locale.getLanguage().equals(requestLocale.getLanguage())) {
closestLocale = locale;
// Exact match.. we can stop the search now
if(locale.equals(requestLocale)) {
break;
}
}
}
}
return closestLocale.getLocale();
}
private synchronized void loadSupportedLocales() {
Set<LocaleWrapper> locales = new LinkedHashSet<LocaleWrapper>();
locales.add(new LocaleWrapper(FacesContext.getCurrentInstance().getApplication().getDefaultLocale()));
Iterator<Locale> localeIt = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
while(localeIt.hasNext()) {
locales.add(new LocaleWrapper(localeIt.next()));
}
supportedLocales = locales;
}
public Locale getLocale() {
return locale;
}
protected synchronized void setLocaleInner(Locale locale) {
this.locale = locale;
if(locale != null) {
bundle = ResourceBundle.getBundle(BUNDLE_BASENAME, locale);
} else {
bundle = null;
}
}
public synchronized void setLocale(Locale locale) {
setLocaleInner(locale);
if(locale != null && projectBean != null && projectBean.getProject() != null && projectBean.getProject().getKey() != null) {
projectBean.getProject().setPreferredLocaleCode(getLocaleWrapper().toString());
ProjectDAO.save(projectBean.getProject(), null);
}
}
public LocaleWrapper getLocaleWrapper() {
return locale != null ? new LocaleWrapper(locale) : null;
}
public void setLocaleWrapper(LocaleWrapper locale) {
setLocale(locale != null ? locale.getLocale() : null);
}
public ProjectBean getProjectBean() {
return projectBean;
}
public void setProjectBean(ProjectBean projectBean) {
this.projectBean = projectBean;
onLogin();
}
public EntityCachingManager getCache() {
return cache;
}
public void setCache(EntityCachingManager cache) {
this.cache = cache;
}
public List<LocaleWrapper> getSupportedLocales() {
return new ArrayList<LocaleWrapper>(supportedLocales);
}
public String getString(String key) {
return bundle != null ? bundle.getString(key) : "??" + key + "??";
}
public void onLogin() {
if(projectBean != null) {
Project project = projectBean.getProject();
if(project != null && project.getPreferredLocaleCode() != null) {
setLocaleInner(LocaleWrapper.loadFromString(project.getPreferredLocaleCode()));
}
}
}
public void onLogoff() {
}
}