]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java
Please rename:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / localization / PizzaLocalizationSessionBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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.jusercore.events.login.UserLoggedInEvent;
27 import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
28 import org.mxchange.pizzaapplication.beans.BasePizzaController;
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 Haeder<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         @Override
53         public void afterUserLogin (@Observes final UserLoggedInEvent event) {
54                 // Trace message
55                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("LandingLocalizationSessionBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
56
57                 // event should not be null
58                 if (null == event) {
59                         // Throw NPE
60                         throw new NullPointerException("event is null"); //NOI18N
61                 } else if (event.getLoggedInUser() == null) {
62                         // Throw NPE again
63                         throw new NullPointerException("event.loggedInUser is null"); //NOI18N
64                 } else if (event.getLoggedInUser().getUserId() == null) {
65                         // userId is null
66                         throw new NullPointerException("event.loggedInUser.userId is null"); //NOI18N
67                 } else if (event.getLoggedInUser().getUserId() < 1) {
68                         // Not avalid id
69                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedInUser(), event.getLoggedInUser().getUserId())); //NOI18N
70                 }
71
72                 // Set locale here
73                 this.setLocale(event.getLoggedInUser().getUserLocale());
74
75                 // Trace message
76                 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
77         }
78
79         @Override
80         public void afterUserLogout (@Observes final ObserveableUserLogoutEvent event) {
81                 // Trace message
82                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("ReportsLocalizationSessionBean:afterUserLogin: event={0} - CALLED!", event)); //NOI18N
83
84                 // event should not be null
85                 if (null == event) {
86                         // Throw NPE
87                         throw new NullPointerException("event is null"); //NOI18N
88                 } else if (event.getLoggedOutUser() == null) {
89                         // Throw NPE again
90                         throw new NullPointerException("event.loggedOutUser is null"); //NOI18N
91                 } else if (event.getLoggedOutUser().getUserId() == null) {
92                         // userId is null
93                         throw new NullPointerException("event.loggedOutUser.userId is null"); //NOI18N
94                 } else if (event.getLoggedOutUser().getUserId() < 1) {
95                         // Not avalid id
96                         throw new IllegalArgumentException(MessageFormat.format("userId of user={0} is not valid: {1}", event.getLoggedOutUser(), event.getLoggedOutUser().getUserId())); //NOI18N
97                 }
98
99                 // Clear this bean as well
100                 this.clear();
101
102                 // Trace message
103                 //* NOISY-DEBUG: */ System.out.println("ReportsLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
104         }
105
106         @Override
107         public String getLanguage () {
108                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
109                 return this.getLocale().getLanguage().toLowerCase();
110         }
111
112         @Override
113         public void setLanguage (final String language) {
114                 // Log trace message
115                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
116
117                 // Is the language null?
118                 if (null == language) {
119                         // This may sometimes happen, so abort here
120                         return;
121                 }
122
123                 // Language splits
124                 String[] splits = language.split("_"); //NOI18N
125                 if (null == splits[1]) {
126                         splits[1] = ""; //NOI18N
127                 }
128
129                 // Get new locale with upper-case country code
130                 Locale loc = new Locale(splits[0], splits[1]);
131
132                 // Log debug message
133                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
134
135                 // Set it here and in the JSF context
136                 this.setLocale(loc);
137                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
138
139                 // Log trace message
140                 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
141         }
142
143         @Override
144         public Locale getLocale () {
145                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
146                 return this.locale;
147         }
148
149         @Override
150         public void setLocale (final Locale locale) {
151                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
152                 this.locale = locale;
153         }
154
155         @Override
156         public Locale[] getSelectableLocalizations () {
157                 Locale[] locales = {
158                         Locale.GERMANY,
159                         Locale.US
160                 };
161                 return locales;
162         }
163
164         /**
165          * Initializer for this bean
166          */
167         @PostConstruct
168         public void init () {
169                 // Log trace message
170                 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::init: CALLED!"); //NOI18N
171
172                 // Create locale instance from context
173                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
174
175                 // Log debug message
176                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("PizzaLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
177
178                 // Set it here
179                 this.setLocale(loc);
180
181                 // Log trace message
182                 //* NOISY-DEBUG: */ System.out.println("PizzaLocalizationSessionBean::init: EXIT!"); //NOI18N
183         }
184
185         /**
186          * Clears this bean
187          */
188         private void clear () {
189                 // Clear all fields
190                 this.setLanguage(null);
191                 this.setLocale(null);
192         }
193
194 }