2 * Copyright (C) 2016, 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 Affero General Public License as
6 * published by the Free Software Foundation, either version 3 of the
7 * License, or (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 Affero General Public License for more details.
14 * You should have received a copy of the GNU Affero General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 package org.mxchange.jjobs.beans.features;
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.ApplicationScoped;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27 import javax.inject.Named;
28 import org.mxchange.jjobs.beans.BaseJobsController;
33 * @author Roland Häder<roland@mxchange.org>
35 @Named ("featureController")
37 public abstract class JobsFeatureWebApplicationBean extends BaseJobsController implements JobsFeaturesWebApplicationController {
42 private static final long serialVersionUID = 64_237_512_690_168_674L;
47 public JobsFeatureWebApplicationBean () {
48 // Call super constructor
53 * Post-construction method
60 public boolean isFeatureEnabled (final String feature) {
61 // The parameter must be set
62 if (null == feature) {
64 throw new NullPointerException("feature is null"); //NOI18N
65 } else if (feature.isEmpty()) {
67 throw new IllegalArgumentException("feature is empty"); //NOI18N
70 // Default is not enabled
71 boolean isEnabled = false;
73 // Get value from property
74 String contextParameter = this.getStringContextParameter(String.format("is_feature_%s_enabled", feature)); //NOI18N
75 //System.out.println(MessageFormat.format("isFeatureSet: feature={0},contextParameter[]={1}", feature, Objects.toString(contextParameter))); //NOI18N
77 // Is the context parameter found?
78 if (contextParameter instanceof String) {
80 isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
88 * Returns given property key or throws an exception if not found.
90 * @param parameterKey Property key
92 * @return Property value
94 * @throws NullPointerException If given key is not found
95 * @throws NumberFormatException If no number is given in context parameter
97 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
98 // Get context parameter
99 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
105 * Returns given property key or throws an exception if not found.
107 * @param parameterKey Property key
109 * @return Property value
111 * @throws NullPointerException If given key is not found
113 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
114 // Get context parameter
115 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
117 if (null == contextValue) {
119 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
126 * Checks whether debug mode is enabled for given controller
128 * @param controllerName Name of controller
130 * @return Whether debug mode is enabled
132 protected boolean isDebugModeEnabled (final String controllerName) {
133 // Parameters should be valid
134 if (null == controllerName) {
136 throw new NullPointerException("controllerName is null"); //NOI18N
137 } else if (controllerName.isEmpty()) {
139 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
141 // Try to get context parameter
142 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
143 // Is it set and true?
144 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
150 * Loads resource bundle for given locale. This must be implemented per
151 * project so all projects can still customize their methods. Calling
152 * ResourceBundleloadBundle() in this class means that also the bundle files
153 * must be present here.
155 * @param locale Locale from e.g. FacesContext
157 * @return Initialized and loaded resource bundle
159 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
162 * Shows a faces message for given causing exception. The message from the
163 * exception is being inserted into the message.
165 * @param clientId Client id to send message to
166 * @param cause Causing exception
168 protected void showFacesMessage (final String clientId, final Throwable cause) {
169 // Get context and add message
170 this.showFacesMessage(clientId, cause.getMessage());
174 * Shows a faces message with given message (i18n) key.
176 * @param clientId Client id to send message to
177 * @param i18nKey Message key
179 * @throws NullPointerException If clientId or i18nKey is null
180 * @throws IllegalArgumentException If clientId or i18nKey is empty
182 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
183 // Both parameter must be valid
184 if (null == clientId) {
186 throw new NullPointerException("clientId is null"); //NOI18N
187 } else if (clientId.isEmpty()) {
189 throw new IllegalArgumentException("clientId is null"); //NOI18N
190 } else if (null == i18nKey) {
192 throw new NullPointerException("i18nKey is null"); //NOI18N
193 } else if (i18nKey.isEmpty()) {
195 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
197 // Get current locale
198 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
200 ResourceBundle bundle = this.loadResourceBundle(locale);
201 // Default is i18nKey
202 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
206 message = bundle.getString(i18nKey);
207 } catch (final MissingResourceException ex) {
208 // Did not find it, ignored
210 // Get context and add message
211 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));