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