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.security.Principal;
20 import java.text.MessageFormat;
21 import java.util.Locale;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.context.FacesContext;
26 import org.mxchange.jcoreee.bean.BaseBean;
29 * An abstract bean for faces (web) projects.
31 * @author Roland Häder<roland@mxchange.org>
33 public abstract class BaseFacesBean extends BaseBean {
38 private static final long serialVersionUID = 18_605_498_672_261L;
41 * Protected constructor
43 protected BaseFacesBean () {
44 // Call super constructor
49 * Determines principal's name or returns null if no principal (security) is
52 * @return Principal's name or null
54 protected String determinePrincipalName () {
56 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
59 String principalName = null;
61 // Is the principal set?
62 if (userPrincipal instanceof Principal) {
63 // Get principal's name
64 principalName = userPrincipal.getName();
72 * Returns given property key or throws an exception if not found.
74 * @param parameterKey Property key
76 * @return Property value
78 * @throws NullPointerException If given key is not found
79 * @throws NumberFormatException If no number is given in context parameter
81 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
82 // Get context parameter
83 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
89 * Returns given property key or throws an exception if not found.
91 * @param parameterKey Property key
93 * @return Property value
95 * @throws NullPointerException If given key is not found
97 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
98 // Get context parameter
99 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
101 if (null == contextValue) {
103 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
110 * Checks whether debug mode is enabled for given controller
112 * @param controllerName Name of controller
114 * @return Whether debug mode is enabled
116 protected boolean isDebugModeEnabled (final String controllerName) {
117 // Parameters should be valid
118 if (null == controllerName) {
120 throw new NullPointerException("controllerName is null"); //NOI18N
121 } else if (controllerName.isEmpty()) {
123 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
125 // Try to get context parameter
126 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
127 // Is it set and true?
128 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
134 * Loads resource bundle for given locale. This must be implemented per
135 * project so all projects can still customize their methods. Calling
136 * ResourceBundleloadBundle() in this class means that also the bundle files
137 * must be present here.
139 * @param locale Locale from e.g. FacesContext
141 * @return Initialized and loaded resource bundle
143 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
146 * Shows a faces message for given causing exception. The message from the
147 * exception is being inserted into the message.
149 * @param clientId Client id to send message to
150 * @param cause Causing exception
152 protected void showFacesMessage (final String clientId, final Throwable cause) {
153 // Get context and add message
154 this.showFacesMessage(clientId, cause.getMessage());
158 * Shows a faces message with given message (i18n) key.
160 * @param clientId Client id to send message to
161 * @param i18nKey Message key
163 * @throws NullPointerException If clientId or i18nKey is null
164 * @throws IllegalArgumentException If clientId or i18nKey is empty
166 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
167 // Both parameter must be valid
168 if (null == clientId) {
170 throw new NullPointerException("clientId is null"); //NOI18N
171 } else if (clientId.isEmpty()) {
173 throw new IllegalArgumentException("clientId is null"); //NOI18N
174 } else if (null == i18nKey) {
176 throw new NullPointerException("i18nKey is null"); //NOI18N
177 } else if (i18nKey.isEmpty()) {
179 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
181 // Get current locale
182 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
184 ResourceBundle bundle = this.loadResourceBundle(locale);
185 // Default is i18nKey
186 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
190 message = bundle.getString(i18nKey);
191 } catch (final MissingResourceException ex) {
192 // Did not find it, ignored
194 // Get context and add message
195 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));