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