]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java
added afterUserLogin() observer to set user's locale as current
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / localization / JobsLocalizationSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.jjobs.beans.localization;
18
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;
30
31 /**
32  * A session bean for handling localization/internationalization changes. This
33  * class is based on an example at [1] from mkyong.
34  * <p>
35  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
36  * <p>
37  * @author Roland Haeder<roland@mxchange.org>
38  */
39 @Named ("localizationController")
40 @SessionScoped
41 public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 1_867_671_657_629_601_528L;
47
48         /**
49          * Current Locale
50          */
51         private Locale locale;
52
53         @Override
54         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
55                 // Trace message
56                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ContactWebBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
57
58                 // event should not be null
59                 if (null == event) {
60                         // Throw NPE
61                         throw new NullPointerException("event is null"); //NOI18N
62                 } else if (event.getLoggedInUser() == null) {
63                         // Throw NPE again
64                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
65                 } else if (event.getLoggedInUser().getUserId() == null) {
66                         // userId is null
67                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
68                 } else if (event.getLoggedInUser().getUserId() < 1) {
69                         // Not avalid id
70                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
71                 }
72
73                 // Set locale here
74                 this.setLocale(event.getLoggedInUser().getUserLocale());
75
76                 // Trace message
77                 //* NOISY-DEBUG: */ System.out.println("ContactWebBean:afterUserLogin - EXIT!"); //NOI18N
78         }
79
80         @Override
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();
84         }
85
86         @Override
87         public void setLanguage (final String language) {
88                 // Log trace message
89                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
90
91                 // Is the language null?
92                 if (null == language) {
93                         // This may sometimes happen, so abort here
94                         return;
95                 }
96
97                 // Language splits
98                 String[] splits = language.split("_"); //NOI18N
99                 if (null == splits[1]) {
100                         splits[1] = ""; //NOI18N
101                 }
102
103                 // Get new locale with upper-case country code
104                 Locale loc = new Locale(splits[0], splits[1]);
105
106                 // Log debug message
107                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("RecruiterLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
108
109                 // Set it here and in the JSF context
110                 this.setLocale(loc);
111                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
112
113                 // Log trace message
114                 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
115         }
116
117         @Override
118         public Locale getLocale () {
119                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
120                 return this.locale;
121         }
122
123         @Override
124         public void setLocale (final Locale locale) {
125                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("RecruiterLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
126                 this.locale = locale;
127         }
128
129         @Override
130         public Locale[] getSelectableLocalizations () {
131                 Locale[] locales = {
132                         Locale.GERMANY,
133                         Locale.US
134                 };
135                 return locales;
136         }
137
138         /**
139          * Initializer for this bean
140          */
141         @PostConstruct
142         public void init () {
143                 // Log trace message
144                 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::init: CALLED!"); //NOI18N
145
146                 // Create locale instance from context
147                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
148
149                 // Log debug message
150                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("RecruiterLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
151
152                 // Set it here
153                 this.setLocale(loc);
154
155                 // Log trace message
156                 //this.getLoggerBeanLocal().logTrace("RecruiterLocalizationSessionBean::init: EXIT!"); //NOI18N
157         }
158
159 }