]> git.mxchange.org Git - jjobs-war.git/blob - src/java/org/mxchange/jjobs/beans/profile/UserProfileWebRequestBean.java
renamed packages (addressbook import) + added event for logging in
[jjobs-war.git] / src / java / org / mxchange / jjobs / beans / profile / UserProfileWebRequestBean.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 General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (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 General Public License for more details.
13  *
14  * You should have received a copy of the GNU 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.UserLoginWebSessionController;
25 import org.mxchange.jjobs.beans.user.UserWebSessionController;
26 import org.mxchange.jusercore.exceptions.UserNotFoundException;
27 import org.mxchange.jusercore.model.user.User;
28 import org.mxchange.jusercore.model.user.profilemodes.ProfileMode;
29
30 /**
31  * A web request bean for user profiles
32  * <p>
33  * @author Roland Haeder
34  */
35 @Named (value = "profileController")
36 @RequestScoped
37 public class UserProfileWebRequestBean implements UserProfileWebRequestController {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 187_687_145_286_710L;
43
44         /**
45          * Login controller
46          */
47         @Inject
48         private UserLoginWebSessionController loginController;
49
50         /**
51          * User instance
52          */
53         private User user;
54
55         /**
56          * User controller
57          */
58         @Inject
59         private UserWebSessionController userController;
60
61         @Override
62         public User getUser () {
63                 return this.user;
64         }
65
66         @Override
67         public void setUser (final User user) {
68                 this.user = user;
69         }
70
71         @Override
72         public boolean isProfileLinkVisible () {
73                 // Check on user
74                 if (this.getUser() == null) {
75                         /*
76                          * Not set, means wrong invocation of this method as the user
77                          * instance needs to be set first.
78                          */
79                         throw new NullPointerException("this.user is null"); //NOI18N
80                 } else if (this.getUser().getUserId() == null) {
81                         /*
82                          * If the id number is not set it means that the user instance has
83                          * not been persisted and the JPA has not been flushed. Or a
84                          * "virgin" instance (e.g. from registration) has been used.
85                          */
86                         throw new NullPointerException("this.user.userId is null"); //NOI18N
87                 } else if (this.getUser().getUserId() < 1) {
88                         /*
89                          * The id number is set invalid for an unknown reason.
90                          */
91                         throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N
92                 } else if (this.getUser().getUserProfileMode() == null) {
93                         /*
94                          * Possibly an out-dated user profile is being used. This should not
95                          * happen.
96                          */
97                         throw new NullPointerException("this.user.userProfileMode is null"); //NOI18N
98                 }
99
100                 // Get profile mode from user instance (safe now)
101                 ProfileMode profileMode = this.getUser().getUserProfileMode();
102
103                 // Check all conditions (except for admin)
104                 // TODO: Add admin role somehow?
105                 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
106                                  (this.loginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
107         }
108
109         @Override
110         public boolean isProfileLinkVisibleById (final Long userId) {
111                 // Init user instance
112                 User u = null;
113
114                 try {
115                         // Try to get it
116                         u = this.userController.lookupUserById(userId);
117                 } catch (final UserNotFoundException ex) {
118                         // Throw again
119                         throw new FaceletException(ex);
120                 }
121
122                 // Set it here
123                 this.setUser(u);
124
125                 // Is it null?
126                 if (null == u) {
127                         // Not found, not visible.
128                         return false;
129                 }
130
131                 // Ask other method
132                 return this.isProfileLinkVisible();
133         }
134
135         @Override
136         public boolean isProfileLinkVisibleByUser (final User user) {
137                 // Is it correctly set?
138                 if (null == user) {
139                         // Throw NPE
140                         throw new NullPointerException("user is null");
141                 } else if (user.getUserId() == null) {
142                         // Throw NPE again
143                         throw new NullPointerException("user.userId is null");
144                 } else if (user.getUserId() < 1) {
145                         // Invalid user id set
146                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is not valid.", user.getUserId()));
147                 }
148
149                 // Set user here
150                 this.setUser(user);
151
152                 // Ask other method
153                 return this.isProfileLinkVisible();
154         }
155 }