]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java
f9ab2b48df663b268f857f1f8062c11b3d15b65e
[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.security.Principal;
20 import java.text.MessageFormat;
21 import java.util.ArrayList;
22 import java.util.List;
23 import java.util.Locale;
24 import java.util.MissingResourceException;
25 import java.util.ResourceBundle;
26 import javax.faces.application.FacesMessage;
27 import javax.faces.context.FacesContext;
28 import org.mxchange.jcoreee.bean.BaseBean;
29
30 /**
31  * An abstract bean for faces (web) projects.
32  * <p>
33  * @author Roland Häder<roland@mxchange.org>
34  */
35 public abstract class BaseFacesBean extends BaseBean {
36
37         /**
38          * Loaded resource bundles ("cached")
39          */
40         private static final List<ResourceBundle> RESOURCE_BUNDLES;
41
42         /**
43          * Serial number
44          */
45         private static final long serialVersionUID = 18_605_498_672_261L;
46
47         /**
48          * Static initializer
49          */
50         static {
51                 // Init resource bundle list
52                 RESOURCE_BUNDLES = new ArrayList<>(3);
53         }
54
55         /**
56          * Removes all bundles from web application. Typically you want to invoke
57          * this method in a ServletContextListener implemetation on the
58          * contextDestroyed() method.
59          */
60         public static void removeBundles () {
61                 // Clear bundles
62                 RESOURCE_BUNDLES.clear();
63         }
64
65         /**
66          * Getter for resource bundle list
67          * <p>
68          * @return Resource bundle list
69          */
70         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
71         protected static List<ResourceBundle> getBundles () {
72                 return RESOURCE_BUNDLES;
73         }
74
75         /**
76          * Protected constructor
77          */
78         protected BaseFacesBean () {
79                 // Call super constructor
80                 super();
81         }
82
83         /**
84          * Determines principal's name or returns null if no principal (security) is
85          * set.
86          * <p>
87          * @return Principal's name or null
88          */
89         protected String determinePrincipalName () {
90                 // Get principal
91                 final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
92
93                 // Init with null
94                 String principalName = null;
95
96                 // Is the principal set?
97                 if (userPrincipal instanceof Principal) {
98                         // Get principal's name
99                         principalName = userPrincipal.getName();
100                 }
101
102                 // Return it
103                 return principalName;
104         }
105
106         /**
107          * Returns given property key or throws an exception if not found.
108          * <p>
109          * @param parameterKey Property key
110          * <p>
111          * @return Property value
112          */
113         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
114                 // Get context parameter
115                 final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
116
117                 // Return it
118                 return contextValue;
119         }
120
121         /**
122          * Returns a message based on given i18nKey or puts it into three question
123          * marks each side when not found.
124          * <p>
125          * @param i18nKey I18n key
126          * <p>
127          * @return Localized message
128          * <p>
129          * @throws NullPointerException If the parameter is null
130          * @throws IllegalArgumentException If the parameter is empty
131          */
132         protected String getMessageFromBundle (final String i18nKey) {
133                 // Validate parameter
134                 if (null == i18nKey) {
135                         // Throw NPE
136                         throw new NullPointerException("i18nKey is null"); //NOI18N
137                 } else if (i18nKey.isEmpty()) {
138                         // Is empty
139                         throw new IllegalArgumentException("i18nKey is empty"); //NOI18N
140                 }
141
142                 // Get current locale
143                 final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
144
145                 // Get bundle bundle
146                 this.loadResourceBundles(locale);
147
148                 // Default is i18nKey
149                 String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N
150
151                 // Loop through all
152                 for (final ResourceBundle bundle : getBundles()) {
153                         // Found message?
154                         // Try it
155                         try {
156                                 // Get message
157                                 message = bundle.getString(i18nKey);
158                                 break;
159                         } catch (final MissingResourceException ex) {
160                                 // Did not find it, ignored
161                         }
162                 }
163
164                 // Return it
165                 return message;
166         }
167
168         /**
169          * Returns given property key or throws an exception if not found.
170          * <p>
171          * @param parameterKey Property key
172          * <p>
173          * @return Property value
174          * <p>
175          * @throws NullPointerException If given key is not found
176          */
177         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
178                 // Get context parameter
179                 final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
180
181                 // Is it null?
182                 if (null == contextValue) {
183                         // Throw NPE
184                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
185                 }
186
187                 // Return it
188                 return contextValue;
189         }
190
191         /**
192          * Checks whether debug mode is enabled for given controller
193          * <p>
194          * @param controllerName Name of controller
195          * <p>
196          * @return Whether debug mode is enabled
197          */
198         protected boolean isDebugModeEnabled (final String controllerName) {
199                 // Parameters should be valid
200                 if (null == controllerName) {
201                         // Throw NPE
202                         throw new NullPointerException("controllerName is null"); //NOI18N
203                 } else if (controllerName.isEmpty()) {
204                         // Is empty
205                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
206                 }
207
208                 // Try to get context parameter
209                 final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
210
211                 // Is it set and true?
212                 final boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
213
214                 // Return it
215                 return isEnabled;
216         }
217
218         /**
219          * Loads resource bundles for given locale. This must be implemented per
220          * project so all projects can still customize their methods. Calling
221          * ResourceBundleloadBundle() in this class means that also the bundle files
222          * must be present here.
223          * <p>
224          * @param locale Locale from e.g. FacesContext
225          */
226         protected abstract void loadResourceBundles (final Locale locale);
227
228         /**
229          * Shows a faces message for given causing exception. The message from the
230          * exception is being inserted into the message.
231          * <p>
232          * @param clientId Client id to send message to
233          * @param cause    Causing exception
234          */
235         protected void showFacesMessage (final String clientId, final Throwable cause) {
236                 // Get context and add message
237                 this.showFacesMessage(clientId, cause.getMessage());
238         }
239
240         /**
241          * Shows a faces message with given message (i18n) key.
242          * <p>
243          * @param clientId Client id to send message to
244          * @param i18nKey  Message key
245          * <p>
246          * @throws NullPointerException If clientId or i18nKey is null
247          * @throws IllegalArgumentException If clientId or i18nKey is empty
248          */
249         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
250                 // Both parameter must be valid
251                 if (null == clientId) {
252                         // Throw NPE
253                         throw new NullPointerException("clientId is null"); //NOI18N
254                 } else if (clientId.isEmpty()) {
255                         // Is empty
256                         throw new IllegalArgumentException("clientId is null"); //NOI18N
257                 } else if (null == i18nKey) {
258                         // Throw NPE
259                         throw new NullPointerException("i18nKey is null"); //NOI18N
260                 } else if (i18nKey.isEmpty()) {
261                         // Is empty
262                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
263                 }
264
265                 // Get message from bundle
266                 final String message = this.getMessageFromBundle(i18nKey);
267
268                 // Get context and add message
269                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
270         }
271
272 }