]> 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 Häder
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.ObservableUserLoggedInEvent;
28 import org.mxchange.jusercore.events.logout.ObservableUserLogoutEvent;
29
30 /**
31  * A session bean for handling localization/internationalization changes. This
32  * class is based on an example at [1] from mkyong.
33  * <p>
34  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
35  * <p>
36  * @author Roland Häder<roland@mxchange.org>
37  */
38 @Named ("localizationController")
39 @SessionScoped
40 public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 1_867_671_657_629_601_528L;
46
47         /**
48          * Current Locale
49          */
50         private Locale locale;
51
52         /**
53          * Event observer for logged-in user
54          * <p>
55          * @param event Event instance
56          */
57         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
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                 // Is the locale set?
74                 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
75                         // Set locale here
76                         this.setLocale(event.getLoggedInUser().getUserLocale());
77                 }
78         }
79
80         /**
81          * Event observer for logged-out user
82          * <p>
83          * @param event Event instance
84          */
85         public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
86                 // event should not be null
87                 if (null == event) {
88                         // Throw NPE
89                         throw new NullPointerException("event is null"); //NOI18N
90                 } else if (event.getLoggedOutUser() == null) {
91                         // Throw NPE again
92                         throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
93                 } else if (event.getLoggedOutUser().getUserId() == null) {
94                         // userId is null
95                         throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
96                 } else if (event.getLoggedOutUser().getUserId() < 1) {
97                         // Not avalid id
98                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
99                 }
100
101                 // Clear this bean as well
102                 this.clear();
103         }
104
105         @Override
106         public String getLanguage () {
107                 return this.getLocale().getLanguage().toLowerCase();
108         }
109
110         @Override
111         public void setLanguage (final String language) {
112                 // Is the language null?
113                 if (null == language) {
114                         // This may sometimes happen, so abort here
115                         return;
116                 }
117
118                 // Language splits
119                 String[] splits = language.split("_"); //NOI18N
120                 if (null == splits[1]) {
121                         splits[1] = ""; //NOI18N
122                 }
123
124                 // Get new locale with upper-case country code
125                 Locale loc = new Locale(splits[0], splits[1]);
126
127                 // Set it here and in the JSF context
128                 this.setLocale(loc);
129                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
130         }
131
132         @Override
133         public Locale getLocale () {
134                 return this.locale;
135         }
136
137         @Override
138         public void setLocale (final Locale locale) {
139                 this.locale = locale;
140         }
141
142         @Override
143         public Locale[] getSelectableLocalizations () {
144                 Locale[] locales = {
145                         Locale.GERMANY,
146                         Locale.US
147                 };
148                 return locales;
149         }
150
151         /**
152          * Initializer for this bean
153          */
154         @PostConstruct
155         public void init () {
156                 // Create locale instance from context
157                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
158
159                 // Set it here
160                 this.setLocale(loc);
161         }
162
163         /**
164          * Clears this bean
165          */
166         private void clear () {
167                 // Clear all fields
168                 this.setLanguage(null);
169                 this.setLocale(null);
170         }
171
172 }