]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/localization/JobsLocalizationSessionBean.java
updated own name and resources
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / localization / JobsLocalizationSessionBean.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.jjobs.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.jjobs.beans.BaseJobsController;
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 Häder<roland@mxchange.org>
37  */
38 @Named ("localizationController")
39 @SessionScoped
40 public class JobsLocalizationSessionBean extends BaseJobsController implements JobsLocalizationSessionController {
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 1_867_671_657_629_601_528L;
46
47         /**
48          * Current Locale
49          */
50         private Locale locale;
51
52         @Override
53         public void afterUserLoginEvent (@Observes final UserLoggedInEvent 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 ObserveableUserLogoutEvent 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                 // Trace message
94                 //* NOISY-DEBUG: */ System.out.println("LandingLocalizationSessionBean:afterUserLogin - EXIT!"); //NOI18N
95         }
96
97         @Override
98         public String getLanguage () {
99                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
100                 return this.getLocale().getLanguage().toLowerCase();
101         }
102
103         @Override
104         public void setLanguage (final String language) {
105                 // Log trace message
106                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
107
108                 // Is the language null?
109                 if (null == language) {
110                         // This may sometimes happen, so abort here
111                         return;
112                 }
113
114                 // Language splits
115                 String[] splits = language.split("_"); //NOI18N
116                 if (null == splits[1]) {
117                         splits[1] = ""; //NOI18N
118                 }
119
120                 // Get new locale with upper-case country code
121                 Locale loc = new Locale(splits[0], splits[1]);
122
123                 // Log debug message
124                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
125
126                 // Set it here and in the JSF context
127                 this.setLocale(loc);
128                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
129
130                 // Log trace message
131                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
132         }
133
134         @Override
135         public Locale getLocale () {
136                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
137                 return this.locale;
138         }
139
140         @Override
141         public void setLocale (final Locale locale) {
142                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
143                 this.locale = locale;
144         }
145
146         @Override
147         public Locale[] getSelectableLocalizations () {
148                 Locale[] locales = {
149                         Locale.GERMANY,
150                         Locale.US
151                 };
152                 return locales;
153         }
154
155         /**
156          * Initializer for this bean
157          */
158         @PostConstruct
159         public void init () {
160                 // Log trace message
161                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: CALLED!"); //NOI18N
162
163                 // Create locale instance from context
164                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
165
166                 // Log debug message
167                 //* NOISY-DEBUG: */ System.out.println(MessageFormat.format("JobsLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
168
169                 // Set it here
170                 this.setLocale(loc);
171
172                 // Log trace message
173                 //* NOISY-DEBUG: */ System.out.println("JobsLocalizationSessionBean::init: EXIT!"); //NOI18N
174         }
175
176 }