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.profilemode;
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;
29 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
34 * @author Roland Häder<roland@mxchange.org>
36 @Named ("profileModeController")
38 public abstract class JobsProfileModeWebApplicationBean extends BaseJobsController implements JobsProfileModeWebApplicationController {
43 private static final long serialVersionUID = 835_482_364_189L;
48 public JobsProfileModeWebApplicationBean () {
49 // Call super constructor
54 public ProfileMode[] getAllProfileModes () {
56 return ProfileMode.values();
60 * Post-construction method
67 * Returns given property key or throws an exception if not found.
69 * @param parameterKey Property key
71 * @return Property value
73 * @throws NullPointerException If given key is not found
74 * @throws NumberFormatException If no number is given in context parameter
76 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
77 // Get context parameter
78 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
84 * Returns given property key or throws an exception if not found.
86 * @param parameterKey Property key
88 * @return Property value
90 * @throws NullPointerException If given key is not found
92 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
93 // Get context parameter
94 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
96 if (null == contextValue) {
98 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
105 * Checks whether debug mode is enabled for given controller
107 * @param controllerName Name of controller
109 * @return Whether debug mode is enabled
111 protected boolean isDebugModeEnabled (final String controllerName) {
112 // Parameters should be valid
113 if (null == controllerName) {
115 throw new NullPointerException("controllerName is null"); //NOI18N
116 } else if (controllerName.isEmpty()) {
118 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
120 // Try to get context parameter
121 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
122 // Is it set and true?
123 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
129 * Loads resource bundle for given locale. This must be implemented per
130 * project so all projects can still customize their methods. Calling
131 * ResourceBundleloadBundle() in this class means that also the bundle files
132 * must be present here.
134 * @param locale Locale from e.g. FacesContext
136 * @return Initialized and loaded resource bundle
138 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
141 * Shows a faces message for given causing exception. The message from the
142 * exception is being inserted into the message.
144 * @param clientId Client id to send message to
145 * @param cause Causing exception
147 protected void showFacesMessage (final String clientId, final Throwable cause) {
148 // Get context and add message
149 this.showFacesMessage(clientId, cause.getMessage());
153 * Shows a faces message with given message (i18n) key.
155 * @param clientId Client id to send message to
156 * @param i18nKey Message key
158 * @throws NullPointerException If clientId or i18nKey is null
159 * @throws IllegalArgumentException If clientId or i18nKey is empty
161 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
162 // Both parameter must be valid
163 if (null == clientId) {
165 throw new NullPointerException("clientId is null"); //NOI18N
166 } else if (clientId.isEmpty()) {
168 throw new IllegalArgumentException("clientId is null"); //NOI18N
169 } else if (null == i18nKey) {
171 throw new NullPointerException("i18nKey is null"); //NOI18N
172 } else if (i18nKey.isEmpty()) {
174 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
176 // Get current locale
177 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
179 ResourceBundle bundle = this.loadResourceBundle(locale);
180 // Default is i18nKey
181 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
185 message = bundle.getString(i18nKey);
186 } catch (final MissingResourceException ex) {
187 // Did not find it, ignored
189 // Get context and add message
190 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));