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