]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/localization/AddressbookLocalizationSessionBean.java
Continued with generic logout (please cherry-pick this):
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / localization / AddressbookLocalizationSessionBean.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.addressbook.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.addressbook.beans.BaseAddressbookController;
27 import org.mxchange.jusercore.events.login.UserLoggedInEvent;
28 import org.mxchange.jusercore.events.logout.ObserveableUserLogoutEvent;
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 AddressbookLocalizationSessionBean extends BaseAddressbookController implements AddressbookLocalizationSessionController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 158_768_216_759_107L;
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("AddressbookLocalizationSessionBean::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("AddressbookLocalizationSessionBean::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("AddressbookLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
134                 // Set it here and in the JSF context
135                 this.setLocale(loc);
136                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
137
138                 // Log trace message
139                 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
140         }
141
142         @Override
143         public Locale getLocale () {
144                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
145                 return this.locale;
146         }
147
148         @Override
149         public void setLocale (final Locale locale) {
150                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
151                 this.locale = locale;
152         }
153
154         @Override
155         public Locale[] getSelectableLocalizations () {
156                 Locale[] locales = {
157                         Locale.GERMANY,
158                         Locale.US
159                 };
160                 return locales;
161         }
162
163         /**
164          * Initializer for this bean
165          */
166         @PostConstruct
167         public void init () {
168                 // Log trace message
169                 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::init: CALLED!"); //NOI18N
170
171                 // Create locale instance from context
172                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
173
174                 // Log debug message
175                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("AddressbookLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
176                 // Set it here
177                 this.setLocale(loc);
178
179                 // Log trace message
180                 //* NOISY-DEBUG: */ System.out.println("AddressbookLocalizationSessionBean::init: EXIT!"); //NOI18N
181         }
182
183         /**
184          * Clears this bean
185          */
186         private void clear () {
187                 // Clear all fields
188                 this.setLanguage(null);
189                 this.setLocale(null);
190         }
191
192 }