2 * Copyright (C) 2016, 2017 Roland Häder
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.
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.
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/>.
17 package org.mxchange.jjobs.beans.businesscontact;
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;
41 * A business contact bean (controller)
43 * @author Roland Häder<roland@mxchange.org>
45 @Named ("businessContactController")
47 public abstract class JobsBusinessContactWebSessionBean extends BaseJobsController implements JobsBusinessContactWebSessionController {
52 private static final long serialVersionUID = 56_189_028_928_371L;
57 private BusinessContactSessionBeanRemote businessContactBean;
60 * A list of all registered companies (globally)
62 private List<BusinessContact> registeredCompanies;
68 private JobsUserLoginWebSessionController userLoginController;
73 public JobsBusinessContactWebSessionBean () {
74 // Call super constructor
79 public List<BusinessContact> allRegisteredCompanies () {
80 return Collections.unmodifiableList(this.registeredCompanies);
84 * Post-initialization of this class
90 // Get initial context
91 Context context = new InitialContext();
94 this.businessContactBean = (BusinessContactSessionBeanRemote) context.lookup("java:global/jfinancials-ejb/businessContact!org.mxchange.jcontactsbusiness.BusinessContactSessionBeanRemote"); //NOI18N
97 this.registeredCompanies = this.businessContactBean.allBusinessContacts();
98 } catch (final NamingException e) {
100 throw new FaceletException(e);
105 * Returns given property key or throws an exception if not found.
107 * @param parameterKey Property key
109 * @return Property value
111 * @throws NullPointerException If given key is not found
112 * @throws NumberFormatException If no number is given in context parameter
114 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
115 // Get context parameter
116 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
122 * Returns given property key or throws an exception if not found.
124 * @param parameterKey Property key
126 * @return Property value
128 * @throws NullPointerException If given key is not found
130 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
131 // Get context parameter
132 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
134 if (null == contextValue) {
136 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
143 * Checks whether debug mode is enabled for given controller
145 * @param controllerName Name of controller
147 * @return Whether debug mode is enabled
149 protected boolean isDebugModeEnabled (final String controllerName) {
150 // Parameters should be valid
151 if (null == controllerName) {
153 throw new NullPointerException("controllerName is null"); //NOI18N
154 } else if (controllerName.isEmpty()) {
156 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
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;
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.
172 * @param locale Locale from e.g. FacesContext
174 * @return Initialized and loaded resource bundle
176 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
179 * Shows a faces message for given causing exception. The message from the
180 * exception is being inserted into the message.
182 * @param clientId Client id to send message to
183 * @param cause Causing exception
185 protected void showFacesMessage (final String clientId, final Throwable cause) {
186 // Get context and add message
187 this.showFacesMessage(clientId, cause.getMessage());
191 * Shows a faces message with given message (i18n) key.
193 * @param clientId Client id to send message to
194 * @param i18nKey Message key
196 * @throws NullPointerException If clientId or i18nKey is null
197 * @throws IllegalArgumentException If clientId or i18nKey is empty
199 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
200 // Both parameter must be valid
201 if (null == clientId) {
203 throw new NullPointerException("clientId is null"); //NOI18N
204 } else if (clientId.isEmpty()) {
206 throw new IllegalArgumentException("clientId is null"); //NOI18N
207 } else if (null == i18nKey) {
209 throw new NullPointerException("i18nKey is null"); //NOI18N
210 } else if (i18nKey.isEmpty()) {
212 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
214 // Get current locale
215 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
217 ResourceBundle bundle = this.loadResourceBundle(locale);
218 // Default is i18nKey
219 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
223 message = bundle.getString(i18nKey);
224 } catch (final MissingResourceException ex) {
225 // Did not find it, ignored
227 // Get context and add message
228 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));