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