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(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
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 * @throws NullPointerException If clientId or i18nKey is null
129 * @throws IllegalArgumentException If clientId or i18nKey is empty
131 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
132 // Both parameter must be valid
133 if (null == clientId) {
135 throw new NullPointerException("clientId is null"); //NOI18N
136 } else if (clientId.isEmpty()) {
138 throw new IllegalArgumentException("clientId is null"); //NOI18N
139 } else if (null == i18nKey) {
141 throw new NullPointerException("i18nKey is null"); //NOI18N
142 } else if (i18nKey.isEmpty()) {
144 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
147 // Get current locale
148 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
151 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
153 // Default is i18nKey
154 String message = i18nKey;
159 message = bundle.getString(i18nKey);
160 } catch (final MissingResourceException ex) {
161 // Did not find it, ignored
164 // Get context and add message
165 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));