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