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