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 javax.faces.application.FacesMessage;
21 import javax.faces.context.FacesContext;
24 * A general controller
26 * @author Roland Häder<roland@mxchange.org>
28 public abstract class BasePizzaController implements Serializable {
33 private static final long serialVersionUID = 50_837_597_127_567_140L;
36 * Returns given property key or throws an exception if not found.
38 * @param parameterKey Property key
40 * @return Property value
42 * @throws NullPointerException If given key is not found
43 * @throws NumberFormatException If no number is given in context parameter
45 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
46 // Get context parameter
47 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
54 * Returns given property key or throws an exception if not found.
56 * @param parameterKey Property key
58 * @return Property value
60 * @throws NullPointerException If given key is not found
62 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
63 // Get context parameter
64 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
67 if (null == contextValue) {
69 throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
77 * Checks whether debug mode is enabled for given controller
79 * @param controllerName Name of controller
81 * @return Whether debug mode is enabled
83 protected boolean isDebugModeEnabled (final String controllerName) {
84 // Parameters should be valid
85 if (null == controllerName) {
87 throw new NullPointerException("controllerName is null"); //NOI18N
88 } else if (controllerName.isEmpty()) {
90 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
93 // Try to get context parameter
94 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
96 // Is it set and true?
97 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
104 * Shows a faces message for given causing exception. The message from the
105 * exception is being inserted into the message.
107 * @param clientId Client id to send message to
108 * @param cause Causing exception
110 protected void showFacesMessage (final String clientId, final Throwable cause) {
111 // Get context and add message
112 this.showFacesMessage(clientId, cause.getMessage());
116 * Shows a faces message with given message.
118 * @param clientId Client id to send message to
119 * @param message Causing exception
121 protected void showFacesMessage (final String clientId, final String message) {
122 // Get context and add message
123 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));