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.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.UserLoggedInEvent;
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 JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
44 private static final long serialVersionUID = 1_867_671_657_629_601_528L;
49 private Locale locale;
52 public void afterUserLoginEvent (@Observes final UserLoggedInEvent event) {
53 // event should not be null
56 throw new NullPointerException("event is null"); //NOI18N
57 } else if (event.getLoggedInUser() == null) {
59 throw new NullPointerException("event.loggedInUser is null"); //NOI18N
60 } else if (event.getLoggedInUser().getUserId() == null) {
62 throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
63 } else if (event.getLoggedInUser().getUserId() < 1) {
65 throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
69 this.setLocale(event.getLoggedInUser().getUserLocale());
72 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
76 public String getLanguage () {
77 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
78 return this.getLocale().getLanguage().toLowerCase();
82 public void setLanguage (final String language) {
84 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
86 // Is the language null?
87 if (null == language) {
88 // This may sometimes happen, so abort here
93 String[] splits = language.split("_"); //NOI18N
94 if (null == splits[1]) {
95 splits[1] = ""; //NOI18N
98 // Get new locale with upper-case country code
99 Locale loc = new Locale(splits[0], splits[1]);
102 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
104 // Set it here and in the JSF context
106 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
109 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
113 public Locale getLocale () {
114 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
119 public void setLocale (final Locale locale) {
120 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
121 this.locale = locale;
125 public Locale[] getSelectableLocalizations () {
134 * Initializer for this bean
137 public void init () {
139 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
141 // Create locale instance from context
142 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
145 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
151 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N