2 * Copyright (C) 2016, 2017 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.jjobs.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.jjobs.beans.BaseJobsController;
27 import org.mxchange.jusercore.events.login.ObservableUserLoggedInEvent;
28 import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent;
31 * A session-scoped bean for handling localization/internationalization changes.
32 * This 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 JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
45 private static final long serialVersionUID = 1_867_671_657_629_601_528L;
50 private Locale locale;
55 public JobsLocalizationSessionBean () {
56 // Call super constructor
61 * Event observer for logged-in user
63 * @param event Event instance
65 public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
66 // event should not be null
69 throw new NullPointerException("event is null"); //NOI18N
70 } else if (event.getLoggedInUser() == null) {
72 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
73 } else if (event.getLoggedInUser().getUserId() == null) {
75 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
76 } else if (event.getLoggedInUser().getUserId() < 1) {
78 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
82 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
84 this.setLocale(event.getLoggedInUser().getUserLocale());
89 * Event observer for logged-out user
91 * @param event Event instance
93 public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
94 // event should not be null
97 throw new NullPointerException("event is null"); //NOI18N
98 } else if (event.getLoggedOutUser() == null) {
100 throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
101 } else if (event.getLoggedOutUser().getUserId() == null) {
103 throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
104 } else if (event.getLoggedOutUser().getUserId() < 1) {
106 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
109 // Clear this bean as well
114 public String getLanguage () {
115 return this.getLocale().getLanguage().toLowerCase();
119 public void setLanguage (final String language) {
120 // Is the language null?
121 if (null == language) {
122 // This may sometimes happen, so abort here
127 String[] splits = language.split("_"); //NOI18N
128 if (null == splits[1]) {
129 splits[1] = ""; //NOI18N
132 // Get new locale with upper-case country code
133 Locale loc = new Locale(splits[0], splits[1]);
135 // Set it here and in the JSF context
137 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
141 public Locale getLocale () {
146 public void setLocale (final Locale locale) {
147 this.locale = locale;
151 public Locale[] getSelectableLocalizations () {
160 * Initializer for this bean
163 public void init () {
164 // Create locale instance from context
165 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
174 private void clear () {
176 this.setLanguage(null);
177 this.setLocale(null);