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