]> git.mxchange.org Git - addressbook-war.git/blob - src/java/org/mxchange/addressbook/beans/profile/AddressbookUserProfileWebRequestBean.java
Cleanups:
[addressbook-war.git] / src / java / org / mxchange / addressbook / beans / profile / AddressbookUserProfileWebRequestBean.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.addressbook.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.addressbook.beans.login.AddressbookUserLoginWebSessionController;
25 import org.mxchange.addressbook.beans.user.AddressbookAdminUserWebRequestController;
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<roland@mxchange.org>
34  */
35 @Named (value = "profileController")
36 @RequestScoped
37 public class AddressbookUserProfileWebRequestBean implements AddressbookUserProfileWebRequestController {
38
39         /**
40          * Serial number
41          */
42         private static final long serialVersionUID = 187_687_145_286_710L;
43
44         /**
45          * User controller
46          */
47         @Inject
48         private AddressbookAdminUserWebRequestController adminUserController;
49
50         /**
51          * Login controller
52          */
53         @Inject
54         private AddressbookUserLoginWebSessionController loginController;
55
56         @Override
57         public boolean isProfileLinkVisibleById (final Long userId) {
58                 // Init user instance
59                 User u = null;
60
61                 try {
62                         // Try to get it
63                         u = this.adminUserController.lookupUserById(userId);
64                 } catch (final UserNotFoundException ex) {
65                         // Throw again
66                         throw new FaceletException(ex);
67                 }
68
69                 // Is it null?
70                 if (null == u) {
71                         // Not found, not visible.
72                         return false;
73                 }
74
75                 // Ask other method
76                 return this.isProfileLinkVisibleByUser(u);
77         }
78
79         @Override
80         public boolean isProfileLinkVisibleByUser (final User user) {
81                 // Check on user
82                 if (null == user) {
83                         /*
84                          * Not set, means wrong invocation of this method as the user
85                          * instance needs to be set first.
86                          */
87                         throw new NullPointerException("user is null"); //NOI18N
88                 } else if (user.getUserId() == null) {
89                         /*
90                          * If the id number is not set it means that the user instance has
91                          * not been persisted and the JPA has not been flushed. Or a
92                          * "virgin" instance (e.g. from registration) has been used.
93                          */
94                         throw new NullPointerException("user.userId is null"); //NOI18N
95                 } else if (user.getUserId() < 1) {
96                         /*
97                          * The id number is set invalid for an unknown reason.
98                          */
99                         throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
100                 } else if (user.getUserProfileMode() == null) {
101                         /*
102                          * Possibly an out-dated user profile is being used. This should not
103                          * happen.
104                          */
105                         throw new NullPointerException("user.userProfileMode is null"); //NOI18N
106                 }
107
108                 // Get profile mode from user instance (safe now)
109                 ProfileMode profileMode = user.getUserProfileMode();
110
111                 // Check all conditions (except for admin)
112                 // TODO: Add admin role somehow?
113                 return ((profileMode.equals(ProfileMode.PUBLIC)) ||
114                                 (this.loginController.isUserLoggedIn()) && (profileMode.equals(ProfileMode.MEMBERS)));
115         }
116
117 }