]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/profile/JobsUserProfileWebRequestBean.java
Rewrites:
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / profile / JobsUserProfileWebRequestBean.java
1 /*
2  * Copyright (C) 2016 Roland Haeder
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 javax.enterprise.context.RequestScoped;
21 import javax.faces.view.facelets.FaceletException;
22 import javax.inject.Inject;
23 import javax.inject.Named;
24 import org.mxchange.jjobs.beans.login.JobsUserLoginWebSessionController;
25 import org.mxchange.jjobs.beans.user.JobsAdminUserWebRequestController;
26 import org.mxchange.jjobs.beans.user.JobsUserWebSessionController;
27 import org.mxchange.jusercore.exceptions.UserNotFoundException;
28 import org.mxchange.jusercore.model.user.User;
29 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
30
31 /**
32  * A web request bean for user profiles
33  * <p>
34  * @author Roland Haeder<roland@mxchange.org>
35  */
36 @Named (value = "profileController")
37 @RequestScoped
38 public class JobsUserProfileWebRequestBean implements JobsUserProfileWebRequestController {
39
40         /**
41          * Serial number
42          */
43         private static final long serialVersionUID = 187_687_145_286_710L;
44
45         /**
46          * Administrative user controller
47          */
48         @Inject
49         private JobsAdminUserWebRequestController adminUserController;
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         @Override
64         public boolean isProfileLinkVisibleById (final Long userId) {
65                 // Init user instance
66                 User u = null;
67
68                 try {
69                         // Try to get it
70                         u = this.adminUserController.lookupUserById(userId);
71                 } catch (final UserNotFoundException ex) {
72                         // Throw again
73                         throw new FaceletException(ex);
74                 }
75
76                 // Is it null?
77                 if (null == u) {
78                         // Not found, not visible.
79                         return false;
80                 }
81
82                 // Ask other method
83                 return this.isProfileLinkVisibleByUser(u);
84         }
85
86         @Override
87         public boolean isProfileLinkVisibleByUser (final User user) {
88                 // Check on user
89                 if (null == user) {
90                         /*
91                          * Not set, means wrong invocation of this method as the user
92                          * instance needs to be set first.
93                          */
94                         throw new NullPointerException("user is null"); //NOI18N
95                 } else if (user.getUserId() == null) {
96                         /*
97                          * If the id number is not set it means that the user instance has
98                          * not been persisted and the JPA has not been flushed. Or a
99                          * "virgin" instance (e.g. from registration) has been used.
100                          */
101                         throw new NullPointerException("user.userId is null"); //NOI18N
102                 } else if (user.getUserId() < 1) {
103                         /*
104                          * The id number is set invalid for an unknown reason.
105                          */
106                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
107                 } else if (user.getUserProfileMode() == null) {
108                         /*
109                          * Possibly an out-dated user profile is being used. This should not
110                          * happen.
111                          */
112                         throw new NullPointerException("user.userProfileMode is null"); //NOI18N
113                 }
114
115                 // Get profile mode from user instance (safe now)
116                 ProfileMode profileMode = user.getUserProfileMode();
117
118                 // Check all conditions (except for admin)
119                 // TODO: Add admin role somehow?
120                 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
121                                 (this.userLoginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
122         }
123
124 }