2 * Copyright (C) 2016 Roland Haeder
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.pizzaapplication.beans.localization;
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import javax.annotation.PostConstruct;
22 import javax.enterprise.context.SessionScoped;
23 import javax.enterprise.event.Observes;
24 import javax.faces.context.FacesContext;
25 import javax.inject.Named;
26 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
27 import org.mxchange.pizzaapplication.beans.BasePizzaController;
30 * A session bean for handling localization/internationalization changes. This
31 * class is based on an example at [1] from mkyong.
33 * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
35 * @author Roland Haeder<roland@mxchange.org>
37 @Named ("localizationController")
39 public class PizzaLocalizationSessionBean extends BasePizzaController implements PizzaLocalizationSessionController {
44 private static final long serialVersionUID = 639_184_671_562_195L;
49 private Locale locale;
52 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
54 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("LandingLocalizationSessionBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
56 // event should not be null
59 throw new NullPointerException("event is null"); //NOI18N
60 } else if (event.getLoggedInUser() == null) {
62 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
63 } else if (event.getLoggedInUser().getUserId() == null) {
65 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
66 } else if (event.getLoggedInUser().getUserId() < 1) {
68 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
72 this.setLocale(event.getLoggedInUser().getUserLocale());
75 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
79 public String getLanguage () {
80 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
81 return this.getLocale().getLanguage().toLowerCase();
85 public void setLanguage (final String language) {
87 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
89 // Is the language null?
90 if (null == language) {
91 // This may sometimes happen, so abort here
96 String[] splits = language.split("_"); //NOI18N
97 if (null == splits[1]) {
98 splits[1] = ""; //NOI18N
101 // Get new locale with upper-case country code
102 Locale loc = new Locale(splits[0], splits[1]);
105 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
107 // Set it here and in the JSF context
109 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
112 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
116 public Locale getLocale () {
117 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
122 public void setLocale (final Locale locale) {
123 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
124 this.locale = locale;
128 public Locale[] getSelectableLocalizations () {
137 * Initializer for this bean
140 public void init () {
142 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::init: CALLED!"); //NOI18N
144 // Create locale instance from context
145 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
148 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
154 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::init: EXIT!"); //NOI18N