2 * Copyright (C) 2016 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.pizzaapplication.beans;
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import javax.faces.application.FacesMessage;
22 import javax.faces.context.FacesContext;
25 * A general controller
27 * @author Roland Häder<roland@mxchange.org>
29 public abstract class BasePizzaController implements Serializable {
34 private static final long serialVersionUID = 50_837_597_127_567_140L;
37 * Returns given property key or throws an exception if not found.
39 * @param parameterKey Property key
41 * @return Property value
43 * @throws NullPointerException If given key is not found
44 * @throws NumberFormatException If no number is given in context parameter
46 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
47 // Get context parameter
48 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
55 * Returns given property key or throws an exception if not found.
57 * @param parameterKey Property key
59 * @return Property value
61 * @throws NullPointerException If given key is not found
63 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
64 // Get context parameter
65 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
68 if (null == contextValue) {
70 throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
78 * Checks whether debug mode is enabled for given controller
80 * @param controllerName Name of controller
82 * @return Whether debug mode is enabled
84 protected boolean isDebugModeEnabled (final String controllerName) {
85 // Parameters should be valid
86 if (null == controllerName) {
88 throw new NullPointerException("controllerName is null"); //NOI18N
89 } else if (controllerName.isEmpty()) {
91 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
94 // Try to get context parameter
95 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
97 // Is it set and true?
98 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
105 * Shows a faces message for given causing exception. The message from the
106 * exception is being inserted into the message.
108 * @param clientId Client id to send message to
109 * @param cause Causing exception
111 protected void showFacesMessage (final String clientId, final Throwable cause) {
113 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
115 // Get context and add message
116 this.showFacesMessage(clientId, cause.getMessage());
120 * Shows a faces message with given message.
122 * @param clientId Client id to send message to
123 * @param message Causing exception
125 protected void showFacesMessage (final String clientId, final String message) {
126 // Get context and add message
127 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));