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.security.Principal;
21 import java.text.MessageFormat;
22 import java.util.Locale;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
29 * A general controller
31 * @author Roland Haeder<roland@mxchange.org>
33 public abstract class BaseJobsController implements Serializable {
38 private static final long serialVersionUID = 50_837_597_127_567_140L;
41 * Determines principal's name or returns null if no principal (security) is
44 * @return Principal's name or null
46 protected String determinePrincipalName () {
48 Principal userPrincipal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
51 String principalName = null;
53 // Is the principal set?
54 if (userPrincipal instanceof Principal) {
55 // Get principal's name
56 principalName = userPrincipal.getName();
64 * Returns given property key or throws an exception if not found.
66 * @param parameterKey Property key
68 * @return Property value
70 * @throws NullPointerException If given key is not found
71 * @throws NumberFormatException If no number is given in context parameter
73 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
74 // Get context parameter
75 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
82 * Returns given property key or throws an exception if not found.
84 * @param parameterKey Property key
86 * @return Property value
88 * @throws NullPointerException If given key is not found
90 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
91 // Get context parameter
92 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
95 if (null == contextValue) {
97 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
121 // Try to get context parameter
122 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
124 // Is it set and true?
125 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
132 * Shows a faces message for given causing exception. The message from the
133 * exception is being inserted into the message.
135 * @param clientId Client id to send message to
136 * @param cause Causing exception
138 protected void showFacesMessage (final String clientId, final Throwable cause) {
140 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
142 // Get context and add message
143 this.showFacesMessage(clientId, cause.getMessage());
147 * Shows a faces message with given message (i18n) key.
149 * @param clientId Client id to send message to
150 * @param i18nKey Message key
152 * @throws NullPointerException If clientId or i18nKey is null
153 * @throws IllegalArgumentException If clientId or i18nKey is empty
155 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
156 // Both parameter must be valid
157 if (null == clientId) {
159 throw new NullPointerException("clientId is null"); //NOI18N
160 } else if (clientId.isEmpty()) {
162 throw new IllegalArgumentException("clientId is null"); //NOI18N
163 } else if (null == i18nKey) {
165 throw new NullPointerException("i18nKey is null"); //NOI18N
166 } else if (i18nKey.isEmpty()) {
168 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
171 // Get current locale
172 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
175 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
177 // Default is i18nKey
178 String message = i18nKey;
183 message = bundle.getString(i18nKey);
184 } catch (final MissingResourceException ex) {
185 // Did not find it, ignored
188 // Get context and add message
189 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));