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.profile;
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.RequestScoped;
25 import javax.faces.application.FacesMessage;
26 import javax.faces.context.FacesContext;
27 import javax.faces.view.facelets.FaceletException;
28 import javax.inject.Inject;
29 import javax.inject.Named;
30 import org.mxchange.jjobs.beans.BaseJobsController;
31 import org.mxchange.jjobs.beans.login.user.JobsUserLoginWebSessionController;
32 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
33 import org.mxchange.jusercore.exceptions.UserNotFoundException;
34 import org.mxchange.jusercore.model.user.User;
35 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
38 * A web request bean for user profiles
40 * @author Roland Häder<roland@mxchange.org>
42 @Named (value = "profileController")
44 public abstract class JobsUserProfileWebRequestBean extends BaseJobsController implements JobsUserProfileWebRequestController {
49 private static final long serialVersionUID = 187_687_145_286_710L;
55 private JobsUserWebSessionController userController;
61 private JobsUserLoginWebSessionController userLoginController;
66 public JobsUserProfileWebRequestBean () {
67 // Call super constructor
72 * Post-construction method
79 public boolean isProfileLinkVisibleById (final Long userId) {
85 user = this.userController.lookupUserById(userId);
86 } catch (final UserNotFoundException ex) {
88 throw new FaceletException(ex);
93 // Not found, not visible.
98 return this.isProfileLinkVisibleByUser(user);
102 public boolean isProfileLinkVisibleByUser (final User user) {
106 * Not set, means wrong invocation of this method as the user
107 * instance needs to be set first.
109 throw new NullPointerException("user is null"); //NOI18N
110 } else if (user.getUserId() == null) {
112 * If the id number is not set it means that the user instance has
113 * not been persisted and the JPA has not been flushed. Or a
114 * "virgin" instance (e.g. from registration) has been used.
116 throw new NullPointerException("user.userId is null"); //NOI18N
117 } else if (user.getUserId() < 1) {
119 * The id number is set invalid for an unknown reason.
121 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
122 } else if (user.getUserProfileMode() == null) {
124 * Possibly an out-dated user profile is being used. This should not
127 throw new NullPointerException("user.userProfileMode is null"); //NOI18N
130 // Get profile mode from user instance (safe now)
131 ProfileMode profileMode = user.getUserProfileMode();
133 // Check all conditions (except for admin)
134 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
135 (this.userLoginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
139 * Returns given property key or throws an exception if not found.
141 * @param parameterKey Property key
143 * @return Property value
145 * @throws NullPointerException If given key is not found
146 * @throws NumberFormatException If no number is given in context parameter
148 protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
149 // Get context parameter
150 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
156 * Returns given property key or throws an exception if not found.
158 * @param parameterKey Property key
160 * @return Property value
162 * @throws NullPointerException If given key is not found
164 protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
165 // Get context parameter
166 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
168 if (null == contextValue) {
170 throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
177 * Checks whether debug mode is enabled for given controller
179 * @param controllerName Name of controller
181 * @return Whether debug mode is enabled
183 protected boolean isDebugModeEnabled (final String controllerName) {
184 // Parameters should be valid
185 if (null == controllerName) {
187 throw new NullPointerException("controllerName is null"); //NOI18N
188 } else if (controllerName.isEmpty()) {
190 throw new IllegalArgumentException("controllerName is empty"); //NOI18N
192 // Try to get context parameter
193 String contextParameter = this.getStringContextParameter(String.format("is_debug_%s_enabled", controllerName)); //NOI18N
194 // Is it set and true?
195 boolean isEnabled = Boolean.parseBoolean(contextParameter) == Boolean.TRUE;
201 * Loads resource bundle for given locale. This must be implemented per
202 * project so all projects can still customize their methods. Calling
203 * ResourceBundleloadBundle() in this class means that also the bundle files
204 * must be present here.
206 * @param locale Locale from e.g. FacesContext
208 * @return Initialized and loaded resource bundle
210 protected abstract ResourceBundle loadResourceBundle (final Locale locale);
213 * Shows a faces message for given causing exception. The message from the
214 * exception is being inserted into the message.
216 * @param clientId Client id to send message to
217 * @param cause Causing exception
219 protected void showFacesMessage (final String clientId, final Throwable cause) {
220 // Get context and add message
221 this.showFacesMessage(clientId, cause.getMessage());
225 * Shows a faces message with given message (i18n) key.
227 * @param clientId Client id to send message to
228 * @param i18nKey Message key
230 * @throws NullPointerException If clientId or i18nKey is null
231 * @throws IllegalArgumentException If clientId or i18nKey is empty
233 protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
234 // Both parameter must be valid
235 if (null == clientId) {
237 throw new NullPointerException("clientId is null"); //NOI18N
238 } else if (clientId.isEmpty()) {
240 throw new IllegalArgumentException("clientId is null"); //NOI18N
241 } else if (null == i18nKey) {
243 throw new NullPointerException("i18nKey is null"); //NOI18N
244 } else if (i18nKey.isEmpty()) {
246 throw new IllegalArgumentException("i18nKey is null"); //NOI18N
248 // Get current locale
249 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
251 ResourceBundle bundle = this.loadResourceBundle(locale);
252 // Default is i18nKey
253 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
257 message = bundle.getString(i18nKey);
258 } catch (final MissingResourceException ex) {
259 // Did not find it, ignored
261 // Get context and add message
262 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));