2 * Copyright (C) 2016 Roland Häder
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.ObservableUserLoggedInEvent;
28 import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent;
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 Häder<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;
55 public AddressbookLocalizationSessionBean () {
59 * Event observer for logged-in user
61 * @param event Event instance
63 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
64 // event should not be null
67 throw new NullPointerException("event is null"); //NOI18N
68 } else if (event.getLoggedInUser() == null) {
70 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
71 } else if (event.getLoggedInUser().getUserId() == null) {
73 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
74 } else if (event.getLoggedInUser().getUserId() < 1) {
76 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
80 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
82 this.setLocale(event.getLoggedInUser().getUserLocale());
87 * Event observer for logged-out user
89 * @param event Event instance
91 public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
92 // event should not be null
95 throw new NullPointerException("event is null"); //NOI18N
96 } else if (event.getLoggedOutUser() == null) {
98 throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
99 } else if (event.getLoggedOutUser().getUserId() == null) {
101 throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
102 } else if (event.getLoggedOutUser().getUserId() < 1) {
104 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
107 // Clear this bean as well
112 public String getLanguage () {
113 return this.getLocale().getLanguage().toLowerCase();
117 public void setLanguage (final String language) {
118 // Is the language null?
119 if (null == language) {
120 // This may sometimes happen, so abort here
125 String[] splits = language.split("_"); //NOI18N
126 if (null == splits[1]) {
127 splits[1] = ""; //NOI18N
130 // Get new locale with upper-case country code
131 Locale loc = new Locale(splits[0], splits[1]);
133 // Set it here and in the JSF context
135 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
139 public Locale getLocale () {
144 public void setLocale (final Locale locale) {
145 this.locale = locale;
149 public Locale[] getSelectableLocalizations () {
158 * Initializer for this bean
161 public void init () {
162 // Create locale instance from context
163 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
172 private void clear () {
174 this.setLanguage(null);
175 this.setLocale(null);