]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/features/JobsFeatureWebApplicationBean.java
Rewrite continued:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / features / JobsFeatureWebApplicationBean.java
1 /*
2  * Copyright (C) 2016, 2017 Roland Häder
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.features;
18
19 import java.text.MessageFormat;
20 import java.util.Locale;
21 import java.util.MissingResourceException;
22 import java.util.ResourceBundle;
23 import javax.annotation.PostConstruct;
24 import javax.enterprise.context.ApplicationScoped;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27 import javax.inject.Named;
28 import org.mxchange.jjobs.beans.BaseJobsController;
29
30 /**
31  * A feature bean
32  * <p>
33  * @author Roland Häder<roland@mxchange.org>
34  */
35 @Named ("featureController")
36 @ApplicationScoped
37 public abstract class JobsFeatureWebApplicationBean extends BaseJobsController implements JobsFeaturesWebApplicationController {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 64_237_512_690_168_674L;
43
44         /**
45          * Default constructor
46          */
47         public JobsFeatureWebApplicationBean () {
48                 // Call super constructor
49                 super();
50         }
51
52         /**
53          * Post-construction method
54          */
55         @PostConstruct
56         public void init () {
57         }
58
59         @Override
60         public boolean isFeatureEnabled (final String feature) {
61                 // The parameter must be set
62                 if (null == feature) {
63                         // Throw NPE
64                         throw new NullPointerException("feature is null"); //NOI18N
65                 } else if (feature.isEmpty()) {
66                         // Is empty
67                         throw new IllegalArgumentException("feature is empty"); //NOI18N
68                 }
69
70                 // Default is not enabled
71                 boolean isEnabled = false;
72
73                 // Get value from property
74                 String contextParameter = this.getStringContextParameter(String.format("is_feature_%s_enabled", feature)); //NOI18N
75                 //System.out.println(MessageFormat.format("isFeatureSet: feature={0},contextParameter[]={1}", feature, Objects.toString(contextParameter))); //NOI18N
76
77                 // Is the context parameter found?
78                 if (contextParameter instanceof String) {
79                         // Is it set?
80                         isEnabled = (Boolean.parseBoolean(contextParameter) == Boolean.TRUE);
81                 }
82
83                 // Return status
84                 return isEnabled;
85         }
86
87         /**
88          * Returns given property key or throws an exception if not found.
89          * <p>
90          * @param parameterKey Property key
91          * <p>
92          * @return Property value
93          * <p>
94          * @throws NullPointerException If given key is not found
95          * @throws NumberFormatException If no number is given in context parameter
96          */
97         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
98                 // Get context parameter
99                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
100                 // Return it
101                 return contextValue;
102         }
103
104         /**
105          * Returns given property key or throws an exception if not found.
106          * <p>
107          * @param parameterKey Property key
108          * <p>
109          * @return Property value
110          * <p>
111          * @throws NullPointerException If given key is not found
112          */
113         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
114                 // Get context parameter
115                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
116                 // Is it null?
117                 if (null == contextValue) {
118                         // Throw NPE
119                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
120                 }
121                 // Return it
122                 return contextValue;
123         }
124
125         /**
126          * Checks whether debug mode is enabled for given controller
127          * <p>
128          * @param controllerName Name of controller
129          * <p>
130          * @return Whether debug mode is enabled
131          */
132         protected boolean isDebugModeEnabled (final String controllerName) {
133                 // Parameters should be valid
134                 if (null == controllerName) {
135                         // Throw NPE
136                         throw new NullPointerException("controllerName is null"); //NOI18N
137                 } else if (controllerName.isEmpty()) {
138                         // Is empty
139                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
140                 }
141                 // Try to get context parameter
142                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
143                 // Is it set and true?
144                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
145                 // Return it
146                 return isEnabled;
147         }
148
149         /**
150          * Loads resource bundle for given locale. This must be implemented per
151          * project so all projects can still customize their methods. Calling
152          * ResourceBundleloadBundle() in this class means that also the bundle files
153          * must be present here.
154          * <p>
155          * @param locale Locale from e.g. FacesContext
156          * <p>
157          * @return Initialized and loaded resource bundle
158          */
159         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
160
161         /**
162          * Shows a faces message for given causing exception. The message from the
163          * exception is being inserted into the message.
164          * <p>
165          * @param clientId Client id to send message to
166          * @param cause    Causing exception
167          */
168         protected void showFacesMessage (final String clientId, final Throwable cause) {
169                 // Get context and add message
170                 this.showFacesMessage(clientId, cause.getMessage());
171         }
172
173         /**
174          * Shows a faces message with given message (i18n) key.
175          * <p>
176          * @param clientId Client id to send message to
177          * @param i18nKey  Message key
178          * <p>
179          * @throws NullPointerException If clientId or i18nKey is null
180          * @throws IllegalArgumentException If clientId or i18nKey is empty
181          */
182         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
183                 // Both parameter must be valid
184                 if (null == clientId) {
185                         // Throw NPE
186                         throw new NullPointerException("clientId is null"); //NOI18N
187                 } else if (clientId.isEmpty()) {
188                         // Is empty
189                         throw new IllegalArgumentException("clientId is null"); //NOI18N
190                 } else if (null == i18nKey) {
191                         // Throw NPE
192                         throw new NullPointerException("i18nKey is null"); //NOI18N
193                 } else if (i18nKey.isEmpty()) {
194                         // Is empty
195                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
196                 }
197                 // Get current locale
198                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
199                 // Get bundle bundle
200                 ResourceBundle bundle = this.loadResourceBundle(locale);
201                 // Default is i18nKey
202                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
203                 // Try it
204                 try {
205                         // Get message
206                         message = bundle.getString(i18nKey);
207                 } catch (final MissingResourceException ex) {
208                         // Did not find it, ignored
209                 }
210                 // Get context and add message
211                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
212         }
213
214 }