*/
package org.mxchange.jjobs.beans.localization;
+import java.text.MessageFormat;
import java.util.Locale;
import javax.annotation.PostConstruct;
-import javax.faces.bean.SessionScoped;
+import javax.enterprise.context.SessionScoped;
+import javax.enterprise.event.Observes;
import javax.faces.context.FacesContext;
import javax.inject.Named;
-import org.mxchange.jcoreee.database.BaseDatabaseBean;
+import org.mxchange.jjobs.beans.BaseJobsController;
+import org.mxchange.jusercore.events.login.UserLoggedInEvent;
+import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
/**
* A session bean for handling localization/internationalization changes. This
* <p>
* @author Roland Haeder<roland@mxchange.org>
*/
-@Named ("localization")
+@Named ("localizationController")
@SessionScoped
-public class JobsLocalizationSessionBean extends BaseDatabaseBean implements JobsLocalizationSessionController {
+public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
/**
* Serial number
*/
private Locale locale;
+ @Override
+ public void afterUserLoginEvent (@Observes final UserLoggedInEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getLoggedInUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.loggedInUser is null"); //NOI18N
+ } else if (event.getLoggedInUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
+ } else if (event.getLoggedInUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
+ }
+
+ // Is the locale set?
+ if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
+ // Set locale here
+ this.setLocale(event.getLoggedInUser().getUserLocale());
+ }
+ }
+
+ @Override
+ public void afterUserLogoutEvent (@Observes final ObserveableUserLogoutEvent event) {
+ // event should not be null
+ if (null == event) {
+ // Throw NPE
+ throw new NullPointerException("event is null"); //NOI18N
+ } else if (event.getLoggedOutUser() == null) {
+ // Throw NPE again
+ throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
+ } else if (event.getLoggedOutUser().getUserId() == null) {
+ // userId is null
+ throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
+ } else if (event.getLoggedOutUser().getUserId() < 1) {
+ // Not avalid id
+ throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
+ }
+
+ // Trace message
+ //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
+ }
+
+ @Override
+ public String getLanguage () {
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
+ return this.getLocale().getLanguage().toLowerCase();
+ }
+
+ @Override
+ public void setLanguage (final String language) {
+ // Log trace message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
+
+ // Is the language null?
+ if (null == language) {
+ // This may sometimes happen, so abort here
+ return;
+ }
+
+ // Language splits
+ String[] splits = language.split("_"); //NOI18N
+ if (null == splits[1]) {
+ splits[1] = ""; //NOI18N
+ }
+
+ // Get new locale with upper-case country code
+ Locale loc = new Locale(splits[0], splits[1]);
+
+ // Log debug message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
+
+ // Set it here and in the JSF context
+ this.setLocale(loc);
+ FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
+
+ // Log trace message
+ //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
+ }
+
@Override
public Locale getLocale () {
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
return this.locale;
}
@Override
public void setLocale (final Locale locale) {
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
this.locale = locale;
}
+ @Override
+ public Locale[] getSelectableLocalizations () {
+ Locale[] locales = {
+ Locale.GERMANY,
+ Locale.US
+ };
+ return locales;
+ }
+
/**
* Initializer for this bean
*/
@PostConstruct
public void init () {
+ // Log trace message
+ //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
+
// Create locale instance from context
Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
+ // Log debug message
+ //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
+
// Set it here
this.setLocale(loc);
+
+ // Log trace message
+ //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N
}
}