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;
53 * Event observer for logged-in user
55 * @param event Event instance
57 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
58 // event should not be null
61 throw new NullPointerException("event is null"); //NOI18N
62 } else if (event.getLoggedInUser() == null) {
64 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
65 } else if (event.getLoggedInUser().getUserId() == null) {
67 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
68 } else if (event.getLoggedInUser().getUserId() < 1) {
70 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
74 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
76 this.setLocale(event.getLoggedInUser().getUserLocale());
81 * Event observer for logged-out user
83 * @param event Event instance
85 public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
86 // event should not be null
89 throw new NullPointerException("event is null"); //NOI18N
90 } else if (event.getLoggedOutUser() == null) {
92 throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
93 } else if (event.getLoggedOutUser().getUserId() == null) {
95 throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
96 } else if (event.getLoggedOutUser().getUserId() < 1) {
98 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
101 // Clear this bean as well
106 public String getLanguage () {
107 return this.getLocale().getLanguage().toLowerCase();
111 public void setLanguage (final String language) {
112 // Is the language null?
113 if (null == language) {
114 // This may sometimes happen, so abort here
119 String[] splits = language.split("_"); //NOI18N
120 if (null == splits[1]) {
121 splits[1] = ""; //NOI18N
124 // Get new locale with upper-case country code
125 Locale loc = new Locale(splits[0], splits[1]);
127 // Set it here and in the JSF context
129 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
133 public Locale getLocale () {
138 public void setLocale (final Locale locale) {
139 this.locale = locale;
143 public Locale[] getSelectableLocalizations () {
152 * Initializer for this bean
155 public void init () {
156 // Create locale instance from context
157 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
166 private void clear () {
168 this.setLanguage(null);
169 this.setLocale(null);