]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/businesscontact/JobsBusinessContactWebSessionBean.java
Please cherry-pick:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / businesscontact / JobsBusinessContactWebSessionBean.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.businesscontact;
18
19 import java.text.MessageFormat;
20 import java.util.Collections;
21 import java.util.List;
22 import java.util.Locale;
23 import java.util.MissingResourceException;
24 import java.util.ResourceBundle;
25 import javax.annotation.PostConstruct;
26 import javax.enterprise.context.SessionScoped;
27 import javax.faces.application.FacesMessage;
28 import javax.faces.context.FacesContext;
29 import javax.faces.view.facelets.FaceletException;
30 import javax.inject.Inject;
31 import javax.inject.Named;
32 import javax.naming.Context;
33 import javax.naming.InitialContext;
34 import javax.naming.NamingException;
35 import org.mxchange.jcontactsbusiness.BusinessContact;
36 import org.mxchange.jcontactsbusiness.BusinessContactSessionBeanRemote;
37 import org.mxchange.jjobs.beans.BaseJobsController;
38 import org.mxchange.jjobs.beans.user.login.JobsUserLoginWebSessionController;
39
40 /**
41  * A business contact bean (controller)
42  * <p>
43  * @author Roland Häder<roland@mxchange.org>
44  */
45 @Named ("businessContactController")
46 @SessionScoped
47 public abstract class JobsBusinessContactWebSessionBean extends BaseJobsController implements JobsBusinessContactWebSessionController {
48
49         /**
50          * Serial number
51          */
52         private static final long serialVersionUID = 56_189_028_928_371L;
53
54         /**
55          * Remote contact bean
56          */
57         private BusinessContactSessionBeanRemote businessContactBean;
58
59         /**
60          * A list of all registered companies (globally)
61          */
62         private List<BusinessContact> registeredCompanies;
63
64         /**
65          * User instance
66          */
67         @Inject
68         private JobsUserLoginWebSessionController userLoginController;
69
70         /**
71          * Constructor
72          */
73         public JobsBusinessContactWebSessionBean () {
74                 // Call super constructor
75                 super();
76         }
77
78         @Override
79         public List<BusinessContact> allRegisteredCompanies () {
80                 return Collections.unmodifiableList(this.registeredCompanies);
81         }
82
83         /**
84          * Post-initialization of this class
85          */
86         @PostConstruct
87         public void init () {
88                 // Try it
89                 try {
90                         // Get initial context
91                         Context context = new InitialContext();
92
93                         // Try to lookup
94                         this.businessContactBean = (BusinessContactSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/businessContact!org.mxchange.jcontactsbusiness.BusinessContactSessionBeanRemote"); //NOI18N
95
96                         // Init list
97                         this.registeredCompanies = this.businessContactBean.allBusinessContacts();
98                 } catch (final NamingException e) {
99                         // Throw again
100                         throw new FaceletException(e);
101                 }
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          * @throws NumberFormatException If no number is given in context parameter
113          */
114         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
115                 // Get context parameter
116                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
117                 // Return it
118                 return contextValue;
119         }
120
121         /**
122          * Returns given property key or throws an exception if not found.
123          * <p>
124          * @param parameterKey Property key
125          * <p>
126          * @return Property value
127          * <p>
128          * @throws NullPointerException If given key is not found
129          */
130         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
131                 // Get context parameter
132                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
133                 // Is it null?
134                 if (null == contextValue) {
135                         // Throw NPE
136                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
137                 }
138                 // Return it
139                 return contextValue;
140         }
141
142         /**
143          * Checks whether debug mode is enabled for given controller
144          * <p>
145          * @param controllerName Name of controller
146          * <p>
147          * @return Whether debug mode is enabled
148          */
149         protected boolean isDebugModeEnabled (final String controllerName) {
150                 // Parameters should be valid
151                 if (null == controllerName) {
152                         // Throw NPE
153                         throw new NullPointerException("controllerName is null"); //NOI18N
154                 } else if (controllerName.isEmpty()) {
155                         // Is empty
156                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
157                 }
158                 // Try to get context parameter
159                 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
160                 // Is it set and true?
161                 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
162                 // Return it
163                 return isEnabled;
164         }
165
166         /**
167          * Loads resource bundle for given locale. This must be implemented per
168          * project so all projects can still customize their methods. Calling
169          * ResourceBundleloadBundle() in this class means that also the bundle files
170          * must be present here.
171          * <p>
172          * @param locale Locale from e.g. FacesContext
173          * <p>
174          * @return Initialized and loaded resource bundle
175          */
176         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
177
178         /**
179          * Shows a faces message for given causing exception. The message from the
180          * exception is being inserted into the message.
181          * <p>
182          * @param clientId Client id to send message to
183          * @param cause    Causing exception
184          */
185         protected void showFacesMessage (final String clientId, final Throwable cause) {
186                 // Get context and add message
187                 this.showFacesMessage(clientId, cause.getMessage());
188         }
189
190         /**
191          * Shows a faces message with given message (i18n) key.
192          * <p>
193          * @param clientId Client id to send message to
194          * @param i18nKey  Message key
195          * <p>
196          * @throws NullPointerException If clientId or i18nKey is null
197          * @throws IllegalArgumentException If clientId or i18nKey is empty
198          */
199         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
200                 // Both parameter must be valid
201                 if (null == clientId) {
202                         // Throw NPE
203                         throw new NullPointerException("clientId is null"); //NOI18N
204                 } else if (clientId.isEmpty()) {
205                         // Is empty
206                         throw new IllegalArgumentException("clientId is null"); //NOI18N
207                 } else if (null == i18nKey) {
208                         // Throw NPE
209                         throw new NullPointerException("i18nKey is null"); //NOI18N
210                 } else if (i18nKey.isEmpty()) {
211                         // Is empty
212                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
213                 }
214                 // Get current locale
215                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
216                 // Get bundle bundle
217                 ResourceBundle bundle = this.loadResourceBundle(locale);
218                 // Default is i18nKey
219                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
220                 // Try it
221                 try {
222                         // Get message
223                         message = bundle.getString(i18nKey);
224                 } catch (final MissingResourceException ex) {
225                         // Did not find it, ignored
226                 }
227                 // Get context and add message
228                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
229         }
230
231 }