]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java
dcb616175620590444b4b247a82550d2292b4046
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / localization / PizzaLocalizationSessionBean.java
1 /*
2  * Copyright (C) 2016 - 2022 Free Software Foundation
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.pizzaapplication.beans.localization;
18
19 import java.text.MessageFormat;
20 import java.util.Iterator;
21 import java.util.LinkedHashMap;
22 import java.util.Locale;
23 import java.util.Map;
24 import java.util.Objects;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.enterprise.event.Event;
28 import javax.enterprise.event.Observes;
29 import javax.enterprise.inject.Any;
30 import javax.faces.context.FacesContext;
31 import javax.inject.Inject;
32 import javax.inject.Named;
33 import org.mxchange.jcoreee.events.locale.LocaleChangeEvent;
34 import org.mxchange.jcoreee.events.locale.ObservableLocaleChangeEvent;
35 import org.mxchange.juserlogincore.events.login.ObservableUserLoggedInEvent;
36 import org.mxchange.juserlogincore.events.logout.ObservableUserLogoutEvent;
37 import org.mxchange.pizzaapplication.beans.BasePizzaBean;
38
39 /**
40  * A session-scoped bean for handling localization/internationalization changes.
41  * This class is based on an example at [1] from mkyong.
42  * <p>
43  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
44  * <p>
45  * @author Roland Häder<roland@mxchange.org>
46  */
47 @Named ("localizationController")
48 @SessionScoped
49 public class PizzaLocalizationSessionBean extends BasePizzaBean implements PizzaLocalizationSessionController {
50
51         /**
52          * Serial number
53          */
54         private static final long serialVersionUID = 158_768_216_759_107L;
55
56         /**
57          * Current Locale
58          */
59         private Locale locale;
60
61         /**
62          * Event being fired when a locale has changed successfully
63          */
64         @Inject
65         @Any
66         private Event<ObservableLocaleChangeEvent> localeChangeEvent;
67
68         /**
69          * Locale code (example: de for Germany)
70          */
71         private String localeCode;
72
73         /**
74          * A map of all supported locales, using language code as key
75          */
76         private final Map<String, Locale> supportedLocales;
77
78         /**
79          * Default constructor
80          */
81         public PizzaLocalizationSessionBean () {
82                 // Call super constructor
83                 super();
84
85                 // Init list
86                 this.supportedLocales = new LinkedHashMap<>(2);
87         }
88
89         /**
90          * Event observer for logged-in user
91          * <p>
92          * @param event Event instance
93          */
94         public void afterUserLoginEvent (@Observes final ObservableUserLoggedInEvent event) {
95                 // event should not be null
96                 if (null == event) {
97                         // Throw NPE
98                         throw new NullPointerException("event is null"); //NOI18N
99                 } else if (event.getLoggedInUser() == null) {
100                         // Throw NPE again
101                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
102                 } else if (event.getLoggedInUser().getUserId() == null) {
103                         // userId is null
104                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
105                 } else if (event.getLoggedInUser().getUserId() < 1) {
106                         // Not avalid id
107                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
108                 }
109
110                 // Is the locale set?
111                 if (event.getLoggedInUser().getUserLocale() instanceof Locale) {
112                         // Get user local
113                         final Locale userLocale = event.getLoggedInUser().getUserLocale();
114
115                         // Change locale
116                         this.changeLocale(userLocale, Boolean.TRUE);
117                 }
118         }
119
120         /**
121          * Event observer for logged-out user
122          * <p>
123          * @param event Event instance
124          */
125         public void afterUserLogoutEvent (@Observes final ObservableUserLogoutEvent event) {
126                 // event should not be null
127                 if (null == event) {
128                         // Throw NPE
129                         throw new NullPointerException("event is null"); //NOI18N
130                 } else if (event.getLoggedOutUser() == null) {
131                         // Throw NPE again
132                         throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
133                 } else if (event.getLoggedOutUser().getUserId() == null) {
134                         // userId is null
135                         throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
136                 } else if (event.getLoggedOutUser().getUserId() < 1) {
137                         // Not avalid id
138                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
139                 }
140
141                 // Clear this bean as well
142                 this.clear();
143         }
144
145         /**
146          * Changes locale in application
147          */
148         public void doChangeLocale () {
149                 // Is locale code set?
150                 if (this.getLocaleCode() == null) {
151                         // Throw NPE
152                         throw new NullPointerException("this.localeCode is null"); //NOI18N
153                 } else if (this.getLocaleCode().isEmpty()) {
154                         // Throw IAE
155                         throw new IllegalArgumentException("this.localeCode is empty"); //NOI18N
156                 }
157
158                 // Default new locale is null, will be handled later
159                 Locale newLocale = null;
160
161                 // Iterate over whole map
162                 for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
163                         final Locale currentLocale = entry.getValue();
164
165                         // Does the language match?
166                         if (Objects.equals(currentLocale.toString(), this.getLocaleCode())) {
167                                 // Is found, take it as request locale
168                                 newLocale = currentLocale;
169                                 break;
170                         }
171                 }
172
173                 // Clear locale code field
174                 this.clear();
175
176                 // Has a matching locale
177                 if (null == newLocale) {
178                         // Throw NPE
179                         throw new NullPointerException("this.localeCode=" + this.getLocaleCode() + " cannot be found."); //NOI18N
180                 }
181
182                 // Then change it
183                 this.changeLocale(newLocale, Boolean.TRUE);
184         }
185
186         /**
187          * Getter for locale
188          * <p>
189          * @return Locale
190          */
191         public Locale getLocale () {
192                 return this.locale;
193         }
194
195         /**
196          * Setter for locale
197          * <p>
198          * @param locale Locale
199          */
200         public void setLocale (final Locale locale) {
201                 this.locale = locale;
202         }
203
204         /**
205          * Getter for localeCode code
206          * <p>
207          * @return Language code
208          */
209         public String getLocaleCode () {
210                 return this.localeCode;
211         }
212
213         /**
214          * Setter for localeCode code
215          * <p>
216          * @param localeCode Language code
217          */
218         public void setLocaleCode (final String localeCode) {
219                 this.localeCode = localeCode;
220         }
221
222         /**
223          * Getter for selectable localizations
224          * <p>
225          * @return Selectable localizations
226          */
227         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
228         public Map<String, Locale> getSupportedLocales () {
229                 // Return full list
230                 return this.supportedLocales;
231         }
232
233         /**
234          * Initializer for this bean
235          */
236         @PostConstruct
237         public void init () {
238                 // Get default locale
239                 final Locale defaultLocale = FacesContext.getCurrentInstance().getApplication().getDefaultLocale();
240
241                 // Add it to list
242                 this.getSupportedLocales().put(defaultLocale.toString(), defaultLocale);
243
244                 // Get iterator from faces context
245                 final Iterator<Locale> iterator = FacesContext.getCurrentInstance().getApplication().getSupportedLocales();
246
247                 // Add all locales
248                 while (iterator.hasNext()) {
249                         // Get next locale
250                         Locale supportedLocale = iterator.next();
251
252                         // Add it
253                         this.getSupportedLocales().put(supportedLocale.toString(), supportedLocale);
254                 }
255
256                 // Get locale instance from request (e.g. browser)
257                 Locale requestLocale = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
258
259                 // Is no country code found?
260                 if (requestLocale.getCountry().isEmpty()) {
261                         // Then try to find one, get language from it
262                         final String language = requestLocale.getLanguage();
263                         Boolean found = Boolean.FALSE;
264
265                         // Iterate over whole map
266                         for (final Map.Entry<String, Locale> entry : this.getSupportedLocales().entrySet()) {
267                                 final String languageCode = entry.getKey();
268                                 final Locale foundLocale = entry.getValue();
269
270                                 // Does the language match?
271                                 if (languageCode.startsWith(language)) {
272                                         // Is found, take it as request locale
273                                         requestLocale = foundLocale;
274                                         found = Boolean.TRUE;
275                                         break;
276                                 }
277                         }
278
279                         // Nothing found?
280                         if (!found) {
281                                 // Then set default locale
282                                 requestLocale = defaultLocale;
283                         }
284                 }
285
286                 // Change locale, may set same back in faces context, but triggers event
287                 this.changeLocale(requestLocale, Boolean.FALSE);
288         }
289
290         /**
291          * Changes current locale in this controller and faces context to given.
292          * This method makes sure that locales with at least language and country
293          * information are being changed to.
294          * <p>
295          * @param locale      New locale to set
296          * @param notifyBeans Whether to notify other beans
297          */
298         private void changeLocale (final Locale locale, final Boolean notifyBeans) {
299                 // Is the locale language_country at least?
300                 if (null == locale) {
301                         // Throw NPE
302                         throw new NullPointerException("locale is null"); //NOI18N
303                 } else if (!locale.toString().contains("_")) { //NOI18N
304                         // Throw IAE
305                         throw new IllegalArgumentException(MessageFormat.format("locale={0} does not have country information set.", locale)); //NOI18N
306                 }
307
308                 // Set locale + code here
309                 this.setLocale(locale);
310                 this.setLocaleCode(locale.toString());
311
312                 // Inform faces context
313                 FacesContext.getCurrentInstance().getViewRoot().setLocale(locale);
314
315                 // Notify other beans?
316                 if (notifyBeans) {
317                         // Fire event
318                         this.localeChangeEvent.fire(new LocaleChangeEvent(locale));
319                 }
320         }
321
322         /**
323          * Clears this bean
324          */
325         private void clear () {
326                 // Clear all fields
327                 this.setLocaleCode(null);
328                 this.setLocale(null);
329         }
330
331 }