]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java
Continued:
[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-scoped 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          * Default constructor
54          */
55         public JobsLocalizationSessionBean () {
56         }
57
58         /**
59          * Event observer for logged-in user
60          * <p>
61          * @param event Event instance
62          */
63         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
64                 // event should not be null
65                 if (null == event) {
66                         // Throw NPE
67                         throw new NullPointerException("event is null"); //NOI18N
68                 } else if (event.getLoggedInUser() == null) {
69                         // Throw NPE again
70                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
71                 } else if (event.getLoggedInUser().getUserId() == null) {
72                         // userId is null
73                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
74                 } else if (event.getLoggedInUser().getUserId() < 1) {
75                         // Not avalid id
76                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
77                 }
78
79                 // Is the locale set?
80                 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
81                         // Set locale here
82                         this.setLocale(event.getLoggedInUser().getUserLocale());
83                 }
84         }
85
86         /**
87          * Event observer for logged-out user
88          * <p>
89          * @param event Event instance
90          */
91         public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
92                 // event should not be null
93                 if (null == event) {
94                         // Throw NPE
95                         throw new NullPointerException("event is null"); //NOI18N
96                 } else if (event.getLoggedOutUser() == null) {
97                         // Throw NPE again
98                         throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
99                 } else if (event.getLoggedOutUser().getUserId() == null) {
100                         // userId is null
101                         throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
102                 } else if (event.getLoggedOutUser().getUserId() < 1) {
103                         // Not avalid id
104                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
105                 }
106
107                 // Clear this bean as well
108                 this.clear();
109         }
110
111         @Override
112         public String getLanguage () {
113                 return this.getLocale().getLanguage().toLowerCase();
114         }
115
116         @Override
117         public void setLanguage (final String language) {
118                 // Is the language null?
119                 if (null == language) {
120                         // This may sometimes happen, so abort here
121                         return;
122                 }
123
124                 // Language splits
125                 String[] splits = language.split("_"); //NOI18N
126                 if (null == splits[1]) {
127                         splits[1] = ""; //NOI18N
128                 }
129
130                 // Get new locale with upper-case country code
131                 Locale loc = new Locale(splits[0], splits[1]);
132
133                 // Set it here and in the JSF context
134                 this.setLocale(loc);
135                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
136         }
137
138         @Override
139         public Locale getLocale () {
140                 return this.locale;
141         }
142
143         @Override
144         public void setLocale (final Locale locale) {
145                 this.locale = locale;
146         }
147
148         @Override
149         public Locale[] getSelectableLocalizations () {
150                 Locale[] locales = {
151                         Locale.GERMANY,
152                         Locale.US
153                 };
154                 return locales;
155         }
156
157         /**
158          * Initializer for this bean
159          */
160         @PostConstruct
161         public void init () {
162                 // Create locale instance from context
163                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
164
165                 // Set it here
166                 this.setLocale(loc);
167         }
168
169         /**
170          * Clears this bean
171          */
172         private void clear () {
173                 // Clear all fields
174                 this.setLanguage(null);
175                 this.setLocale(null);
176         }
177
178 }