]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java
updated jar(s)
[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 afterUserLoginEvent (@Observes final UserLoggedInEvent event) {
53                 // event should not be null
54                 if (null == event) {
55                         // Throw NPE
56                         throw new NullPointerException("event is null"); //NOI18N
57                 } else if (event.getLoggedInUser() == null) {
58                         // Throw NPE again
59                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
60                 } else if (event.getLoggedInUser().getUserId() == null) {
61                         // userId is null
62                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
63                 } else if (event.getLoggedInUser().getUserId() < 1) {
64                         // Not avalid id
65                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
66                 }
67
68                 // Set locale here
69                 this.setLocale(event.getLoggedInUser().getUserLocale());
70
71                 // Trace message
72                 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
73         }
74
75         @Override
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();
79         }
80
81         @Override
82         public void setLanguage (final String language) {
83                 // Log trace message
84                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
85
86                 // Is the language null?
87                 if (null == language) {
88                         // This may sometimes happen, so abort here
89                         return;
90                 }
91
92                 // Language splits
93                 String[] splits = language.split("_"); //NOI18N
94                 if (null == splits[1]) {
95                         splits[1] = ""; //NOI18N
96                 }
97
98                 // Get new locale with upper-case country code
99                 Locale loc = new Locale(splits[0], splits[1]);
100
101                 // Log debug message
102                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
103
104                 // Set it here and in the JSF context
105                 this.setLocale(loc);
106                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
107
108                 // Log trace message
109                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
110         }
111
112         @Override
113         public Locale getLocale () {
114                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
115                 return this.locale;
116         }
117
118         @Override
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;
122         }
123
124         @Override
125         public Locale[] getSelectableLocalizations () {
126                 Locale[] locales = {
127                         Locale.GERMANY,
128                         Locale.US
129                 };
130                 return locales;
131         }
132
133         /**
134          * Initializer for this bean
135          */
136         @PostConstruct
137         public void init () {
138                 // Log trace message
139                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
140
141                 // Create locale instance from context
142                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
143
144                 // Log debug message
145                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
146
147                 // Set it here
148                 this.setLocale(loc);
149
150                 // Log trace message
151                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N
152         }
153
154 }