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.addressbook.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.addressbook.beans.BaseAddressbookController;
27 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
28 import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
31 * A session bean for handling localization/internationalization changes. This
32 * class is based on an example at [1] from mkyong.
34 * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
36 * @author Roland Haeder<roland@mxchange.org>
38 @Named ("localizationController")
40 public class AddressbookLocalizationSessionBean extends BaseAddressbookController implements AddressbookLocalizationSessionController {
45 private static final long serialVersionUID = 158_768_216_759_107L;
50 private Locale locale;
53 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
55 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("LandingLocalizationSessionBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
57 // event should not be null
60 throw new NullPointerException("event is null"); //NOI18N
61 } else if (event.getLoggedInUser() == null) {
63 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
64 } else if (event.getLoggedInUser().getUserId() == null) {
66 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
67 } else if (event.getLoggedInUser().getUserId() < 1) {
69 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
73 this.setLocale(event.getLoggedInUser().getUserLocale());
76 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
80 public void afterUserLogout (@Observes final ObserveableUserLogoutEvent event) {
82 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ReportsLocalizationSessionBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
84 // event should not be null
87 throw new NullPointerException("event is null"); //NOI18N
88 } else if (event.getLoggedOutUser() == null) {
90 throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
91 } else if (event.getLoggedOutUser().getUserId() == null) {
93 throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
94 } else if (event.getLoggedOutUser().getUserId() < 1) {
96 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
99 // Clear this bean as well
103 //* NOISY-DEBUG: */ System.out.println("ReportsLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
107 public String getLanguage () {
108 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
109 return this.getLocale().getLanguage().toLowerCase();
113 public void setLanguage (final String language) {
115 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
117 // Is the language null?
118 if (null == language) {
119 // This may sometimes happen, so abort here
124 String[] splits = language.split("_"); //NOI18N
125 if (null == splits[1]) {
126 splits[1] = ""; //NOI18N
129 // Get new locale with upper-case country code
130 Locale loc = new Locale(splits[0], splits[1]);
133 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
134 // Set it here and in the JSF context
136 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
139 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
143 public Locale getLocale () {
144 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
149 public void setLocale (final Locale locale) {
150 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
151 this.locale = locale;
155 public Locale[] getSelectableLocalizations () {
164 * Initializer for this bean
167 public void init () {
169 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::init: CALLED!"); //NOI18N
171 // Create locale instance from context
172 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
175 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
180 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::init: EXIT!"); //NOI18N
186 private void clear () {
188 this.setLanguage(null);
189 this.setLocale(null);