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.faces.context.FacesContext;
24 import javax.inject.Named;
25 import javax.enterprise.event.Observes;
26 import javax.faces.context.FacesContext;
27 import javax.inject.Named;
28 import org.mxchange.jjobs.beans.BaseJobsController;
29 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
32 * A session bean for handling localization/internationalization changes. This
33 * class is based on an example at [1] from mkyong.
35 * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
37 * @author Roland Haeder<roland@mxchange.org>
39 @Named ("localizationController")
41 public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
46 private static final long serialVersionUID = 1_867_671_657_629_601_528L;
51 private Locale locale;
54 public void afterUserLogin (@Observes final UserLoggedInEvent event) {
56 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
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 this.setLocale(event.getLoggedInUser().getUserLocale());
77 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
81 public String getLanguage () {
82 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
83 return this.getLocale().getLanguage().toLowerCase();
87 public void setLanguage (final String language) {
89 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
91 // Is the language null?
92 if (null == language) {
93 // This may sometimes happen, so abort here
98 String[] splits = language.split("_"); //NOI18N
99 if (null == splits[1]) {
100 splits[1] = ""; //NOI18N
103 // Get new locale with upper-case country code
104 Locale loc = new Locale(splits[0], splits[1]);
107 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("RecruiterLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
109 // Set it here and in the JSF context
111 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
114 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
118 public Locale getLocale () {
119 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
124 public void setLocale (final Locale locale) {
125 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
126 this.locale = locale;
130 public Locale[] getSelectableLocalizations () {
139 * Initializer for this bean
142 public void init () {
144 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::init: CALLED!"); //NOI18N
146 // Create locale instance from context
147 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
150 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("RecruiterLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
156 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::init: EXIT!"); //NOI18N