]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/localization/PizzaLocalizationSessionBean.java
Prevent NPE
[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.util.Locale;
20 import javax.annotation.PostConstruct;
21 import javax.faces.bean.SessionScoped;
22 import javax.faces.context.FacesContext;
23 import javax.inject.Named;
24 import org.mxchange.jcoreee.database.BaseDatabaseBean;
25
26 /**
27  * A session bean for handling localization/internationalization changes. This
28  * class is based on an example at [1] from mkyong.
29  * <p>
30  * 1: http://www.mkyong.com/jsf2/jsf-2-internationalization-example/
31  * <p>
32  * @author Roland Haeder<roland@mxchange.org>
33  */
34 @Named ("localizationController")
35 @SessionScoped
36 public class PizzaLocalizationSessionBean extends BaseDatabaseBean implements PizzaLocalizationSessionController {
37
38         /**
39          * Serial number
40          */
41         private static final long serialVersionUID = 639_184_671_562_195L;
42
43         /**
44          * Current Locale
45          */
46         private Locale locale;
47
48         @Override
49         public String getLanguage () {
50                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("PizzaLocalizationSessionBean::getLanguage(): locale.language={0} - EXIT!", this.getLocale().getLanguage())); //NOI18N
51                 return this.getLocale().getLanguage().toLowerCase();
52         }
53
54         @Override
55         public void setLanguage (final String language) {
56                 // Log trace message
57                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: language={0} - CALLED!", language)); //NOI18N
58
59                 // Is the language null?
60                 if (null == language) {
61                         // This may sometimes happen, so abort here
62                         return;
63                 }
64
65                 // Language splits
66                 String[] splits = language.split("_"); //NOI18N
67                 if (null == splits[1]) {
68                         splits[1] = ""; //NOI18N
69                 }
70
71                 // Get new locale with upper-case country code
72                 Locale loc = new Locale(splits[0], splits[1]);
73
74                 // Log debug message
75                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("PizzaLocalizationSessionBean::setLanguage: loc={0}", loc)); //NOI18N
76
77                 // Set it here and in the JSF context
78                 this.setLocale(loc);
79                 FacesContext.getCurrentInstance().getViewRoot().setLocale(loc);
80
81                 // Log trace message
82                 //this.getLoggerBeanLocal().logTrace("PizzaLocalizationSessionBean::setLanguage: EXIT!"); //NOI18N
83         }
84
85         @Override
86         public Locale getLocale () {
87                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("PizzaLocalizationSessionBean::getLocale(): locale={0} - EXIT!", this.locale)); //NOI18N
88                 return this.locale;
89         }
90
91         @Override
92         public void setLocale (final Locale locale) {
93                 //this.getLoggerBeanLocal().logTrace(MessageFormat.format("PizzaLocalizationSessionBean::setLocale(): locale={0} - CALLED!", locale)); //NOI18N
94                 this.locale = locale;
95         }
96
97         @Override
98         public Locale[] getSelectableLocalizations () {
99                 Locale[] locales = {
100                         Locale.GERMANY,
101                         Locale.US
102                 };
103                 return locales;
104         }
105
106         /**
107          * Initializer for this bean
108          */
109         @PostConstruct
110         public void init () {
111                 // Log trace message
112                 //this.getLoggerBeanLocal().logTrace("PizzaLocalizationSessionBean::init: CALLED!"); //NOI18N
113
114                 // Create locale instance from context
115                 Locale loc = FacesContext.getCurrentInstance().getExternalContext().getRequestLocale();
116
117                 // Log debug message
118                 //this.getLoggerBeanLocal().logDebug(MessageFormat.format("PizzaLocalizationSessionBean::init: loc={0}", loc)); //NOI18N
119
120                 // Set it here
121                 this.setLocale(loc);
122
123                 // Log trace message
124                 //this.getLoggerBeanLocal().logTrace("PizzaLocalizationSessionBean::init: EXIT!"); //NOI18N
125         }
126
127 }