]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java
Rewrite continued:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / profile / JobsUserProfileWebRequestBean.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.profile;
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.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;
36
37 /**
38  * A web request bean for user profiles
39  * <p>
40  * @author Roland Häder<roland@mxchange.org>
41  */
42 @Named (value = "profileController")
43 @RequestScoped
44 public abstract class JobsUserProfileWebRequestBean extends BaseJobsController implements JobsUserProfileWebRequestController {
45
46         /**
47          * Serial number
48          */
49         private static final long serialVersionUID = 187_687_145_286_710L;
50
51         /**
52          * User controller
53          */
54         @Inject
55         private JobsUserWebSessionController userController;
56
57         /**
58          * Login controller
59          */
60         @Inject
61         private JobsUserLoginWebSessionController userLoginController;
62
63         /**
64          * Default constructor
65          */
66         public JobsUserProfileWebRequestBean () {
67                 // Call super constructor
68                 super();
69         }
70
71         /**
72          * Post-construction method
73          */
74         @PostConstruct
75         public void init () {
76         }
77
78         @Override
79         public boolean isProfileLinkVisibleById (final Long userId) {
80                 // Init user instance
81                 User user = null;
82
83                 try {
84                         // Try to get it
85                         user = this.userController.lookupUserById(userId);
86                 } catch (final UserNotFoundException ex) {
87                         // Throw again
88                         throw new FaceletException(ex);
89                 }
90
91                 // Is it null?
92                 if (null == user) {
93                         // Not found, not visible.
94                         return false;
95                 }
96
97                 // Ask other method
98                 return this.isProfileLinkVisibleByUser(user);
99         }
100
101         @Override
102         public boolean isProfileLinkVisibleByUser (final User user) {
103                 // Check on user
104                 if (null == user) {
105                         /*
106                          * Not set, means wrong invocation of this method as the user
107                          * instance needs to be set first.
108                          */
109                         throw new NullPointerException("user is null"); //NOI18N
110                 } else if (user.getUserId() == null) {
111                         /*
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.
115                          */
116                         throw new NullPointerException("user.userId is null"); //NOI18N
117                 } else if (user.getUserId() < 1) {
118                         /*
119                          * The id number is set invalid for an unknown reason.
120                          */
121                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
122                 } else if (user.getUserProfileMode() == null) {
123                         /*
124                          * Possibly an out-dated user profile is being used. This should not
125                          * happen.
126                          */
127                         throw new NullPointerException("user.userProfileMode is null"); //NOI18N
128                 }
129
130                 // Get profile mode from user instance (safe now)
131                 ProfileMode profileMode = user.getUserProfileMode();
132
133                 // Check all conditions (except for admin)
134                 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
135                                 (this.userLoginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
136         }
137
138         /**
139          * Returns given property key or throws an exception if not found.
140          * <p>
141          * @param parameterKey Property key
142          * <p>
143          * @return Property value
144          * <p>
145          * @throws NullPointerException If given key is not found
146          * @throws NumberFormatException If no number is given in context parameter
147          */
148         protected int getIntegerContextParameter (final String parameterKey) throws NullPointerException, NumberFormatException {
149                 // Get context parameter
150                 Integer contextValue = Integer.parseInt(this.getStringContextParameter(parameterKey));
151                 // Return it
152                 return contextValue;
153         }
154
155         /**
156          * Returns given property key or throws an exception if not found.
157          * <p>
158          * @param parameterKey Property key
159          * <p>
160          * @return Property value
161          * <p>
162          * @throws NullPointerException If given key is not found
163          */
164         protected String getStringContextParameter (final String parameterKey) throws NullPointerException {
165                 // Get context parameter
166                 String contextValue = FacesContext.getCurrentInstance().getExternalContext().getInitParameter(parameterKey);
167                 // Is it null?
168                 if (null == contextValue) {
169                         // Throw NPE
170                         throw new NullPointerException(MessageFormat.format("parameterKey={0} is not set.", parameterKey)); //NOI18N
171                 }
172                 // Return it
173                 return contextValue;
174         }
175
176         /**
177          * Checks whether debug mode is enabled for given controller
178          * <p>
179          * @param controllerName Name of controller
180          * <p>
181          * @return Whether debug mode is enabled
182          */
183         protected boolean isDebugModeEnabled (final String controllerName) {
184                 // Parameters should be valid
185                 if (null == controllerName) {
186                         // Throw NPE
187                         throw new NullPointerException("controllerName is null"); //NOI18N
188                 } else if (controllerName.isEmpty()) {
189                         // Is empty
190                         throw new IllegalArgumentException("controllerName is empty"); //NOI18N
191                 }
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;
196                 // Return it
197                 return isEnabled;
198         }
199
200         /**
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.
205          * <p>
206          * @param locale Locale from e.g. FacesContext
207          * <p>
208          * @return Initialized and loaded resource bundle
209          */
210         protected abstract ResourceBundle loadResourceBundle (final Locale locale);
211
212         /**
213          * Shows a faces message for given causing exception. The message from the
214          * exception is being inserted into the message.
215          * <p>
216          * @param clientId Client id to send message to
217          * @param cause    Causing exception
218          */
219         protected void showFacesMessage (final String clientId, final Throwable cause) {
220                 // Get context and add message
221                 this.showFacesMessage(clientId, cause.getMessage());
222         }
223
224         /**
225          * Shows a faces message with given message (i18n) key.
226          * <p>
227          * @param clientId Client id to send message to
228          * @param i18nKey  Message key
229          * <p>
230          * @throws NullPointerException If clientId or i18nKey is null
231          * @throws IllegalArgumentException If clientId or i18nKey is empty
232          */
233         protected void showFacesMessage (final String clientId, final String i18nKey) throws NullPointerException, IllegalArgumentException {
234                 // Both parameter must be valid
235                 if (null == clientId) {
236                         // Throw NPE
237                         throw new NullPointerException("clientId is null"); //NOI18N
238                 } else if (clientId.isEmpty()) {
239                         // Is empty
240                         throw new IllegalArgumentException("clientId is null"); //NOI18N
241                 } else if (null == i18nKey) {
242                         // Throw NPE
243                         throw new NullPointerException("i18nKey is null"); //NOI18N
244                 } else if (i18nKey.isEmpty()) {
245                         // Is empty
246                         throw new IllegalArgumentException("i18nKey is null"); //NOI18N
247                 }
248                 // Get current locale
249                 Locale locale = FacesContext.getCurrentInstance().getViewRoot().getLocale();
250                 // Get bundle bundle
251                 ResourceBundle bundle = this.loadResourceBundle(locale);
252                 // Default is i18nKey
253                 String message = MessageFormat.format("!{0}!", i18nKey); //NOI18N
254                 // Try it
255                 try {
256                         // Get message
257                         message = bundle.getString(i18nKey);
258                 } catch (final MissingResourceException ex) {
259                         // Did not find it, ignored
260                 }
261                 // Get context and add message
262                 FacesContext.getCurrentInstance().addMessage(clientId, new FacesMessage(message));
263         }
264
265 }