]> git.mxchange.org Git - pizzaservice-war.git/blob - src/java/org/mxchange/pizzaapplication/beans/BasePizzaController.java
Renamed controller (please 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          * Checks whether debug mode is enabled for given controller
38          * <p>
39          * @param controllerName Name of controller
40          * <p>
41          * @return Whether debug mode is enabled
42          */
43         protected boolean isDebugModeEnabled (final String controllerName) {
44                 // Parameters should be valid
45                 if (null == controllerName) {
46                         // Throw NPE
47                         throw new NullPointerException("controllerName is null"); //NOI18N
48                 } else if (controllerName.isEmpty()) {
49                         // Is empty
50                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
51                 }
52
53                 // Try to get context parameter
54                 String contextParameter = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
55
56                 // Is it set and true?
57                 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
58
59                 // Return it
60                 return isEnabled;
61         }
62
63         /**
64          * Shows a faces message for given causing exception. The message from the
65          * exception is being inserted into the message.
66          * <p>
67          * @param clientId Client id to send message to
68          * @param cause Causing exception
69          */
70         protected void showFacesMessage (final String clientId, final Throwable cause) {
71                 // Trace message
72                 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
73
74                 // Get context and add message
75                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(cause.getMessage()));
76         }
77
78 }