From c1928d6fbf445cbae63cb43e177221a3489491fc Mon Sep 17 00:00:00 2001 From: =?utf8?q?Roland=20H=C3=A4der?= Date: Mon, 23 May 2016 09:26:47 +0200 Subject: [PATCH] added afterUserLogin() observer to set user's locale as current MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Roland Häder --- .../PizzaLocalizationSessionBean.java | 30 +++++++++++++++++++ .../PizzaLocalizationSessionController.java | 8 +++++ 2 files changed, 38 insertions(+) diff --git a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java index 4496bfc5..5e7acb64 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java +++ b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java @@ -16,12 +16,15 @@ */ package org.mxchange.pizzaapplication.beans.localization; +import java.text.MessageFormat; import java.util.Locale; import javax.annotation.PostConstruct; +import javax.enterprise.event.Observes; import javax.faces.bean.SessionScoped; import javax.faces.context.FacesContext; import javax.inject.Named; import org.mxchange.pizzaapplication.beans.BasePizzaController; +import org.mxchange.jusercore.events.login.UserLoggedInEvent; /** * A session bean for handling localization/internationalization changes. This @@ -45,6 +48,33 @@ public class PizzaLocalizationSessionBean extends BasePizzaController implements */ private Locale locale; + @Override + public void afterUserLogin (@Observes final UserLoggedInEvent event) { + // Trace message + //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N + + // 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 + } + + // Set locale here + this.setLocale(event.getLoggedInUser().getUserLocale()); + + // Trace message + //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N + } + @Override public String getLanguage () { //this.getLoggerBeanLocal().logTrace(MessageFormat.format("PizzaLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N diff --git a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java index 7150b330..7dbb017e 100644 --- a/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java +++ b/src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionController.java @@ -18,6 +18,7 @@ package org.mxchange.pizzaapplication.beans.localization; import java.io.Serializable; import java.util.Locale; +import org.mxchange.jusercore.events.login.UserLoggedInEvent; /** * An interface for localization change beans @@ -26,6 +27,13 @@ import java.util.Locale; */ public interface PizzaLocalizationSessionController extends Serializable { + /** + * Event observer for logged-in user + *

+ * @param event Event instance + */ + void afterUserLogin (final UserLoggedInEvent event); + /** * Getter for locale *

-- 2.39.5