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