2 * Copyright (C) 2016 Roland Haeder
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.
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.
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/>.
17 package org.mxchange.addressbook.beans.profile;
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.UserLoginWebSessionController;
25 import org.mxchange.addressbook.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;
31 * A web request bean for user profiles
33 * @author Roland Haeder<roland@mxchange.org>
35 @Named (value = "profileController")
37 public class UserProfileWebRequestBean implements UserProfileWebRequestController {
42 private static final long serialVersionUID = 187_687_145_286_710L;
48 private UserLoginWebSessionController loginController;
54 private UserWebSessionController userController;
57 public boolean isProfileLinkVisibleById (final Long userId) {
63 u = this.userController.lookupUserById(userId);
64 } catch (final UserNotFoundException ex) {
66 throw new FaceletException(ex);
71 // Not found, not visible.
76 return this.isProfileLinkVisibleByUser(u);
80 public boolean isProfileLinkVisibleByUser (final User user) {
84 * Not set, means wrong invocation of this method as the user
85 * instance needs to be set first.
87 throw new NullPointerException("user is null"); //NOI18N
88 } else if (user.getUserId() == null) {
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.
94 throw new NullPointerException("user.userId is null"); //NOI18N
95 } else if (user.getUserId() < 1) {
97 * The id number is set invalid for an unknown reason.
99 throw new IllegalArgumentException(MessageFormat.format("user.userId={0} is invalid", user.getUserId())); //NOI18N
100 } else if (user.getUserProfileMode() == null) {
102 * Possibly an out-dated user profile is being used. This should not
105 throw new NullPointerException("user.userProfileMode is null"); //NOI18N
108 // Get profile mode from user instance (safe now)
109 ProfileMode profileMode = user.getUserProfileMode();
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)));