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.MissingResourceException;
22 import java.util.ResourceBundle;
23 import javax.faces.application.FacesMessage;
24 import javax.faces.context.FacesContext;
27 * A general controller
29 * @author Roland Haeder<roland@mxchange.org>
31 public abstract class BaseJobsController implements Serializable {
36 private static final long serialVersionUID = 50_837_597_127_567_140L;
39 * Returns given property key or throws an exception if not found.
41 * @param parameterKey Property key
43 * @return Property value
45 * @throws NullPointerException If given key is not found
46 * @throws NumberFormatException If no number is given in context parameter
48 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
49 // Get context parameter
50 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
57 * Returns given property key or throws an exception if not found.
59 * @param parameterKey Property key
61 * @return Property value
63 * @throws NullPointerException If given key is not found
65 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
66 // Get context parameter
67 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
70 if (null == contextValue) {
72 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
80 * Checks whether debug mode is enabled for given controller
82 * @param controllerName Name of controller
84 * @return Whether debug mode is enabled
86 protected boolean isDebugModeEnabled (final String controllerName) {
87 // Parameters should be valid
88 if (null == controllerName) {
90 throw new NullPointerException("controllerName is null"); //NOI18N
91 } else if (controllerName.isEmpty()) {
93 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
96 // Try to get context parameter
97 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
99 // Is it set and true?
100 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
107 * Shows a faces message for given causing exception. The message from the
108 * exception is being inserted into the message.
110 * @param clientId Client id to send message to
111 * @param cause Causing exception
113 protected void showFacesMessage (final String clientId, final Throwable cause) {
115 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
117 // Get context and add message
118 this.showFacesMessage(clientId, cause.getMessage());
122 * Shows a faces message with given message (i18n) key.
124 * @param clientId Client id to send message to
125 * @param i18nKey Message key
127 * @throws NullPointerException If clientId or i18nKey is null
128 * @throws IllegalArgumentException If clientId or i18nKey is empty
130 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
131 // Both parameter must be valid
132 if (null == clientId) {
134 throw new NullPointerException("clientId is null");
135 } else if (clientId.isEmpty()) {
137 throw new IllegalArgumentException("clientId is null");
138 } else if (null == i18nKey) {
140 throw new NullPointerException("i18nKey is null");
141 } else if (i18nKey.isEmpty()) {
143 throw new IllegalArgumentException("i18nKey is null");
147 FacesContext context = FacesContext.getCurrentInstance();
150 ResourceBundle bundle = context.getApplication().getResourceBundle(context, "msg"); //NOI18N
152 // Default is i18nKey
153 String message = i18nKey;
158 message = bundle.getString(i18nKey);
159 } catch (final MissingResourceException ex) {
160 // Did not find it, ignored
163 // Get context and add message
164 context.addMessage(clientId, new FacesMessage(message));