2 * Copyright (C) 2016 Roland Haeder
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;
19 import java.io.Serializable;
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;
28 * A general controller
30 * @author Roland Haeder<roland@mxchange.org>
32 public abstract class BaseJobsController implements Serializable {
37 private static final long serialVersionUID = 50_837_597_127_567_140L;
40 * Returns given property key or throws an exception if not found.
42 * @param parameterKey Property key
44 * @return Property value
46 * @throws NullPointerException If given key is not found
47 * @throws NumberFormatException If no number is given in context parameter
49 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
50 // Get context parameter
51 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
58 * Returns given property key or throws an exception if not found.
60 * @param parameterKey Property key
62 * @return Property value
64 * @throws NullPointerException If given key is not found
66 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
67 // Get context parameter
68 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
71 if (null == contextValue) {
73 throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
81 * Checks whether debug mode is enabled for given controller
83 * @param controllerName Name of controller
85 * @return Whether debug mode is enabled
87 protected boolean isDebugModeEnabled (final String controllerName) {
88 // Parameters should be valid
89 if (null == controllerName) {
91 throw new NullPointerException("controllerName is null"); //NOI18N
92 } else if (controllerName.isEmpty()) {
94 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
97 // Try to get context parameter
98 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
100 // Is it set and true?
101 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
108 * Shows a faces message for given causing exception. The message from the
109 * exception is being inserted into the message.
111 * @param clientId Client id to send message to
112 * @param cause Causing exception
114 protected void showFacesMessage (final String clientId, final Throwable cause) {
116 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
118 // Get context and add message
119 this.showFacesMessage(clientId, cause.getMessage());
123 * Shows a faces message with given message (i18n) key.
125 * @param clientId Client id to send message to
126 * @param i18nKey Message key
128 protected void showFacesMessage (final String clientId, final String i18nKey) {
130 FacesContext context = FacesContext.getCurrentInstance();
133 String messageBundleName = context.getApplication().getMessageBundle();
136 Locale locale = context.getViewRoot().getLocale();
138 // Init bundle variable
139 ResourceBundle bundle;
143 bundle = ResourceBundle.getBundle(messageBundleName, locale);
144 } catch (final MissingResourceException ex) {
146 this.showFacesMessage(clientId, ex);
151 String message = bundle.getString(i18nKey);
153 // Get context and add message
154 context.addMessage(clientId, new FacesMessage(message));