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.jjobs.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.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;
31 * A web request bean for user profiles
33 * @author Roland Haeder
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;
59 private UserWebSessionController userController;
62 public User getUser () {
67 public void setUser (final User user) {
72 public boolean isProfileLinkVisible () {
74 if (this.getUser() == null) {
76 * Not set, means wrong invocation of this method as the user
77 * instance needs to be set first.
79 throw new NullPointerException("this.user is null"); //NOI18N
80 } else if (this.getUser().getUserId() == null) {
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.
86 throw new NullPointerException("this.user.userId is null"); //NOI18N
87 } else if (this.getUser().getUserId() < 1) {
89 * The id number is set invalid for an unknown reason.
91 throw new IllegalArgumentException(MessageFormat.format("this.user.userId={0} is invalid", this.getUser().getUserId())); //NOI18N
92 } else if (this.getUser().getUserProfileMode() == null) {
94 * Possibly an out-dated user profile is being used. This should not
97 throw new NullPointerException("this.user.userProfileMode is null"); //NOI18N
100 // Get profile mode from user instance (safe now)
101 ProfileMode profileMode = this.getUser().getUserProfileMode();
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)));
110 public boolean isProfileLinkVisibleById (final Long userId) {
111 // Init user instance
116 u = this.userController.lookupUserById(userId);
117 } catch (final UserNotFoundException ex) {
119 throw new FaceletException(ex);
127 // Not found, not visible.
132 return this.isProfileLinkVisible();
136 public boolean isProfileLinkVisibleByUser (final User user) {
137 // Is it correctly set?
140 throw new NullPointerException("user is null");
141 } else if (user.getUserId() == null) {
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()));
153 return this.isProfileLinkVisible();