]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/profilemode/JobsProfileModeWebApplicationBean.java
Rewrite continued:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / profilemode / JobsProfileModeWebApplicationBean.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.profilemode;
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 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
30
31 /**
32  * A profile mode bean
33  * <p>
34  * @author Roland Häder<roland@mxchange.org>
35  */
36 @Named ("profileModeController")
37 @ApplicationScoped
38 public abstract class JobsProfileModeWebApplicationBean extends BaseJobsController implements JobsProfileModeWebApplicationController {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 835_482_364_189L;
44
45         /**
46          * Default constructor
47          */
48         public JobsProfileModeWebApplicationBean () {
49                 // Call super constructor
50                 super();
51         }
52
53         @Override
54         public ProfileMode[] getAllProfileModes () {
55                 // Return it
56                 return ProfileMode.values();
57         }
58
59         /**
60          * Post-construction method
61          */
62         @PostConstruct
63         public void init () {
64         }
65
66         /**
67          * Returns given property key or throws an exception if not found.
68          * <p>
69          * @param parameterKey Property key
70          * <p>
71          * @return Property value
72          * <p>
73          * @throws NullPointerException If given key is not found
74          * @throws NumberFormatException If no number is given in context parameter
75          */
76         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
77                 // Get context parameter
78                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
79                 // Return it
80                 return contextValue;
81         }
82
83         /**
84          * Returns given property key or throws an exception if not found.
85          * <p>
86          * @param parameterKey Property key
87          * <p>
88          * @return Property value
89          * <p>
90          * @throws NullPointerException If given key is not found
91          */
92         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
93                 // Get context parameter
94                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
95                 // Is it null?
96                 if (null == contextValue) {
97                         // Throw NPE
98                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
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                 // Try to get context parameter
121                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
122                 // Is it set and true?
123                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
124                 // Return it
125                 return isEnabled;
126         }
127
128         /**
129          * Loads resource bundle for given locale. This must be implemented per
130          * project so all projects can still customize their methods. Calling
131          * ResourceBundleloadBundle() in this class means that also the bundle files
132          * must be present here.
133          * <p>
134          * @param locale Locale from e.g. FacesContext
135          * <p>
136          * @return Initialized and loaded resource bundle
137          */
138         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
139
140         /**
141          * Shows a faces message for given causing exception. The message from the
142          * exception is being inserted into the message.
143          * <p>
144          * @param clientId Client id to send message to
145          * @param cause    Causing exception
146          */
147         protected void showFacesMessage (final String clientId, final Throwable cause) {
148                 // Get context and add message
149                 this.showFacesMessage(clientId, cause.getMessage());
150         }
151
152         /**
153          * Shows a faces message with given message (i18n) key.
154          * <p>
155          * @param clientId Client id to send message to
156          * @param i18nKey  Message key
157          * <p>
158          * @throws NullPointerException If clientId or i18nKey is null
159          * @throws IllegalArgumentException If clientId or i18nKey is empty
160          */
161         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
162                 // Both parameter must be valid
163                 if (null == clientId) {
164                         // Throw NPE
165                         throw new NullPointerException("clientId is null"); //NOI18N
166                 } else if (clientId.isEmpty()) {
167                         // Is empty
168                         throw new IllegalArgumentException("clientId is null"); //NOI18N
169                 } else if (null == i18nKey) {
170                         // Throw NPE
171                         throw new NullPointerException("i18nKey is null"); //NOI18N
172                 } else if (i18nKey.isEmpty()) {
173                         // Is empty
174                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
175                 }
176                 // Get current locale
177                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
178                 // Get bundle bundle
179                 ResourceBundle bundle = this.loadResourceBundle(locale);
180                 // Default is i18nKey
181                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
182                 // Try it
183                 try {
184                         // Get message
185                         message = bundle.getString(i18nKey);
186                 } catch (final MissingResourceException ex) {
187                         // Did not find it, ignored
188                 }
189                 // Get context and add message
190                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
191         }
192
193 }