]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/BaseJobsController.java
de5289f47f88887857c4e31a6328a3473300a702
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / BaseJobsController.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.jjobs.beans;
18
19 import java.io.Serializable;
20 import java.text.MessageFormat;
21 import java.util.Locale;
22 import java.util.MissingResourceException;
23 import java.util.ResourceBundle;
24 import javax.faces.application.FacesMessage;
25 import javax.faces.context.FacesContext;
26
27 /**
28  * A general controller
29  * <p>
30  * @author Roland Haeder<roland@mxchange.org>
31  */
32 public abstract class BaseJobsController implements Serializable {
33
34         /**
35          * Serial number
36          */
37         private static final long serialVersionUID = 50_837_597_127_567_140L;
38
39         /**
40          * Returns given property key or throws an exception if not found.
41          * <p>
42          * @param parameterKey Property key
43          * <p>
44          * @return Property value
45          * <p>
46          * @throws NullPointerException If given key is not found
47          * @throws NumberFormatException If no number is given in context parameter
48          */
49         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
50                 // Get context parameter
51                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
52
53                 // Return it
54                 return contextValue;
55         }
56
57         /**
58          * Returns given property key or throws an exception if not found.
59          * <p>
60          * @param parameterKey Property key
61          * <p>
62          * @return Property value
63          * <p>
64          * @throws NullPointerException If given key is not found
65          */
66         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
67                 // Get context parameter
68                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
69
70                 // Is it null?
71                 if (null == contextValue) {
72                         // Throw NPE
73                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
74                 }
75
76                 // Return it
77                 return contextValue;
78         }
79
80         /**
81          * Checks whether debug mode is enabled for given controller
82          * <p>
83          * @param controllerName Name of controller
84          * <p>
85          * @return Whether debug mode is enabled
86          */
87         protected boolean isDebugModeEnabled (final String controllerName) {
88                 // Parameters should be valid
89                 if (null == controllerName) {
90                         // Throw NPE
91                         throw new NullPointerException("controllerName is null"); //NOI18N
92                 } else if (controllerName.isEmpty()) {
93                         // Is empty
94                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
95                 }
96
97                 // Try to get context parameter
98                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
99
100                 // Is it set and true?
101                 boolean isEnabled = ((contextParameter instanceof String) && (contextParameter.equals("true"))); //NOI18N
102
103                 // Return it
104                 return isEnabled;
105         }
106
107         /**
108          * Shows a faces message for given causing exception. The message from the
109          * exception is being inserted into the message.
110          * <p>
111          * @param clientId Client id to send message to
112          * @param cause Causing exception
113          */
114         protected void showFacesMessage (final String clientId, final Throwable cause) {
115                 // Trace message
116                 System.out.println(MessageFormat.format("showFacesMessage: clientId={0},cause={1} - CALLED!", clientId, cause));
117
118                 // Get context and add message
119                 this.showFacesMessage(clientId, cause.getMessage());
120         }
121
122         /**
123          * Shows a faces message with given message (i18n) key.
124          * <p>
125          * @param clientId Client id to send message to
126          * @param i18nKey Message key
127          * <p>
128          * @throws NullPointerException If clientId or i18nKey is null
129          * @throws IllegalArgumentException If clientId or i18nKey is empty
130          */
131         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
132                 // Both parameter must be valid
133                 if (null == clientId) {
134                         // Throw NPE
135                         throw new NullPointerException("clientId is null"); //NOI18N
136                 } else if (clientId.isEmpty()) {
137                         // Is empty
138                         throw new IllegalArgumentException("clientId is null"); //NOI18N
139                 } else if (null == i18nKey) {
140                         // Throw NPE
141                         throw new NullPointerException("i18nKey is null"); //NOI18N
142                 } else if (i18nKey.isEmpty()) {
143                         // Is empty
144                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
145                 }
146
147                 // Get current locale
148                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
149
150                 // Get bundle bundle
151                 ResourceBundle bundle = ResourceBundle.getBundle("org.mxchange.localization.bundle", locale);
152
153                 // Default is i18nKey
154                 String message = i18nKey;
155
156                 // Try it
157                 try {
158                         // Get message
159                         message = bundle.getString(i18nKey);
160                 } catch (final MissingResourceException ex) {
161                         // Did not find it, ignored
162                 }
163
164                 // Get context and add message
165                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
166         }
167
168 }