]> git.mxchange.org Git - jcoreee.git/blob - src/org/mxchange/jcoreee/bean/faces/BaseFacesBean.java
Continued:
[jcoreee.git] / src / org / mxchange / jcoreee / bean / faces / BaseFacesBean.java
1 /*
2  * Copyright (C) 2017 - 2022 Free Software Foundation
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.io.Serializable;
20 import java.math.BigDecimal;
21 import java.security.Principal;
22 import java.text.MessageFormat;
23 import java.util.ArrayList;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.MissingResourceException;
27 import java.util.ResourceBundle;
28 import javax.faces.application.FacesMessage;
29 import javax.faces.context.FacesContext;
30
31 /**
32  * An abstract bean for faces (web) projects.
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 public abstract class BaseFacesBean implements Serializable {
37
38         /**
39          * Loaded resource bundles ("cached")
40          */
41         private static final List<ResourceBundle> RESOURCE_BUNDLES;
42
43         /**
44          * Serial number
45          */
46         private static final long serialVersionUID = 18_605_498_672_261L;
47
48         /**
49          * Static initializer
50          */
51         static {
52                 // Init resource bundle list
53                 RESOURCE_BUNDLES = new ArrayList<>(3);
54         }
55
56         /**
57          * Removes all bundles from web application. Typically you want to invoke
58          * this method in a ServletContextListener implementation on the
59          * contextDestroyed() method.
60          */
61         public static void removeBundles () {
62                 // Clear bundles
63                 RESOURCE_BUNDLES.clear();
64         }
65
66         /**
67          * Getter for resource bundle list
68          * <p>
69          * @return Resource bundle list
70          */
71         @SuppressWarnings ("ReturnOfCollectionOrArrayField")
72         protected static List<ResourceBundle> getBundles () {
73                 return RESOURCE_BUNDLES;
74         }
75
76         /**
77          * Protected constructor
78          */
79         protected BaseFacesBean () {
80         }
81
82         /**
83          * A filter for prices
84          * <p>
85          * @param value  Actual value to check
86          * @param filter Filter value to check against {@code value}
87          * @param locale Locale
88          * <p>
89          * @return Whether price ({@code value}) is at least {@code filter}
90          *
91          * @see https://www.primefaces.org/showcase/ui/data/datatable/filter.xhtml
92          */
93         @SuppressWarnings ("unchecked")
94         public boolean filterByPrice (final Object value, final Object filter, final Locale locale) {
95                 // Get trimmed string or NULL
96                 final String filterText = (filter == null) ? null : filter.toString().trim();
97
98                 // Is null or empty?
99                 if (filterText == null || filterText.isEmpty()) {
100                         // Then return TRUE
101                         return true;
102                 }
103
104                 // Is value NULL or not castable?
105                 if (value == null) {
106                         // Return FALSE
107                         return false;
108                 } else if (!(value instanceof Comparable)) {
109                         // Not castable
110                         throw new ClassCastException("value is not instance of Comparable"); //NOI18N
111                 }
112
113                 // Do the cast and compare
114                 return ((Comparable<BigDecimal>) value).compareTo(BigDecimal.valueOf(Double.valueOf(filterText))) >= 0;
115         }
116
117         /**
118          * Determines principal's name or returns null if no principal (security) is
119          * set.
120          * <p>
121          * @return Principal's name or null
122          */
123         protected String determinePrincipalName () {
124                 // Get principal
125                 final Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
126
127                 // Init with null
128                 String principalName = null;
129
130                 // Is the principal set?
131                 if (userPrincipal instanceof Principal) {
132                         // Get principal's name
133                         principalName = userPrincipal.getName();
134                 }
135
136                 // Return it
137                 return principalName;
138         }
139
140         /**
141          * Returns given property key or throws an exception if not found.
142          * <p>
143          * @param parameterKey Property key
144          * <p>
145          * @return Property value
146          */
147         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
148                 // Validate parameter
149                 if (null == parameterKey) {
150                         // Throw IAE
151                         throw new NullPointerException("parameterKey is null"); //NOI18N
152                 } else if (parameterKey.isEmpty()) {
153                         // Throw IAE
154                         throw new IllegalArgumentException("parameterKey is empty"); //NOI18N
155                 }
156
157                 // Get context parameter
158                 final Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
159
160                 // Return it
161                 return contextValue;
162         }
163
164         /**
165          * Returns a message based on given i18nKey or puts it into three question
166          * marks each side when not found.
167          * <p>
168          * @param i18nKey I18n key
169          * <p>
170          * @return Localized message
171          * <p>
172          * @throws NullPointerException If the parameter is null
173          * @throws IllegalArgumentException If the parameter is empty
174          */
175         protected String getMessageFromBundle (final String i18nKey) {
176                 // Validate parameter
177                 if (null == i18nKey) {
178                         // Throw NPE
179                         throw new NullPointerException("i18nKey is null"); //NOI18N
180                 } else if (i18nKey.isEmpty()) {
181                         // Is empty
182                         throw new IllegalArgumentException("i18nKey is empty"); //NOI18N
183                 }
184
185                 // Get current locale
186                 final Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
187
188                 // Get bundle bundle
189                 this.loadResourceBundles(locale);
190
191                 // Default is i18nKey
192                 String message = MessageFormat.format("???{0}???", i18nKey); //NOI18N
193
194                 // Loop through all
195                 for (final ResourceBundle bundle : getBundles()) {
196                         // Try it
197                         try {
198                                 // Get message
199                                 message = bundle.getString(i18nKey);
200
201                                 // Skip further iterations
202                                 break;
203                         } catch (final MissingResourceException ex) {
204                                 // Did not find it, ignored
205                         }
206                 }
207
208                 // Return it
209                 return message;
210         }
211
212         /**
213          * Returns given property key or throws an exception if not found.
214          * <p>
215          * @param parameterKey Property key
216          * <p>
217          * @return Property value
218          * <p>
219          * @throws NullPointerException If given key is NULL or not found
220          * @throws IllegalArgumentException If given key is empty
221          */
222         protected String getStringContextParameter (final String parameterKey) throws NullPointerException, IllegalArgumentException {
223                 // Is the parameter valid?
224                 if (null == parameterKey) {
225                         // Throw NPE
226                         throw new NullPointerException("parameterKey is null"); //NOI18N
227                 } else if (parameterKey.isEmpty()) {
228                         // Throw IAE
229                         throw new IllegalArgumentException("parameterKey is empty"); //NOI18N
230                 }
231
232                 // Get context parameter
233                 final String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
234
235                 // Is it null?
236                 if (null == contextValue) {
237                         // Throw NPE
238                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
239                 }
240
241                 // Return it
242                 return contextValue;
243         }
244
245         /**
246          * Checks whether debug mode is enabled for given controller
247          * <p>
248          * @param controllerName Name of controller
249          * <p>
250          * @return Whether debug mode is enabled
251          */
252         protected boolean isDebugModeEnabled (final String controllerName) {
253                 // Parameters should be valid
254                 if (null == controllerName) {
255                         // Throw NPE
256                         throw new NullPointerException("controllerName is null"); //NOI18N
257                 } else if (controllerName.isEmpty()) {
258                         // Is empty
259                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
260                 }
261
262                 // Try to get context parameter
263                 final String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
264
265                 // Return it
266                 return (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
267         }
268
269         /**
270          * Loads resource bundles for given locale. This must be implemented per
271          * project so all projects can still customize their methods. Calling
272          * ResourceBundleloadBundle() in this class means that also the bundle files
273          * must be present here.
274          * <p>
275          * @param locale Locale from e.g. FacesContext
276          */
277         protected abstract void loadResourceBundles (final Locale locale);
278
279         /**
280          * Shows a faces message for given causing exception. The message from the
281          * exception is being inserted into the message.
282          * <p>
283          * @param clientId Client id to send message to
284          * @param cause    Causing exception
285          * @param severity Severity
286          */
287         protected void showFacesMessage (final String clientId, final Throwable cause, final FacesMessage.Severity severity) {
288                 // Both parameter must be valid
289                 if (null == clientId) {
290                         // Throw NPE
291                         throw new NullPointerException("clientId is null"); //NOI18N
292                 } else if (clientId.isEmpty()) {
293                         // Is empty
294                         throw new IllegalArgumentException("clientId is null"); //NOI18N
295                 } else if (null == cause) {
296                         // Throw NPE
297                         throw new NullPointerException("cause is null"); //NOI18N
298                 } else if (null == severity) {
299                         // Throw NPE
300                         throw new NullPointerException("severity is null"); //NOI18N
301                 }
302
303                 // Get context and add message
304                 this.showFacesMessage(clientId, cause.getMessage(), severity);
305         }
306
307         /**
308          * Shows a faces message with given message (i18n) key.
309          * <p>
310          * @param clientId Client id to send message to
311          * @param i18nKey  Message key
312          * @param severity Severity
313          * <p>
314          * @throws NullPointerException If clientId or i18nKey is null
315          * @throws IllegalArgumentException If clientId or i18nKey is empty
316          */
317         protected void showFacesMessage (final String clientId, final String i18nKey, final FacesMessage.Severity severity) throws NullPointerException, IllegalArgumentException {
318                 // Both parameter must be valid
319                 if (null == clientId) {
320                         // Throw NPE
321                         throw new NullPointerException("clientId is null"); //NOI18N
322                 } else if (clientId.isEmpty()) {
323                         // Is empty
324                         throw new IllegalArgumentException("clientId is null"); //NOI18N
325                 } else if (null == i18nKey) {
326                         // Throw NPE
327                         throw new NullPointerException("i18nKey is null"); //NOI18N
328                 } else if (i18nKey.isEmpty()) {
329                         // Is empty
330                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
331                 } else if (null == severity) {
332                         // Throw NPE
333                         throw new NullPointerException("severity is null"); //NOI18N
334                 }
335
336                 // Get message from bundle
337                 final String message = this.getMessageFromBundle(i18nKey);
338
339                 // Get context and add message
340                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(severity, message, message));
341         }
342
343 }