]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java
454b2a353d8ee9cdfc8d022c2e32aa5ec14df52b
[jcoreee.git] / src / org / mxchange / jcoreee / bean / faces / BaseFacesBean.java
1 /*
2  * Copyright (C) 2017 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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 package org.mxchange.jcoreee.bean.faces;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.context.FacesContext;
25 import org.mxchange.jcoreee.bean.BaseBean;
26
27 /**
28  * An abstract bean for faces (web) projects.
29  * <p>
30  * @author Roland Häder<roland@mxchange.org>
31  */
32 public abstract class BaseFacesBean extends BaseBean {
33
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 18_605_498_672_261L;
38
39         /**
40          * Protected constructor
41          */
42         protected BaseFacesBean () {
43                 // Call super constructor
44                 super();
45         }
46
47         /**
48          * Returns given property key or throws an exception if not found.
49          * <p>
50          * @param parameterKey Property key
51          * <p>
52          * @return Property value
53          * <p>
54          * @throws NullPointerException If given key is not found
55          * @throws NumberFormatException If no number is given in context parameter
56          */
57         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
58                 // Get context parameter
59                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
60                 // Return it
61                 return contextValue;
62         }
63
64         /**
65          * Returns given property key or throws an exception if not found.
66          * <p>
67          * @param parameterKey Property key
68          * <p>
69          * @return Property value
70          * <p>
71          * @throws NullPointerException If given key is not found
72          */
73         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
74                 // Get context parameter
75                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
76                 // Is it null?
77                 if (null == contextValue) {
78                         // Throw NPE
79                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
80                 }
81                 // Return it
82                 return contextValue;
83         }
84
85         /**
86          * Checks whether debug mode is enabled for given controller
87          * <p>
88          * @param controllerName Name of controller
89          * <p>
90          * @return Whether debug mode is enabled
91          */
92         protected boolean isDebugModeEnabled (final String controllerName) {
93                 // Parameters should be valid
94                 if (null == controllerName) {
95                         // Throw NPE
96                         throw new NullPointerException("controllerName is null"); //NOI18N
97                 } else if (controllerName.isEmpty()) {
98                         // Is empty
99                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
100                 }
101                 // Try to get context parameter
102                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
103                 // Is it set and true?
104                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
105                 // Return it
106                 return isEnabled;
107         }
108
109         /**
110          * Loads resource bundle for given locale. This must be implemented per
111          * project so all projects can still customize their methods. Calling
112          * ResourceBundleloadBundle() in this class means that also the bundle files
113          * must be present here.
114          * <p>
115          * @param locale Locale from e.g. FacesContext
116          * <p>
117          * @return Initialized and loaded resource bundle
118          */
119         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
120
121         /**
122          * Shows a faces message for given causing exception. The message from the
123          * exception is being inserted into the message.
124          * <p>
125          * @param clientId Client id to send message to
126          * @param cause    Causing exception
127          */
128         protected void showFacesMessage (final String clientId, final Throwable cause) {
129                 // Get context and add message
130                 this.showFacesMessage(clientId, cause.getMessage());
131         }
132
133         /**
134          * Shows a faces message with given message (i18n) key.
135          * <p>
136          * @param clientId Client id to send message to
137          * @param i18nKey  Message key
138          * <p>
139          * @throws NullPointerException If clientId or i18nKey is null
140          * @throws IllegalArgumentException If clientId or i18nKey is empty
141          */
142         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
143                 // Both parameter must be valid
144                 if (null == clientId) {
145                         // Throw NPE
146                         throw new NullPointerException("clientId is null"); //NOI18N
147                 } else if (clientId.isEmpty()) {
148                         // Is empty
149                         throw new IllegalArgumentException("clientId is null"); //NOI18N
150                 } else if (null == i18nKey) {
151                         // Throw NPE
152                         throw new NullPointerException("i18nKey is null"); //NOI18N
153                 } else if (i18nKey.isEmpty()) {
154                         // Is empty
155                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
156                 }
157                 // Get current locale
158                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
159                 // Get bundle bundle
160                 ResourceBundle bundle = this.loadResourceBundle(locale);
161                 // Default is i18nKey
162                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
163                 // Try it
164                 try {
165                         // Get message
166                         message = bundle.getString(i18nKey);
167                 } catch (final MissingResourceException ex) {
168                         // Did not find it, ignored
169                 }
170                 // Get context and add message
171                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
172         }
173
174 }