2 * Copyright (C) 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jcoreee.bean.faces;
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;
28 * An abstract bean for faces (web) projects.
30 * @author Roland Häder<roland@mxchange.org>
32 public abstract class BaseFacesBean extends BaseBean {
37 private static final long serialVersionUID = 18_605_498_672_261L;
40 * Protected constructor
42 protected BaseFacesBean () {
43 // Call super constructor
48 * Returns given property key or throws an exception if not found.
50 * @param parameterKey Property key
52 * @return Property value
54 * @throws NullPointerException If given key is not found
55 * @throws NumberFormatException If no number is given in context parameter
57 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
58 // Get context parameter
59 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
65 * Returns given property key or throws an exception if not found.
67 * @param parameterKey Property key
69 * @return Property value
71 * @throws NullPointerException If given key is not found
73 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
74 // Get context parameter
75 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
77 if (null == contextValue) {
79 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
86 * Checks whether debug mode is enabled for given controller
88 * @param controllerName Name of controller
90 * @return Whether debug mode is enabled
92 protected boolean isDebugModeEnabled (final String controllerName) {
93 // Parameters should be valid
94 if (null == controllerName) {
96 throw new NullPointerException("controllerName is null"); //NOI18N
97 } else if (controllerName.isEmpty()) {
99 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
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;
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.
115 * @param locale Locale from e.g. FacesContext
117 * @return Initialized and loaded resource bundle
119 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
122 * Shows a faces message for given causing exception. The message from the
123 * exception is being inserted into the message.
125 * @param clientId Client id to send message to
126 * @param cause Causing exception
128 protected void showFacesMessage (final String clientId, final Throwable cause) {
129 // Get context and add message
130 this.showFacesMessage(clientId, cause.getMessage());
134 * Shows a faces message with given message (i18n) key.
136 * @param clientId Client id to send message to
137 * @param i18nKey Message key
139 * @throws NullPointerException If clientId or i18nKey is null
140 * @throws IllegalArgumentException If clientId or i18nKey is empty
142 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
143 // Both parameter must be valid
144 if (null == clientId) {
146 throw new NullPointerException("clientId is null"); //NOI18N
147 } else if (clientId.isEmpty()) {
149 throw new IllegalArgumentException("clientId is null"); //NOI18N
150 } else if (null == i18nKey) {
152 throw new NullPointerException("i18nKey is null"); //NOI18N
153 } else if (i18nKey.isEmpty()) {
155 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
157 // Get current locale
158 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
160 ResourceBundle bundle = this.loadResourceBundle(locale);
161 // Default is i18nKey
162 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
166 message = bundle.getString(i18nKey);
167 } catch (final MissingResourceException ex) {
168 // Did not find it, ignored
170 // Get context and add message
171 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));