]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/BasePizzaController.java
Please cherry-pick:
[pizzaservice-war.git] / src / java / org / mxchange / pizzaapplication / beans / BasePizzaController.java
1 /*
2  * Copyright (C) 2016 Roland Häder
3  *
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.
8  *
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.
13  *
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/>.
16  */
17 package org.mxchange.pizzaapplication.beans;
18
19 import java.io.Serializable;
20 import javax.faces.application.FacesMessage;
21 import javax.faces.context.FacesContext;
22
23 /**
24  * A general controller
25  * <p>
26  * @author Roland Häder<roland@mxchange.org>
27  */
28 public abstract class BasePizzaController implements Serializable {
29
30         /**
31          * Serial number
32          */
33         private static final long serialVersionUID = 50_837_597_127_567_140L;
34
35         /**
36          * Returns given property key or throws an exception if not found.
37          * <p>
38          * @param parameterKey Property key
39          * <p>
40          * @return Property value
41          * <p>
42          * @throws NullPointerException If given key is not found
43          * @throws NumberFormatException If no number is given in context parameter
44          */
45         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
46                 // Get context parameter
47                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
48
49                 // Return it
50                 return contextValue;
51         }
52
53         /**
54          * Returns given property key or throws an exception if not found.
55          * <p>
56          * @param parameterKey Property key
57          * <p>
58          * @return Property value
59          * <p>
60          * @throws NullPointerException If given key is not found
61          */
62         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
63                 // Get context parameter
64                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
65
66                 // Is it null?
67                 if (null == contextValue) {
68                         // Throw NPE
69                         throw new NullPointerException("parameterKey=" + parameterKey + " is not set.");
70                 }
71
72                 // Return it
73                 return contextValue;
74         }
75
76         /**
77          * Checks whether debug mode is enabled for given controller
78          * <p>
79          * @param controllerName Name of controller
80          * <p>
81          * @return Whether debug mode is enabled
82          */
83         protected boolean isDebugModeEnabled (final String controllerName) {
84                 // Parameters should be valid
85                 if (null == controllerName) {
86                         // Throw NPE
87                         throw new NullPointerException("controllerName is null"); //NOI18N
88                 } else if (controllerName.isEmpty()) {
89                         // Is empty
90                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
91                 }
92
93                 // Try to get context parameter
94                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
95
96                 // Is it set and true?
97                 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
98
99                 // Return it
100                 return isEnabled;
101         }
102
103         /**
104          * Shows a faces message for given causing exception. The message from the
105          * exception is being inserted into the message.
106          * <p>
107          * @param clientId Client id to send message to
108          * @param cause    Causing exception
109          */
110         protected void showFacesMessage (final String clientId, final Throwable cause) {
111                 // Get context and add message
112                 this.showFacesMessage(clientId, cause.getMessage());
113         }
114
115         /**
116          * Shows a faces message with given message.
117          * <p>
118          * @param clientId Client id to send message to
119          * @param message Causing exception
120          */
121         protected void showFacesMessage (final String clientId, final String message) {
122                 // Get context and add message
123                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
124         }
125
126 }